-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass.reusablestats.plugin.php
More file actions
executable file
·202 lines (174 loc) · 6.74 KB
/
class.reusablestats.plugin.php
File metadata and controls
executable file
·202 lines (174 loc) · 6.74 KB
1
2
3
4
5
6
7
8
9
10
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php if (!defined('APPLICATION')) exit();
/*
Copyright 2013 Alessandro Miliucci <lifeisfoo@gmail.com>
This file is part of ReusableStats <https://github.com/lifeisfoo/ReusableStats>
ReusableStats is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ReusableStats is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ReusableStats. If not, see <http://www.gnu.org/licenses/>.
*/
// Define the plugin:
$PluginInfo['ReusableStats'] = array(
'Description' => 'Expose additiona smarty tag to get forum stats. Moreover add a special url to provide this data in json (enabled via configuration). Provide additional stats if you are using WhoisOnline plugin.',
'Version' => '0.2.3',
'RequiredApplications' => array('Vanilla' => '2.0.18'),
'RequiredTheme' => FALSE,
'RequiredPlugins' => FALSE,
'HasLocale' => FALSE,
'MobileFriendly' => TRUE,
'SettingsUrl' => '/plugin/reusablestats',
'SettingsPermission' => 'Garden.AdminUser.Only',
'Author' => "Alessandro Miliucci",
'AuthorEmail' => 'lifeisfoo@gmail.com',
'AuthorUrl' => 'http://forkwait.net',
'License' => 'GPLv3'
);
class ReusableStatsPlugin extends Gdn_Plugin {
public function __construct() {}
public function Base_Render_Before($Sender) {
$Sender->SetData('threads', $this->DiscussionsCount());
$Sender->SetData('posts', $this->CommentsCount());
$Sender->SetData('members', $this->MembersCount());
$Sender->SetData('role_members', $this->MembersPerRole());
$Sender->SetData('total_views', $this->totalViews());
if(C('EnabledPlugins.WhosOnline')){
$Sender->SetData('role_members_online', $this->MembersOnlinePerRole());
}
/*
HOWTO use these vars in your theme tpl file? Just add this code to your file!
Threads: {$threads} |
Posts: {$posts} |
Members: {$members} |
RoleMembers: {$role_members.PDI}
RoleMembersOnline: {$role_members_online.PDI}
TotalViews: {$total_views}
HOWTO use these vars in your theme php file?
$this->Data['threads'];
$this->Data['posts'];
$this->Data['members'];
$this->Data['role_members']['PDI'];//vanilla role name, case sensitive
$this->Data['role_members_online']['PDI'];//vanilla role name, case sensitive
$this->Data['total_views'];
*/
}
public function PluginController_ReusableStats_Create($Sender) {
//Settings page
$Sender->Permission('Garden.Plugins.Manage');
$Sender->AddSideMenu();
$Sender->Title('Reusable Stats Settings');
$ConfigurationModule = new ConfigurationModule($Sender);
$ConfigurationModule->RenderAll = True;
$Schema = array(
'Plugins.ReusableStats.JsonStat' =>
array('LabelCode' => T('Expose stats via json at http://example.com/forum/jsonstat'),
'Control' => 'CheckBox',
'Default' => C('Plugins.ReusableStats.JsonStat', '0')
)
);
$ConfigurationModule->Schema($Schema);
$ConfigurationModule->Initialize();
$Sender->View = dirname(__FILE__) . DS . 'views' . DS . 'settings.php';
$Sender->ConfigurationModule = $ConfigurationModule;
$Sender->Render();
}
public function PluginController_ReusableStatsJSON_Create($Sender) {
if(C('Plugins.ReusableStats.JsonStat', '0') == true) {
$Sender->DeliveryMethod(DELIVERY_METHOD_JSON);
$Sender->DeliveryType(DELIVERY_TYPE_DATA);
$Sender->Render();
}
}
private function MembersCount(){
$UserModel = new UserModel();
return $UserModel->GetCountWhere();
}
private function DiscussionsCount(){
$DiscussionModel = new DiscussionModel();
return $DiscussionModel->GetCount();
}
private function CommentsCount(){
$CommentModel = new CommentModel();
return $CommentModel->GetCountWhere();
}
private function MembersPerRole() {
$RoleModel = new RoleModel();
$RolesCount = array();
foreach ($RoleModel->GetArray() as $RoleID => $RoleName) {
$RolesCount[$RoleName] = $this->RoleMembers($RoleID);
}
return $RolesCount;
}
private function MembersOnlinePerRole() {
$Frequency = C('WhosOnline.Frequency', 4);
$History = time() - $Frequency;
$OnlineRolesCount = array();
$Online = Gdn::SQL()->Select('r.Name')
->Select('u.userID', 'count', 'CountUsers')
->From('Whosonline w')
->Join('User u', 'w.UserID = u.UserID')
->Join('UserRole ur', 'u.UserID = ur.UserID')
->Join('Role r', 'ur.RoleID = r.RoleID')
->Where('w.Timestamp >=', date('Y-m-d H:i:s', $History))
->Where('w.Invisible', 0)//only non-invisible users
->GroupBy('r.Name')
->Get()->Result();
foreach ($Online as $OnlinePerRole) {
$OnlineRolesCount[$OnlinePerRole->Name] = $OnlinePerRole->CountUsers;
}
//fill eventually empty roles
$RoleModel = new RoleModel();
foreach ($RoleModel->GetArray() as $RoleID => $RoleName) {
if(!$OnlineRolesCount[$RoleName]) {
$OnlineRolesCount[$RoleName] = "0";
}
}
return $OnlineRolesCount;
}
private function RoleMembers($RoleID){
$Data = Gdn::SQL()->Select('u.UserID', 'count', 'UserCount')
->From('User u')
->Join('UserRole ur', 'u.UserID = ur.UserID ')
->Where('u.Deleted', 0)
->Where('ur.RoleID', $RoleID)
->Get()
->FirstRow();
return $Data === FALSE ? 0 : $Data->UserCount;
}
private function totalViews() {
// Get from cache to be nice to db.
$totalViewCount = Gdn::cache()->get('ReusableStatsTotalViews');
if ($totalViewCount !== Gdn_Cache::CACHEOP_FAILURE) {
return $totalViewCount;
}
// If not in cache, get from db.
$totalViewCount = Gdn::sql()
->select('CountViews', 'sum', 'TotalViewCount')
->from('Discussion')
->get()
->firstRow()
->TotalViewCount;
// Store for later retrievable.
Gdn::cache()->store(
'ReusableStatsTotalViews',
$totalViewCount,
[Gdn_Cache::FEATURE_EXPIRY => 30] // 30 seconds
);
return $totalViewCount;
}
public function Setup() {
if(!Gdn::Router()->MatchRoute('jsonstat')) {
Gdn::Router()->SetRoute('jsonstat', '/plugin/ReusableStatsJSON', 'Internal');
}
}
public function OnDisable() {
if(Gdn::Router()->MatchRoute('jsonstat')) {
Gdn::Router()->DeleteRoute('jsonstat');
}
}
}