/
participantsaction.php
2035 lines (1871 loc) · 84.2 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.
*
* $Id$
*/
/*
* This is the main controller for Participants Panel
*/
class participantsaction extends Survey_Common_Action
{
/**
* Loads jqGrid for the view
* @param string $sScript Subaction
*/
private function _loadjqGrid($sScript = '', $aData = array())
{
$this->getController()->_css_admin_includes(Yii::app()->getConfig('generalscripts') . 'jquery/jqGrid/css/ui.jqgrid.css');
$this->getController()->_css_admin_includes(Yii::app()->getConfig('generalscripts') . 'jquery/jqGrid/css/jquery.ui.datepicker.css');
$this->getController()->_js_admin_includes(Yii::app()->getConfig('generalscripts') . 'jquery/jquery.js');
$this->getController()->_js_admin_includes(Yii::app()->getConfig('generalscripts') . 'jquery/jquery-ui.js');
$this->getController()->_js_admin_includes(Yii::app()->getConfig('generalscripts') . 'jquery/jqGrid/js/i18n/grid.locale-en.js');
$this->getController()->_js_admin_includes(Yii::app()->getConfig('generalscripts') . 'jquery/jqGrid/js/jquery.jqGrid.min.js');
$this->getController()->_js_admin_includes(Yii::app()->getConfig('generalscripts') . 'jquery/jqGrid/plugins/jquery.searchFilter.js');
$this->getController()->_js_admin_includes(Yii::app()->getConfig('generalscripts') . 'jquery/jqGrid/src/grid.celledit.js');
$this->getController()->_js_admin_includes(Yii::app()->getConfig('generalscripts') . 'jquery/jqGrid/js/i18n/grid.locale-en.js');
if (!empty($sScript))
{
$this->getController()->_js_admin_includes(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())
{
$aData['display']['menu_bars'] = false;
foreach((array) $aViewUrls as $sViewUrl)
{
$a_ViewUrls[] = $sViewUrl . '_view';
}
parent::_renderWrappedTemplate($sAction, $a_ViewUrls, $aData);
}
/**
* Loads the view 'participantsPanel'
*/
function index()
{
$iUserID = Yii::app()->session['loginID'];
// if superadmin all the records in the cpdb will be displayed
if (Yii::app()->session['USER_RIGHT_SUPERADMIN'])
{
$iTotalRecords = Participants::model()->count();
}
// if not only the participants on which he has right on (shared and owned)
else
{
$iTotalRecords = Participants::model()->getParticipantsOwnerCount($iUserID);
}
// gets the count of participants, their attributes and other such details
$aData = array(
'totalrecords' => $iTotalRecords,
'owned' => Participants::model()->count('owner_uid = ' . $iUserID),
'shared' => Participants::model()->getParticipantsSharedCount($iUserID),
'attributecount' => ParticipantAttributeNames::model()->count(),
'blacklisted' => Participants::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()
{
$this->_renderWrappedTemplate('participants', array('participantsPanel', 'importCSV'));
}
/**
* 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
$urlSearch=Yii::app()->request->getQuery('searchurl');
$urlSearch=!empty($urlSearch) ? "getParticipantsResults_json/search/$urlSearch" : "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 (empty(Yii::app()->session['USER_RIGHT_SUPERADMIN']))
$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->languagesettings[0]->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' => ParticipantAttributeNames::model()->getVisibleAttributes(),
'allattributes' => ParticipantAttributeNames::model()->getAllAttributes(),
'attributeValues' => ParticipantAttributeNames::model()->getAllAttributesValues(),
'surveynames' => $aSurveyNames,
'tokensurveynames' => $tSurveyNames,
'urlsearch' => $urlSearch
);
$this->getController()->_js_admin_includes(Yii::app()->getConfig('generalscripts') . 'jquery/jqGrid/js/i18n/grid.locale-en.js');
$this->getController()->_js_admin_includes(Yii::app()->getConfig('generalscripts') . 'jquery/jqGrid/js/jquery.jqGrid.min.js');
$this->getController()->_css_admin_includes(Yii::app()->getConfig('generalscripts') . 'jquery/css/jquery.multiselect.css');
$this->getController()->_css_admin_includes(Yii::app()->getConfig('generalscripts') . 'jquery/css/jquery.multiselect.filter.css');
$this->getController()->_css_admin_includes(Yii::app()->getConfig('adminstyleurl') . 'displayParticipants.css');
$this->getController()->_css_admin_includes(Yii::app()->getConfig('generalscripts') . 'jquery/jqGrid/css/ui.jqgrid.css');
$this->getController()->_css_admin_includes(Yii::app()->getConfig('generalscripts') . 'jquery/jqGrid/css/jquery.ui.datepicker.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')
);
$this->_renderWrappedTemplate('participants', array('participantsPanel', 'blacklist'), $aData);
}
/**
* Loads the view 'userControl'
*/
function userControl()
{
$aData = array(
'userideditable' => Yii::app()->getConfig('userideditable')
);
$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->page = 1;
// If super administrator all the share info in the links table will be shown
if (Yii::app()->session['USER_RIGHT_SUPERADMIN'])
{
$records = Participants::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 = User::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
{
ParticipantShares::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')
);
ParticipantShares::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 = ParticipantAttributeNames::model()->getAttributes();
$attribute_types = array(
'DD' => $clang->gT("Drop-down list"),
'DP' => $clang->gT("Date"),
'TB' => $clang->gT("Text box")
);
$aData->page = $page;
$aData->records = count($records);
$aData->total = ceil(ParticipantAttributeNames::model()->getAttributes(true) / $limit);
$i = 0;
foreach ($records as $row)
{
$aData->rows[$i]['id'] = $row['attribute_id'];
$aData->rows[$i]['cell'] = array('', $row['attribute_name'], $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)
{
ParticipantAttributeNames::model()->delAttribute($iAttributeId);
}
}
elseif ($operation == 'add' && Yii::app()->request->getPost('attribute_name'))
{
$aData = array(
'attribute_name' => Yii::app()->request->getPost('attribute_name'),
'attribute_type' => Yii::app()->request->getPost('attribute_type'),
'visible' => Yii::app()->request->getPost('visible') == 'TRUE' ? 'TRUE' : 'FALSE'
);
echo ParticipantAttributeNames::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') == 'TRUE' ? 'TRUE' : 'FALSE'
);
ParticipantAttributeNames::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()
{
$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')
{
Participants::model()->deleteParticipant($iParticipantId);
}
// Deletes from central and token table
elseif ($selectoption == 'ptt')
{
Participants::model()->deleteParticipantToken($iParticipantId);
}
// Deletes from central , token and assosiated responses as well
else
{
Participants::model()->deleteParticipantTokenAnswer($iParticipantId);
}
}
/**
* Resposible for editing data on the jqGrid
*/
function editParticipant()
{
$operation = Yii::app()->request->getPost('oper');
//In case the uid is not editable, then user id is not posted and hence the current user is added in the uid
if (Yii::app()->request->getPost('owner_uid') == '')
{
$oid = Yii::app()->session['loginID'];
}
//otherwise the one which is posted is added
else
{
$oid = Yii::app()->request->getPost('owner_uid');
}
if (Yii::app()->request->getPost('language') == '')
{
$lang = Yii::app()->session['adminlang'];
}
else
{
$lang = Yii::app()->request->getPost('language');
}
// if edit it will update the row
if ($operation == 'edit')
{
$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'),
'owner_uid' => $oid
);
Participants::model()->updateRow($aData);
}
// if add it will insert a new row
elseif ($operation == 'add')
{
$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' => $oid
);
Participants::model()->insertParticipant($aData);
}
}
/**
* Stores the user control setting to the database
*/
function storeUserControlValues()
{
if ($find = Settings_global::model()->findByPk('userideditable'))
{
Settings_global::model()->updateByPk('userideditable', array('stg_value'=>Yii::app()->request->getPost('userideditable')));
}
else
{
$stg = new Settings_global;
$stg ->stg_name='userideditable';
$stg ->stg_value=Yii::app()->request->getPost('userideditable');
$stg->save();
}
Yii::app()->getController()->redirect(Yii::app()->getController()->createUrl('admin/participants/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 = Settings_global::model()->findByPk($value))
{
Settings_global::model()->updateByPk($value, array('stg_value'=>Yii::app()->request->getPost($value)));
}
else
{
$stg = new Settings_global;
$stg ->stg_name=$value;
$stg ->stg_value=Yii::app()->request->getPost($value);
$stg->save();
}
}
Yii::app()->getController()->redirect(Yii::app()->getController()->createUrl('admin/participants/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 = Survey_links::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)
{
$surveyname = Surveys_languagesettings::model()->getSurveyNames($row['survey_id']);
$surveylink = "";
/* Check permissions of each survey before creating a link*/
if (!hasSurveyPermission($row['survey_id'], 'tokens', 'read'))
{
$surveylink = $row['survey_id'];
} else
{
$surveylink = '<a href=' . Yii::app()->getController()->createUrl("/admin/tokens/browse/surveyid/{$row['survey_id']}") . '>' . $row['survey_id'].'</a>';
}
$aData->rows[$i]['cell'] = array($surveyname[0]['surveyls_title'], $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()
{
$clang = $this->getController()->lang;
$searchconditionurl = Yii::app()->request->getPost('searchcondition');
$searchcondition = basename($searchconditionurl);
if (Yii::app()->session['USER_RIGHT_SUPERADMIN']) //If super admin all the participants in the cpdb are counted
{
if ($searchcondition != 'getParticipants_json') // if there is a search condition then only the participants that match the search criteria are counted
{
$condition = explode("||", $searchcondition);
$query = Participants::model()->getParticipantsSearchMultiple($condition, 0, 0);
}
else // if no search criteria all the participants will be counted
{
$query = Participants::model()->getParticipantsWithoutLimit();
}
}
else // If no search criteria it will simply return the number of participants
{
$iUserID = Yii::app()->session['loginID'];
$query = Participants::model()->getParticipantsOwner($iUserID);
}
echo sprintf($clang->gT("Export %s participant(s) to CSV"), count($query));
}
/**
* Outputs the count of participants when using the export all button on the top
*/
function exporttocsvcountAll()
{
if (Yii::app()->session['USER_RIGHT_SUPERADMIN']) //If super admin all the participants in the central table will be counted
{
$query = Participants::model()->getParticipantsWithoutLimit();
}
else // otherwise only the participants on which the logged in user has the rights
{
$iUserID = Yii::app()->session['loginID'];
$query = Participants::model()->getParticipantsOwner($iUserID);
}
if (count($query) > 0) // If count is greater than 0 it will show the message
{
printf($clang->gT("Export %s participant(s) to CSV"), count($query));
}
else // else it will return a numeric count which will tell that there is no participant to be exported
{
echo count($query);
}
}
/**
* Responsible to export all the participants in the central table
*/
function exporttocsvAll()
{
Yii::app()->loadHelper('export'); // loads the export helper
if (Yii::app()->session['USER_RIGHT_SUPERADMIN']) //If super admin all the participants will be exported
{
$query = Participants::model()->getParticipantsWithoutLimit();
}
else // otherwise only the ones over which the user has rights on
{
$iUserID = Yii::app()->session['loginID'];
$query = Participants::model()->getParticipantsOwner($iUserID);
}
if (!$query)
return false;
// These are the consistent fields that will be exported
$fields = array('participant_id', 'firstname', 'lastname', 'email', 'language', 'blacklisted', 'owner_uid');
$i = 0;
$outputarray = array();
foreach ($fields as $field)
{
$outputarray[0][$i] = $field; // The fields are being added to the index 0 of the array to be written to the header of the csv file
$i++;
}
$attributenames = ParticipantAttributeNames::model()->getAttributes();
// Attribute names are being added to the index 0 of the array
foreach ($attributenames as $key => $value)
{
$outputarray[0][$i] = $value['attribute_name'];
$i++;
}
// Fetching the table data
$i = 1;
$j = 0;
// Read through the query result and add it to the array
// Please not it will give only basic field in the central database
foreach ($query as $field => $aData)
{
foreach ($fields as $field)
{
$outputarray[$i][$j] = $aData[$field];
//increment the column
$j++;
}
// it will iterate through the additional attributes that the user has choosen to export and will fetch the values
// that are to be exported to the CSV file
foreach ($attributenames as $key => $value)
{
$answer = ParticipantAttributeNames::model()->getAttributeValue($aData['participant_id'], $value['attribute_id']);
if (isset($answer['value']))
{ // if the attribute value is there for that attribute and the user then it will written to the array
$outputarray[$i][$j] = $answer['value'];
//increment the column
$j++;
}
else
{ // otherwise blank value will be written to the array
$outputarray[$i][$j] = "";
//increment the column
$j++;
}
}
// increment the row
$i++;
}
// Load the helper and pass the array to be written to a CSV file
CPDBExport($outputarray, "central_" . time());
}
/**
* 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 = Participants::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 (Yii::app()->session['USER_RIGHT_SUPERADMIN']) //If super admin all the participants will be visible
{
$query = Participants::model()->getParticipantsWithoutLimit();
}
else
{
$query = Participants::model()->getParticipantsOwner(Yii::app()->session['loginID']);
}
printf($this->getController()->lang->gT("%s participant(s) are to be copied "), count($query));
}
}
/**
* Gets the ids of participants to be copied to the individual survey
*/
function getSearchIDs()
{
$searchcondition = basename(Yii::app()->request->getPost('searchcondition')); // get the search condition from the URL
/* a search contains posted data inside $_POST['searchcondition'].
* Each seperate query is made up of 3 fields, seperated by double-pipes ("|")
* EG: fname||eq||jason||lname||ct||c
*
*/
if ($searchcondition != 'getParticipants_json') // if there is a search condition present
{
$participantid = "";
$condition = explode("||", $searchcondition); // explode the condition to the array
$query = Participants::model()->getParticipantsSearchMultiple($condition, 0, 0);
foreach ($query as $key => $value)
{
if (Yii::app()->session['USER_RIGHT_SUPERADMIN'])
{
$participantid .= "," . $value['participant_id']; // combine the participant id's in an string
} else
{
if(Participants::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 (Yii::app()->session['USER_RIGHT_SUPERADMIN']) //If super admin all the participants will be visible
{
$query = Participants::model()->getParticipantsWithoutLimit(); // get all the participant id if it is a super admin
}
else // get participants on which the user has right on
{
$query = Participants::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
}
}
/**
* Responsible for reading the CSV file line by line, check for duplicate participants
* invalid participants and invalid attributes and copy them to the central table
* Also responsible for creation of new attribute and mapping of old attribute to attribute in csv
*/
function exporttocsv()
{
Yii::app()->loadHelper('export');
$searchconditionurl = Yii::app()->request->getPost('searchcondition');
$searchcondition = basename($searchconditionurl);
if (Yii::app()->session['USER_RIGHT_SUPERADMIN']) //If super admin all the participants will be visible
{
if ($searchcondition != 'getParticipants_json') // If there is a search condition then only does participants are exported
{
$condition = explode("||", $searchcondition);
$query = Participants::model()->getParticipantsSearchMultiple($condition, 0, 0);
} // else all the participants in the central table will be exported since it's superadmin
else
{
$query = Participants::model()->getParticipantsWithoutLimit();
}
}
else
{
$iUserID = Yii::app()->session['loginID']; // else only the
$query = Participants::model()->getParticipantsOwner($iUserID);
}
if (!$query)
return false;
// Field names in the first row
$fields = array('participant_id', 'firstname', 'lastname', 'email', 'language', 'blacklisted', 'owner_uid');
$i = 0;
$outputarray = array(); // The array to be passed to the export helper to be written to a csv file
foreach ($fields as $field)
{
$outputarray[0][$i] = $field; //fields written to output array
$i++;
}
if (Yii::app()->request->getQuery('id') == "null")
{
$i = 1;
$j = 0;
foreach ($query as $field => $aData)
{
foreach ($fields as $field)
{
$outputarray[$i][$j] = $aData[$field];
$j++;
}
$i++;
}
CPDBExport($outputarray, "central_" . time());
}
else
{
$iAttributeId = explode(",", Yii::app()->request->getQuery('id'));
foreach ($iAttributeId as $key => $value)
{
$attributename = ParticipantAttributeNames::model()->getAttributeNames($value);
$outputarray[0][$i] = $attributename[0]['attribute_name'];
$i++;
}
$i = 1;
$j = 0;
// Fetching the table data
foreach ($query as $field => $aData)
{
foreach ($fields as $field)
{
$outputarray[$i][$j] = $aData[$field];
$j++;
}
foreach ($iAttributeId as $key => $value)
{
$answer = ParticipantAttributeNames::model()->getAttributeValue($aData['participant_id'], $value);
if (isset($answer['value']))
{
$outputarray[$i][$j] = $answer['value'];
$j++;
}
else
{
$outputarray[$i][$j] = "";
$j++;
}
}
$i++;
}
CPDBExport($outputarray, "central_" . time());
}
}
function getParticipantsResults_json()
{
///admin/participants/getParticipantsResults_json/search/email||contains||com
//Possible methods: equal,contains,notequal,notcontains,greaterthan,lessthan
//First entry is field to search, second method, third value, seperated by double pipe "||"
$page = Yii::app()->request->getPost('page');
$limit = Yii::app()->request->getPost('rows');
$page=($page) ? $page : 1;
$limit=($limit) ? $limit : 25;
$attid = ParticipantAttributeNames::model()->getAttributeVisibleID();
$participantfields = array('participant_id', 'can_edit', 'firstname', 'lastname', 'email', 'blacklisted', 'survey', 'language', 'owner_uid');
//If super admin all the participants will be visible
if (Yii::app()->session['USER_RIGHT_SUPERADMIN'])
{
$searchcondition = Yii::app()->request->getQuery('search');
$searchcondition = urldecode($searchcondition);
$finalcondition = array();
$condition = explode("||", $searchcondition);
$aData = new stdClass();
$aData->page = $page;
$records = Participants::model()->getParticipantsSearchMultiple($condition, $page, $limit);
$aData->records = count(Participants::model()->getParticipantsSearchMultiple($condition, 0, 0));
$aData->total = ceil($aData->records / $limit);
$i = 0;
foreach ($records as $row => $value)
{
$username = User::model()->getName($value['owner_uid']); //for conversion of uid to human readable names
$surveycount = Participants::model()->getSurveyCount($value['participant_id']);
$sortablearray[$i] = array($value['participant_id'], "true", $value['firstname'], $value['lastname'], $value['email'], $value['blacklisted'], $surveycount, $value['language'], $username[0]['full_name']); // since it's the admin he has access to all editing on the participants inspite of what can_edit option is
$attributes = ParticipantAttributeNames::model()->getParticipantVisibleAttribute($value['participant_id']);
foreach ($attid as $iAttributeId)
{
$participantfields[]=$iAttributeId['attribute_id'];
$answer = ParticipantAttributeNames::model()->getAttributeValue($value['participant_id'], $iAttributeId['attribute_id']);
if (isset($answer['value']))
{
array_push($sortablearray[$i], $answer['value']);
}
else
{
array_push($sortablearray[$i], "");
}
}
$i++;
}
function subval_sort($a, $subkey, $order)
{
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;
}
if (!empty($sortablearray))
{
$indexsort = array_search(Yii::app()->request->getPost('sidx'), $participantfields);
if(is_numeric(Yii::app()->request->getPost('sidx'))) {
}
//var_dump($sortablearray);echo "\r\n\r\n";
$sortedarray = subval_sort($sortablearray, $indexsort, Yii::app()->request->getPost('sord'));
$i = 0;
$count = count($sortedarray[0]);
foreach ($sortedarray as $key => $value)
{
$aData->rows[$i]['id'] = $value[0];
$aData->rows[$i]['cell'] = array();
for ($j = 0; $j < $count; $j++)
{
array_push($aData->rows[$i]['cell'], $value[$j]);
}
$i++;
}
}
echo ls_json_encode($aData);
}
// Only the owned and shared participants will be visible
else
{
$searchcondition = Yii::app()->request->getQuery('search');
$searchcondition = urldecode($searchcondition);
$finalcondition = array();
$condition = explode("||", $searchcondition);
$aData = new stdClass();
$aData->page = $page;
$records = Participants::model()->getParticipantsSearchMultiple($condition, $page, $limit);
$i = 0;
foreach ($records as $row => $value)
{
if (Participants::model()->is_owner($value['participant_id']))
{
$username = User::model()->getName($value['owner_uid']); //for conversion of uid to human readable names
$surveycount = Participants::model()->getSurveyCount($value['participant_id']);
$sortablearray[$i] = array($value['participant_id'], "true", $value['firstname'], $value['lastname'], $value['email'], $value['blacklisted'], $surveycount, $value['language'], $username[0]['full_name']); // since it's the admin he has access to all editing on the participants inspite of what can_edit option is
$attributes = ParticipantAttributeNames::model()->getParticipantVisibleAttribute($value['participant_id']);
foreach ($attid as $iAttributeId)
{
$answer = ParticipantAttributeNames::model()->getAttributeValue($value['participant_id'], $iAttributeId['attribute_id']);
if (isset($answer['value']))
{
array_push($sortablearray[$i], $answer['value']);
}
else
{
array_push($sortablearray[$i], "");
}
}
$i++;
}
}
function subval_sort($a, $subkey, $order)
{
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;
}
if (!empty($sortablearray))
{
$aData->records = count($sortablearray);
$aData->total = ceil(count($sortablearray) / $limit);
$indexsort = array_search(Yii::app()->request->getPost('sidx'), $participantfields);
$sortedarray = subval_sort($sortablearray, $indexsort, Yii::app()->request->getPost('sord'));
$i = 0;
$count = count($sortedarray[0]);
foreach ($sortedarray as $key => $value)
{
$aData->rows[$i]['id'] = $value[0];
$aData->rows[$i]['cell'] = array();
for ($j = 0; $j < $count; $j++)
{
array_push($aData->rows[$i]['cell'], $value[$j]);