5
5
*/
6
6
class TrialController extends BaseModuleController
7
7
{
8
+ /**
9
+ * The return code for actionTransitionState() if the transition was a success
10
+ */
8
11
const RETURN_CODE_OK = '0 ' ;
12
+ /**
13
+ * The return code for actionTransitionState() if the user tried to transition an open trial to in progress
14
+ * while a patient is still shortlisted
15
+ */
9
16
const RETURN_CODE_CANT_OPEN_SHORTLISTED_TRIAL = '1 ' ;
17
+
18
+ /**
19
+ * The return code for actionAddPermission() if the user tried to share the trial with a user that it is
20
+ * already shared with
21
+ */
10
22
const RETURN_CODE_USER_PERMISSION_ALREADY_EXISTS = '2 ' ;
11
23
24
+
25
+ /**
26
+ * The return code for actionRemovePermission() if all went well
27
+ */
28
+ const REMOVE_PERMISSION_RESULT_SUCCESS = 'success ' ;
29
+ /**
30
+ * The return code for actionRemovePermission() if the user tried to remove the last user with manage privileges
31
+ */
32
+ const REMOVE_PERMISSION_RESULT_CANT_REMOVE_LAST = 'remove_last_fail ' ;
33
+ /**
34
+ * The return code for actionRemovePermission() if the user tried to remove themselves from the Trial
35
+ */
36
+ const REMOVE_PERMISSION_RESULT_CANT_REMOVE_SELF = 'remove_self_fail ' ;
37
+
12
38
/**
13
39
* @return array action filters
14
40
*/
15
41
public function filters ()
16
42
{
17
43
return array (
18
44
'accessControl ' ,
19
- 'ajaxOnly + getTrialList '
45
+ 'ajaxOnly + getTrialList ' ,
20
46
);
21
47
}
22
48
@@ -29,29 +55,37 @@ public function accessRules()
29
55
{
30
56
return array (
31
57
array (
32
- 'allow ' , // allow authenticated users to perform the 'index' action
33
- 'actions ' => array ('index ' , ' getTrialList ' ),
58
+ 'allow ' ,
59
+ 'actions ' => array ('getTrialList ' ),
34
60
'users ' => array ('@ ' ),
35
61
),
62
+ array (
63
+ 'allow ' ,
64
+ 'actions ' => array ('index ' , 'userAutoComplete ' ),
65
+ 'roles ' => array ('TaskCreateTrial ' , 'TaskViewTrial ' ),
66
+ ),
36
67
array (
37
68
'allow ' ,
38
69
'actions ' => array ('view ' ),
70
+ 'roles ' => array ('TaskViewTrial ' ),
39
71
'expression ' => 'Trial::checkTrialAccess($user, Yii::app()->getRequest()->getQuery("id"), UserTrialPermission::PERMISSION_VIEW) ' ,
40
72
),
41
73
array (
42
74
'allow ' ,
43
75
'actions ' => array ('update ' , 'addPatient ' , 'removePatient ' ),
76
+ 'roles ' => array ('TaskViewTrial ' ),
44
77
'expression ' => 'Trial::checkTrialAccess($user, Yii::app()->getRequest()->getQuery("id"), UserTrialPermission::PERMISSION_EDIT) ' ,
45
78
),
46
79
array (
47
80
'allow ' ,
48
81
'actions ' => array ('permissions ' , 'addPermission ' , 'removePermission ' , 'transitionState ' ),
82
+ 'roles ' => array ('TaskViewTrial ' ),
49
83
'expression ' => 'Trial::checkTrialAccess($user, Yii::app()->getRequest()->getQuery("id"), UserTrialPermission::PERMISSION_MANAGE) ' ,
50
84
),
51
85
array (
52
- 'allow ' , // allow authenticated user to perform the 'create' action
86
+ 'allow ' ,
53
87
'actions ' => array ('create ' ),
54
- 'users ' => array ('@ ' ),
88
+ 'roles ' => array ('TaskCreateTrial ' ),
55
89
),
56
90
array (
57
91
'deny ' , // deny all users
@@ -72,7 +106,7 @@ public function actionView($id)
72
106
73
107
$ this ->render ('view ' , array (
74
108
'model ' => $ model ,
75
- 'userPermission ' => $ model ->getTrialAccess (Yii::app ()->user -> id ),
109
+ 'userPermission ' => $ model ->getTrialAccess (Yii::app ()->user ),
76
110
'report ' => $ report ,
77
111
'dataProviders ' => $ model ->getPatientDataProviders (),
78
112
));
@@ -163,11 +197,9 @@ public function actionDelete($id)
163
197
*/
164
198
public function actionIndex ()
165
199
{
166
- $ condition = 'trial_type = :trialType AND (
167
- owner_user_id = :userId
168
- OR EXISTS (
200
+ $ condition = 'trial_type = :trialType AND EXISTS (
169
201
SELECT * FROM user_trial_permission utp WHERE utp.user_id = :userId AND utp.trial_id = t.id
170
- )) ' ;
202
+ ) ' ;
171
203
172
204
$ interventionTrialDataProvider = new CActiveDataProvider ('Trial ' , array (
173
205
'criteria ' => array (
@@ -232,7 +264,8 @@ public function actionAddPatient($id, $patient_id, $patient_status = TrialPatien
232
264
$ trialPatient ->patient_status = $ patient_status ;
233
265
234
266
if (!$ trialPatient ->save ()) {
235
- throw new CHttpException (400 , 'Unable to create TrialPatient: ' . print_r ($ trialPatient ->getErrors (), true ));
267
+ throw new CHttpException (400 ,
268
+ 'Unable to create TrialPatient: ' . print_r ($ trialPatient ->getErrors (), true ));
236
269
}
237
270
}
238
271
@@ -258,7 +291,8 @@ public function actionRemovePatient($id, $patient_id)
258
291
259
292
260
293
if (!$ trialPatient ->delete ()) {
261
- throw new CHttpException (400 , 'Unable to delete TrialPatient: ' . print_r ($ trialPatient ->getErrors (), true ));
294
+ throw new CHttpException (400 ,
295
+ 'Unable to delete TrialPatient: ' . print_r ($ trialPatient ->getErrors (), true ));
262
296
}
263
297
}
264
298
@@ -328,7 +362,6 @@ public function actionAddPermission($id, $user_id, $permission)
328
362
*
329
363
* @param integer $id The ID of the trial
330
364
* @param integer $permission_id The ID of the permission to remove
331
- * @return string 'success' if the permission is removed successfully
332
365
* @throws CHttpException Thrown if the permission cannot be found
333
366
* @throws CDbException Thrown if the permission cannot be deleted
334
367
*/
@@ -340,9 +373,33 @@ public function actionRemovePermission($id, $permission_id)
340
373
throw new CHttpException (400 );
341
374
}
342
375
343
- $ permission ->delete ();
376
+ if ($ permission ->user_id === Yii::app ()->user ->id ) {
377
+ echo self ::REMOVE_PERMISSION_RESULT_CANT_REMOVE_SELF ;
378
+
379
+ return ;
380
+ }
381
+
382
+ $ count = UserTrialPermission::model ()->count ('trial_id = :trialId AND permission = :permission ' ,
383
+ array (
384
+ ':trialId ' => $ id ,
385
+ ':permission ' => UserTrialPermission::PERMISSION_MANAGE ,
386
+ )
387
+ );
388
+
389
+ if ($ count <= 1 ) {
390
+ echo self ::REMOVE_PERMISSION_RESULT_CANT_REMOVE_LAST ;
344
391
345
- return 'success ' ;
392
+ return ;
393
+ }
394
+
395
+
396
+ if (!$ permission ->delete ()) {
397
+ throw new CHttpException (500 ,
398
+ 'An error occurred when attempting to delete the permission: '
399
+ . print_r ($ permission ->getErrors (), true ));
400
+ }
401
+
402
+ echo self ::REMOVE_PERMISSION_RESULT_SUCCESS ;
346
403
}
347
404
348
405
/**
@@ -357,6 +414,14 @@ protected function performAjaxValidation($model)
357
414
}
358
415
}
359
416
417
+ /**
418
+ * Transitions the given Trial to a new state.
419
+ * A different return code is echoed out depending on whether the transition was successful
420
+ *
421
+ * @param integer $id The ID of the trial to transition
422
+ * @param integer $new_state The new state to transition to (must be a valid state within Trial::getAllowedStatusRange()
423
+ * @throws CHttpException Thrown if an error occurs when saving
424
+ */
360
425
public function actionTransitionState ($ id , $ new_state )
361
426
{
362
427
/* @var Trial $model */
@@ -392,4 +457,54 @@ public function actionGetTrialList($trialID, $type)
392
457
393
458
echo '<div class="large-3 column trial-list"> ' . $ dropDown . '</div> ' ;
394
459
}
460
+
461
+ /**
462
+ * Quries users that can be assigned to the Trial and that match the search term.
463
+ * Users will not be returned if they are already assigned to the trial, or if they don't have the "View Trial" permission.
464
+ *
465
+ * @param integer $id The trial ID
466
+ * @param string $term The term to search for
467
+ * @return string A JSON encoded array of users with id, label, username and value
468
+ */
469
+ public function actionUserAutoComplete ($ id , $ term )
470
+ {
471
+ $ model = $ this ->loadModel ($ id );
472
+
473
+ $ res = array ();
474
+ $ term = strtolower ($ term );
475
+
476
+ $ criteria = new \CDbCriteria ;
477
+ $ criteria ->compare ('LOWER(username) ' , $ term , true , 'OR ' );
478
+ $ criteria ->compare ('LOWER(first_name) ' , $ term , true , 'OR ' );
479
+ $ criteria ->compare ('LOWER(last_name) ' , $ term , true , 'OR ' );
480
+
481
+ $ criteria ->addCondition ('id NOT IN (SELECT user_id FROM user_trial_permission WHERE trial_id = ' . $ model ->id . ') ' );
482
+ $ criteria ->addCondition ("EXISTS( SELECT * FROM authassignment WHERE userid = id AND itemname = 'View Trial') " );
483
+
484
+ $ words = explode (' ' , $ term );
485
+ if (count ($ words ) > 1 ) {
486
+ $ first_criteria = new \CDbCriteria ();
487
+ $ first_criteria ->compare ('LOWER(first_name) ' , $ words [0 ], true );
488
+ $ first_criteria ->compare ('LOWER(last_name) ' , implode (" " , array_slice ($ words , 1 , count ($ words ) - 1 )), true );
489
+ $ last_criteria = new \CDbCriteria ();
490
+ $ last_criteria ->compare ('LOWER(first_name) ' , $ words [count ($ words ) - 1 ], true );
491
+ $ last_criteria ->compare ('LOWER(last_name) ' , implode (" " , array_slice ($ words , 0 , count ($ words ) - 2 )), true );
492
+ $ first_criteria ->mergeWith ($ last_criteria , 'OR ' );
493
+ $ criteria ->mergeWith ($ first_criteria , 'OR ' );
494
+ }
495
+
496
+ $ criteria ->compare ('active ' , true );
497
+
498
+ foreach (\User::model ()->findAll ($ criteria ) as $ user ) {
499
+
500
+ $ res [] = array (
501
+ 'id ' => $ user ->id ,
502
+ 'label ' => $ user ->getFullNameAndTitle (),
503
+ 'value ' => $ user ->getFullName (),
504
+ 'username ' => $ user ->username ,
505
+ );
506
+ }
507
+
508
+ echo \CJSON ::encode ($ res );
509
+ }
395
510
}
0 commit comments