]> git.mxchange.org Git - friendica-addons.git/blob - statistics_json/statistics_json.php
statistics: A module to take part to the statistics at http://pods.jasonrobinson.me/
[friendica-addons.git] / statistics_json / statistics_json.php
1 <?php
2
3 /**
4  * Name: Statistics
5  * Description: Generates some statistics for http://pods.jasonrobinson.me/
6  * Version: 0.1
7  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
8  */
9
10 function statistics_json_install() {
11 }
12
13
14 function statistics_json_uninstall() {
15 }
16
17 function statistics_json_module() {}
18
19 function statistics_json_init() {
20         global $a;
21
22         $statistics = array(
23                         "name" => $a->config["sitename"],
24                         "version" => FRIENDICA_VERSION,
25                         "registrations_open" => ($a->config['register_policy'] != 0),
26                         );
27
28         $users = q("SELECT `user`.`login_date`, `lastitem`.`lastitem_date`
29                         FROM (SELECT MAX(`item`.`changed`) as `lastitem_date`, `item`.`uid`
30                                 FROM `item`
31                                         WHERE `item`.`type` = 'wall'
32                                                 GROUP BY `item`.`uid`) AS `lastitem`
33                                                 RIGHT OUTER JOIN `user` ON `user`.`uid` = `lastitem`.`uid`, `contact`
34                                 WHERE
35                                         `user`.`uid` = `contact`.`uid`
36                                         AND `user`.`verified` AND `contact`.`self`
37                                         AND NOT `user`.`blocked` AND NOT `user`.`hidewall`
38                                         AND NOT `user`.`account_removed`
39                                         AND NOT `user`.`account_expired`");
40
41         if (!is_array($users)) {
42                         $statistics["total_users"] = -1;
43                         $statistics["active_users_halfyear"] = -1;
44                         $statistics["active_users_monthly"] = -1;
45         } else {
46                         $statistics["total_users"] = count($users);
47                         $statistics["active_users_halfyear"] = 0;
48                         $statistics["active_users_monthly"] = 0;
49
50                         $halfyear = time() - (180 * 24 * 60 * 60);
51                         $month = time() - (30 * 24 * 60 * 60);
52
53                         foreach ($users AS $user) {
54                                 if ((strtotime($user['login_date']) > $halfyear) OR
55                                         (strtotime($user['lastitem_date']) > $halfyear))
56                                         ++$statistics["active_users_halfyear"];
57
58                                 if ((strtotime($user['login_date']) > $month) OR
59                                         (strtotime($user['lastitem_date']) > $month))
60                                         ++$statistics["active_users_monthly"];
61
62                         }
63         }
64
65         $posts = q("SELECT COUNT(*) AS local_posts FROM `item` WHERE `wall`");
66         if (!is_array($posts))
67                 $statistics["local_posts"] = -1;
68         else
69                 $statistics["local_posts"] = $posts[0]["local_posts"];
70
71         header("Content-Type: application/json");
72         echo json_encode($statistics);
73
74         logger("statistics_init: printed ".print_r($statistics, true));
75         killme();
76 }