/
survey.lib.php
executable file
·5384 lines (4912 loc) · 241 KB
/
survey.lib.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
/* For licensing terms, see /license.txt */
$config['survey']['debug'] = false;
$htmlHeadXtra[] = '<script>
function setFocus(){
$("#search_title").focus();
}
$(document).ready(function () {
setFocus();
});
</script>';
/**
* Class survey_manager
* @package chamilo.survey
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University:
* cleanup, refactoring and rewriting large parts (if not all) of the code
* @author Julio Montoya <gugli100@gmail.com>, Personality Test modification
* and rewriting large parts of the code
* @author cfasanando
* @todo move this file to inc/lib
* @todo use consistent naming for the functions (save vs store for instance)
*/
class survey_manager
{
/**
* @param $code
*
* @return string
*/
public static function generate_unique_code($code)
{
if (empty($code)) {
return false;
}
$course_id = api_get_course_int_id();
$table_survey = Database::get_course_table(TABLE_SURVEY);
$code = Database::escape_string($code);
$num = 0;
$new_code = $code;
while (true) {
$sql = "SELECT * FROM $table_survey
WHERE code = '$new_code' AND c_id = $course_id";
$result = Database::query($sql);
if (Database::num_rows($result)) {
$num++;
$new_code = $code . $num;
} else {
break;
}
}
return $code.$num;
}
/**
* Deletes all survey invitations of a user
* @param int $user_id
*
* @return boolean
* @assert ('') === false
*/
public static function delete_all_survey_invitations_by_user($user_id)
{
$user_id = intval($user_id);
if (empty($user_id)) {
return false;
}
$table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
$table_survey = Database :: get_course_table(TABLE_SURVEY);
$sql = "SELECT survey_invitation_id, survey_code
FROM $table_survey_invitation WHERE user = '$user_id' AND c_id <> 0 ";
$result = Database::query($sql);
while ($row = Database::fetch_array($result ,'ASSOC')){
$survey_invitation_id = $row['survey_invitation_id'];
$survey_code = $row['survey_code'];
$sql2 = "DELETE FROM $table_survey_invitation
WHERE survey_invitation_id = '$survey_invitation_id' AND c_id <> 0";
if (Database::query($sql2)) {
$sql3 = "UPDATE $table_survey SET
invited = invited-1
WHERE c_id <> 0 AND code ='$survey_code'";
Database::query($sql3);
}
}
}
/**
*
* @param string $course_code
* @param int $session_id
* @return type
* @assert ('') === false
*/
public static function get_surveys($course_code, $session_id = 0)
{
$table_survey = Database :: get_course_table(TABLE_SURVEY);
if (empty($course_code)) {
return false;
}
$course_info = api_get_course_info($course_code);
$session_condition = api_get_session_condition($session_id, true, true);
$sql = "SELECT * FROM $table_survey
WHERE c_id = {$course_info['real_id']} $session_condition ";
$result = Database::query($sql);
$result = Database::store_result($result, 'ASSOC');
return $result;
}
/**
* Retrieves all the survey information
*
* @param integer $survey_id the id of the survey
* @param boolean $shared this parameter determines if
* we have to get the information of a survey from the central (shared) database or from the
* course database
* @param string course code optional
*
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @version February 2007
* @assert ('') === false
*
* @todo this is the same function as in create_new_survey.php
*/
public static function get_survey($survey_id, $shared = 0, $course_code = '', $simple_return = false)
{
// Table definition
if (!empty($course_code)) {
$my_course_id = $course_code;
} else if (isset($_GET['course'])) {
$my_course_id = Security::remove_XSS($_GET['course']);
} else {
$my_course_id = api_get_course_id();
}
$my_course_info = api_get_course_info($my_course_id);
$table_survey = Database :: get_course_table(TABLE_SURVEY);
if ($shared != 0) {
$table_survey = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION);
$sql = "SELECT * FROM $table_survey
WHERE survey_id='".intval($survey_id)."' ";
} else {
$sql = "SELECT * FROM $table_survey
WHERE
survey_id='".intval($survey_id)."' AND
c_id = ".$my_course_info['real_id'];
}
$result = Database::query($sql);
$return = array();
if (Database::num_rows($result)> 0) {
$return = Database::fetch_array($result,'ASSOC');
if ($simple_return) {
return $return;
}
// We do this (temporarily) to have the array match the quickform elements immediately
// idealiter the fields in the db match the quickform fields
$return['survey_code'] = $return['code'];
$return['survey_title'] = $return['title'];
$return['survey_subtitle'] = $return['subtitle'];
$return['survey_language'] = $return['lang'];
$return['start_date'] = $return['avail_from'];
$return['end_date'] = $return['avail_till'];
$return['survey_share'] = $return['is_shared'];
$return['survey_introduction'] = $return['intro'];
$return['survey_thanks'] = $return['surveythanks'];
$return['survey_type'] = $return['survey_type'];
$return['one_question_per_page']= $return['one_question_per_page'];
$return['show_form_profile'] = $return['show_form_profile'];
$return['input_name_list'] = isset($return['input_name_list']) ? $return['input_name_list'] : null;
$return['shuffle'] = $return['shuffle'];
$return['parent_id'] = $return['parent_id'];
$return['survey_version'] = $return['survey_version'];
$return['anonymous'] = $return['anonymous'];
}
return $return;
}
/**
* This function stores a survey in the database.
*
* @param array $values
*
* @return array $return the type of return message that has to be displayed and the message in it
*
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @version February 2007
*/
public static function store_survey($values)
{
$_user = api_get_user_info();
$course_id = api_get_course_int_id();
$table_survey = Database :: get_course_table(TABLE_SURVEY);
$shared_survey_id = 0;
if (!isset($values['survey_id'])) {
// Check if the code doesn't soon exists in this language
$sql = 'SELECT 1 FROM '.$table_survey.'
WHERE
c_id = '.$course_id.' AND
code="'.Database::escape_string($values['survey_code']).'" AND
lang="'.Database::escape_string($values['survey_language']).'"';
$rs = Database::query($sql);
if (Database::num_rows($rs) > 0) {
$return['message'] = 'ThisSurveyCodeSoonExistsInThisLanguage';
$return['type'] = 'error';
$return['id'] = isset($values['survey_id']) ? $values['survey_id'] : 0;
return $return;
}
if (!isset($values['anonymous'])) {
$values['anonymous'] = 0;
}
$values['anonymous'] = intval($values['anonymous']);
$additional['columns'] = '';
$additional['values'] = '';
if ($values['anonymous'] == 0) {
// Input_name_list
$additional['columns'] .= ', show_form_profile';
$values['show_form_profile'] = isset($values['show_form_profile']) ? $values['show_form_profile'] : null;
$additional['values'] .= ",'".Database::escape_string($values['show_form_profile'])."'";
if ($values['show_form_profile'] == 1) {
// Input_name_list
$fields = explode(',', $values['input_name_list']);
$field_values = '';
foreach ($fields as & $field) {
if ($field != '') {
if ($values[$field] == '') {
$values[$field] = 0;
}
$field_values.= $field.':'.$values[$field].'@';
}
}
$additional['columns'] .= ', form_fields';
$additional['values'] .= ",'".Database::escape_string($field_values)."'";
} else {
$additional['columns'] .= ', form_fields';
$additional['values'] .= ",''";
}
} else {
// Input_name_list
$additional['columns'] .= ', show_form_profile';
$additional['values'] .= ",'0'";
$additional['columns'] .= ', form_fields';
$additional['values'] .= ",''";
}
if ($values['survey_type'] == 1) {
$additional['columns'] .= ', survey_type';
$additional['values'] .= ",'1'";
$additional['columns'] .= ', shuffle';
$additional['values'] .= ",'".Database::escape_string($values['shuffle'])."'";
$additional['columns'] .= ', one_question_per_page';
$additional['values'] .= ",'".Database::escape_string($values['one_question_per_page'])."'";
$additional['columns'] .= ', parent_id';
$additional['values'] .= ",'".Database::escape_string($values['parent_id'])."'";
// Logic for versioning surveys
if (!empty($values['parent_id'])) {
$additional['columns'] .= ', survey_version';
$sql = 'SELECT survey_version
FROM '.$table_survey.'
WHERE
c_id = '.$course_id.' AND
parent_id = '.intval($values['parent_id']).'
ORDER BY survey_version DESC
LIMIT 1';
$rs = Database::query($sql);
if (Database::num_rows($rs) === 0) {
$sql = 'SELECT survey_version FROM '.$table_survey.'
WHERE
c_id = '.$course_id.' AND
survey_id = '.intval($values['parent_id']);
$rs = Database::query($sql);
$getversion = Database::fetch_array($rs, 'ASSOC');
if (empty($getversion['survey_version'])) {
$additional['values'] .= ",'".++$getversion['survey_version']."'";
} else {
$additional['values'] .= ",'".$getversion['survey_version'].".1'";
}
} else {
$row = Database::fetch_array($rs, 'ASSOC');
$pos = api_strpos($row['survey_version']);
if ($pos === false) {
$row['survey_version'] = $row['survey_version'] + 1;
$additional['values'] .= ",'".$row['survey_version']."'";
} else {
$getlast = api_split('\.', $row['survey_version']);
$lastversion = array_pop($getlast);
$lastversion = $lastversion + 1;
$add = implode('.', $getlast);
if ($add != '') {
$insertnewversion = $add.'.'.$lastversion;
} else {
$insertnewversion = $lastversion;
}
$additional['values'] .= ",'".$insertnewversion."'";
}
}
}
}
$course_id = api_get_course_int_id();
$sql = "INSERT INTO $table_survey(
c_id,
code,
title,
subtitle,
author,
lang,
avail_from,
avail_till,
is_shared,
template,
intro,
surveythanks,
creation_date,
anonymous".$additional['columns'].",
session_id,
visible_results
) VALUES (
$course_id,
'".Database::escape_string(strtolower(generate_course_code(api_substr($values['survey_code'],0))))."',
'".Database::escape_string($values['survey_title'])."',
'".Database::escape_string($values['survey_subtitle'])."',
'".intval($_user['user_id'])."',
'".Database::escape_string($values['survey_language'])."',
'".Database::escape_string($values['start_date'])."',
'".Database::escape_string($values['end_date'])."',
'".Database::escape_string($shared_survey_id)."',
'".Database::escape_string('template')."',
'".Database::escape_string($values['survey_introduction'])."',
'".Database::escape_string($values['survey_thanks'])."',
'".api_get_utc_datetime()."',
'".Database::escape_string($values['anonymous'])."'".$additional['values'].",
'".api_get_session_id()."',
".Database::escape_string($values['visible_results'])."
)";
Database::query($sql);
$survey_id = Database::insert_id();
if ($survey_id > 0) {
// Insert into item_property
api_item_property_update(api_get_course_info(), TOOL_SURVEY, $survey_id, 'SurveyAdded', api_get_user_id());
}
if ($values['survey_type'] == 1 && !empty($values['parent_id'])) {
survey_manager::copy_survey($values['parent_id'], $survey_id);
}
$return['message'] = 'SurveyCreatedSuccesfully';
$return['type'] = 'confirmation';
$return['id'] = $survey_id;
} else {
// Check whether the code doesn't soon exists in this language
$sql = 'SELECT 1 FROM '.$table_survey.'
WHERE
c_id = '.$course_id.' AND
code = "'.Database::escape_string($values['survey_code']).'" AND
lang = "'.Database::escape_string($values['survey_language']).'" AND
survey_id !='.intval($values['survey_id']);
$rs = Database::query($sql);
if (Database::num_rows($rs) > 0) {
$return['message'] = 'ThisSurveyCodeSoonExistsInThisLanguage';
$return['type'] = 'error';
$return['id'] = isset($values['survey_id']) ? $values['survey_id'] : 0;
return $return;
}
if (!isset($values['anonymous']) ||
isset($values['anonymous']) && $values['anonymous'] == ''
) {
$values['anonymous'] = 0;
}
$values['shuffle'] = isset($values['shuffle']) ? $values['shuffle'] : null;
$values['one_question_per_page'] = isset($values['one_question_per_page']) ? $values['one_question_per_page'] : null;
$values['show_form_profile'] = isset($values['show_form_profile']) ? $values['show_form_profile'] : null;
$additionalsets = ", shuffle = '".Database::escape_string($values['shuffle'])."'";
$additionalsets .= ", one_question_per_page = '".Database::escape_string($values['one_question_per_page'])."'";
if ($values['anonymous'] == 0) {
$additionalsets .= ", show_form_profile = '".Database::escape_string($values['show_form_profile'])."'";
if ($values['show_form_profile'] == 1) {
$fields = explode(',',$values['input_name_list']);
$field_values = '';
foreach ($fields as & $field) {
if ($field != '') {
if ($values[$field] == '') {
$values[$field] = 0;
}
$field_values.= $field.':'.$values[$field].'@';
}
}
$additionalsets .= ", form_fields = '".Database::escape_string($field_values)."'";
} else {
$additionalsets .= ", form_fields = '' ";
}
} else {
$additionalsets .= ", show_form_profile = '0'";
$additionalsets .= ", form_fields = '' ";
}
$sql = "UPDATE $table_survey SET
title = '".Database::escape_string($values['survey_title'])."',
subtitle = '".Database::escape_string($values['survey_subtitle'])."',
author = '".Database::escape_string($_user['user_id'])."',
lang = '".Database::escape_string($values['survey_language'])."',
avail_from = '".Database::escape_string($values['start_date'])."',
avail_till = '".Database::escape_string($values['end_date'])."',
is_shared = '".Database::escape_string($shared_survey_id)."',
template = '".Database::escape_string('template')."',
intro = '".Database::escape_string($values['survey_introduction'])."',
surveythanks = '".Database::escape_string($values['survey_thanks'])."',
anonymous = '".Database::escape_string($values['anonymous'])."'".$additionalsets.",
visible_results = '".Database::escape_string($values['visible_results'])."'
WHERE c_id = $course_id AND survey_id = '".Database::escape_string($values['survey_id'])."'";
Database::query($sql);
// Update into item_property (update)
api_item_property_update(api_get_course_info(), TOOL_SURVEY, $values['survey_id'], 'SurveyUpdated', api_get_user_id());
$return['message'] = 'SurveyUpdatedSuccesfully';
$return['type'] = 'confirmation';
$return['id'] = $values['survey_id'];
}
return $return;
}
/**
* This function stores a shared survey in the central database.
*
* @param array $values
* @return array $return the type of return message that has to be displayed and the message in it
*
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @version February 2007
*/
public function store_shared_survey($values)
{
$_user = api_get_user_info();
$_course = api_get_course_info();
// Table definitions
$table_survey = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY);
if (!$values['survey_id'] ||
!is_numeric($values['survey_id']) ||
$values['survey_share']['survey_share'] == 'true'
) {
$sql = "INSERT INTO $table_survey (code, title, subtitle, author, lang, template, intro, surveythanks, creation_date, course_code) VALUES (
'".Database::escape_string($values['survey_code'])."',
'".Database::escape_string($values['survey_title'])."',
'".Database::escape_string($values['survey_subtitle'])."',
'".intval($_user['user_id'])."',
'".Database::escape_string($values['survey_language'])."',
'".Database::escape_string('template')."',
'".Database::escape_string($values['survey_introduction'])."',
'".Database::escape_string($values['survey_thanks'])."',
'".date('Y-m-d H:i:s')."',
'".$_course['id']."')";
Database::query($sql);
$return = Database::insert_id();
} else {
$sql = "UPDATE $table_survey SET
code = '".Database::escape_string($values['survey_code'])."',
title = '".Database::escape_string($values['survey_title'])."',
subtitle = '".Database::escape_string($values['survey_subtitle'])."',
author = '".intval($_user['user_id'])."',
lang = '".Database::escape_string($values['survey_language'])."',
template = '".Database::escape_string('template')."',
intro = '".Database::escape_string($values['survey_introduction'])."',
surveythanks = '".Database::escape_string($values['survey_thanks'])."'
WHERE survey_id = '".Database::escape_string($values['survey_share']['survey_share'])."'";
Database::query($sql);
$return = $values['survey_share']['survey_share'];
}
return $return;
}
/**
* This function deletes a survey (and also all the question in that survey
*
* @param int $survey_id id of the survey that has to be deleted
* @return true
*
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @version January 2007
*/
public static function delete_survey($survey_id, $shared = false, $course_id = '')
{
// Database table definitions
if (empty($course_id)) {
$course_id = api_get_course_int_id();
}
$survey_id = intval($survey_id);
if (empty($survey_id)) {
return false;
}
$course_info = api_get_course_info_by_id($course_id);
$course_id = $course_info['real_id'];
$table_survey = Database :: get_course_table(TABLE_SURVEY);
$table_survey_question_group = Database :: get_course_table(TABLE_SURVEY_QUESTION_GROUP);
if ($shared) {
$table_survey = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY);
// Deleting the survey
$sql = "DELETE FROM $table_survey WHERE survey_id='".$survey_id."'";
Database::query($sql);
} else {
$sql = "DELETE FROM $table_survey WHERE c_id = $course_id AND survey_id='".$survey_id."'";
Database::query($sql);
}
// Deleting groups of this survey
$sql = "DELETE FROM $table_survey_question_group WHERE c_id = $course_id AND survey_id='".$survey_id."'";
Database::query($sql);
// Deleting the questions of the survey
survey_manager::delete_all_survey_questions($survey_id, $shared);
// Update into item_property (delete)
api_item_property_update($course_info, TOOL_SURVEY, $survey_id, 'SurveyDeleted', api_get_user_id());
return true;
}
/**
* @param int $survey_id
* @param int $new_survey_id
*
* @return bool
*/
public static function copy_survey($survey_id, $new_survey_id = null)
{
$course_id = api_get_course_int_id();
// Database table definitions
$table_survey = Database::get_course_table(TABLE_SURVEY);
$table_survey_question_group = Database::get_course_table(TABLE_SURVEY_QUESTION_GROUP);
$table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION);
$table_survey_options = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION);
$survey_id = intval($survey_id);
// Get groups
$survey_data = self::get_survey($survey_id, 0, null, true);
if (empty($survey_data)) {
return true;
}
if (empty($new_survey_id)) {
$params = $survey_data;
$params['code'] = self::generate_unique_code($params['code']);
$params['c_id'] = $course_id;
unset($params['survey_id']);
$params['session_id'] = api_get_session_id();
$params['title'] = $params['title'] . ' ' . get_lang('Copy');
Database::insert($table_survey, $params);
$new_survey_id = Database::insert_id();
// Insert into item_property
api_item_property_update(api_get_course_info(), TOOL_SURVEY, $new_survey_id, 'SurveyAdded', api_get_user_id());
} else {
$new_survey_id = intval($new_survey_id);
}
$sql = "SELECT * FROM $table_survey_question_group
WHERE c_id = $course_id AND survey_id='".$survey_id."'";
$res = Database::query($sql);
while($row = Database::fetch_array($res, 'ASSOC')) {
$params = array(
'c_id' => $course_id,
'name' => $row['name'],
'description' => $row['description'],
'survey_id' => $new_survey_id
);
$insertId = Database::insert($table_survey_question_group, $params);
$group_id[$row['id']] = $insertId;
}
// Get questions
$sql = "SELECT * FROM $table_survey_question
WHERE c_id = $course_id AND survey_id='".$survey_id."'";
$res = Database::query($sql);
while ($row = Database::fetch_array($res, 'ASSOC')) {
$params = array(
'c_id' => $course_id,
'survey_id' => $new_survey_id,
'survey_question' => $row['survey_question'],
'survey_question_comment' => $row['survey_question_comment'],
'type' => $row['type'],
'display' => $row['display'],
'sort' => $row['sort'],
'shared_question_id' => $row['shared_question_id'],
'max_value' => $row['max_value'],
'survey_group_pri' => $row['survey_group_pri'],
'survey_group_sec1' => $row['survey_group_sec1'],
'survey_group_sec2' => $row['survey_group_sec2']
);
$insertId = Database::insert($table_survey_question, $params);
$question_id[$row['question_id']] = $insertId;
}
// Get questions options
$sql = "SELECT * FROM $table_survey_options
WHERE c_id = $course_id AND survey_id='".$survey_id."'";
$res = Database::query($sql);
while ($row = Database::fetch_array($res ,'ASSOC')) {
$params = array(
'c_id' => $course_id,
'question_id' => $question_id[$row['question_id']],
'survey_id' => $new_survey_id,
'option_text' => $row['option_text'],
'sort' => $row['sort'],
'value' => $row['value']
);
Database::insert($table_survey_options, $params);
}
return true;
}
/**
* This function duplicates a survey (and also all the question in that survey
*
* @param int $survey_id id of the survey that has to be duplicated
* @return true
*
* @author Eric Marguin <e.marguin@elixir-interactive.com>, Elixir Interactive
* @version October 2007
*/
public static function empty_survey($survey_id)
{
// Database table definitions
$table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
$table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
$table_survey = Database :: get_course_table(TABLE_SURVEY);
$course_id = api_get_course_int_id();
$datas = survey_manager::get_survey($survey_id);
$session_where = '';
if (api_get_session_id() != 0) {
$session_where = ' AND session_id = "'.api_get_session_id().'" ';
}
$sql = 'DELETE FROM '.$table_survey_invitation.'
WHERE c_id = '.$course_id.' AND survey_code = "'.Database::escape_string($datas['code']).'" '.$session_where.' ';
Database::query($sql);
$sql = 'DELETE FROM '.$table_survey_answer.'
WHERE c_id = '.$course_id.' AND survey_id='.intval($survey_id);
Database::query($sql);
$sql = 'UPDATE '.$table_survey.' SET invited=0, answered=0
WHERE c_id = '.$course_id.' AND survey_id='.intval($survey_id);
Database::query($sql);
return true;
}
/**
* This function recalculates the number of people who have taken the survey (=filled at least one question)
*
* @param int $survey_id the id of the survey somebody
* @return true
*
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @version February 2007
*/
public static function update_survey_answered($survey_data, $user, $survey_code)
{
// Database table definitions
$table_survey = Database :: get_course_table(TABLE_SURVEY);
$table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
$survey_id = $survey_data['survey_id'];
$course_id = $survey_data['c_id'];
$session_id = $survey_data['session_id'];
// Getting a list with all the people who have filled the survey
$people_filled = survey_manager::get_people_who_filled_survey($survey_id, false, $course_id);
$number = intval(count($people_filled));
// Storing this value in the survey table
$sql = "UPDATE $table_survey
SET answered = $number
WHERE
c_id = $course_id AND
survey_id = ".intval($survey_id);
Database::query($sql);
// Storing that the user has finished the survey.
$sql = "UPDATE $table_survey_invitation SET answered='1'
WHERE
c_id = $course_id AND
session_id='".$session_id."' AND
user='".Database::escape_string($user)."' AND
survey_code='".Database::escape_string($survey_code)."'";
Database::query($sql);
}
/**
* Gets a complete structure of a survey
* (all survey information, all question information of all the questions
* and all the options of all the questions.
*
* @param integer $survey_id the id of the survey
* @param boolean $shared this parameter determines if we have to get the
* information of a survey from the central (shared) database or from the
* course database
*
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @version February 2007
*/
public static function get_complete_survey_structure($survey_id, $shared = 0)
{
$structure = survey_manager::get_survey($survey_id, $shared);
$structure['questions'] = survey_manager::get_questions($survey_id);
}
/***
* SURVEY QUESTION FUNCTIONS
*/
/**
* This function return the "icon" of the question type
*
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @version February 2007
*/
public static function icon_question($type)
{
// the possible question types
$possible_types = array(
'personality', 'yesno', 'multiplechoice', 'multipleresponse', 'open', 'dropdown', 'comment', 'pagebreak', 'percentage', 'score'
);
// the images array
$icon_question = array(
'yesno' => 'yesno.gif',
'personality' => 'yesno.gif',
'multiplechoice' => 'mcua.png',
'multipleresponse' => 'mcma.png',
'open' => 'open_answer.png',
'dropdown' => 'dropdown.gif',
'percentage' => 'percentagequestion.gif',
'score' => 'scorequestion.gif',
'comment' => 'commentquestion.gif',
'pagebreak' => 'page_end.gif',
);
if (in_array($type, $possible_types)) {
return $icon_question[$type];
} else {
return false;
}
}
/**
* This function retrieves all the information of a question
*
* @param integer $question_id the id of the question
* @return array
*
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @version January 2007
*
* @todo one sql call should do the trick
*/
public static function get_question($question_id, $shared = false)
{
// Table definitions
$tbl_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
$table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
$course_id = api_get_course_int_id();
$sql = "SELECT * FROM $tbl_survey_question
WHERE c_id = $course_id AND question_id='".intval($question_id)."'
ORDER BY `sort` ";
$sqlOption = " SELECT * FROM $table_survey_question_option
WHERE c_id = $course_id AND question_id='".intval($question_id)."'
ORDER BY `sort` ";
if ($shared) {
$tbl_survey_question = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION);
$table_survey_question_option = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION);
$sql = "SELECT * FROM $tbl_survey_question
WHERE question_id='".intval($question_id)."'
ORDER BY `sort` ";
$sqlOption = "SELECT * FROM $table_survey_question_option
WHERE question_id='".intval($question_id)."'
ORDER BY `sort` ";
}
// Getting the information of the question
$result = Database::query($sql);
$row = Database::fetch_array($result,'ASSOC');
$return['survey_id'] = $row['survey_id'];
$return['question_id'] = $row['question_id'];
$return['type'] = $row['type'];
$return['question'] = $row['survey_question'];
$return['horizontalvertical'] = $row['display'];
$return['shared_question_id'] = $row['shared_question_id'];
$return['maximum_score'] = $row['max_value'];
if ($row['survey_group_pri'] != 0) {
$return['assigned'] = $row['survey_group_pri'];
$return['choose'] = 1;
} else {
$return['assigned1'] = $row['survey_group_sec1'];
$return['assigned2'] = $row['survey_group_sec2'];
$return['choose'] = 2;
}
// Getting the information of the question options
$result = Database::query($sqlOption);
while ($row = Database::fetch_array($result, 'ASSOC')) {
/** @todo this should be renamed to options instead of answers */
$return['answers'][] = $row['option_text'];
$return['values'][] = $row['value'];
/** @todo this can be done more elegantly (used in reporting) */
$return['answersid'][] = $row['question_option_id'];
}
return $return;
}
/**
* This function gets all the question of any given survey
*
* @param integer $survey_id the id of the survey
* @return array containing all the questions of the survey
*
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @version February 2007
*
* @todo one sql call should do the trick
*/
public static function get_questions($survey_id, $course_id = '')
{
// Table definitions
$tbl_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
$table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
if (empty($course_id))
{
$course_id = api_get_course_int_id();
}
// Getting the information of the question
$sql = "SELECT * FROM $tbl_survey_question
WHERE c_id = $course_id AND survey_id='".intval($survey_id)."'";
$result = Database::query($sql);
$return = array();
while ($row = Database::fetch_array($result, 'ASSOC')) {
$return[$row['question_id']]['survey_id'] = $row['survey_id'];
$return[$row['question_id']]['question_id'] = $row['question_id'];
$return[$row['question_id']]['type'] = $row['type'];
$return[$row['question_id']]['question'] = $row['survey_question'];
$return[$row['question_id']]['horizontalvertical'] = $row['display'];
$return[$row['question_id']]['maximum_score'] = $row['max_value'];
$return[$row['question_id']]['sort'] = $row['sort'];
}
// Getting the information of the question options
$sql = "SELECT * FROM $table_survey_question_option
WHERE c_id = $course_id AND survey_id='".intval($survey_id)."'";
$result = Database::query($sql);
while ($row = Database::fetch_array($result, 'ASSOC')) {
$return[$row['question_id']]['answers'][] = $row['option_text'];
}
return $return;
}
/**
* This function saves a question in the database.
* This can be either an update of an existing survey or storing a new survey
* @param array $survey_data
* @param array $form_content all the information of the form
*
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @version January 2007
*/
public static function save_question($survey_data, $form_content)
{
if (strlen($form_content['question']) > 1) {
// Checks length of the question
$empty_answer = false;
if ($survey_data['survey_type'] == 1) {
if (empty($form_content['choose'])) {
$return_message = 'PleaseChooseACondition';
return $return_message;
}
if (($form_content['choose']==2)&&($form_content['assigned1'] == $form_content['assigned2'])) {
$return_message = 'ChooseDifferentCategories';
return $return_message;
}
}
if ($form_content['type'] != 'percentage') {
for ($i = 0; $i < count($form_content['answers']); $i++) {
if (strlen($form_content['answers'][$i]) < 1) {
$empty_answer = true;
break;
}
}
}
if ($form_content['type'] == 'score') {
if (strlen($form_content['maximum_score']) < 1) {
$empty_answer = true;
}
}
$additional = array();
$course_id = api_get_course_int_id();
if (!$empty_answer) {
// Table definitions
$tbl_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
// Getting all the information of the survey
$survey_data = survey_manager::get_survey($form_content['survey_id']);
// Storing the question in the shared database
if (is_numeric($survey_data['survey_share']) && $survey_data['survey_share'] != 0) {
$shared_question_id = survey_manager::save_shared_question($form_content, $survey_data);
$form_content['shared_question_id'] = $shared_question_id;
}
// Storing a new question
if ($form_content['question_id'] == '' || !is_numeric($form_content['question_id'])) {
// Finding the max sort order of the questions in the given survey
$sql = "SELECT max(sort) AS max_sort
FROM $tbl_survey_question
WHERE c_id = $course_id AND survey_id='".intval($form_content['survey_id'])."'";
$result = Database::query($sql);
$row = Database::fetch_array($result,'ASSOC');
$max_sort = $row['max_sort'];
// Some variables defined for survey-test type
$additional['column'] = '';
$additional['value'] = '';
if (isset($_POST['choose'])) {
if ($_POST['choose'] == 1) {
$additional['column'] = ',survey_group_pri';
$additional['value'] = ",'" . Database::escape_string($_POST['assigned']) . "'";
} elseif ($_POST['choose'] == 2) {
$additional['column'] = ',survey_group_sec1, survey_group_sec2';
$additional['value'] = ",'" . Database::escape_string($_POST['assigned1']) . "'" . ",'" . Database::escape_string($_POST['assigned2']) . "'";
}
}
// Adding the question to the survey_question table
$sql = "INSERT INTO $tbl_survey_question
(c_id, survey_id,survey_question,survey_question_comment,type,display, sort, shared_question_id, max_value".$additional['column'].")
VALUES (
$course_id,
'".Database::escape_string($form_content['survey_id'])."',
'".Database::escape_string($form_content['question'])."',
'".Database::escape_string($form_content['question_comment'])."',
'".Database::escape_string($form_content['type'])."',
'".Database::escape_string($form_content['horizontalvertical'])."',
'".Database::escape_string($max_sort+1)."',
'".Database::escape_string($form_content['shared_question_id'])."',
'".Database::escape_string($form_content['maximum_score'])."'".
$additional['value']."
)";
Database::query($sql);
$question_id = Database::insert_id();
$form_content['question_id'] = $question_id;