wikiplugin_stat.php 12.5 KB
Newer Older
1
<?php
2

3
// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
rjsmelo's avatar
rjsmelo committed
4
//
5
6
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
7
// $Id$
8

9
10
function wikiplugin_stat_info()
{
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
    return [
        'name' => tra('Stat'),
        'documentation' => 'PluginStat',
        'description' => tra('Show various statistics for an object'),
        'prefs' => ['wikiplugin_stat'],
        'iconname' => 'chart',
        'introduced' => 4,
        'params' => [
            'type' => [
                'required' => true,
                'name' => tra('Object Type'),
                'description' => tra('Colon-separated list of object type to show stats for.'),
                'since' => '4.0',
                'filter' => 'text',
                'default' => '',
                'options' => [
                    ['text' => '', 'value' => ''],
                    ['text' => tra('Article'), 'value' => 'article'],
                    ['text' => tra('Article & Tracker Item'), 'value' => 'article:trackeritem'],
                    ['text' => tra('Article & Tracker Item & Wiki'), 'value' => 'article:trackeritem:wiki'],
                    ['text' => tra('Article & Wiki'), 'value' => 'article:wiki'],
                    ['text' => tra('Article & Wiki & Tracker Item'), 'value' => 'article:wiki:trackeritem'],
                    ['text' => tra('Tracker Item'), 'value' => 'trackeritem'],
                    ['text' => tra('Tracker Item & Article'), 'value' => 'trackeritem:article'],
                    ['text' => tra('Tracker Item & Article & Wiki'), 'value' => 'trackeritem:article:wiki'],
                    ['text' => tra('Tracker Item & Wiki'), 'value' => 'trackeritem:wiki'],
                    ['text' => tra('Tracker Item & Wiki & Article'), 'value' => 'trackeritem:wiki:article'],
                    ['text' => tra('Wiki'), 'value' => 'wiki'],
                    ['text' => tra('Wiki & Article'), 'value' => 'wiki:article'],
                    ['text' => tra('Wiki & Article & Tracker Item'), 'value' => 'wiki:article:trackeritem'],
                    ['text' => tra('Wiki & Tracker Item'), 'value' => 'wiki:trackeritem'],
                    ['text' => tra('Wiki & Tracker Item & Article'), 'value' => 'wiki:trackeritem:article'],
                ]
            ],
            'parentId' => [
                'required' => false,
                'name' => tra('Parent ID'),
                'description' => tra('Enter a tracker ID to restrict stats to that tracker (for use with trackeritems only).'),
                'since' => '4.0',
                'filter' => 'digits',
                'profile_reference' => 'tracker',
            ],
            'lastday' => [
                'required' => false,
                'name' => tra('Last 24 Hours'),
                'description' => tr('Added and/or viewed in the last 24 hours (only added items shown for tracker
57
                    items whether %0a%1 (added) or %0v%1 (viewed) or both is set)', '<code>', '</code>'),
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
                'since' => '4.0',
                'filter' => 'text',
                'accepted' => tra('a or v or both separated by a colon. Example: "a:v" or "v:a"'),
                'default' => '',
                'options' => [
                    ['text' => '', 'value' => ''],
                    ['text' => tra('Added'), 'value' => 'a'],
                    ['text' => tra('Added and Viewed'), 'value' => 'a:v'],
                    ['text' => tra('Viewed'), 'value' => 'v'],
                    ['text' => tra('Viewed & Added'), 'value' => 'v:a']
                ]
            ],
            'day' => [
                'required' => false,
                'name' => tra('Today'),
                'description' => tr('Added and/or viewed since the beginning of the day (only added items shown for
74
                    tracker items whether %0a%1 (added) or %0v%1 (viewed) or both is set)', '<code>', '</code>'),
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
                'since' => '4.0',
                'filter' => 'text',
                'accepted' => tra('a or v or both separated by a colon. Example: "a:v" or "v:a"'),
                'default' => '',
                'options' => [
                    ['text' => '', 'value' => ''],
                    ['text' => tra('Added'), 'value' => 'a'],
                    ['text' => tra('Added and Viewed'), 'value' => 'a:v'],
                    ['text' => tra('Viewed'), 'value' => 'v'],
                    ['text' => tra('Viewed & Added'), 'value' => 'v:a']
                ]
            ],
            'lastweek' => [
                'required' => false,
                'name' => tra('Last 7 Days'),
                'description' => tr('Added and/or viewed in the last 7 days (only added items shown for tracker items
91
                    whether %0a%1 (added) or %0v%1 (viewed) or both is set)', '<code>', '</code>'),
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
                'since' => '4.0',
                'filter' => 'text',
                'accepted' => tra('a or v or both separated by a colon. Example: "a:v" or "v:a"'),
                'default' => '',
                'options' => [
                    ['text' => '', 'value' => ''],
                    ['text' => tra('Added'), 'value' => 'a'],
                    ['text' => tra('Added and Viewed'), 'value' => 'a:v'],
                    ['text' => tra('Viewed'), 'value' => 'v'],
                    ['text' => tra('Viewed & Added'), 'value' => 'v:a']
                ]
            ],
            'week' => [
                'required' => false,
                'name' => tra('This Week'),
                'description' => tr('Added and/or viewed since the beginning of the week (only added items shown for
108
                    tracker items whether %0a%1 (added) or %0v%1 (viewed) or both is set)', '<code>', '</code>'),
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
                'since' => '4.0',
                'filter' => 'text',
                'accepted' => tra('a or v or both separated by a colon. Example: "a:v" or "v:a"'),
                'default' => '',
                'options' => [
                    ['text' => '', 'value' => ''],
                    ['text' => tra('Added'), 'value' => 'a'],
                    ['text' => tra('Added and Viewed'), 'value' => 'a:v'],
                    ['text' => tra('Viewed'), 'value' => 'v'],
                    ['text' => tra('Viewed & Added'), 'value' => 'v:a']
                ]
            ],
            'lastmonth' => [
                'required' => false,
                'name' => tr('Last Month'),
                'description' => tr('Added and/or viewed last month (only added items shown for tracker items
125
                    whether %0a%1 (added) or %0v%1 (viewed) or both is set)', '<code>', '</code>'),
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
                'since' => '4.0',
                'filter' => 'text',
                'accepted' => tra('a or v or both separated by a colon. Example: "a:v" or "v:a"'),
                'default' => '',
                'options' => [
                    ['text' => '', 'value' => ''],
                    ['text' => tra('Added'), 'value' => 'a'],
                    ['text' => tra('Added and Viewed'), 'value' => 'a:v'],
                    ['text' => tra('Viewed'), 'value' => 'v'],
                    ['text' => tra('Viewed & Added'), 'value' => 'v:a']
                ]
            ],
            'month' => [
                'required' => false,
                'name' => tra('This Month'),
                'description' => tr('Added and/or viewed since the beginning of the month (only added items shown for
142
                    tracker items whether %0a%1 (added) or %0v%1 (viewed) or both is set)', '<code>', '</code>'),
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
                'since' => '4.0',
                'filter' => 'text',
                'accepted' => tra('a or v or both separated by a colon. Example: "a:v" or "v:a"'),
                'default' => '',
                'options' => [
                    ['text' => '', 'value' => ''],
                    ['text' => tra('Added'), 'value' => 'a'],
                    ['text' => tra('Added and Viewed'), 'value' => 'a:v'],
                    ['text' => tra('Viewed'), 'value' => 'v'],
                    ['text' => tra('Viewed & Added'), 'value' => 'v:a']
                ]
            ],
            'lastyear' => [
                'required' => false,
                'name' => tra('Last Year'),
                'description' => tr('Added and/or viewed in the last 365 days (only added items shown for tracker
159
                    items whether %0a%1 (added) or %0v%1 (viewed) or both is set)', '<code>', '</code>'),
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
                'since' => '4.0',
                'filter' => 'text',
                'accepted' => tra('a or v or both separated by a colon. Example: "a:v" or "v:a"'),
                'default' => '',
                'options' => [
                    ['text' => '', 'value' => ''],
                    ['text' => tra('Added'), 'value' => 'a'],
                    ['text' => tra('Added and Viewed'), 'value' => 'a:v'],
                    ['text' => tra('Viewed'), 'value' => 'v'],
                    ['text' => tra('Viewed & Added'), 'value' => 'v:a']
                ]
            ],
            'year' => [
                'required' => false,
                'name' => tra('This Year'),
                'description' => tr('Added and/or viewed since the beginning of the year (only added items shown for
176
                    tracker items whether %0a%1 (added) or %0v%1 (viewed) or both is set)', '<code>', '</code>'),
177
178
179
180
181
182
183
184
185
186
187
188
189
190
                'since' => '4.0',
                'filter' => 'text',
                'accepted' => tra('a or v or both separated by a colon. Example: "a:v" or "v:a"'),
                'default' => '',
                'options' => [
                    ['text' => '', 'value' => ''],
                    ['text' => tra('Added'), 'value' => 'a'],
                    ['text' => tra('Added and Viewed'), 'value' => 'a:v'],
                    ['text' => tra('Viewed'), 'value' => 'v'],
                    ['text' => tra('Viewed & Added'), 'value' => 'v:a']
                ]
            ],
        ],
    ];
191
192
}

