-
Notifications
You must be signed in to change notification settings - Fork 988
/
participantsaction.php
1770 lines (1628 loc) · 74.6 KB
/
participantsaction.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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* LimeSurvey
* Copyright (C) 2007-2011 The LimeSurvey Project Team / Carsten Schmitz
* All rights reserved.
* License: GNU/GPL License v2 or later, see LICENSE.php
* LimeSurvey is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*
*/
function subval_sort($a, $subkey, $order)
{
$b = array();
$c = array();
foreach ($a as $k => $v)
{
$b[$k] = strtolower($v[$subkey]);
}
if ($order == "asc")
{
asort($b, SORT_REGULAR);
}
else
{
arsort($b, SORT_REGULAR);
}
foreach ($b as $key => $val)
{
$c[] = $a[$key];
}
return $c;
}
/*
* This is the main controller for Participants Panel
*/
class participantsaction extends Survey_Common_Action
{
public function runWithParams($params)
{
if (!Permission::model()->hasGlobalPermission('participantpanel','read'))
{
die('No permission');
}
parent::runWithParams($params);
}
/**
* Loads jqGrid for the view
* @param string $sScript Subaction
*/
private function _loadjqGrid($sScript = '', $aData = array())
{
$aData['aAttributes'] = ParticipantAttributeName::model()->getAllAttributes();
App()->getClientScript()->registerPackage('jqgrid');
if (!empty($sScript))
{
App()->getClientScript()->registerScriptFile(Yii::app()->getConfig('adminscripts') . $sScript . '.js');
$this->_renderWrappedTemplate('participants', array('participantsPanel', $sScript), $aData);
}
}
/**
* Renders template(s) wrapped in header and footer
*
* @param string $sAction Current action, the folder to fetch views from
* @param string|array $aViewUrls View url(s)
* @param array $aData Data to be passed on. Optional.
*/
protected function _renderWrappedTemplate($sAction = 'participants', $aViewUrls = array(), $aData = array())
{
App()->getClientScript()->registerPackage('bootstrap-multiselect');
$aData['display']['menu_bars'] = false;
foreach((array) $aViewUrls as $sViewUrl)
{
$a_ViewUrls[] = $sViewUrl . '_view';
}
parent::_renderWrappedTemplate($sAction, $a_ViewUrls, $aData);
}
/**
* Export to csv using optional search/filter
*
* @param type $search CDCriteria?
* @paran mixed $mAttributeIDs Empty array for no attributes, or array of attribute IDs or null for all attributes
*/
private function csvExport($search = null, $aAttributeIDs=null) {
Yii::app()->loadHelper('export');
//If super admin all the participants will be visible
if (Permission::model()->hasGlobalPermission('superadmin','read'))
{
$iUserID = null;
} else {
$iUserID = Yii::app()->session['loginID'];
}
$query = Participant::model()->getParticipants(0, 0, $aAttributeIDs, null, $search, $iUserID);
if (!$query)
return false;
// Field names in the first row
$fields = array('participant_id', 'firstname', 'lastname', 'email', 'language', 'blacklisted', 'owner_uid');
$outputarray = array(); // The array to be passed to the export helper to be written to a csv file
$outputarray[0] = $fields; //fields written to output array
// If attribute fields are selected, add them to the output
if ($aAttributeIDs==null)
{
$aAttributes = ParticipantAttributeName::model()->getAllAttributes();
}
else
{
foreach ($aAttributeIDs as $value)
{
if ($value==0) continue;
$fields[] = 'a'.$value;
$attributename = ParticipantAttributeName::model()->getAttributeNames($value);
$outputarray[0][] = $attributename[0]['attribute_name'];
}
}
$fieldKeys = array_flip($fields);
foreach ($query as $field => $aData)
{
$outputarray[] = array_intersect_key($aData, $fieldKeys);
}
CPDBExport($outputarray, "central_" . time());
}
/**
* Returns a string with the number of participants available for export or 0
*
* @param type $search
* @return string|0
*/
protected function csvExportCount($search = null)
{
$clang = $this->getController()->lang;
$attid = ParticipantAttributeName::model()->getVisibleAttributes();
//If super admin all the participants will be visible
if (Permission::model()->hasGlobalPermission('superadmin','read'))
{
$iUserID = null;
} else {
$iUserID = Yii::app()->session['loginID'];
}
$count = Participant::model()->getParticipantsCount($attid, $search, $iUserID);
if ($count > 0) {
return sprintf($clang->ngT("Export %s participant to CSV","Export %s participants to CSV", $count),$count);
} else {
return $count;
}
}
/**
* Loads the view 'participantsPanel'
*/
function index()
{
$iUserID = Yii::app()->session['loginID'];
// if superadmin all the records in the cpdb will be displayed
if (Permission::model()->hasGlobalPermission('superadmin','read'))
{
$iTotalRecords = Participant::model()->count();
}
// if not only the participants on which he has right on (shared and owned)
else
{
$iTotalRecords = Participant::model()->getParticipantsOwnerCount($iUserID);
}
// gets the count of participants, their attributes and other such details
$aData = array(
'totalrecords' => $iTotalRecords,
'owned' => Participant::model()->count('owner_uid = ' . $iUserID),
'shared' => Participant::model()->getParticipantsSharedCount($iUserID),
'aAttributes' => ParticipantAttributeName::model()->getAllAttributes(),
'attributecount' => ParticipantAttributeName::model()->count(),
'blacklisted' => Participant::model()->count('owner_uid = ' . $iUserID . ' AND blacklisted = \'Y\'')
);
// loads the participant panel and summary view
$this->_renderWrappedTemplate('participants', array('participantsPanel', 'summary'), $aData);
}
/**
* Loads the view 'importCSV'
*/
function importCSV()
{
$aData = array(
'aAttributes' => ParticipantAttributeName::model()->getAllAttributes()
);
$this->_renderWrappedTemplate('participants', array('participantsPanel', 'importCSV'),$aData);
}
/**
* Loads the view 'displayParticipants' which contains the main grid
*/
function displayParticipants()
{
$lang = Yii::app()->session['adminlang'];
// loads the survey names to be shown in add to survey
// if user is superadmin, all survey names
$sSearchCondition=Yii::app()->request->getPost('searchcondition','');
$urlSearch=!empty($sSearchCondition) ? "getParticipantsResults_json" : "getParticipants_json";
//Get list of surveys.
//Should be all surveys owned by user (or all surveys for super admin)
$surveys = Survey::model();
//!!! Is this even possible to execute?
if (!Permission::model()->hasGlobalPermission('superadmin','read'))
$surveys->permission(Yii::app()->user->getId());
$aSurveyNames = $surveys->model()->with(array('languagesettings'=>array('condition'=>'surveyls_language=language'), 'owner'))->findAll();
/* Build a list of surveys that have tokens tables */
$tSurveyNames=array();
foreach($aSurveyNames as $row)
{
$row = array_merge($row->attributes, $row->defaultlanguage->attributes);
$bTokenExists = tableExists('{{tokens_' . $row['sid'] . '}}');
if ($bTokenExists) //If tokens table exists
{
$tSurveyNames[]=$row;
}
}
// data to be passed to view
$aData = array(
'names' => User::model()->findAll(),
'attributes' => ParticipantAttributeName::model()->getVisibleAttributes(),
'allattributes' => ParticipantAttributeName::model()->getAllAttributes(),
'attributeValues' => ParticipantAttributeName::model()->getAllAttributesValues(),
'surveynames' => $aSurveyNames,
'tokensurveynames' => $tSurveyNames,
'urlsearch' => $urlSearch,
'sSearchCondition' => $sSearchCondition,
'aAttributes' => ParticipantAttributeName::model()->getAllAttributes()
);
App()->getClientScript()->registerPackage('jqgrid');
App()->getClientScript()->registerCssFile(Yii::app()->getConfig('adminstyleurl') . 'displayParticipants.css');
// loads the participant panel view and display participant view
$this->_renderWrappedTemplate('participants', array('participantsPanel', 'displayParticipants'), $aData);
}
/**
* Loads the view 'blacklistControl'
*/
function blacklistControl()
{
$aData = array(
'blacklistallsurveys' => Yii::app()->getConfig('blacklistallsurveys'),
'blacklistnewsurveys' => Yii::app()->getConfig('blacklistnewsurveys'),
'blockaddingtosurveys' => Yii::app()->getConfig('blockaddingtosurveys'),
'hideblacklisted' => Yii::app()->getConfig('hideblacklisted'),
'deleteblacklisted' => Yii::app()->getConfig('deleteblacklisted'),
'allowunblacklist' => Yii::app()->getConfig('allowunblacklist'),
'aAttributes' => ParticipantAttributeName::model()->getAllAttributes()
);
$this->_renderWrappedTemplate('participants', array('participantsPanel', 'blacklist'), $aData);
}
/**
* Loads the view 'userControl'
*/
function userControl()
{
$aData = array(
'userideditable' => Yii::app()->getConfig('userideditable'),
'aAttributes' => ParticipantAttributeName::model()->getAllAttributes()
);
$this->_renderWrappedTemplate('participants', array('participantsPanel', 'userControl'), $aData);
}
/**
* Loads the view 'sharePanel'
*/
function sharePanel()
{
$this->_loadjqGrid('sharePanel');
}
/**
* Sends the shared participant info to the share panel using JSON encoding
* Called after the share panel grid is loaded
* Returns the json depending on the user logged in by checking it from the session
* @return JSON encoded string containg sharing information
*/
function getShareInfo_json()
{
$aData = new stdClass();
$aData->page = 1;
// If super administrator all the share info in the links table will be shown
if (Permission::model()->hasGlobalPermission('superadmin','read'))
{
$records = Participant::model()->getParticipantSharedAll();
$aData->records = count($records);
$aData->total = ceil($aData->records / 10);
$i = 0;
foreach ($records as $row)
{
$oShared = User::model()->getName($row['share_uid']); //for conversion of uid to human readable names
$owner = User::model()->getName($row['owner_uid']);
$aData->rows[$i]['id'] = $row['participant_id']."--".$row['share_uid']; //This is the unique combination per record
$aData->rows[$i]['cell'] = array($row['firstname'], $row['lastname'], $row['email'], $oShared[0]['full_name'], $row['share_uid'], $owner[0]['full_name'], $row['date_added'], $row['can_edit']);
$i++;
}
echo ls_json_encode($aData);
}
// otherwise only the shared participants by that user
else
{
$records = Participant::model()->getParticipantShared(Yii::app()->session['loginID']);
$aData->records = count($records);
$aData->total = ceil($aData->records / 10);
$i = 0;
foreach ($records as $row)
{
$sharename = User::model()->getName($row['share_uid']); //for conversion of uid to human readable names
$aData->rows[$i]['id'] = $row['participant_id'];
$aData['rows'][$i]['cell'] = array($row['firstname'], $row['lastname'], $row['email'], $sharename['full_name'], $row['share_uid'], $row['date_added'], $row['can_edit']);
$i++;
}
echo ls_json_encode($aData);
}
}
/**
* Takes the edit call from the share panel, which either edits or deletes the share information
* Basically takes the call on can_edit
*/
function editShareInfo()
{
$operation = Yii::app()->request->getPost('oper');
$shareIds = Yii::app()->request->getPost('id');
if ($operation == 'del') // If operation is delete , it will delete, otherwise edit it
{
ParticipantShare::model()->deleteRow($shareIds);
}
else
{
$aData = array(
'participant_id' => Yii::app()->request->getPost('participant_id'),
'can_edit' => Yii::app()->request->getPost('can_edit'),
'share_uid' => Yii::app()->request->getPost('shared_uid')
);
ParticipantShare::model()->updateShare($aData);
}
}
/**
* Loads the view 'attributeControl'
*/
function attributeControl()
{
$this->_loadjqGrid('attributeControl');
}
/**
* Sends the attributes info using JSON encoding
* Called after the Attribute management grid is loaded
* @return JSON encoded string containg sharing information
*/
function getAttributeInfo_json()
{
$clang = Yii::app()->lang;
$page = Yii::app()->request->getPost('page');
$limit = Yii::app()->request->getPost('rows');
$limit = isset($limit) ? $limit : 50; //Stop division by zero errors
$records = ParticipantAttributeName::model()->with('participant_attribute_names_lang')->findAll(array('order'=>'attribute_name'));
$attribute_types = array(
'DD' => $clang->gT("Drop-down list"),
'DP' => $clang->gT("Date"),
'TB' => $clang->gT("Text box")
);
$aData = new stdClass();
$aData->page = $page;
$aData->records = count($records);
$aData->total = ceil(ParticipantAttributeName::model()->getCPDBAttributes(true) / $limit);
$i = 0;
foreach($records as $row) { //Iterate through each attribute
$sAttributeCaption=$row->defaultname; //Choose the first item by default
foreach($row->participant_attribute_names_lang as $names) { //Iterate through each language version of this attribute
if($names->lang == Yii::app()->session['adminlang']) {$sAttributeCaption= $sAttributeCaption." ({$names->attribute_name})";} //Override the default with the admin language version if found
}
$aData->rows[$i]['id'] = $row->attribute_id;
$aData->rows[$i]['cell'] = array('', $sAttributeCaption, $attribute_types[$row->attribute_type], $row->visible);
$i++;
}
echo ls_json_encode($aData);
}
/**
* Takes the edit call from the share panel, which either edits or deletes the share information
* Basically takes the call on can_edit
*/
function editAttributeInfo()
{
$clang = Yii::app()->lang;
$operation = Yii::app()->request->getPost('oper');
if ($operation == 'del' && Yii::app()->request->getPost('id'))
{
$aAttributeIds = (array) explode(',', Yii::app()->request->getPost('id'));
$aAttributeIds = array_map('trim', $aAttributeIds);
$aAttributeIds = array_map('intval', $aAttributeIds);
foreach ($aAttributeIds as $iAttributeId)
{
ParticipantAttributeName::model()->delAttribute($iAttributeId);
}
}
elseif ($operation == 'add' && Yii::app()->request->getPost('attribute_name'))
{
$aData = array(
'defaultname' => Yii::app()->request->getPost('attribute_name'),
'attribute_name' => Yii::app()->request->getPost('attribute_name'),
'attribute_type' => Yii::app()->request->getPost('attribute_type'),
'visible' => Yii::app()->request->getPost('visible')? 'TRUE' : 'FALSE'
);
echo ParticipantAttributeName::model()->storeAttribute($aData);
}
elseif ($operation == 'edit' && Yii::app()->request->getPost('id'))
{
$aData = array(
'attribute_id' => Yii::app()->request->getPost('id'),
'attribute_name' => Yii::app()->request->getPost('attribute_name'),
'attribute_type' => Yii::app()->request->getPost('attribute_type'),
'visible' => Yii::app()->request->getPost('visible','FALSE') != 'FALSE' ? 'TRUE' : 'FALSE'
);
ParticipantAttributeName::model()->saveAttribute($aData);
$clang->eT("Attribute display setting updated");
}
}
/**
* Takes the delete call from the display participants and take appropriate action depending on the condition
*/
function delParticipant()
{
if (Permission::model()->hasGlobalPermission('participantpanel','delete'))
{
$selectoption = Yii::app()->request->getPost('selectedoption');
$iParticipantId = Yii::app()->request->getPost('participant_id');
//echo $selectoption." -- ".$iParticipantId."<br />"; die();
// Deletes from participants only
if ($selectoption == 'po')
{
Participant::model()->deleteParticipants($iParticipantId);
}
// Deletes from central and token table
elseif ($selectoption == 'ptt')
{
Participant::model()->deleteParticipantToken($iParticipantId);
}
// Deletes from central , token and assosiated responses as well
elseif ($selectoption == 'ptta')
{
Participant::model()->deleteParticipantTokenAnswer($iParticipantId);
}
}
}
/**
* Resposible for editing data on the jqGrid
*/
function editParticipant()
{
$sOperation = Yii::app()->request->getPost('oper');
// if edit it will update the row
if ($sOperation == 'edit' && Permission::model()->hasGlobalPermission('participantpanel','update') && Participant::model()->is_owner(Yii::app()->request->getPost('id')))
{
$aData = array(
'participant_id' => Yii::app()->request->getPost('id'),
'firstname' => Yii::app()->request->getPost('firstname'),
'lastname' => Yii::app()->request->getPost('lastname'),
'email' => Yii::app()->request->getPost('email'),
'language' => Yii::app()->request->getPost('language'),
'blacklisted' => Yii::app()->request->getPost('blacklisted')
);
Participant::model()->updateRow($aData);
}
// if add it will insert a new row
elseif ($sOperation == 'add' && Permission::model()->hasGlobalPermission('participantpanel','create'))
{
$uuid = $this->gen_uuid();
$aData = array(
'participant_id' => $uuid,
'firstname' => Yii::app()->request->getPost('firstname'),
'lastname' => Yii::app()->request->getPost('lastname'),
'email' => Yii::app()->request->getPost('email'),
'language' => Yii::app()->request->getPost('language'),
'blacklisted' => Yii::app()->request->getPost('blacklisted'),
'owner_uid' => Yii::app()->session['loginID'],
'created_by' => Yii::app()->session['loginID']
);
Participant::model()->insertParticipant($aData);
}
}
/**
* Stores the user control setting to the database
*/
function storeUserControlValues()
{
if ($find = SettingGlobal::model()->findByPk('userideditable'))
{
SettingGlobal::model()->updateByPk('userideditable', array('stg_value'=>Yii::app()->request->getPost('userideditable')));
}
else
{
$stg = new SettingGlobal;
$stg ->stg_name='userideditable';
$stg ->stg_value=Yii::app()->request->getPost('userideditable');
$stg->save();
}
Yii::app()->getController()->redirect(array('admin/participants/sa/userControl'));
}
/**
* Stores the blacklist setting to the database
*/
function storeBlacklistValues()
{
$values = Array('blacklistallsurveys', 'blacklistnewsurveys', 'blockaddingtosurveys', 'hideblacklisted', 'deleteblacklisted', 'allowunblacklist', 'userideditable');
foreach ($values as $value)
{
if ($find = SettingGlobal::model()->findByPk($value))
{
SettingGlobal::model()->updateByPk($value, array('stg_value'=>Yii::app()->request->getPost($value)));
}
else
{
$stg = new SettingGlobal;
$stg ->stg_name=$value;
$stg ->stg_value=Yii::app()->request->getPost($value);
$stg->save();
}
}
Yii::app()->getController()->redirect(array('admin/participants/sa/blacklistControl'));
}
/**
* Receives an ajax call containing the participant id in the fourth segment of the url
* Supplies list of survey links - surveys of which this participant is on the tokens table
* URL: [localurl]/limesurvey/admin/participants/getSurveyInfo_json/pid/[participant_id]
* RETURNS: json data containing linked survey information (Survey name, survey id, token_id and date_added)
*/
function getSurveyInfo_json()
{
$participantid = Yii::app()->request->getQuery('pid');
$records = SurveyLink::model()->findAllByAttributes((array('participant_id' => $participantid)));
$aData = new stdClass();
$aData->page = 1;
$aData->records = count($records);
$aData->total = ceil($aData->records / 10);
$i = 0;
foreach ($records as $row)
{
$oSurvey=Survey::model()->with(array('languagesettings'=>array('condition'=>'surveyls_language=language')))->findByAttributes(array('sid' => $row['survey_id']));
foreach($oSurvey->languagesettings as $oLanguageSetting)
{
$surveyname= $oLanguageSetting->surveyls_title;
}
$surveylink = "";
/* Check permissions of each survey before creating a link*/
if (!Permission::model()->hasSurveyPermission($row['survey_id'], 'tokens', 'read'))
{
$surveylink = $row['survey_id'];
} else
{
$surveylink = '<a href=' . Yii::app()->getController()->createUrl("/admin/tokens/sa/browse/surveyid/{$row['survey_id']}") . '>' . $row['survey_id'].'</a>';
}
$aData->rows[$i]['cell'] = array($surveyname, $surveylink, $row['token_id'], $row['date_created'], $row['date_invited'], $row['date_completed']);
$i++;
}
echo ls_json_encode($aData);
}
/**
* Returns the count of the participants in the CSV and show it in the title of the modal box
* This is to give the user the hint to see the number of participants he is exporting
*/
function exporttocsvcount()
{
$searchconditionurl = Yii::app()->request->getPost('searchURL');
$searchcondition = Yii::app()->request->getPost('searchcondition');
$searchconditionurl = basename($searchconditionurl);
if ($searchconditionurl != 'getParticipants_json') // if there is a search condition then only the participants that match the search criteria are counted
{
$condition = explode("||", $searchcondition);
$search = Participant::model()->getParticipantsSearchMultipleCondition($condition);
} else {
$search = null;
}
echo $this->csvExportCount($search);
}
/**
* Outputs the count of participants when using the export all button on the top
*/
function exporttocsvcountAll()
{
echo $this->csvExportCount();
}
/**
* Responsible to export all the participants in the central table
*/
function exporttocsvAll()
{
$this->csvExport(); // no search
}
/**
* Similar to export to all message where it counts the number to participants to be copied
* and echo them to be displayed in modal box header
*/
function getaddtosurveymsg()
{
$searchcondition = basename(Yii::app()->request->getPost('searchcondition'));
// If there is a search condition in the url of the jqGrid
if ($searchcondition != 'getParticipants_json')
{
$participantid = "";
$condition = explode("||", $searchcondition);
$query = Participant::model()->getParticipantsSearchMultiple($condition, 0, 0);
printf( $this->getController()->lang->gT("%s participant(s) are to be copied "), count($query));
}
// if there is no search condition the participants will be counted on the basis of who is logged in
else
{
if (Permission::model()->hasGlobalPermission('superadmin','read')) //If super admin all the participants will be visible
{
$count = Participant::model()->getParticipantsCountWithoutLimit();
}
else
{
$query = Participant::model()->getParticipantsOwner(Yii::app()->session['loginID']);
$count = count($query);
}
printf($this->getController()->lang->gT("%s participant(s) are to be copied "), $count);
}
}
/**
* Gets the ids of participants to be copied to the individual survey
*/
function getSearchIDs()
{
$searchcondition = Yii::app()->request->getPost('searchcondition'); // get the search condition from the URL
$sSearchURL = basename(Yii::app()->request->getPost('searchURL')); // get the search condition from the URL
/* a search contains posted data inside $_POST['searchcondition'].
* Each separate query is made up of 3 fields, separated by double-pipes ("|")
* EG: fname||eq||jason||lname||ct||c
*
*/
if ($sSearchURL != 'getParticipants_json') // if there is a search condition present
{
$participantid = "";
$condition = explode("||", $searchcondition); // explode the condition to the array
$query = Participant::model()->getParticipantsSearchMultiple($condition, 0, 0);
foreach ($query as $key => $value)
{
if (Permission::model()->hasGlobalPermission('superadmin','read'))
{
$participantid .= "," . $value['participant_id']; // combine the participant id's in an string
} else
{
if(Participant::model()->is_owner($value['participant_id']))
{
$participantid .= "," . $value['participant_id']; // combine the participant id's in an string
}
}
}
echo $participantid; //echo the participant id's
}
else// if no search condition
{
$participantid = ""; // initiallise the participant id to blank
if (Permission::model()->hasGlobalPermission('superadmin','read')) //If super admin all the participants will be visible
{
$query = Participant::model()->getParticipantsWithoutLimit(); // get all the participant id if it is a super admin
}
else // get participants on which the user has right on
{
$query = Participant::model()->getParticipantsOwner(Yii::app()->session['loginID']);
}
foreach ($query as $key => $value)
{
$participantid = $participantid . "," . $value['participant_id']; // combine the participant id's in an string
}
echo $participantid; //echo the participant id's
}
}
/**
* Exports participants as CSV - receiver function for the GUI
*/
function exporttocsv()
{
if (Yii::app()->request->getPost('searchcondition','') != '') // if there is a search condition then only the participants that match the search criteria are counted
{
$condition = explode("%7C%7C", Yii::app()->request->getPost('searchcondition',''));
$search = Participant::model()->getParticipantsSearchMultipleCondition($condition);
} else {
$search = null;
}
$aAttributes=explode('+',Yii::app()->request->getPost('attributes',''));
$this->csvExport($search,$aAttributes);
}
/**
* Equal to getParticipants_json() but now with a search
*/
function getParticipantsResults_json()
{
$searchcondition = Yii::app()->request->getpost('searchcondition');
$finalcondition = array();
$condition = explode("||", $searchcondition);
$search = Participant::model()->getParticipantsSearchMultipleCondition($condition);
return $this->getParticipants_json($search);
}
/*
* Sends the data in JSON format extracted from the database to be displayed using the jqGrid
*/
function getParticipants_json($search = null)
{
$page = Yii::app()->request->getPost('page');
$limit = Yii::app()->request->getPost('rows');
$limit = isset($limit) ? $limit : 50; //Stop division by zero errors
$attid = ParticipantAttributeName::model()->getVisibleAttributes();
$participantfields = array('participant_id', 'can_edit', 'firstname', 'lastname', 'email', 'blacklisted', 'survey', 'language', 'owner_uid');
foreach ($attid as $key => $value)
{
array_push($participantfields, $value['attribute_id']);
}
$sidx = Yii::app()->request->getPost('sidx');
$sidx = !empty($sidx) ? $sidx : "lastname";
$sord = Yii::app()->request->getPost('sord');
$sord = !empty($sord) ? $sord : "asc";
$order = $sidx. " ". $sord;
$aData = new stdClass;
//If super admin all the participants will be visible
if (Permission::model()->hasGlobalPermission('superadmin','read'))
{
$iUserID = null;
} else {
$iUserID = Yii::app()->session['loginID'];
}
$aData->records = Participant::model()->getParticipantsCount($attid, $search, $iUserID);
$aData->total = ceil($aData->records / $limit);
if ($page>$aData->total) {
$page = $aData->total;
}
$aData->page = $page;
$records = Participant::model()->getParticipants($page, $limit,$attid, $order, $search, $iUserID);
$aRowToAdd=array();
foreach ($records as $key => $row)
{
if (array_key_exists('can_edit', $row)) {
$sCanEdit = $row['can_edit'];
if (is_null($sCanEdit)) {
$sCanEdit = 'true';
}
} else {
// Super admin
$sCanEdit = "true";
}
if (trim($row['ownername'])=='') {
$row['ownername']=$row['username'];
}
$aRowToAdd['cell'] = array($row['participant_id'], $sCanEdit, htmlspecialchars($row['firstname']), htmlspecialchars($row['lastname']), htmlspecialchars($row['email']), $row['blacklisted'], $row['survey'], $row['language'], $row['ownername']);
$aRowToAdd['id'] = $row['participant_id'];
// add attribute values
foreach($row as $key=>$attvalue)
{
if(preg_match('/^a\d+$/', $key))
{
$aRowToAdd['cell'][] = $attvalue;
}
}
$aData->rows[] = $aRowToAdd;
}
echo ls_json_encode($aData);
}
/*
* Fetches the attributes of a participant to be displayed in the attribute subgrid
*/
function getAttribute_json()
{
$iParticipantId = Yii::app()->request->getQuery('pid');
$records = ParticipantAttributeName::model()->getParticipantVisibleAttribute($iParticipantId);
$records = subval_sort($records, "attribute_name", "asc");
$i = 0;
$doneattributes = array(); //If the user has any actual attribute values, they'll be stored here
/* Iterate through each attribute owned by this user */
foreach ($records as $row)
{
$outputs[$i] = array("", $row['participant_id']."_".$row['attribute_id'], $row['attribute_type'], $row['attribute_id'], $row['attribute_name'], $row['value']);
/* Collect allowed values for a DropDown attribute */
if ($row['attribute_type'] == "DD")
{
$attvalues = ParticipantAttributeName::model()->getAttributesValues($row['attribute_id']);
if (!empty($attvalues))
{
$attval = "";
foreach ($attvalues as $val)
{
$attval .= $val['value'] . ":" . $val['value'];
$attval .= ";";
}
$attval = substr($attval, 0, -1);
array_push($outputs[$i], $attval);
}
else
{
array_push($outputs[$i], "");
}
}
else
{
array_push($outputs[$i], "");
}
array_push($doneattributes, $row['attribute_id']);
$i++;
}
/* Build a list of attribute names for which this user has NO values stored, keep it in $attributenotdone */
$attributenotdone=array();
/* The user has NO values stored against any attribute */
if (count($doneattributes) == 0)
{
$attributenotdone = ParticipantAttributeName::model()->getCPDBAttributes();
}
/* The user has SOME values stored against attributes */
else
{
$attributenotdone = ParticipantAttributeName::model()->getnotaddedAttributes($doneattributes);
}
/* Go through the empty attributes and build an entry in the output for them */
foreach ($attributenotdone as $row)
{
$outputs[$i] = array("", $iParticipantId."_".$row['attribute_id'], $row['attribute_type'], $row['attribute_id'], $row['attribute_name'], "");
if ($row['attribute_type'] == "DD")
{
$attvalues = ParticipantAttributeName::model()->getAttributesValues($row['attribute_id']);
if (!empty($attvalues))
{
$attval = "";
foreach ($attvalues as $val)
{
$attval .= $val['value'] . ":" . $val['value'];
$attval .= ";";
}
$attval = substr($attval, 0, -1);
array_push($outputs[$i], $attval);
}
else
{
array_push($outputs[$i], "");
}
}
else
{
array_push($outputs[$i], "");
}
$i++;
}
$outputs=subval_sort($outputs, 3, "asc");
$aData = new stdClass();
$aData->page = 1;
$aData->rows[0]['id'] = $iParticipantId;
$aData->rows[0]['cell'] = array();
$aData->records = count($outputs);
$aData->total = ceil($aData->records / 10);
foreach($outputs as $key=>$output) {
$aData->rows[$key]['id']=$output[1];
$aData->rows[$key]['cell']=$output;
}
/* TODO: It'd be nice to do a natural sort on the attribute list at some point.
Currently they're returned in order of attributes WITH values, then WITHOUT values
*/
echo ls_json_encode($aData);
}
/*
* Responsible for showing the additional attribute for central database
*/
function viewAttribute()
{
$iAttributeId = Yii::app()->request->getQuery('aid');
$aData = array(
'attributes' => ParticipantAttributeName::model()->getAttribute($iAttributeId),
'attributenames' => ParticipantAttributeName::model()->getAttributeNames($iAttributeId),
'attributevalues' => ParticipantAttributeName::model()->getAttributesValues($iAttributeId),
'aAttributes' => ParticipantAttributeName::model()->getAllAttributes()
);
App()->getClientScript()->registerCssFile(Yii::app()->getConfig('adminstyleurl').'participants.css');
App()->getClientScript()->registerCssFile(Yii::app()->getConfig('adminstyleurl').'viewAttribute.css');
App()->getClientScript()->registerScriptFile(Yii::app()->getConfig('adminscripts') . "viewAttribute.js");
$this->_renderWrappedTemplate('participants', array('participantsPanel', 'viewAttribute'), $aData);
}
/*
* Responsible for saving the additional attribute. It iterates through all the new attributes added dynamically
* and iterates through them
*/
function saveAttribute()
{
$iAttributeId = Yii::app()->request->getQuery('aid');
$aData = array(
'attribute_id' => $iAttributeId,
'attribute_type' => Yii::app()->request->getPost('attribute_type'),
'defaultname' => Yii::app()->request->getPost('defaultname'),
'visible' => Yii::app()->request->getPost('visible')
);
ParticipantAttributeName::model()->saveAttribute($aData);
foreach ($_POST as $key => $value)
{
// check for language code in the post variables this is a hack as the only way to check for language data
if (strlen($key) == 2)
{
$langdata = array(
'attribute_id' => $iAttributeId,
'attribute_name' => $value,
'lang' => $key
);
ParticipantAttributeName::model()->saveAttributeLanguages($langdata);
}
}
if (Yii::app()->request->getPost('langdata'))
{
$langdata = array(
'attribute_id' => $iAttributeId,
'attribute_name' => Yii::app()->request->getPost('attname'),
'lang' => Yii::app()->request->getPost('langdata')
);
ParticipantAttributeName::model()->saveAttributeLanguages($langdata);
}
/* Create new attribute value */
if (Yii::app()->request->getPost('attribute_value_name_1') || Yii::app()->request->getPost('attribute_value_name_1') == "0")
{
$i = 1;
$attvaluename = 'attribute_value_name_' . $i;
while (array_key_exists($attvaluename, $_POST) && $_POST[$attvaluename] != "")
{