-
Notifications
You must be signed in to change notification settings - Fork 994
/
remotecontrol_handle.php
3033 lines (2694 loc) · 129 KB
/
remotecontrol_handle.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
/**
* This class handles all methods of the RemoteControl 2 API
*/
class remotecontrol_handle
{
/**
* @var AdminController
*/
protected $controller;
/**
* Constructor, stores the action instance into this handle class
*
* @access public
* @param AdminController $controller
* @return void
*/
public function __construct(AdminController $controller)
{
$this->controller = $controller;
}
/**
* Create and return a session key.
*
* Using this function you can create a new XML-RPC/JSON-RPC session key.
* This is mandatory for all following LSRC2 function calls.
*
* * In case of success : Return the session key in string
* * In case of error:
* * for protocol-level errors (invalid format etc), an error message.
* * For invalid username and password, returns a null error and the result body contains a 'status' name-value pair with the error message.
*
* @access public
* @param string $username
* @param string $password
* @return string|array
*/
public function get_session_key($username, $password)
{
if ($this->_doLogin($username, $password))
{
$this->_jumpStartSession($username);
$sSessionKey = randomChars(32);
$sDatabasetype = Yii::app()->db->getDriverName();
$session = new Session;
$session->id = $sSessionKey;
$session->expire = time() + Yii::app()->getConfig('iSessionExpirationTime');
if($sDatabasetype=='pgsql')
$username=new CDbExpression(Yii::app()->db->quoteValueExtended($username, PDO::PARAM_LOB)."::bytea");
if($sDatabasetype=='sqlsrv' || $sDatabasetype=='mssql' || $sDatabasetype=='dblib')
$username=new CDbExpression('CONVERT(VARBINARY(MAX), '.Yii::app()->db->quoteValue($username).')');
$session->data = $username;
$session->save();
return $sSessionKey;
}
else
return array('status' => 'Invalid user name or password');
}
/**
* Close the RPC session
*
* Using this function you can close a previously opened XML-RPC/JSON-RPC session.
*
* @access public
* @param string $sSessionKey the session key
* @return string OK
*/
public function release_session_key($sSessionKey)
{
Session::model()->deleteAllByAttributes(array('id' => $sSessionKey));
$criteria = new CDbCriteria;
$criteria->condition = 'expire < ' . time();
Session::model()->deleteAll($criteria);
return 'OK';
}
/**
* Get a global setting
*
* Function to query site settings. Can only be used by super administrators.
*
* @access public
* @param string $sSessionKey Auth Credentials
* @param string $sSetttingName Name of the setting to get
* @return string|array The requested value or an array with the error in case of error
*/
public function get_site_settings($sSessionKey,$sSetttingName)
{
if ($this->_checkSessionKey($sSessionKey)) {
if(Permission::model()->hasGlobalPermission('superadmin','read')) {
if (Yii::app()->getConfig($sSetttingName) !== false) {
return Yii::app()->getConfig($sSetttingName);
} else {
return array('status' => 'Invalid setting');
}
} else {
return array('status' => 'Invalid setting');
}
} else {
return array('status' => 'Invalid session key');
}
}
/* Survey specific functions */
/**
* Add an empty survey with minimum details
*
* This just tries to create an empty survey with the minimal settings.
*
* Failure status: Invalid session key, No permission, Faulty parameters, Creation Failed result
*
* @access public
* @param string $sSessionKey Auth credentials
* @param int $iSurveyID The desired ID of the Survey to add
* @param string $sSurveyTitle Title of the new Survey
* @param string $sSurveyLanguage Default language of the Survey
* @param string $sformat (optional) Question appearance format (A, G or S) for "All on one page", "Group by Group", "Single questions", default to group by group (G)
* @return int|array The survey id in case of success
*/
public function add_survey($sSessionKey, $iSurveyID, $sSurveyTitle, $sSurveyLanguage, $sformat = 'G')
{
Yii::app()->loadHelper("surveytranslator");
if ($this->_checkSessionKey($sSessionKey))
{
if (Permission::model()->hasGlobalPermission('surveys','create'))
{
if( $sSurveyTitle=='' || $sSurveyLanguage=='' || !array_key_exists($sSurveyLanguage,getLanguageDataRestricted()) || !in_array($sformat, array('A','G','S')))
return array('status' => 'Faulty parameters');
$aInsertData = array('template' => 'default',
'owner_id' => Yii::app()->session['loginID'],
'active' => 'N',
'language'=>$sSurveyLanguage,
'format' => $sformat
);
if (!is_null($iSurveyID))
$aInsertData['wishSID'] = $iSurveyID;
try
{
$iNewSurveyid = Survey::model()->insertNewSurvey($aInsertData);
if (!$iNewSurveyid)
return array('status' => 'Creation Failed');
$sTitle = html_entity_decode($sSurveyTitle, ENT_QUOTES, "UTF-8");
$aInsertData = array(
'surveyls_survey_id' => $iNewSurveyid,
'surveyls_title' => $sTitle,
'surveyls_language' => $sSurveyLanguage,
);
$langsettings = new SurveyLanguageSetting;
$langsettings->insertNewSurvey($aInsertData);
Permission::model()->giveAllSurveyPermissions(Yii::app()->session['loginID'], $iNewSurveyid);
return (int)$iNewSurveyid;
}
catch(Exception $e)
{
return array('status' => $e->getmessage());
}
}
else
return array('status' => 'No permission');
}
else
return array('status' => 'Invalid session key');
}
/**
* Delete a survey.
*
* Failure status: Invalid session key, No permission
*
* @access public
* @param string $sSessionKey Auth credentials
* @param int $iSurveyID The ID of the Survey to be deleted
* @return array Returns status : status are OK in case of success
*/
public function delete_survey($sSessionKey, $iSurveyID)
{
if ($this->_checkSessionKey($sSessionKey))
{
if (Permission::model()->hasSurveyPermission($iSurveyID, 'survey', 'delete'))
{
Survey::model()->deleteSurvey($iSurveyID,true);
return array('status' => 'OK');
}
else
return array('status' => 'No permission');
}
else
return array('status' => 'Invalid session key');
}
/**
* Import a survey in a known format
*
* Allow importing lss, csv, xls or survey zip archive in BASE 64 encoded.
*
* Failure status: Invalid session key, No permission, The import error
*
* @access public
* @param string $sSessionKey Auth Credentials
* @param string $sImportData String containing the BASE 64 encoded data of a lss, csv, txt or survey lsa archive
* @param string $sImportDataType lss, csv, txt or lsa
* @param string $sNewSurveyName (optional) The optional new name of the survey
* @param integer $DestSurveyID (optional) This is the new ID of the survey - if already used a random one will be taken instead
* @return int|array The ID of the new survey in case of success
*/
public function import_survey($sSessionKey, $sImportData, $sImportDataType, $sNewSurveyName=NULL, $DestSurveyID=NULL)
{
if ($this->_checkSessionKey($sSessionKey))
{
if (Permission::model()->hasGlobalPermission('surveys','create'))
{
if (!in_array($sImportDataType,array('lsa','csv','txt','lss'))) return array('status' => 'Invalid extension');
Yii::app()->loadHelper('admin/import');
// First save the data to a temporary file
$sFullFilePath = Yii::app()->getConfig('tempdir') . DIRECTORY_SEPARATOR . randomChars(40).'.'.$sImportDataType;
file_put_contents($sFullFilePath,base64_decode(chunk_split($sImportData)));
$aImportResults = importSurveyFile($sFullFilePath, true, $sNewSurveyName, $DestSurveyID);
unlink($sFullFilePath);
if (isset($aImportResults['error']) && $aImportResults['error']) return array('status' => 'Error: '.$aImportResults['error']);
else
{
return (int)$aImportResults['newsid'];
}
}
else
return array('status' => 'No permission');
}
else
return array('status' => 'Invalid session key');
}
/**
* Get properties of a survey
*
* All internal properties of a survey are available.
* @see \Survey for the list of available properties
*
* Failure status : Invalid survey ID, Invalid session key, No permission, No valid Data
*
* @access public
* @param string $sSessionKey Auth credentials
* @param int $iSurveyID The id of the Survey to be checked
* @param array|null $aSurveySettings (optional) The properties to get
* @return array
*/
public function get_survey_properties($sSessionKey,$iSurveyID, $aSurveySettings=null)
{
Yii::app()->loadHelper("surveytranslator");
if ($this->_checkSessionKey($sSessionKey))
{
$oSurvey = Survey::model()->findByPk($iSurveyID);
if (!isset($oSurvey))
{
return array('status' => 'Error: Invalid survey ID');
}
if (Permission::model()->hasSurveyPermission($iSurveyID, 'surveysettings', 'read'))
{
$aBasicDestinationFields=Survey::model()->tableSchema->columnNames;
if(!empty($aSurveySettings)) {
$aSurveySettings=array_intersect($aSurveySettings,$aBasicDestinationFields);
} else {
$aSurveySettings=$aBasicDestinationFields;
}
if (empty($aSurveySettings)) {
return array('status' => 'No valid Data');
}
$aResult = array();
foreach($aSurveySettings as $sPropertyName)
{
$aResult[$sPropertyName]=$oSurvey->$sPropertyName;
}
return $aResult;
}
else
return array('status' => 'No permission');
}
else
return array('status' => 'Invalid Session key');
}
/**
* Set survey properties.
*
* @see \Survey for the list of available properties
* Properties available are restricted
* * Always
* * sid
* * owner_id
* * active
* * language
* * additional_languages
* * If survey is active
* * anonymized
* * datestamp
* * savetimings
* * ipaddr
* * refurl
*
* In case of partial success : return an array with key as properties and value as boolean , true if saved with success.
*
* Failure status : Invalid survey ID, Invalid session key, No permission, No valid Data
*
* @access public
* @param string $sSessionKey Auth credentials
* @param integer $iSurveyID - ID of the Survey
* @param array $aSurveyData - An array with the particular fieldnames as keys and their values to set on that particular Survey
* @return array Of succeeded and failed nodifications according to internal validation
*/
public function set_survey_properties($sSessionKey, $iSurveyID, $aSurveyData)
{
if ($this->_checkSessionKey($sSessionKey))
{
$oSurvey=Survey::model()->findByPk($iSurveyID);
if (is_null($oSurvey))
{
return array('status' => 'Error: Invalid survey ID');
}
if (Permission::model()->hasSurveyPermission($iSurveyID, 'surveysettings', 'update'))
{
// Remove fields that may not be modified
unset($aSurveyData['sid']);
unset($aSurveyData['owner_id']);
unset($aSurveyData['active']);
unset($aSurveyData['language']);
unset($aSurveyData['additional_languages']);
// Remove invalid fields
$aDestinationFields=array_flip(Survey::model()->tableSchema->columnNames);
$aSurveyData=array_intersect_key($aSurveyData,$aDestinationFields);
$oSurvey=Survey::model()->findByPk($iSurveyID);
$aBasicAttributes = $oSurvey->getAttributes();
$aResult = array();
if ($oSurvey->active=='Y')
{
// remove all fields that may not be changed when a survey is active
unset($aSurveyData['anonymized']);
unset($aSurveyData['datestamp']);
unset($aSurveyData['savetimings']);
unset($aSurveyData['ipaddr']);
unset($aSurveyData['refurl']);
}
if (empty($aSurveyData))
return array('status' => 'No valid Data');
foreach($aSurveyData as $sFieldName=>$sValue)
{
$oSurvey->$sFieldName=$sValue;
try
{
$bSaveResult=$oSurvey->save(); // save the change to database
//unset the value if it fails, so as to prevent future fails
$aResult[$sFieldName]=$bSaveResult;
if (!$bSaveResult)
$oSurvey->$sFieldName=$aBasicAttributes[$sFieldName];
}
catch(Exception $e)
{
//unset the value that caused the exception
$oSurvey->$sFieldName=$aBasicAttributes[$sFieldName];
}
}
return $aResult;
}
else
return array('status' => 'No permission');
}
else
return array('status' => 'Invalid Session key');
}
/**
* Activate an existing survey
*
* Return the result of the activation
* Failure status : Invalid Survey ID, Activation Error, Invalid session key, No permission
*
* @access public
* @param string $sSessionKey Auth credentials
* @param int $iSurveyID ID of the Survey to be activated
* @return array in case of success result of the activation
*/
public function activate_survey($sSessionKey, $iSurveyID)
{
if ($this->_checkSessionKey($sSessionKey))
{
$oSurvey=Survey::model()->findByPk($iSurveyID);
if (is_null($oSurvey)) {
return array('status' => 'Error: Invalid survey ID');
}
if (Permission::model()->hasSurveyPermission($iSurveyID, 'surveyactivation', 'update'))
{
Yii::app()->loadHelper('admin/activate');
$aActivateResults = activateSurvey($iSurveyID);
if (isset($aActivateResults['error'])) {
return array('status' => 'Error: '.$aActivateResults['error']);
} else {
return $aActivateResults;
}
}
else
return array('status' => 'No permission');
}
else
return array('status' => 'Invalid session key');
}
/**
* Export statistics of a survey to a user.
*
* Allow to export statistics available Returns string - base64 encoding of the statistics.
*
* @access public
* @param string $sSessionKey Auth credentials
* @param int $iSurveyID ID of the Survey
* @param string $docType (optional) Type of documents the exported statistics should be (pdf|xls|html)
* @param string $sLanguage (optional) language of the survey to use (default from Survey)
* @param string $graph (optional) Create graph option (default : no)
* @param int|array $groupIDs (optional) array or integer containing the groups we choose to generate statistics from
* @return string in case of success : Base64 encoded string with the statistics file
*/
public function export_statistics($sSessionKey, $iSurveyID, $docType='pdf', $sLanguage=null, $graph='0', $groupIDs=null)
{
Yii::app()->loadHelper('admin/statistics');
if (!$this->_checkSessionKey($sSessionKey)) return array('status' => 'Invalid session key');
$oSurvey = Survey::model()->findByPk($iSurveyID);
if (!isset($oSurvey))
return array('status' => 'Error: Invalid survey ID');;
if (!Permission::model()->hasSurveyPermission($iSurveyID, 'statistics', 'read'))
return array('status' => 'Error: No Permission');
$aAdditionalLanguages = array_filter(explode(" ", $oSurvey->additional_languages));
if (is_null($sLanguage)|| !in_array($sLanguage,$aAdditionalLanguages))
$sLanguage = $oSurvey->language;
$oAllQuestions =Question::model()->getQuestionList($iSurveyID, $sLanguage);
if (!isset($oAllQuestions))
return array('status' => 'No available data');
if($groupIDs!=null)
{
if(is_int($groupIDs))
$groupIDs = array($groupIDs);
if(is_array($groupIDs))
{
//check that every value of the array belongs to the survey defined
$aGroups = QuestionGroup::model()->findAllByAttributes(array('sid' => $iSurveyID));
foreach( $aGroups as $group)
$validGroups[] = $group['gid'];
$groupIDs=array_intersect($groupIDs,$validGroups);
if (empty($groupIDs))
return array('status' => 'Error: Invalid group ID');
foreach($oAllQuestions as $key => $aQuestion)
{
if(!in_array($aQuestion['gid'],$groupIDs))
unset($oAllQuestions[$key]);
}
}
else
return array('status' => 'Error: Invalid group ID');
}
if (!isset($oAllQuestions))
return array('status' => 'No available data');
usort($oAllQuestions, 'groupOrderThenQuestionOrder');
$aSummary = createCompleteSGQA($iSurveyID,$oAllQuestions,$sLanguage);
$helper = new statistics_helper();
switch ($docType)
{
case 'pdf':
$sTempFile = $helper->generate_statistics($iSurveyID,$aSummary,$aSummary,$graph,$docType,'F',$sLanguage);
$sResult = file_get_contents($sTempFile);
unlink($sTempFile);
break;
case 'xls':
$sTempFile = $helper->generate_statistics($iSurveyID,$aSummary,$aSummary,'0',$docType, 'F',$sLanguage);
$sResult = file_get_contents($sTempFile);
unlink($sTempFile);
break;
case 'html':
$sResult = $helper->generate_statistics($iSurveyID,$aSummary,$aSummary,'0',$docType, 'DD',$sLanguage);
break;
}
return base64_encode($sResult);
}
/**
* RPC Routine to export submission timeline.
* Returns an array of values (count and period)
*
* @access public
* @param string $sSessionKey Auth credentials
* @param int $iSurveyID ID of the Survey
* @param string $sType (day|hour)
* @param string $dStart
* @param string $dEnd
* @return array On success: The timeline. On failure array with error information
* */
public function export_timeline($sSessionKey, $iSurveyID, $sType, $dStart, $dEnd)
{
$survey = Survey::model()->findByPk($iSurveyID);
if (!$this->_checkSessionKey($sSessionKey)) return array('status' => 'Invalid session key');
if (!in_array($sType, array('day','hour'))) return array('status' => 'Invalid Period');
if (!Permission::model()->hasSurveyPermission($iSurveyID, 'responses', 'read')) return array('status' => 'No permission');
if (is_null($survey)) return array('status' => 'Error: Invalid survey ID');
if (!tableExists($survey->responsesTableName)) return array('status' => 'No available data');
$oResponses = SurveyDynamic::model($iSurveyID)->timeline($sType, $dStart, $dEnd);
if (empty($oResponses)) return array('status' => 'No valid Data');
return $oResponses;
}
/**
* Get survey summary, regarding token usage and survey participation.
*
* Returns the requested value as string, or all status in an array
*
* Available status are
* * For Survey stats
* * completed_responses
* * incomplete_responses
* * full_responses
* * For token part
* * token_count
* * token_invalid
* * token_sent
* * token_opted_out
* * token_completed
* All available status can be sent using `all`
*
* Failure status : No available data, No such property, Invalid session key, No permission
*
* @access public
* @param string $sSessionKey Auth credentials
* @param int $iSurveyID ID of the Survey to get summary
* @param string $sStatName (optional) Name of the summary option, or all to send all in an array (all by default)
* @return string|array in case of success the requested value or an array of all values
*/
public function get_summary($sSessionKey,$iSurveyID, $sStatName='all')
{
if ($this->_checkSessionKey($sSessionKey)) {
$aPermittedTokenStats = array(
'token_count',
'token_invalid',
'token_sent',
'token_opted_out',
'token_completed'
);
$aPermittedSurveyStats = array(
'completed_responses',
'incomplete_responses',
'full_responses'
);
$aPermittedStats = array_merge($aPermittedSurveyStats, $aPermittedTokenStats, array('all'));
// Check if survey exists
$oSurvey = Survey::model()->findByPk($iSurveyID);
if (!isset($oSurvey))
return array('status' => 'Invalid surveyid');
if (!in_array($sStatName, $aPermittedStats))
return array('status' => 'Invalid summary key');
//Check permissions to access this survey
if (Permission::model()->hasSurveyPermission($iSurveyID, 'survey', 'read'))
{
$aSummary=array();
if (in_array($sStatName, $aPermittedTokenStats) || $sStatName=='all') {
if ($oSurvey->hasTokensTable) {
$aTokenSummary = Token::model($iSurveyID)->summary();
if ($aTokenSummary) {
$aSummary['token_count']=$aTokenSummary['count'];
$aSummary['token_invalid']=$aTokenSummary['invalid'];
$aSummary['token_sent']=$aTokenSummary['sent'];
$aSummary['token_opted_out']=$aTokenSummary['optout'];
$aSummary['token_completed']=$aTokenSummary['completed'];
}
}
elseif ($sStatName!='all')
{
return array('status' => 'No available data');
}
}
if (in_array($sStatName, $aPermittedSurveyStats) || $sStatName=='all') {
if (tableExists($oSurvey->responsesTableName)) {
$aSummary['completed_responses']=SurveyDynamic::model($iSurveyID)->count('submitdate is NOT NULL');
$aSummary['incomplete_responses']=SurveyDynamic::model($iSurveyID)->countByAttributes(array('submitdate' => null));
$aSummary['full_responses']=SurveyDynamic::model($iSurveyID)->count();
} elseif ($sStatName!='all') {
return array('status' => 'No available data');
}
}
if ($sStatName=='all') {
return $aSummary;
}
else
{
return $aSummary[$sStatName];
}
}
else
return array('status' => 'No permission');
}
else
return array('status' => 'Invalid session key');
}
/*Survey language specific functions */
/**
* RPC Routine to add a survey language.
*
* @access public
* @param string $sSessionKey Auth credentials
* @param integer $iSurveyID ID of the Survey for which a token table will be created
* @param string $sLanguage A valid language shortcut to add to the current Survey. If the language already exists no error will be given.
* @return array Status=>OK when successful, otherwise the error description
*/
public function add_language($sSessionKey, $iSurveyID, $sLanguage)
{
if ($this->_checkSessionKey($sSessionKey))
{
$oSurvey=Survey::model()->findByPk($iSurveyID);
if (is_null($oSurvey))
{
return array('status' => 'Error: Invalid survey ID');
}
if (Permission::model()->hasSurveyPermission($iSurveyID, 'surveysettings', 'update'))
{
Yii::app()->loadHelper('surveytranslator');
$aLanguages=getLanguageData();
if(!isset($aLanguages[$sLanguage]))
{
return array('status' => 'Invalid language');
}
$oSurvey=Survey::model()->findByPk($iSurveyID);
if ($sLanguage==$oSurvey->language)
{
return array('status' => 'OK');
}
$aLanguages=$oSurvey->getAdditionalLanguages();
$aLanguages[]=$sLanguage;
$aLanguages=array_unique($aLanguages);
$oSurvey->additional_languages=implode(' ',$aLanguages);
try
{
$oSurvey->save(); // save the change to database
$languagedetails=getLanguageDetails($sLanguage);
$insertdata = array(
'surveyls_survey_id' => $iSurveyID,
'surveyls_language' => $sLanguage,
'surveyls_title' => '',
'surveyls_dateformat' => $languagedetails['dateformat']
);
$setting= new SurveyLanguageSetting;
foreach ($insertdata as $k => $v)
$setting->$k = $v;
$setting->save();
fixLanguageConsistency($iSurveyID,$sLanguage);
return array('status' => 'OK');
}
catch(Exception $e)
{
return array('status' => 'Error');
}
}
else
return array('status' => 'No permission');
}
}
/**
* RPC Routine to delete a language from a survey.
*
* @access public
* @param string $sSessionKey Auth credentials
* @param integer $iSurveyID ID of the Survey for which a token table will be created
* @param string $sLanguage A valid language shortcut to delete from the current Survey. If the language does not exist in that Survey no error will be given.
* @return array Status=>OK when successful, otherwise the error description
*/
public function delete_language($sSessionKey, $iSurveyID, $sLanguage)
{
if ($this->_checkSessionKey($sSessionKey))
{
$oSurvey=Survey::model()->findByPk($iSurveyID);
if (is_null($oSurvey))
{
return array('status' => 'Error: Invalid survey ID');
}
if (Permission::model()->hasSurveyPermission($iSurveyID, 'surveysettings', 'update'))
{
Yii::app()->loadHelper('surveytranslator');
$aLanguages=getLanguageData();
if(!isset($aLanguages[$sLanguage]))
{
return array('status' => 'Invalid language');
}
$oSurvey=Survey::model()->findByPk($iSurveyID);
if ($sLanguage==$oSurvey->language)
{
return array('status' => 'Cannot remove base language');
}
$aLanguages=$oSurvey->getAdditionalLanguages();
unset($aLanguages[$sLanguage]);
$oSurvey->additional_languages=implode(' ',$aLanguages);
try
{
$oSurvey->save(); // save the change to database
SurveyLanguageSetting::model()->deleteByPk(array('surveyls_survey_id' => $iSurveyID, 'surveyls_language' => $sLanguage));
cleanLanguagesFromSurvey($iSurveyID,$oSurvey->additional_languages);
return array('status' => 'OK');
}
catch(Exception $e)
{
return array('status' => 'Error');
}
}
else
return array('status' => 'No permission');
}
}
/**
* Get survey language properties.
*
* @see \SurveyLanguageSetting for available properties
*
* @access public
* @param string $sSessionKey Auth credentials
* @param int $iSurveyID ID of the Survey
* @param array|null $aSurveyLocaleSettings (optional) Properties to get, default to all attributes
* @param string|null $sLang (optional) Language to use, default to Survey->language
* @return array in case of success The requested values
*/
public function get_language_properties($sSessionKey,$iSurveyID, $aSurveyLocaleSettings=null, $sLang=null)
{
Yii::app()->loadHelper("surveytranslator");
if ($this->_checkSessionKey($sSessionKey))
{
$oSurvey = Survey::model()->findByPk($iSurveyID);
if (!isset($oSurvey))
{
return array('status' => 'Error: Invalid survey ID');
}
if (Permission::model()->hasSurveyPermission($iSurveyID, 'surveysettings', 'read'))
{
$aBasicDestinationFields=SurveyLanguageSetting::model()->tableSchema->columnNames;
if(!empty($aSurveyLocaleSettings)) {
$aSurveyLocaleSettings=array_intersect($aSurveyLocaleSettings,$aBasicDestinationFields);
} else {
$aSurveyLocaleSettings=$aBasicDestinationFields;
}
if ($sLang == NULL || !array_key_exists($sLang,getLanguageDataRestricted()))
$sLang = $oSurvey->language;
$oSurveyLocale=SurveyLanguageSetting::model()->findByAttributes(array('surveyls_survey_id' => $iSurveyID, 'surveyls_language' => $sLang));
$aResult = array();
if (empty($aSurveyLocaleSettings))
return array('status' => 'No valid Data');
foreach($aSurveyLocaleSettings as $sPropertyName)
{
$aResult[$sPropertyName]=$oSurveyLocale->$sPropertyName;
//$aResult[$sPropertyName]=$aLangAttributes[$sPropertyName];
}
return $aResult;
}
else
return array('status' => 'No permission');
}
else
return array('status' => 'Invalid Session key');
}
/**
* Set survey language properties.
*
* @see \SurveyLanguageSetting for available properties.
*
* Some properties can not be set
* * surveyls_language
* * surveyls_survey_id
*
* @access public
* @param string $sSessionKey Auth credentials
* @param integer $iSurveyID - ID of the Survey
* @param array $aSurveyLocaleData - An array with the particular fieldnames as keys and their values to set on that particular survey
* @param string $sLanguage - Optional - Language to update - if not give the base language of the particular survey is used
* @return array in case of success 'status'=>'OK', when save successful otherwise error text.
*/
public function set_language_properties($sSessionKey, $iSurveyID, $aSurveyLocaleData, $sLanguage=NULL)
{
Yii::app()->loadHelper("surveytranslator");
if ($this->_checkSessionKey($sSessionKey))
{
$oSurvey=Survey::model()->findByPk($iSurveyID);
if (is_null($oSurvey))
{
return array('status' => 'Error: Invalid survey ID');
}
if (is_null($sLanguage))
{
$sLanguage=$oSurvey->language;
}
if (!array_key_exists($sLanguage,getLanguageDataRestricted()))
return array('status' => 'Error: Invalid language');
if (Permission::model()->hasSurveyPermission($iSurveyID, 'surveylocale', 'update'))
{
// Remove fields that may not be modified
unset($aSurveyLocaleData['surveyls_language']);
unset($aSurveyLocaleData['surveyls_survey_id']);
// Remove invalid fields
$aDestinationFields=array_flip(SurveyLanguageSetting::model()->tableSchema->columnNames);
$aSurveyLocaleData=array_intersect_key($aSurveyLocaleData,$aDestinationFields);
$oSurveyLocale = SurveyLanguageSetting::model()->findByPk(array('surveyls_survey_id' => $iSurveyID, 'surveyls_language' => $sLanguage));
$aLangAttributes = $oSurveyLocale->getAttributes();
$aResult = array();
if (empty($aSurveyLocaleData))
return array('status' => 'No valid Data');
foreach($aSurveyLocaleData as $sFieldName=>$sValue)
{
$oSurveyLocale->$sFieldName=$sValue;
try
{
// save the change to database - Every single change alone - to allow for validation to work
$bSaveResult=$oSurveyLocale->save();
$aResult[$sFieldName]=$bSaveResult;
//unset failed values
if (!$bSaveResult)
$oSurveyLocale->$sFieldName=$aLangAttributes[$sFieldName];
}
catch(Exception $e)
{
$oSurveyLocale->$sFieldName=$aLangAttributes[$sFieldName];
}
}
$aResult['status'] = 'OK';
return $aResult;
}
else
return array('status' => 'No permission');
}
else
return array('status' => 'Invalid Session key');
}
/* Group specific functions */
/**
* Add an empty group with minimum details to a chosen survey.
* Used as a placeholder for importing questions.
* Returns the groupid of the created group.
*
* @access public
* @param string $sSessionKey Auth credentials
* @param int $iSurveyID ID of the Survey to add the group
* @param string $sGroupTitle Name of the group
* @param string $sGroupDescription Optional description of the group
* @return array|int The id of the new group - Or status
*/
public function add_group($sSessionKey, $iSurveyID, $sGroupTitle, $sGroupDescription='')
{
if ($this->_checkSessionKey($sSessionKey))
{
if (Permission::model()->hasSurveyPermission($iSurveyID, 'survey', 'update'))
{
$oSurvey = Survey::model()->findByPk($iSurveyID);
if (!isset($oSurvey))
return array('status' => 'Error: Invalid survey ID');
if($oSurvey['active']=='Y')
return array('status' => 'Error:Survey is active and not editable');
$oGroup = new QuestionGroup;
$oGroup->sid = $iSurveyID;
$oGroup->group_name = $sGroupTitle;
$oGroup->description = $sGroupDescription;
$oGroup->group_order = getMaxGroupOrder($iSurveyID);
$oGroup->language = Survey::model()->findByPk($iSurveyID)->language;
if($oGroup->save())
return (int)$oGroup->gid;
else
return array('status' => 'Creation Failed');
}
else
return array('status' => 'No permission');
}
else
return array('status' => 'Invalid Session Key');
}
/**
* Delete a group from a chosen survey .
* Returns the id of the deleted group.
*
* @access public
* @param string $sSessionKey Auth credentials
* @param int $iSurveyID ID of the Survey that the group belongs
* @param int $iGroupID ID of the group to delete
* @return array|int The ID of the deleted group or status
*/
public function delete_group($sSessionKey, $iSurveyID, $iGroupID)
{
if ($this->_checkSessionKey($sSessionKey))
{
$iSurveyID = sanitize_int($iSurveyID);
$iGroupID = sanitize_int($iGroupID);
$oSurvey = Survey::model()->findByPk($iSurveyID);
if (!isset($oSurvey))
return array('status' => 'Error: Invalid survey ID');
if (Permission::model()->hasSurveyPermission($iSurveyID, 'surveycontent', 'delete'))
{
$oGroup = QuestionGroup::model()->findByAttributes(array('gid' => $iGroupID));
if (!isset($oGroup))
return array('status' => 'Error: Invalid group ID');
if($oSurvey['active']=='Y')
return array('status' => 'Error:Survey is active and not editable');
$depented_on = getGroupDepsForConditions($oGroup->sid,"all",$iGroupID,"by-targgid");
if(isset($depented_on))
return array('status' => 'Group with depencdencies - deletion not allowed');
$iGroupsDeleted = QuestionGroup::deleteWithDependency($iGroupID, $iSurveyID);
if ($iGroupsDeleted === 1)
{
fixSortOrderGroups($iSurveyID);
return (int)$iGroupID;
}
else
return array('status' => 'Group deletion failed');
}
else
return array('status' => 'No permission');
}
else
return array('status' => 'Invalid Session Key');
}
/**
* Import a group and add to a chosen survey - imports lsg,csv
*
* @access public