rjsmelo's avatar
rjsmelo committed
193
function wikiplugin_stat($data, $params)
194
{
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
    $smarty = TikiLib::lib('smarty');
    $statslib = TikiLib::lib('stats');
    $stat = [];
    foreach ($params as $when => $whats) {
        if ($when == 'type' || $when == 'parentId') {
            continue;
        }
        if (! in_array($when, ['day', 'lastday', 'week', 'lastweek', 'month', 'lastmonth', 'year', 'lastyear'])) {
            return tra('Incorrect parameter:') . $when;
        }
        $whats = explode(':', $whats);
        $types = explode(':', $params['type']);
        foreach ($types as $type) {
            foreach ($whats as $what) {
                switch ($type) {
                    case 'trackeritem':
                        if ($what != 'v' && $what != 'a') {
                            return tra('Incorrect parameter: ') . $what;
                        }
                        if (empty($params['parentId'])) {
                            $params['parentId'] = 0;
                        }
                        //for tracker items, only added items can be shown, so eith a or v will result in added items being displayed
                        $stat[$when][$type]['Added tracker items'] = $statslib->count_this_period('tiki_tracker_items', 'created', $when, 'trackerId', $params['parentId']);
                        break;
                    case 'wiki':
                        if ($what == 'v') {
                            $stat[$when][$type]['Viewed wiki pages'] = $statslib->hit_this_period('wiki', $when);
                        } elseif ($what == 'a') {
                            $stat[$when][$type]['Added wiki pages'] = $statslib->count_this_period('tiki_pages', 'created', $when);
                        } else {
                            return tra('Incorrect parameter: ') . $what;
                        }
                        break;
                    case 'article':
                        if ($what == 'v') {
                            $stat[$when][$type]['Viewed articles'] = $statslib->hit_this_period('article', $when);
                        } elseif ($what == 'a') {
                            $stat[$when][$type]['Added articles'] = $statslib->count_this_period('tiki_articles', 'created', $when);
                        } else {
                            return tra('Incorrect parameter: ') . $what;
                        }
                        break;
                    default:
                        return tra('Incorrect parameter: ') . $type;
                }
            }
        }
    }
    $smarty->assign_by_ref('stat', $stat);
    $code = $smarty->fetch('wiki-plugins/wikiplugin_stat.tpl');
    return "~np~$code~/np~";
247
}