/
class.promoteaftermoderated.plugin.php
251 lines (230 loc) · 9.27 KB
/
class.promoteaftermoderated.plugin.php
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
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
247
248
249
250
251
<?php
$PluginInfo['promoteAfterModerated'] = [
'Name' => 'Promote After Moderated',
'Description' => 'Allows automatic role changing after a given number of posts have been approved.',
'Version' => '0.4',
'RequiredApplications' => ['Vanilla' => '2.3'],
'MobileFriendly' => true,
'HasLocale' => true,
'Author' => 'Robin Jurinka',
'AuthorUrl' => 'https://vanillaforums.org/profile/r_j',
'SettingsUrl' => '/settings/promoteaftermoderated',
'License' => 'MIT'
];
/**
* Plugin that allows automatic role promotion after X moderated posts.
*
* Admin can set a number for moderated comments and/or discussions after which
* a user will be promoted. The role that should be revoked and the role that
* will be given can be configured.
*/
class PromoteAfterModeratedPlugin extends Gdn_Plugin {
/**
* Pre-fill settings with sane settings.
*
* @return void.
*/
public function setup() {
touchConfig(
'promoteAfterModerated.ToRoleID',
RoleModel::getDefaultRoles(RoleModel::TYPE_MEMBER)
);
touchConfig('Preferences.Popup.RolePromotion', 1);
$this->structure();
}
/**
* Create activity type for role promotion.
*
* @return void.
*/
public function structure() {
$activityModel = new ActivityModel();
$activityModel->defineType(
'RolePromotion',
[
'Notify' => 1,
'Public' => 0
]
);
}
/**
* Notify user of his role promotion.
*
* @param Integer $userID ID of the user to notify.
* @param Integer $roleID ID of the role the user has been assigned.
*
* @return void.
*/
private function roleChangeActivity($userID, $roleID) {
$activityModel = new ActivityModel();
$activityModel->queue(
[
'ActivityType' => 'RolePromotion',
'ActivityUserID' => $userID,
'RegardingUserID' => $userID,
'NotifyUserID' => $userID,
'HeadlineFormat' => t('{NotifyUserID,You} have been promoted.'),
'Story' => t('Your posts do no longer require moderation.')
],
'RolePromotion',
['Force' => true]
);
$activityModel->saveQueue();
}
/**
* Settings page.
*
* @param SettingsController $sender Instance of the calling class.
*
* @return void.
*/
public function settingsController_promoteAfterModerated_create($sender) {
$sender->permission('Garden.Settings.Manage');
$sender->addSideMenu('dashboard/settings/plugins');
$sender->setData('Title', t('Promotion Rule'));
// Get role names.
$roleModel = new RoleModel();
$roles = $roleModel->getArray();
// Filter out admins and mods (too dangerous).
$adminRoles = $roleModel->getbyType($roleModel::TYPE_ADMINISTRATOR);
foreach ($adminRoles as $role) {
unset($roles[$role->RoleID]);
}
$modRoles = $roleModel->getbyType($roleModel::TYPE_MODERATOR);
foreach ($modRoles as $role) {
unset($roles[$role->RoleID]);
}
$sender->setData('AvailableRoles', $roles);
// Prepare form fields.
$validation = new Gdn_Validation();
$configurationModel = new Gdn_ConfigurationModel($validation);
$configurationModel->setField(
[
'promoteAfterModerated.MinComments',
'promoteAfterModerated.MinDiscussions',
'promoteAfterModerated.MinPosts',
'promoteAfterModerated.FromRoleID',
'promoteAfterModerated.ToRoleID'
]
);
$sender->Form->setModel($configurationModel);
if ($sender->Form->authenticatedPostBack() === false) {
// If form is displayed "unposted".
$sender->Form->setData($configurationModel->Data);
} else {
// Validate posted form.
$sender->Form->validateRule('promoteAfterModerated.ToRoleID', 'ValidateRequired');
$sender->Form->validateRule('promoteAfterModerated.ToRoleID', 'ValidateInteger');
$sender->Form->validateRule('promoteAfterModerated.FromRoleID', 'ValidateRequired');
$sender->Form->validateRule('promoteAfterModerated.FromRoleID', 'ValidateInteger');
$sender->Form->validateRule('promoteAfterModerated.MinComments', 'ValidateRequired');
$sender->Form->validateRule('promoteAfterModerated.MinComments', 'ValidateInteger');
$sender->Form->validateRule('promoteAfterModerated.MinDiscussions', 'ValidateRequired');
$sender->Form->validateRule('promoteAfterModerated.MinDiscussions', 'ValidateInteger');
$sender->Form->validateRule('promoteAfterModerated.MinPosts', 'ValidateRequired');
$sender->Form->validateRule('promoteAfterModerated.MinPosts', 'ValidateInteger');
// Check if either comment/discussion or post is set, but not both.
if ($sender->Form->getValue('promoteAfterModerated.MinPosts', 0) != 0) {
if (
$sender->Form->getValue('promoteAfterModerated.MinComments', 0) +
$sender->Form->getValue('promoteAfterModerated.MinDiscussions', 0) > 0
) {
$sender->Form->addError('Please set either min. comment/discussion count or post count, but not both.');
}
}
// Ensure that new role doesn't need moderation.
$roleModel = new RoleModel();
$permissions = $roleModel->getPermissions($sender->Form->getValue('promoteAfterModerated.ToRoleID'));
if ($permissions[0]['Vanilla.Approval.Require'] == true) {
$sender->Form->addError('This role hasn\'t permission to post unmoderated. Choosing this role doesn\'t make sense', 'promoteAfterModerated.ToRoleID');
}
// Try saving values.
if ($sender->Form->save() !== false) {
$sender->informMessage(
sprite('Check', 'InformSprite').t('Your settings have been saved.'),
['CssClass' => 'Dismissable AutoDismiss HasSprite']
);
}
}
$sender->render($this->getView('settings.php'));
}
/**
* Check if log entry is pending post and level up user.
*
* @param LogModel $sender Instance of the calling class.
* @param mixed $args Event arguments.
*
* @return void.
*/
public function logModel_afterRestore_handler($sender, $args) {
// Only take action for pending posts.
if (
$args['Log']['Operation'] != 'Pending' ||
!in_array($args['Log']['RecordType'], ['Comment', 'Discussion'])
) {
return;
}
// Make sure plugin is configured.
$config = c('promoteAfterModerated');
$minComments = c('promoteAfterModerated.MinComments', false);
$minDiscussions = c('promoteAfterModerated.MinDiscussions', false);
$minPosts = c('promoteAfterModerated.MinPosts', false);
$fromRoleID = c('promoteAfterModerated.FromRoleID', false);
$roleID = c('promoteAfterModerated.ToRoleID', false);
// All settings must be set.
if (
$minComments === false ||
$minDiscussions === false ||
$minPosts === false ||
$roleID === false
) {
return;
}
// Get the current users post counts.
$countComments = Gdn::sql()->getCount(
'Comment',
['InsertUserID' => $args['Log']['InsertUserID']]
);
$countDiscussions = Gdn::sql()->getCount(
'Discussion',
['InsertUserID' => $args['Log']['InsertUserID']]
);
if ($minPosts > 0) {
if ($countComments + $countDiscussions < $minPosts) {
// Break if check for posts but not enough yet.
return;
}
} elseif ($minComments > 0 || $minDiscussions > 0) {
if ($countComments < $minComments || $countDiscussions < $minDiscussions) {
// Break if comment and discussion check but not enough yet.
return;
}
} else {
// If not configured properly, break anyway.
return;
}
// Get all current roles.
$currentRoles = Gdn::userModel()->getRoles(
$args['Log']['InsertUserID']
)->resultArray();
$newRoles = array_column($currentRoles, 'RoleID');
// Ensure user has FromRoleID.
if (!in_array($fromRoleID, $newRoles)) {
return;
}
// Remove old role.
$newRoles = array_diff($newRoles, [$fromRoleID]);
// Add the new role.
$newRoles[] = $roleID;
// Level up!
Gdn::userModel()->saveRoles(
$args['Log']['InsertUserID'],
$newRoles,
true
);
$this->roleChangeActivity($args['Log']['InsertUserID'], $roleID);
// Give feedback to admin.
$user = Gdn::userModel()->getID($userID);
Gdn::controller()->informMessage(sprintf(t('%1$s has been promoted and his/her posts will no longer need moderation'), $user->Name));
}
}