-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.php
1193 lines (1017 loc) · 38.6 KB
/
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
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package mod_survey
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Graph size
* @global int $SURVEY_GHEIGHT
*/
global $SURVEY_GHEIGHT;
$SURVEY_GHEIGHT = 500;
/**
* Graph size
* @global int $SURVEY_GWIDTH
*/
global $SURVEY_GWIDTH;
$SURVEY_GWIDTH = 900;
/**
* Question Type
* @global array $SURVEY_QTYPE
*/
global $SURVEY_QTYPE;
$SURVEY_QTYPE = array (
"-3" => "Virtual Actual and Preferred",
"-2" => "Virtual Preferred",
"-1" => "Virtual Actual",
"0" => "Text",
"1" => "Actual",
"2" => "Preferred",
"3" => "Actual and Preferred",
);
define("SURVEY_COLLES_ACTUAL", "1");
define("SURVEY_COLLES_PREFERRED", "2");
define("SURVEY_COLLES_PREFERRED_ACTUAL", "3");
define("SURVEY_ATTLS", "4");
define("SURVEY_CIQ", "5");
// Question length to wrap.
define("SURVEY_QLENGTH_WRAP", "80");
require_once(__DIR__ . '/deprecatedlib.php');
// STANDARD FUNCTIONS ////////////////////////////////////////////////////////
/**
* Given an object containing all the necessary data,
* (defined by the form in mod_form.php) this function
* will create a new instance and return the id number
* of the new instance.
*
* @global object
* @param object $survey
* @return int|bool
*/
function survey_add_instance($survey) {
global $DB;
if (!$template = $DB->get_record("survey", array("id"=>$survey->template))) {
return 0;
}
$survey->questions = $template->questions;
$survey->timecreated = time();
$survey->timemodified = $survey->timecreated;
$id = $DB->insert_record("survey", $survey);
$completiontimeexpected = !empty($survey->completionexpected) ? $survey->completionexpected : null;
\core_completion\api::update_completion_date_event($survey->coursemodule, 'survey', $id, $completiontimeexpected);
return $id;
}
/**
* Given an object containing all the necessary data,
* (defined by the form in mod_form.php) this function
* will update an existing instance with new data.
*
* @global object
* @param object $survey
* @return bool
*/
function survey_update_instance($survey) {
global $DB;
if (!$template = $DB->get_record("survey", array("id"=>$survey->template))) {
return 0;
}
$survey->id = $survey->instance;
$survey->questions = $template->questions;
$survey->timemodified = time();
$completiontimeexpected = !empty($survey->completionexpected) ? $survey->completionexpected : null;
\core_completion\api::update_completion_date_event($survey->coursemodule, 'survey', $survey->id, $completiontimeexpected);
return $DB->update_record("survey", $survey);
}
/**
* Given an ID of an instance of this module,
* this function will permanently delete the instance
* and any data that depends on it.
*
* @global object
* @param int $id
* @return bool
*/
function survey_delete_instance($id) {
global $DB;
if (! $survey = $DB->get_record("survey", array("id"=>$id))) {
return false;
}
$cm = get_coursemodule_from_instance('survey', $id);
\core_completion\api::update_completion_date_event($cm->id, 'survey', $id, null);
$result = true;
if (! $DB->delete_records("survey_analysis", array("survey"=>$survey->id))) {
$result = false;
}
if (! $DB->delete_records("survey_answers", array("survey"=>$survey->id))) {
$result = false;
}
if (! $DB->delete_records("survey", array("id"=>$survey->id))) {
$result = false;
}
return $result;
}
/**
* @global object
* @param object $course
* @param object $user
* @param object $mod
* @param object $survey
* @return $result
*/
function survey_user_outline($course, $user, $mod, $survey) {
global $DB;
if ($answers = $DB->get_records("survey_answers", array('survey'=>$survey->id, 'userid'=>$user->id))) {
$lastanswer = array_pop($answers);
$result = new stdClass();
$result->info = get_string("done", "survey");
$result->time = $lastanswer->time;
return $result;
}
return NULL;
}
/**
* @global stdObject
* @global object
* @uses SURVEY_CIQ
* @param object $course
* @param object $user
* @param object $mod
* @param object $survey
*/
function survey_user_complete($course, $user, $mod, $survey) {
global $CFG, $DB, $OUTPUT;
if (survey_already_done($survey->id, $user->id)) {
if ($survey->template == SURVEY_CIQ) { // print out answers for critical incidents
$table = new html_table();
$table->align = array("left", "left");
$questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions));
$questionorder = explode(",", $survey->questions);
foreach ($questionorder as $key=>$val) {
$question = $questions[$val];
$questiontext = get_string($question->shorttext, "survey");
if ($answer = survey_get_user_answer($survey->id, $question->id, $user->id)) {
$answertext = "$answer->answer1";
} else {
$answertext = "No answer";
}
$table->data[] = array("<b>$questiontext</b>", s($answertext));
}
echo html_writer::table($table);
} else {
survey_print_graph("id=$mod->id&sid=$user->id&type=student.png");
}
} else {
print_string("notdone", "survey");
}
}
/**
* @global stdClass
* @global object
* @param object $course
* @param mixed $viewfullnames
* @param int $timestamp
* @return bool
*/
function survey_print_recent_activity($course, $viewfullnames, $timestart) {
global $CFG, $DB, $OUTPUT;
$modinfo = get_fast_modinfo($course);
$ids = array();
foreach ($modinfo->cms as $cm) {
if ($cm->modname != 'survey') {
continue;
}
if (!$cm->uservisible) {
continue;
}
$ids[$cm->instance] = $cm->instance;
}
if (!$ids) {
return false;
}
$slist = implode(',', $ids); // there should not be hundreds of glossaries in one course, right?
$userfieldsapi = \core_user\fields::for_userpic();
$allusernames = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$rs = $DB->get_recordset_sql("SELECT sa.userid, sa.survey, MAX(sa.time) AS time,
$allusernames
FROM {survey_answers} sa
JOIN {user} u ON u.id = sa.userid
WHERE sa.survey IN ($slist) AND sa.time > ?
GROUP BY sa.userid, sa.survey, $allusernames
ORDER BY time ASC", array($timestart));
if (!$rs->valid()) {
$rs->close(); // Not going to iterate (but exit), close rs
return false;
}
$surveys = array();
foreach ($rs as $survey) {
$cm = $modinfo->instances['survey'][$survey->survey];
$survey->name = $cm->name;
$survey->cmid = $cm->id;
$surveys[] = $survey;
}
$rs->close();
if (!$surveys) {
return false;
}
echo $OUTPUT->heading(get_string('newsurveyresponses', 'survey') . ':', 6);
foreach ($surveys as $survey) {
$url = $CFG->wwwroot.'/mod/survey/view.php?id='.$survey->cmid;
print_recent_activity_note($survey->time, $survey, $survey->name, $url, false, $viewfullnames);
}
return true;
}
// SQL FUNCTIONS ////////////////////////////////////////////////////////
/**
* @global object
* @param sting $log
* @return array
*/
function survey_log_info($log) {
global $DB;
return $DB->get_record_sql("SELECT s.name, u.firstname, u.lastname, u.picture
FROM {survey} s, {user} u
WHERE s.id = ? AND u.id = ?", array($log->info, $log->userid));
}
/**
* @global object
* @param int $surveyid
* @param int $groupid
* @param int $groupingid
* @return array
*/
function survey_get_responses($surveyid, $groupid, $groupingid) {
global $DB;
$params = array('surveyid'=>$surveyid, 'groupid'=>$groupid, 'groupingid'=>$groupingid);
if ($groupid) {
$groupsjoin = "JOIN {groups_members} gm ON u.id = gm.userid AND gm.groupid = :groupid ";
} else if ($groupingid) {
$groupsjoin = "JOIN {groups_members} gm ON u.id = gm.userid
JOIN {groupings_groups} gg ON gm.groupid = gg.groupid AND gg.groupingid = :groupingid ";
} else {
$groupsjoin = "";
}
$userfieldsapi = \core_user\fields::for_userpic();
$userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
return $DB->get_records_sql("SELECT $userfields, MAX(a.time) as time
FROM {survey_answers} a
JOIN {user} u ON a.userid = u.id
$groupsjoin
WHERE a.survey = :surveyid
GROUP BY $userfields
ORDER BY time ASC", $params);
}
/**
* @global object
* @param int $survey
* @param int $user
* @return array
*/
function survey_get_analysis($survey, $user) {
global $DB;
return $DB->get_record_sql("SELECT notes
FROM {survey_analysis}
WHERE survey=? AND userid=?", array($survey, $user));
}
/**
* @global object
* @param int $survey
* @param int $user
* @param string $notes
*/
function survey_update_analysis($survey, $user, $notes) {
global $DB;
return $DB->execute("UPDATE {survey_analysis}
SET notes=?
WHERE survey=?
AND userid=?", array($notes, $survey, $user));
}
/**
* @global object
* @param int $surveyid
* @param int $groupid
* @param string $sort
* @return array
*/
function survey_get_user_answers($surveyid, $questionid, $groupid, $sort="sa.answer1,sa.answer2 ASC") {
global $DB;
$params = array('surveyid'=>$surveyid, 'questionid'=>$questionid);
if ($groupid) {
$groupfrom = ', {groups_members} gm';
$groupsql = 'AND gm.groupid = :groupid AND u.id = gm.userid';
$params['groupid'] = $groupid;
} else {
$groupfrom = '';
$groupsql = '';
}
$userfieldsapi = \core_user\fields::for_userpic();
$userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
return $DB->get_records_sql("SELECT sa.*, $userfields
FROM {survey_answers} sa, {user} u $groupfrom
WHERE sa.survey = :surveyid
AND sa.question = :questionid
AND u.id = sa.userid $groupsql
ORDER BY $sort", $params);
}
/**
* @global object
* @param int $surveyid
* @param int $questionid
* @param int $userid
* @return stdClass|false
*/
function survey_get_user_answer($surveyid, $questionid, $userid) {
global $DB;
return $DB->get_record_sql("SELECT sa.*
FROM {survey_answers} sa
WHERE sa.survey = ?
AND sa.question = ?
AND sa.userid = ?", array($surveyid, $questionid, $userid));
}
// MODULE FUNCTIONS ////////////////////////////////////////////////////////
/**
* @global object
* @param int $survey
* @param int $user
* @param string $notes
* @return bool|int
*/
function survey_add_analysis($survey, $user, $notes) {
global $DB;
$record = new stdClass();
$record->survey = $survey;
$record->userid = $user;
$record->notes = $notes;
return $DB->insert_record("survey_analysis", $record, false);
}
/**
* @global object
* @param int $survey
* @param int $user
* @return bool
*/
function survey_already_done($survey, $user) {
global $DB;
return $DB->record_exists("survey_answers", array("survey"=>$survey, "userid"=>$user));
}
/**
* @param int $surveyid
* @param int $groupid
* @param int $groupingid
* @return int
*/
function survey_count_responses($surveyid, $groupid, $groupingid) {
if ($responses = survey_get_responses($surveyid, $groupid, $groupingid)) {
return count($responses);
} else {
return 0;
}
}
/**
* @param int $cmid
* @param array $results
* @param int $courseid
*/
function survey_print_all_responses($cmid, $results, $courseid) {
global $OUTPUT;
$table = new html_table();
$table->head = array ("", get_string("name"), get_string("time"));
$table->align = array ("", "left", "left");
$table->size = array (35, "", "" );
foreach ($results as $a) {
$table->data[] = array($OUTPUT->user_picture($a, array('courseid'=>$courseid)),
html_writer::link("report.php?action=student&student=$a->id&id=$cmid", fullname($a)),
userdate($a->time));
}
echo html_writer::table($table);
}
/**
* @global object
* @param int $templateid
* @return string
*/
function survey_get_template_name($templateid) {
global $DB;
if ($templateid) {
if ($ss = $DB->get_record("surveys", array("id"=>$templateid))) {
return $ss->name;
}
} else {
return "";
}
}
/**
* @param string $name
* @param array $numwords
* @return string
*/
function survey_shorten_name ($name, $numwords) {
$words = explode(" ", $name);
$output = '';
for ($i=0; $i < $numwords; $i++) {
$output .= $words[$i]." ";
}
return $output;
}
/**
* @todo Check this function
*
* @global object
* @global object
* @global int
* @global void This is never defined
* @global object This is defined twice?
* @param object $question
*/
function survey_print_multi($question) {
global $USER, $DB, $qnum, $DB, $OUTPUT; //TODO: this is sloppy globals abuse
$stripreferthat = get_string("ipreferthat", "survey");
$strifoundthat = get_string("ifoundthat", "survey");
$strdefault = get_string('notyetanswered', 'survey');
$strresponses = get_string('responses', 'survey');
echo $OUTPUT->heading($question->text, 3);
echo "\n<table width=\"90%\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\" class=\"surveytable\">";
$options = explode( ",", $question->options);
$numoptions = count($options);
// COLLES Actual (which is having questions of type 1) and COLLES Preferred (type 2)
// expect just one answer per question. COLLES Actual and Preferred (type 3) expects
// two answers per question. ATTLS (having a single question of type 1) expects one
// answer per question. CIQ is not using multiquestions (i.e. a question with subquestions).
// Note that the type of subquestions does not really matter, it's the type of the
// question itself that determines everything.
$oneanswer = ($question->type == 1 || $question->type == 2) ? true : false;
// COLLES Preferred (having questions of type 2) will use the radio elements with the name
// like qP1, qP2 etc. COLLES Actual and ATTLS have radios like q1, q2 etc.
if ($question->type == 2) {
$P = "P";
} else {
$P = "";
}
echo "<colgroup colspan=\"7\"></colgroup>";
echo "<tr class=\"smalltext\"><th scope=\"row\">$strresponses</th>";
echo "<th scope=\"col\" class=\"hresponse\">". get_string('notyetanswered', 'survey'). "</th>";
foreach ($options as $key => $val) {
echo "<th scope=\"col\" class=\"hresponse\">$val</th>\n";
}
echo "</tr>\n";
echo "<tr><th scope=\"colgroup\" colspan=\"7\">$question->intro</th></tr>\n";
$subquestions = survey_get_subquestions($question);
foreach ($subquestions as $q) {
$qnum++;
if ($oneanswer) {
$rowclass = survey_question_rowclass($qnum);
} else {
$rowclass = survey_question_rowclass(round($qnum / 2));
}
if ($q->text) {
$q->text = get_string($q->text, "survey");
}
echo "<tr class=\"$rowclass rblock\">";
if ($oneanswer) {
echo "<th scope=\"row\" class=\"optioncell\">";
echo "<b class=\"qnumtopcell\">$qnum</b> ";
echo $q->text ."</th>\n";
$default = get_accesshide($strdefault);
echo "<td class=\"whitecell\"><label for=\"q$P$q->id\"><input type=\"radio\" name=\"q$P$q->id\" id=\"q$P" . $q->id . "_D\" value=\"0\" checked=\"checked\" data-survey-default=\"true\" />$default</label></td>";
for ($i=1;$i<=$numoptions;$i++) {
$hiddentext = get_accesshide($options[$i-1]);
$id = "q$P" . $q->id . "_$i";
echo "<td><label for=\"$id\"><input type=\"radio\" name=\"q$P$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
}
} else {
echo "<th scope=\"row\" class=\"optioncell\">";
echo "<b class=\"qnumtopcell\">$qnum</b> ";
$qnum++;
echo "<span class=\"preferthat\">$stripreferthat</span> ";
echo "<span class=\"option\">$q->text</span></th>\n";
$default = get_accesshide($strdefault);
echo '<td class="whitecell"><label for="qP'.$q->id.'"><input type="radio" name="qP'.$q->id.'" id="qP'.$q->id.'" value="0" checked="checked" data-survey-default="true" />'.$default.'</label></td>';
for ($i=1;$i<=$numoptions;$i++) {
$hiddentext = get_accesshide($options[$i-1]);
$id = "qP" . $q->id . "_$i";
echo "<td><label for=\"$id\"><input type=\"radio\" name=\"qP$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
}
echo "</tr>";
echo "<tr class=\"$rowclass rblock\">";
echo "<th scope=\"row\" class=\"optioncell\">";
echo "<b class=\"qnumtopcell\">$qnum</b> ";
echo "<span class=\"foundthat\">$strifoundthat</span> ";
echo "<span class=\"option\">$q->text</span></th>\n";
$default = get_accesshide($strdefault);
echo '<td class="whitecell"><label for="q'. $q->id .'"><input type="radio" name="q'.$q->id. '" id="q'. $q->id .'" value="0" checked="checked" data-survey-default="true" />'.$default.'</label></td>';
for ($i=1;$i<=$numoptions;$i++) {
$hiddentext = get_accesshide($options[$i-1]);
$id = "q" . $q->id . "_$i";
echo "<td><label for=\"$id\"><input type=\"radio\" name=\"q$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
}
}
echo "</tr>\n";
}
echo "</table>";
}
/**
* @global object
* @global int
* @param object $question
*/
function survey_print_single($question) {
global $DB, $qnum, $OUTPUT;
$rowclass = survey_question_rowclass(0);
$qnum++;
echo "<br />\n";
echo "<table width=\"90%\" cellpadding=\"4\" cellspacing=\"0\">\n";
echo "<tr class=\"$rowclass\">";
echo "<th scope=\"row\" class=\"optioncell\"><label for=\"q$question->id\"><b class=\"qnumtopcell\">$qnum</b> ";
echo "<span class=\"questioncell\">$question->text</span></label></th>\n";
echo "<td class=\"questioncell smalltext\">\n";
if ($question->type == 0) { // Plain text field
echo "<textarea rows=\"3\" cols=\"30\" class=\"form-control\" name=\"q$question->id\" id=\"q$question->id\">$question->options</textarea>";
} else if ($question->type > 0) { // Choose one of a number
$strchoose = get_string("choose");
echo "<select name=\"q$question->id\" id=\"q$question->id\" class=\"custom-select\">";
echo "<option value=\"0\" selected=\"selected\">$strchoose...</option>";
$options = explode( ",", $question->options);
foreach ($options as $key => $val) {
$key++;
echo "<option value=\"$key\">$val</option>";
}
echo "</select>";
} else if ($question->type < 0) { // Choose several of a number
$options = explode( ",", $question->options);
echo $OUTPUT->notification("This question type not supported yet");
}
echo "</td></tr></table>";
}
/**
*
* @param int $qnum
* @return string
*/
function survey_question_rowclass($qnum) {
if ($qnum) {
return $qnum % 2 ? 'r0' : 'r1';
} else {
return 'r0';
}
}
/**
* @global object
* @global int
* @global int
* @param string $url
*/
function survey_print_graph($url) {
global $CFG, $SURVEY_GHEIGHT, $SURVEY_GWIDTH;
echo "<img class='resultgraph' height=\"$SURVEY_GHEIGHT\" width=\"$SURVEY_GWIDTH\"".
" src=\"$CFG->wwwroot/mod/survey/graph.php?$url\" alt=\"".get_string("surveygraph", "survey")."\" />";
}
/**
* List the actions that correspond to a view of this module.
* This is used by the participation report.
*
* Note: This is not used by new logging system. Event with
* crud = 'r' and edulevel = LEVEL_PARTICIPATING will
* be considered as view action.
*
* @return array
*/
function survey_get_view_actions() {
return array('download','view all','view form','view graph','view report');
}
/**
* List the actions that correspond to a post of this module.
* This is used by the participation report.
*
* Note: This is not used by new logging system. Event with
* crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
* will be considered as post action.
*
* @return array
*/
function survey_get_post_actions() {
return array('submit');
}
/**
* Implementation of the function for printing the form elements that control
* whether the course reset functionality affects the survey.
*
* @param MoodleQuickForm $mform form passed by reference
*/
function survey_reset_course_form_definition(&$mform) {
$mform->addElement('header', 'surveyheader', get_string('modulenameplural', 'survey'));
$mform->addElement('static', 'surveydelete', get_string('delete'));
$mform->addElement('checkbox', 'reset_survey_answers', get_string('deleteallanswers', 'survey'));
$mform->addElement('checkbox', 'reset_survey_analysis', get_string('deleteanalysis', 'survey'));
$mform->hideIf('reset_survey_analysis', 'reset_survey_answers', 'checked');
}
/**
* Course reset form defaults.
* @return array
*/
function survey_reset_course_form_defaults($course) {
return array('reset_survey_answers'=>1, 'reset_survey_analysis'=>1);
}
/**
* Actual implementation of the reset course functionality, delete all the
* survey responses for course $data->courseid.
*
* @global object
* @param $data the data submitted from the reset course.
* @return array status array
*/
function survey_reset_userdata($data) {
global $DB;
$componentstr = get_string('modulenameplural', 'survey');
$status = array();
$surveyssql = "SELECT ch.id
FROM {survey} ch
WHERE ch.course=?";
$params = array($data->courseid);
if (!empty($data->reset_survey_answers)) {
$DB->delete_records_select('survey_answers', "survey IN ($surveyssql)", $params);
$DB->delete_records_select('survey_analysis', "survey IN ($surveyssql)", $params);
$status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallanswers', 'survey'), 'error'=>false);
}
if (!empty($data->reset_survey_analysis)) {
$DB->delete_records_select('survey_analysis', "survey IN ($surveyssql)", $params);
$status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallanswers', 'survey'), 'error'=>false);
}
// No date shifting.
// Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
// See MDL-9367.
return $status;
}
/**
* @uses FEATURE_GROUPS
* @uses FEATURE_GROUPINGS
* @uses FEATURE_MOD_INTRO
* @uses FEATURE_COMPLETION_TRACKS_VIEWS
* @uses FEATURE_GRADE_HAS_GRADE
* @uses FEATURE_GRADE_OUTCOMES
* @param string $feature FEATURE_xx constant for requested feature
* @return mixed True if module supports feature, false if not, null if doesn't know or string for the module purpose.
*/
function survey_supports($feature) {
switch($feature) {
case FEATURE_GROUPS: return true;
case FEATURE_GROUPINGS: return true;
case FEATURE_MOD_INTRO: return true;
case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
case FEATURE_COMPLETION_HAS_RULES: return true;
case FEATURE_GRADE_HAS_GRADE: return false;
case FEATURE_GRADE_OUTCOMES: return false;
case FEATURE_BACKUP_MOODLE2: return true;
case FEATURE_SHOW_DESCRIPTION: return true;
case FEATURE_MOD_PURPOSE: return MOD_PURPOSE_COMMUNICATION;
default: return null;
}
}
/**
* This function extends the settings navigation block for the site.
*
* It is safe to rely on PAGE here as we will only ever be within the module
* context when this is called
*
* @param settings_navigation $settings
* @param navigation_node $surveynode
*/
function survey_extend_settings_navigation(settings_navigation $settings, navigation_node $surveynode) {
global $DB;
$cm = get_coursemodule_from_id('survey', $settings->get_page()->cm->id);
$context = context_module::instance($cm->id);
// Check to see if groups are being used in this survey, confirm user can access.
$groupmode = groups_get_activity_groupmode($cm);
$currentgroup = groups_get_activity_group($cm, true);
if (has_capability('mod/survey:readresponses', $context) &&
!($currentgroup === 0 && $groupmode == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $context))) {
$survey = $DB->get_record("survey", ["id" => $cm->instance]);
$url = new moodle_url('/mod/survey/report.php', ['id' => $cm->id]);
if ($survey && ($survey->template != SURVEY_CIQ)) {
$url->param('action', 'summary');
} else {
$url->param('action', 'questions');
}
$surveynode->add(get_string("responsereports", "survey"), $url);
}
}
/**
* Return a list of page types
* @param string $pagetype current page type
* @param stdClass $parentcontext Block's parent context
* @param stdClass $currentcontext Current context of block
*/
function survey_page_type_list($pagetype, $parentcontext, $currentcontext) {
$module_pagetype = array('mod-survey-*'=>get_string('page-mod-survey-x', 'survey'));
return $module_pagetype;
}
/**
* Mark the activity completed (if required) and trigger the course_module_viewed event.
*
* @param stdClass $survey survey object
* @param stdClass $course course object
* @param stdClass $cm course module object
* @param stdClass $context context object
* @param string $viewed which page viewed
* @since Moodle 3.0
*/
function survey_view($survey, $course, $cm, $context, $viewed) {
// Trigger course_module_viewed event.
$params = array(
'context' => $context,
'objectid' => $survey->id,
'courseid' => $course->id,
'other' => array('viewed' => $viewed)
);
$event = \mod_survey\event\course_module_viewed::create($params);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('survey', $survey);
$event->trigger();
// Completion.
$completion = new completion_info($course);
$completion->set_module_viewed($cm);
}
/**
* Helper function for ordering a set of questions by the given ids.
*
* @param array $questions array of questions objects
* @param array $questionorder array of questions ids indicating the correct order
* @return array list of questions ordered
* @since Moodle 3.0
*/
function survey_order_questions($questions, $questionorder) {
$finalquestions = array();
foreach ($questionorder as $qid) {
$finalquestions[] = $questions[$qid];
}
return $finalquestions;
}
/**
* Translate the question texts and options.
*
* @param stdClass $question question object
* @return stdClass question object with all the text fields translated
* @since Moodle 3.0
*/
function survey_translate_question($question) {
if ($question->text) {
$question->text = get_string($question->text, "survey");
}
if ($question->shorttext) {
$question->shorttext = get_string($question->shorttext, "survey");
}
if ($question->intro) {
$question->intro = get_string($question->intro, "survey");
}
if ($question->options) {
$question->options = get_string($question->options, "survey");
}
return $question;
}
/**
* Returns the questions for a survey (ordered).
*
* @param stdClass $survey survey object
* @return array list of questions ordered
* @since Moodle 3.0
* @throws moodle_exception
*/
function survey_get_questions($survey) {
global $DB;
$questionids = explode(',', $survey->questions);
if (! $questions = $DB->get_records_list("survey_questions", "id", $questionids)) {
throw new moodle_exception('cannotfindquestion', 'survey');
}
return survey_order_questions($questions, $questionids);
}
/**
* Returns subquestions for a given question (ordered).
*
* @param stdClass $question questin object
* @return array list of subquestions ordered
* @since Moodle 3.0
*/
function survey_get_subquestions($question) {
global $DB;
$questionids = explode(',', $question->multi);
$questions = $DB->get_records_list("survey_questions", "id", $questionids);
return survey_order_questions($questions, $questionids);
}
/**
* Save the answer for the given survey
*
* @param stdClass $survey a survey object
* @param array $answersrawdata the answers to be saved
* @param stdClass $course a course object (required for trigger the submitted event)
* @param stdClass $context a context object (required for trigger the submitted event)
* @since Moodle 3.0
*/
function survey_save_answers($survey, $answersrawdata, $course, $context) {
global $DB, $USER;
$answers = array();
// Sort through the data and arrange it.
// This is necessary because some of the questions may have two answers, eg Question 1 -> 1 and P1.
foreach ($answersrawdata as $key => $val) {
if ($key != "userid" && $key != "id") {
if (substr($key, 0, 1) == "q") {
$key = clean_param(substr($key, 1), PARAM_ALPHANUM); // Keep everything but the 'q', number or P number.
}
if (substr($key, 0, 1) == "P") {
$realkey = (int) substr($key, 1);
$answers[$realkey][1] = $val;
} else {
$answers[$key][0] = $val;
}
}
}
// Now store the data.
$timenow = time();
$answerstoinsert = array();
foreach ($answers as $key => $val) {
if ($key != 'sesskey') {
$newdata = new stdClass();
$newdata->time = $timenow;
$newdata->userid = $USER->id;
$newdata->survey = $survey->id;
$newdata->question = $key;