-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickMenu.php
561 lines (525 loc) · 21.3 KB
/
QuickMenu.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
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
<?php
/**
* Some extra quick-menu items to ease everyday usage
*
* @since 2016-04-22
* @author Olle Härstedt
*/
class QuickMenu extends \ls\pluginmanager\PluginBase
{
static protected $description = 'Add a quick-menu when the question explorer is collapsed';
static protected $name = 'QuickMenu';
protected $storage = 'DbStorage';
protected $settings = array(
'info' => array(
'type' => 'info',
'content' => '<div class="well col-sm-8"><span class="fa fa-info-circle"></span> Choose which buttons to show in the quick-menu. The buttons are visible to all back-end users. Some buttons will be hidden due to permissions.</div>'
),
'activateSurvey' => array(
'type' => 'checkbox',
'label' => 'Activate survey <span class="glyphicon glyphicon-play"></span>',
'default' => '0',
'help' => 'Needed permission: Survey activation - Update'
),
'deactivateSurvey' => array(
'type' => 'checkbox',
'label' => 'Deactivate survey <span class="glyphicon glyphicon-stop"></span>',
'default' => '0',
'help' => 'Needed permission: Survey activation - Update'
),
'testSurvey' => array(
'type' => 'checkbox',
'label' => 'Test or execute survey <span class="glyphicon glyphicon-cog"></span>',
'default' => '0',
'help' => 'Available for everyone. Uses survey base language.'
),
'listQuestions' => array(
'type' => 'checkbox',
'label' => 'List questions <span class="glyphicon glyphicon-list"></span>',
'default' => '0',
'help' => 'Needed permission: Survey content - View'
),
'listQuestionGroups' => array(
'type' => 'checkbox',
'label' => 'List question groups <span class="glyphicon glyphicon-list"></span>',
'default' => '0',
'help' => 'Needed permission: Survey content - View'
),
'surveySettings' => array(
'type' => 'checkbox',
'label' => 'Survey settings <span class="glyphicon icon-edit"></span>',
'default' => '0',
'help' => 'Needed permission: Survey settings - View'
),
'surveySecurity' => array(
'type' => 'checkbox',
'label' => 'Survey security <span class="glyphicon icon-security"></span>',
'default' => '0',
'help' => 'Needed permission: Survey security - View'
),
'quotas' => array(
'type' => 'checkbox',
'label' => 'Quotas <span class="glyphicon icon-quota"></span>',
'default' => '0',
'help' => 'Needed permission: Quotas - View'
),
'assessments' => array(
'type' => 'checkbox',
'label' => 'Assessments <span class="glyphicon icon-assessments"></span>',
'default' => '0',
'help' => 'Needed permission: Assessments - View'
),
'emailTemplates' => array(
'type' => 'checkbox',
'label' => 'E-mail templates <span class="glyphicon icon-emailtemplates"></span>',
'default' => '0',
'help' => 'Needed permission: Locale - View'
),
'surveyLogicFile' => array(
'type' => 'checkbox',
'label' => 'Survey logic file <span class="glyphicon icon-expressionmanagercheck"></span>',
'default' => '0',
'help' => 'Needed permission: Survey content - View. Uses survey base language.'
),
'tokenManagement' => array(
'type' => 'checkbox',
'label' => 'Token management <span class="glyphicon glyphicon-user"></span>',
'default' => '0',
'help' => 'Needed permission: Token - View'
),
'cpdb' => array(
'type' => 'checkbox',
'label' => 'Central participant database <span class="fa fa-users"></span>',
'default' => '0',
'help' => 'Needed permission: ?'
),
'responses' => array(
'type' => 'checkbox',
'label' => 'Responses <span class="glyphicon icon-browse"></span>',
'default' => '0',
'help' => 'Needed permission: Responses - View'
),
'statistics' => array(
'type' => 'checkbox',
'label' => 'Statistics <span class="glyphicon glyphicon-stats"></span>',
'default' => '0',
'help' => 'Needed permission: Statistics - View'
),
'reorder' => array(
'type' => 'checkbox',
'label' => 'Reorder questions and question groups <span class="icon-organize"></span>',
'default' => 0,
'help' => 'Needed permission: Survey content - Update'
)
);
private $buttons = array();
public function init()
{
$this->subscribe('afterQuickMenuLoad');
$this->subscribe('beforeActivate');
$this->subscribe('beforeDeactivate');
$this->subscribe('newDirectRequest');
}
/**
* At activation, create database for
* quick-menu items sort order
*/
public function beforeActivate()
{
$oDB = Yii::app()->getDb();
$oDB->schemaCachingDuration=0; // Deactivate schema caching
$oTransaction = $oDB->beginTransaction();
try
{
$aFields = array(
'uid' => 'integer NOT NULL',
'button_name' => 'string(64)',
'sort_order' => 'integer',
'PRIMARY KEY (button_name, uid)'
);
$oDB->createCommand()->createTable('{{plugin_extraquickmenuitems_sortorder}}', $aFields);
$oDB->createCommand()->addForeignKey(
'fk_survey_id',
'{{plugin_extraquickmenuitems_sortorder}}',
'uid',
'{{users}}',
'uid',
'CASCADE',
'CASCADE'
);
$oTransaction->commit();
}
catch(Exception $e)
{
$oTransaction->rollback();
// Activate schema caching
$oDB->schemaCachingDuration = 3600;
// Load all tables of the application in the schema
$oDB->schema->getTables();
// Clear the cache of all loaded tables
$oDB->schema->refresh();
$event = $this->getEvent();
$event->set('success', false);
$event->set(
'message',
gT('An non-recoverable error happened during the update. Error details:')
. "<p>"
. htmlspecialchars($e->getMessage())
. "</p>"
);
return;
}
}
/**
* Remove database tables at deactivation
*/
public function beforeDeactivate()
{
// Remove table
$oDB = Yii::app()->getDb();
$oDB->schemaCachingDuration=0; // Deactivate schema caching
$oTransaction = $oDB->beginTransaction();
try
{
$oDB->createCommand()->dropTable('{{plugin_extraquickmenuitems_sortorder}}');
$oTransaction->commit();
}
catch(Exception $e)
{
$oTransaction->rollback();
// Activate schema caching
$oDB->schemaCachingDuration = 3600;
// Load all tables of the application in the schema
$oDB->schema->getTables();
// Clear the cache of all loaded tables
$oDB->schema->refresh();
$event = $this->getEvent();
$event->set(
'message',
gT('An non-recoverable error happened during the update. Error details:')
. "<p>"
. htmlspecialchars($e->getMessage())
. '</p>'
);
return;
}
}
/**
* @param array $data
* @return void
*/
private function initialiseButtons(array $data)
{
$surveyId = $data['surveyid'];
$activated = $data['activated'];
$survey = $data['oSurvey'];
$baselang = $survey->language;
$this->buttons = array(
'activateSurvey' => new QuickMenuButton(array(
'name' => 'activateSurvey',
'href' => Yii::app()->getController()->createUrl("admin/survey/sa/activate/surveyid/$surveyId"),
'tooltip' => gT('Activate survey'),
'iconClass' => 'glyphicon glyphicon-play navbar-brand',
'showOnlyWhenSurveyIsDeactivated' => true,
'neededPermission' => array('surveyactivation', 'update')
)),
'deactivateSurvey' => new QuickMenuButton(array(
'name' => 'deactivateSurvey',
'href' => Yii::app()->getController()->createUrl("admin/survey/sa/deactivate/surveyid/$surveyId"),
'tooltip' => gT('Stop this survey'),
'iconClass' => 'glyphicon glyphicon-stop navbar-brand',
'showOnlyWhenSurveyIsActivated' => true,
'neededPermission' => array('surveyactivation', 'update')
)),
'testSurvey' => new QuickMenuButton(array(
'name' => 'testSurvey',
'openInNewTab' => true,
'href' => Yii::app()->getController()->createUrl("survey/index/sid/$surveyId/newtest/Y/lang/$baselang"),
'tooltip' => $activated ? gT('Execute survey') : gT('Test survey'),
'iconClass' => 'glyphicon glyphicon-cog navbar-brand'
)),
'listQuestions' => new QuickMenuButton(array(
'name' => 'listQuestions',
'href' => Yii::app()->createUrl("admin/survey/sa/listquestions/surveyid/$surveyId"),
'tooltip' => gT('List questions'),
'iconClass' => 'glyphicon glyphicon-list navbar-brand',
'neededPermission' => array('surveycontent', 'read')
)),
'listQuestionGroups' => new QuickMenuButton(array(
'name' => 'listQuestionGroups',
'href' => Yii::app()->createUrl("admin/survey/sa/listquestiongroups/surveyid/$surveyId"),
'tooltip' => gT('List question groups'),
'iconClass' => 'glyphicon glyphicon-list navbar-brand',
'neededPermission' => array('surveycontent', 'read')
)),
'surveySettings' => new QuickMenuButton(array(
'name' => 'surveySettings',
'href' => Yii::app()->getController()->createUrl("admin/survey/sa/editlocalsettings/surveyid/$surveyId"),
'tooltip' => gT('General settings & texts'),
'iconClass' => 'glyphicon icon-edit navbar-brand',
'neededPermission' => array('surveysettings', 'read')
)),
'surveySecurity' => new QuickMenuButton(array(
'name' => 'surveySecurity',
'href' => Yii::app()->getController()->createUrl("admin/surveypermission/sa/view/surveyid/$surveyId"),
'tooltip' => gT('Survey permissions'),
'iconClass' => 'glyphicon icon-security navbar-brand',
'neededPermission' => array('surveysecurity', 'read')
)),
'quotas' => new QuickMenuButton(array(
'name' => 'quotas',
'href' => Yii::app()->getController()->createUrl("admin/quotas/sa/view/surveyid/$surveyId"),
'tooltip' => gT('Quotas'),
'iconClass' => 'glyphicon icon-quota navbar-brand',
'neededPermission' => array('quotas', 'read')
)),
'assessments' => new QuickMenuButton(array(
'name' => 'assessments',
'href' => Yii::app()->getController()->createUrl("admin/assessments/sa/view/surveyid/$surveyId"),
'tooltip' => gT('Assessments'),
'iconClass' => 'glyphicon icon-assessments navbar-brand',
'neededPermission' => array('assessments', 'read')
)),
'emailTemplates' => new QuickMenuButton(array(
'name' => 'emailTemplates',
'href' => Yii::app()->getController()->createUrl("admin/emailtemplates/sa/view/surveyid/$surveyId"),
'tooltip' => gT('E-mail templates'),
'iconClass' => 'glyphicon icon-emailtemplates navbar-brand',
'neededPermission' => array('surveylocale', 'read')
)),
'surveyLogicFile' => new QuickMenuButton(array(
'name' => 'surveyLogicFile',
'href' => Yii::app()->getController()->createUrl("admin/expressions/sa/survey_logic_file/sid/$surveyId/"),
'tooltip' => gT('Survey logic file'),
'iconClass' => 'glyphicon icon-expressionmanagercheck navbar-brand',
'neededPermission' => array('surveycontent', 'read')
)),
'tokenManagement' => new QuickMenuButton(array(
'name' => 'tokenManagement',
'href' => Yii::app()->getController()->createUrl("admin/tokens/sa/index/surveyid/$surveyId"),
'tooltip' => gT('Token management'),
'iconClass' => 'glyphicon glyphicon-user navbar-brand',
'neededPermission' => array('tokens', 'read')
)),
'cpdb' => new QuickMenuButton(array(
'name' => 'cpdb',
'href' => Yii::app()->getController()->createUrl("admin/participants/sa/displayParticipants"),
'tooltip' => gT('Central participant database'),
'iconClass' => 'fa fa-users navbar-brand',
'neededPermission' => array('tokens', 'read')
)),
'responses' => new QuickMenuButton(array(
'name' => 'responses',
'href' => Yii::app()->getController()->createUrl("admin/responses/sa/browse/surveyid/$surveyId/"),
'tooltip' => gT('Responses'),
'iconClass' => 'glyphicon icon-browse navbar-brand',
'showOnlyWhenSurveyIsActivated' => true,
'neededPermission' => array('responses', 'read')
)),
'statistics' => new QuickMenuButton(array(
'name' => 'statistics',
'href' => Yii::app()->getController()->createUrl("admin/responses/sa/browse/surveyid/$surveyId/"),
'href' => Yii::app()->getController()->createUrl("admin/statistics/sa/index/surveyid/$surveyId"),
'tooltip' => gT('Statistics'),
'iconClass' => 'glyphicon glyphicon-stats navbar-brand',
'showOnlyWhenSurveyIsActivated' => true,
'neededPermission' => array('statistics', 'read')
)),
'reorder' => new QuickMenuButton(array(
'name' => 'reorder',
'href' =>Yii::app()->getController()->createUrl("admin/survey/sa/organize/surveyid/$surveyId"),
'tooltip' => gT('Reorder questions/question groups'),
'iconClass' => 'icon-organize',
'showOnlyWhenSurveyIsDeactivated' => true,
'neededPermission' => array('surveycontent', 'update')
))
);
// Central participant database
/*
$buttons[] = array(
'openInNewTab' => false,
'href' => Yii::app()->getController()->createUrl("admin/participants/sa/displayParticipants"),
'tooltip' => gT('Central participant database'),
'iconClass' => 'glyphicon TODO: Icon navbar-brand'
);
*/
}
/**
* Check if user has permission to show this button
*
* @param int $surveyId
* @param QuickMenuButton $button
* @return bool
*/
private function hasPermission($surveyId, $button)
{
// Check for permission to show button
if ($button['neededPermission'] !== null)
{
$hasPermission = Permission::model()->hasSurveyPermission(
$surveyId,
$button['neededPermission'][0],
$button['neededPermission'][1]
);
return $hasPermission;
}
return true;
}
/**
* Return list of buttons that will be shown for this page load
*
* @param int $surveyId
* @param bool $activated - True if survey is activated
* @param array $settings - Plugin settings
* @return array<QuickMenuButton>
*/
private function getButtonsToShow($surveyId, $activated, $settings)
{
$buttonsToShow = array();
// Loop through all buttons and check settings and activation
foreach ($this->buttons as $buttonName => $button)
{
if ($settings[$buttonName]['current'] === '1')
{
if (!$this->hasPermission($surveyId, $button))
{
continue;
}
// Check if survey is active and whether or not to show button
if ($button['showOnlyWhenSurveyIsActivated'] && $activated)
{
$buttonsToShow[$buttonName] = $button;
}
elseif ($button['showOnlyWhenSurveyIsDeactivated'] && !$activated)
{
$buttonsToShow[$buttonName] = $button;
}
elseif (!$button['showOnlyWhenSurveyIsActivated'] &&
!$button['showOnlyWhenSurveyIsDeactivated'])
{
$buttonsToShow[$buttonName] = $button;
}
}
}
return $buttonsToShow;
}
public function afterQuickMenuLoad()
{
$event = $this->getEvent();
$settings = $this->getPluginSettings(true);
$data = $event->get('aData');
$activated = $data['activated'];
$surveyId = $data['surveyid'];
$this->initialiseButtons($data);
$buttonsToShow = $this->getButtonsToShow($surveyId, $activated, $settings);
$userId = Yii::app()->user->getId();
$buttonOrders = self::getOrder($userId);
foreach ($buttonsToShow as $button)
{
if (isset($buttonOrders[$button['name']]))
{
$button->setOrder($buttonOrders[$button['name']]);
}
}
$event->append('quickMenuItems', $buttonsToShow);
}
/**
* Save order after drag-n-drop sorting
*
* @param LSHttpRequest $request
* @return void
*/
public function saveOrder(LSHttpRequest $request)
{
$buttons = $request->getParam('buttons');
$userId = Yii::app()->user->getId();
try
{
$this->deleteOldSortings($userId);
$this->insertNewSortings($userId, $buttons);
}
catch(Exception $ex)
{
// Any error is sent as JSON to client
return json_encode(array(
'result' => 'error',
'message' => $ex->getMessage()
));
}
return json_encode(array('result' => 'success'));
}
/**
* Delete all old button sortings for the user
*
* @param int $userId
* @return void
*/
protected function deleteOldSortings($userId)
{
// Delete all old sortings
$db = Yii::app()->db;
$db->createCommand()
->delete(
'{{plugin_extraquickmenuitems_sortorder}}',
'uid=:uid',
array(':uid' => $userId)
);
}
/**
* Insert new button sortings
*
* @param int $userId
* @param array<string, int> $buttons - button name => sorting index
*/
protected function insertNewSortings($userId, $buttons)
{
$db = Yii::app()->db;
foreach ($buttons as $buttonName => $buttonIndex)
{
$db->createCommand()->insert(
'{{plugin_extraquickmenuitems_sortorder}}',
array(
'uid' => $userId,
'button_name' => $buttonName,
'sort_order' => $buttonIndex)
);
}
}
/**
* Get sort order of buttons from database
*
* @param int $uid
* @return array
*/
public static function getOrder($userId)
{
$db = Yii::app()->db;
$orders = $db->createCommand()
->select(array('button_name', 'sort_order'))
->from('{{plugin_extraquickmenuitems_sortorder}}')
->where('uid=:uid', array(':uid' => $userId))
->order('sort_order')
->queryAll();
$result = array();
foreach ($orders as $ordering)
{
$result[$ordering['button_name']] = $ordering['sort_order'];
}
return $result;
}
public function newDirectRequest()
{
$event = $this->event;
if ($event->get('target') == "QuickMenu")
{
// you can get other params from the request object
$request = $event->get('request');
//get the function name to call and use the method call_user_func
$functionToCall = $event->get('function');
//$content = call_user_func(array($this,$functionToCall), $surveyId);
//set the content on the event
//$event->setContent($this, $content);
echo $this->$functionToCall($request);
}
}
}