-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLimeAdminNotifications.php
More file actions
475 lines (403 loc) · 17.1 KB
/
LimeAdminNotifications.php
File metadata and controls
475 lines (403 loc) · 17.1 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<?php
/**
* Plugin to send notifications to all survey administrators / backend users
*
*
* @author Eddy Lackmann <a.eddy@hotmail.de>
* @license GPL 2.0 or later
*/
//Get necessary libraries and component plugins
spl_autoload_register(function ($class_name) {
if (preg_match("/^LSAN.*/", $class_name)) {
if (file_exists(__DIR__ . DIRECTORY_SEPARATOR . 'models' . DIRECTORY_SEPARATOR . $class_name . '.php')) {
include __DIR__ . DIRECTORY_SEPARATOR . 'models' . DIRECTORY_SEPARATOR . $class_name . '.php';
} elseif (file_exists(__DIR__ . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . $class_name . '.php')) {
include __DIR__ . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . $class_name . '.php';
} elseif (file_exists(__DIR__ . DIRECTORY_SEPARATOR . 'installer' . DIRECTORY_SEPARATOR . $class_name . '.php')) {
include __DIR__ . DIRECTORY_SEPARATOR . 'installer' . DIRECTORY_SEPARATOR . $class_name . '.php';
}
}
});
class LimeAdminNotifications extends PluginBase
{
const DBNAME = "{{ls_admin_notifications}}";
protected static $name = 'LimeAdminNotifications';
protected static $description = 'Send Notifications to other users';
protected $storage = 'DbStorage';
protected $settings = array(
'autodeletedExpired' => array(
'type' => 'select',
'label' => 'Auto delete expired notifications.',
'default' => '0',
'options' => [
'0' => 'No',
'1' => 'Yes',
],
'help' => 'This option deletes all expired notifications when admin is logged in.'
),
);
/**
* Init the plugin / Suscribing to plugin events
*
* @return void
*
*/
public function init()
{
$this->subscribe('direct');
$this->subscribe('beforeActivate');
$this->subscribe('beforeDeactivate');
$this->subscribe('beforeAdminMenuRender');
$this->subscribe('afterSuccessfulLogin');
}
/**
* Install operation
*
* @return void
*/
public function beforeActivate()
{
//Register new table and populate it.
LSANPluginInstaller::instance()->install();
//create demo notification after install
$this->createDemoNotification();
}
/**
* Uninstall operation
* Plugin event
*
* @return void
*/
public function beforeDeactivate()
{
//Delete plugin table in the db
LSANPluginInstaller::instance()->uninstall();
}
/**
* Append new menu item to the admin topbar
* @return void
*/
public function beforeAdminMenuRender()
{
$oEvent = $this->getEvent();
$this->pageScripts();
if ($this->dbTableExist()) {
$counts = LSANNotification::countNotifications();
$badge = '';
$additionalClass = "";
if ($counts > 0) {
$badge = '<span class="badge badge-success">' . $counts . '</span>';
$additionalClass = "text-warning";
}
//Visible for all users
$aMenuItemUserOptions = [
'isDivider' => false,
'isSmallText' => false,
'label' => 'Notifications ' . $badge,
'href' => $this->api->createUrl('admin/pluginhelper/sa/fullpagewrapper/plugin/LimeAdminNotifications/method/index', []),
'iconClass' => 'fa fa-bullhorn ',
];
$aMenuItems[] = (new \LimeSurvey\Menu\MenuItem($aMenuItemUserOptions));
//Visible for users with users update permission
if (Permission::model()->hasGlobalPermission('users', 'update')) {
$aMenuItemAdminOptions = [
'isDivider' => false,
'isSmallText' => false,
'label' => gT('Administration'),
'href' => $this->api->createUrl('admin/pluginhelper/sa/fullpagewrapper/plugin/LimeAdminNotifications/method/admin', []),
'iconClass' => 'fa fa-gears',
];
$aMenuItems[] = (new \LimeSurvey\Menu\MenuItem($aMenuItemAdminOptions));
$aNewMenuOptions = [
'isDropDown' => true,
'label' => gT('Admin notifications') .' '. $badge,
'href' => '#',
'menuItems' => $aMenuItems,
'iconClass' => 'fa fa-bullhorn ' . $additionalClass,
];
} else {
$aNewMenuOptions = [
'isDropDown' => false,
'label' => ' Admin Notifications ' . $badge,
'href' => $this->api->createUrl('admin/pluginhelper/sa/fullpagewrapper/plugin/LimeAdminNotifications/method/index', []),
'iconClass' => 'fa fa-bullhorn ' . $additionalClass,
];
}
$oNewMenu = new LSANMenuClass($aNewMenuOptions);
$oEvent->append('extraMenus', [$oNewMenu]);
}
}
/**
* Method to run after user logged in.
* Plugin event
*
* @return void
*/
public function afterSuccessfulLogin()
{
if ($this->dbTableExist()) {
//Check high priorities notifications and pop up windows to user
LSANNotification::initHighPriorityNotifications();
}
}
/**
* Renders the list of all active notifications posted by admin users
* To be called by fullpagewrapper
*
* @return mixed
*/
public function index()
{
$notifications = LSANNotification::getNotifications();
$this->pageScripts();
return $this->renderPartial(
'index',
[
'notifications' => $notifications,
],
true
);
}
/**
* Method to create new notification
* To be called by fullpagewrapper
*
* @return mixed
*/
public function create()
{
//check user permission
if (!Permission::model()->hasGlobalPermission('users', 'update')) {
Yii::app()->setFlashMessage(gT("you are not allowed to enter this page."), 'error');
Yii::app()->getController()->redirect(array('/admin/pluginhelper/sa/fullpagewrapper/plugin/LimeAdminNotifications/method/index'));
}
$model = new LSANNotification();
//Post request
if (Yii::app()->request->getPost('LSANNotification')) {
//loads data into notification model
$model->attributes = Yii::app()->request->getPost('LSANNotification');
$model->priority = Yii::app()->request->getPost('LSANNotification')['priority'];
if ($model->save()) {
Yii::app()->setFlashMessage($model->title . ' ' . gT("published to all users."), 'success');
$model->appendToSystemNotification();
Yii::app()->getController()->redirect(array('/admin/pluginhelper/sa/fullpagewrapper/plugin/LimeAdminNotifications/method/admin'));
} else {
Yii::app()->setFlashMessage(gT("Error while creating new notification"), 'error');
}
}
$this->pageScripts();
return $this->renderPartial('admin/create', [
"model" => $model
], true);
}
/**
* Method to update a notification
* To be called by fullpagewrapper
*
* @return mixed
*/
public function update()
{
//check user permission
if (!Permission::model()->hasGlobalPermission('users', 'update')) {
Yii::app()->setFlashMessage(gT("you are not allowed to enter this page."), 'error');
Yii::app()->getController()->redirect(array('/admin/pluginhelper/sa/fullpagewrapper/plugin/LimeAdminNotifications/method/index'));
}
$model = new LSANNotification();
//chek request params
if (Yii::app()->getRequest()->getParam('id')) {
//find notification by id
$model = LSANNotification::model()->findByAttributes(
array("id" => Yii::app()->getRequest()->getParam('id'))
);
if ($model) {
//check post request for update
if (Yii::app()->request->getPost('LSANNotification')) {
//loads data in to notification model
$model->attributes = Yii::app()->request->getPost('LSANNotification');
$model->priority = Yii::app()->request->getPost('LSANNotification')['priority'];
$model->read_by_users = "[]";
if ($model->save()) {
Yii::app()->setFlashMessage($model->title . ' ' . gT("Notification updated"), 'success');
Yii::app()->getController()->redirect(array('/admin/pluginhelper/sa/fullpagewrapper/plugin/LimeAdminNotifications/method/admin'));
} else {
Yii::app()->setFlashMessage(gT("Error while creating new notification"), 'error');
}
}
$this->pageScripts();
return $this->renderPartial('admin/update', [
"model" => $model
], true);
}
} else {
Yii::app()->getController()->redirect(array('/admin/pluginhelper/sa/fullpagewrapper/plugin/LimeAdminNotifications/method/index'));
}
}
/**
* Method to mark a notification as read for connected user
* To be called by fullpagewrapper
*
* @return mixed
*/
public function markasread()
{
//get notification by id
if (Yii::app()->getRequest()->getParam('notif_id')) {
$notification = LSANNotification::model()->findByAttributes(
array("id" => Yii::app()->getRequest()->getParam('notif_id'))
);
}
//mark notification als read
if ($notification->markedAsRead()) {
Yii::app()->setFlashMessage($notification->title . ' ' . gT("marked as read"), 'success');
}
Yii::app()->getController()->redirect(array('/admin/pluginhelper/sa/fullpagewrapper/plugin/LimeAdminNotifications/method/index'));
}
/**
* This method delete a notification
* To be called by fullpagewrapper
*
* @return mixed
*/
public function delete()
{
//check user permission
if (!Permission::model()->hasGlobalPermission('users', 'update')) {
Yii::app()->setFlashMessage(gT("you are not allowed to enter this page."), 'error');
Yii::app()->getController()->redirect(array('/admin/pluginhelper/sa/fullpagewrapper/plugin/LimeAdminNotifications/method/index'));
}
//check request params
if (Yii::app()->getRequest()->getParam('notif_id')) {
//Find notification to delete by ID
$notification = LSANNotification::model()->findByAttributes(
array("id" => Yii::app()->getRequest()->getParam('notif_id'))
);
if ($notification) {
if ($notification->delete()) {
Yii::app()->setFlashMessage(gT("Notification deleted!"), 'success');
}
}
}
Yii::app()->getController()->redirect(array('/admin/pluginhelper/sa/fullpagewrapper/plugin/LimeAdminNotifications/method/admin'));
}
/**
* Renders the administration part for the Admin users | management of the notifications
* To be called by fullpagewrapper
*
* @return mixed
*/
public function admin()
{
//check user permission
if (!Permission::model()->hasGlobalPermission('users', 'update')) {
Yii::app()->setFlashMessage(gT("you are not allowed to enter this page."), 'error');
Yii::app()->getController()->redirect(array('/admin/pluginhelper/sa/fullpagewrapper/plugin/LimeAdminNotifications/method/index'));
}
if (Yii::app()->getRequest()->getQuery('pageSize')) {
Yii::app()->user->setState('pageSize', (int) Yii::app()->getRequest()->getQuery('pageSize'));
}
$model = new LSANNotification('search');
$iPageSize = Yii::app()->user->getState('pageSize', Yii::app()->params['defaultPageSize']);
$aData = [
'model' => $model,
'pageSize' => $iPageSize
];
$this->pageScripts();
return $this->renderPartial('admin/index', $aData, true);
}
/**
* Applies the necessary page scripts to the page through CClientScript derivate
*
* @return void
*/
protected function pageScripts()
{
$this->registerScript('assets/script.js', null, LSYii_ClientScript::POS_HEAD);
$this->registerCss('assets/style.css', null);
}
/**
* Adding a script depending on path of the plugin
* This method checks if the file exists depending on the possible different plugin locations, which makes this Plugin LimeSurvey Pro safe.
*
* @param string $relativePathToScript
* @param integer $pos See LSYii_ClientScript constants for options, default: LSYii_ClientScript::POS_BEGIN
* @return void
*/
protected function registerScript($relativePathToScript, $pos = LSYii_ClientScript::POS_BEGIN)
{
$parentPlugin = get_class($this);
$pathPossibilities = [
YiiBase::getPathOfAlias('userdir') . '/plugins/' . $parentPlugin . '/' . $relativePathToScript,
YiiBase::getPathOfAlias('webroot') . '/plugins/' . $parentPlugin . '/' . $relativePathToScript,
Yii::app()->getBasePath() . '/application/core/plugins/' . $parentPlugin . '/' . $relativePathToScript,
//added limesurvey 4 compatibilities
YiiBase::getPathOfAlias('webroot') . '/upload/plugins/' . $parentPlugin . '/' . $relativePathToScript,
];
$scriptToRegister = null;
foreach ($pathPossibilities as $path) {
if (file_exists($path)) {
$scriptToRegister = Yii::app()->getAssetManager()->publish($path);
}
}
Yii::app()->getClientScript()->registerScriptFile($scriptToRegister, $pos);
}
/**
* Adding a stylesheet depending on path of the plugin
* This method checks if the file exists depending on the possible different plugin locations, which makes this Plugin LimeSurvey Pro safe.
*
* @param string $relativePathToCss
* @return void
*/
protected function registerCss($relativePathToCss, $parentPlugin = null)
{
$parentPlugin = get_class($this);
$pathPossibilities = [
YiiBase::getPathOfAlias('userdir') . '/plugins/' . $parentPlugin . '/' . $relativePathToCss,
YiiBase::getPathOfAlias('webroot') . '/plugins/' . $parentPlugin . '/' . $relativePathToCss,
Yii::app()->getBasePath() . '/application/core/plugins/' . $parentPlugin . '/' . $relativePathToCss,
//added limesurvey 4 compatibilities
YiiBase::getPathOfAlias('webroot') . '/upload/plugins/' . $parentPlugin . '/' . $relativePathToCss,
];
$cssToRegister = null;
foreach ($pathPossibilities as $path) {
if (file_exists($path)) {
$cssToRegister = Yii::app()->getAssetManager()->publish($path);
}
}
Yii::app()->getClientScript()->registerCssFile($cssToRegister);
}
/**
* method to check if database exist before doing db operation || Some events are fired even when the puglin is not activated
*
* @return bool false if db not exists and true if db exits
*/
protected function dbTableExist()
{
return (Yii::app()->db->schema->getTable(self::DBNAME, true) !== null);
}
/**
* Create a dummy notification
* This method creates the first notification when the plugin is activated
*
* @return bool save state of the notification
*/
protected function createDemoNotification()
{
$user = User::model()->findByPk(Yii::app()->user->id);
$notification = new LSANNotification();
$notification->uid = Yii::app()->user->id;
$notification->priority = $notification::HIGH_PRIORITY;
$notification->username = $user->users_name;
$notification->title = gT("Welcome to Lime Admin Notifications");
$notification->message = gT("This notification is automatically generated by the admin notification plugin. The admin notification section will contains messages, announcements, news and notifications shared by the administrators") . ". High priority messages will be displayed in a pop up box when you are logged in." . ' <p> Visit our <a href="https://account.limesurvey.org/limestore">Plugins store (LimeStore) </a> for more informations.</p>';
$notification->created = date('Y-m-d H:i:s');
$notification->modified = date('Y-m-d H:i:s');
$notification->expires = date('Y-m-d', strtotime($notification->created . ' +1 day'));
if ($notification->save()) {
$notification->appendToSystemNotification();
return true;
} else {
return false;
}
}
}