forked from PoetOS/moodle-mod_questionnaire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
1278 lines (1127 loc) · 50.1 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/>.
/**
* Library of functions and constants for module questionnaire.
* @package mod_questionnaire
* @copyright 2016 Mike Churchward (mike.churchward@poetopensource.org)
* @author Mike Churchward
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/** This may no longer be needed. */
define('QUESTIONNAIRE_RESETFORM_RESET', 'questionnaire_reset_data_');
/** This may no longer be needed. */
define('QUESTIONNAIRE_RESETFORM_DROP', 'questionnaire_drop_questionnaire_');
/**
* Library supports implementation.
* @param string $feature
* @return bool|null
*/
function questionnaire_supports($feature) {
switch($feature) {
case FEATURE_BACKUP_MOODLE2:
return true;
case FEATURE_COMPLETION_TRACKS_VIEWS:
return false;
case FEATURE_COMPLETION_HAS_RULES:
return true;
case FEATURE_GRADE_HAS_GRADE:
return false;
case FEATURE_GRADE_OUTCOMES:
return false;
case FEATURE_GROUPINGS:
return true;
case FEATURE_GROUPS:
return true;
case FEATURE_MOD_INTRO:
return true;
case FEATURE_SHOW_DESCRIPTION:
return true;
default:
return null;
}
}
/**
* Return any extra capabilities.
* @return array all other caps used in module
*/
function questionnaire_get_extra_capabilities() {
return array('moodle/site:accessallgroups');
}
/**
* Implementation of get_instance.
* @param int $questionnaireid
* @return false|mixed|stdClass
*/
function questionnaire_get_instance($questionnaireid) {
global $DB;
return $DB->get_record('questionnaire', array('id' => $questionnaireid));
}
/**
* Implementation of add_instance.
* @param stdClass $questionnaire
* @return bool|int
*/
function questionnaire_add_instance($questionnaire) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will create a new instance and return the id number
// of the new instance.
global $DB, $CFG;
require_once($CFG->dirroot.'/mod/questionnaire/questionnaire.class.php');
require_once($CFG->dirroot.'/mod/questionnaire/locallib.php');
$copyfiles = false;
// Check the realm and set it to the survey if it's set.
if (empty($questionnaire->sid)) {
// Create a new survey.
$course = get_course($questionnaire->course);
$cm = new stdClass();
$qobject = new questionnaire($course, $cm, 0, $questionnaire);
if ($questionnaire->create == 'new-0') {
$sdata = new stdClass();
$sdata->name = $questionnaire->name;
$sdata->realm = 'private';
$sdata->title = $questionnaire->name;
$sdata->subtitle = '';
$sdata->info = '';
$sdata->theme = ''; // Theme is deprecated.
$sdata->thanks_page = '';
$sdata->thank_head = '';
$sdata->thank_body = '';
$sdata->email = '';
$sdata->feedbacknotes = '';
$sdata->courseid = $course->id;
if (!($sid = $qobject->survey_update($sdata))) {
throw new \moodle_exception('couldnotcreatenewsurvey', 'mod_questionnaire');
}
} else {
$copyid = explode('-', $questionnaire->create);
$copyrealm = $copyid[0];
$copyid = $copyid[1];
if (empty($qobject->survey)) {
$qobject->add_survey($copyid);
$qobject->add_questions($copyid);
}
// New questionnaires created as "use public" should not create a new survey instance.
if ($copyrealm == 'public') {
$sid = $copyid;
} else {
$sid = $qobject->sid = $qobject->survey_copy($course->id);
// All new questionnaires should be created as "private".
// Even if they are *copies* of public or template questionnaires.
$DB->set_field('questionnaire_survey', 'realm', 'private', array('id' => $sid));
// Need to copy any files from the old questionnaire instance to the new one.
$questionnaire->copyid = $copyid;
}
// If the survey has dependency data, need to set the questionnaire to allow dependencies.
if ($DB->count_records('questionnaire_dependency', ['surveyid' => $sid]) > 0) {
$questionnaire->navigate = 1;
}
}
$questionnaire->sid = $sid;
}
$questionnaire->timemodified = time();
if ($questionnaire->resume == '1') {
$questionnaire->resume = 1;
} else {
$questionnaire->resume = 0;
}
if (!$questionnaire->id = $DB->insert_record("questionnaire", $questionnaire)) {
return false;
}
questionnaire_set_events($questionnaire);
$completiontimeexpected = !empty($questionnaire->completionexpected) ? $questionnaire->completionexpected : null;
\core_completion\api::update_completion_date_event($questionnaire->coursemodule, 'questionnaire',
$questionnaire->id, $completiontimeexpected);
return $questionnaire->id;
}
/**
* Given an object containing all the necessary data, (defined by the form in mod.html) this function will update an existing
* instance with new data.
* @param stdClass $questionnaire
* @return bool
*/
function questionnaire_update_instance($questionnaire) {
global $DB, $CFG;
require_once($CFG->dirroot.'/mod/questionnaire/locallib.php');
// Check the realm and set it to the survey if its set.
if (!empty($questionnaire->sid) && !empty($questionnaire->realm)) {
$DB->set_field('questionnaire_survey', 'realm', $questionnaire->realm, array('id' => $questionnaire->sid));
}
$questionnaire->timemodified = time();
$questionnaire->id = $questionnaire->instance;
if ($questionnaire->resume == '1') {
$questionnaire->resume = 1;
} else {
$questionnaire->resume = 0;
}
// Get existing grade item.
questionnaire_grade_item_update($questionnaire);
questionnaire_set_events($questionnaire);
$completiontimeexpected = !empty($questionnaire->completionexpected) ? $questionnaire->completionexpected : null;
\core_completion\api::update_completion_date_event($questionnaire->coursemodule, 'questionnaire',
$questionnaire->id, $completiontimeexpected);
return $DB->update_record("questionnaire", $questionnaire);
}
/**
* Given an ID of an instance of this module, this function will permanently delete the instance and any data that depends on it.
* @param int $id
* @return bool
*/
function questionnaire_delete_instance($id) {
global $DB, $CFG;
require_once($CFG->dirroot.'/mod/questionnaire/locallib.php');
if (! $questionnaire = $DB->get_record('questionnaire', array('id' => $id))) {
return false;
}
$result = true;
if ($events = $DB->get_records('event', array("modulename" => 'questionnaire', "instance" => $questionnaire->id))) {
foreach ($events as $event) {
$event = calendar_event::load($event);
$event->delete();
}
}
if (! $DB->delete_records('questionnaire', array('id' => $questionnaire->id))) {
$result = false;
}
if ($survey = $DB->get_record('questionnaire_survey', array('id' => $questionnaire->sid))) {
// If this survey is owned by this course, delete all of the survey records and responses.
if ($survey->courseid == $questionnaire->course) {
$result = $result && questionnaire_delete_survey($questionnaire->sid, $questionnaire->id);
}
}
return $result;
}
/**
* Return a small object with summary information about what a user has done with a given particular instance of this module.
* Used for user activity reports.
* $return->time = the time they did it
* $return->info = a short text description.
* $course and $mod are unused, but API requires them. Suppress PHPMD warning.
* @param stdClass $course
* @param stdClass $user
* @param stdClass $mod
* @param stdClass $questionnaire
* @return stdClass
*/
function questionnaire_user_outline($course, $user, $mod, $questionnaire) {
global $CFG;
require_once($CFG->dirroot . '/mod/questionnaire/locallib.php');
$result = new stdClass();
if ($responses = questionnaire_get_user_responses($questionnaire->id, $user->id, true)) {
$n = count($responses);
if ($n == 1) {
$result->info = $n.' '.get_string("response", "questionnaire");
} else {
$result->info = $n.' '.get_string("responses", "questionnaire");
}
$lastresponse = array_pop($responses);
$result->time = $lastresponse->submitted;
} else {
$result->info = get_string("noresponses", "questionnaire");
}
return $result;
}
/**
* Print a detailed representation of what a user has done with a given particular instance of this module, for user
* activity reports.
* $course and $mod are unused, but API requires them. Suppress PHPMD warning.
* @param stdClass $course
* @param stdClass $user
* @param stdClass $mod
* @param stdClass $questionnaire
* @return bool
*/
function questionnaire_user_complete($course, $user, $mod, $questionnaire) {
global $CFG;
require_once($CFG->dirroot . '/mod/questionnaire/locallib.php');
if ($responses = questionnaire_get_user_responses($questionnaire->id, $user->id, false)) {
foreach ($responses as $response) {
if ($response->complete == 'y') {
echo get_string('submitted', 'questionnaire').' '.userdate($response->submitted).'<br />';
} else {
echo get_string('attemptstillinprogress', 'questionnaire').' '.userdate($response->submitted).'<br />';
}
}
} else {
print_string('noresponses', 'questionnaire');
}
return true;
}
/**
* Given a course and a time, this module should find recent activity that has occurred in questionnaire activities and print it
* out.
* Return true if there was output, or false is there was none.
* $course, $isteacher and $timestart are unused, but API requires them. Suppress PHPMD warning.
* @param stdClass $course
* @param bool $isteacher
* @param int $timestart
* @return false
*/
function questionnaire_print_recent_activity($course, $isteacher, $timestart) {
return false; // True if anything was printed, otherwise false.
}
/**
* Must return an array of grades for a given instance of this module, indexed by user. It also returns a maximum allowed grade.
* $questionnaireid is unused, but API requires it. Suppress PHPMD warning.
* @param int $questionnaireid
* @return null
*/
function questionnaire_grades($questionnaireid) {
return null;
}
/**
* Return grade for given user or all users.
*
* @param stdClass $questionnaire
* @param int $userid optional user id, 0 means all users
* @return array array of grades, false if none
*/
function questionnaire_get_user_grades($questionnaire, $userid=0) {
global $DB;
$params = array();
$usersql = '';
if (!empty($userid)) {
$usersql = "AND u.id = ?";
$params[] = $userid;
}
$sql = "SELECT r.id, u.id AS userid, r.grade AS rawgrade, r.submitted AS dategraded, r.submitted AS datesubmitted
FROM {user} u, {questionnaire_response} r
WHERE u.id = r.userid AND r.questionnaireid = $questionnaire->id AND r.complete = 'y' $usersql";
return $DB->get_records_sql($sql, $params);
}
/**
* Update grades by firing grade_updated event.
* $nullifnone is unused, but API requires it. Suppress PHPMD warning.
* @param stdClass $questionnaire
* @param int $userid
* @param bool $nullifnone
*/
function questionnaire_update_grades($questionnaire=null, $userid=0, $nullifnone=true) {
global $CFG, $DB;
if (!function_exists('grade_update')) { // Workaround for buggy PHP versions.
require_once($CFG->libdir.'/gradelib.php');
}
if ($questionnaire != null) {
if ($graderecs = questionnaire_get_user_grades($questionnaire, $userid)) {
$grades = array();
foreach ($graderecs as $v) {
if (!isset($grades[$v->userid])) {
$grades[$v->userid] = new stdClass();
if ($v->rawgrade == -1) {
$grades[$v->userid]->rawgrade = null;
} else {
$grades[$v->userid]->rawgrade = $v->rawgrade;
}
$grades[$v->userid]->userid = $v->userid;
} else if (isset($grades[$v->userid]) && ($v->rawgrade > $grades[$v->userid]->rawgrade)) {
$grades[$v->userid]->rawgrade = $v->rawgrade;
}
}
questionnaire_grade_item_update($questionnaire, $grades);
} else {
questionnaire_grade_item_update($questionnaire);
}
} else {
$sql = "SELECT q.*, cm.idnumber as cmidnumber, q.course as courseid
FROM {questionnaire} q, {course_modules} cm, {modules} m
WHERE m.name='questionnaire' AND m.id=cm.module AND cm.instance=q.id";
if ($rs = $DB->get_recordset_sql($sql)) {
foreach ($rs as $questionnaire) {
if ($questionnaire->grade != 0) {
questionnaire_update_grades($questionnaire);
} else {
questionnaire_grade_item_update($questionnaire);
}
}
$rs->close();
}
}
}
/**
* Create grade item for given questionnaire
*
* @param stdClass $questionnaire object with extra cmidnumber
* @param mixed $grades optional array/object of grade(s); 'reset' means reset grades in gradebook
* @return int 0 if ok, error code otherwise
*/
function questionnaire_grade_item_update($questionnaire, $grades = null) {
global $CFG;
if (!function_exists('grade_update')) { // Workaround for buggy PHP versions.
require_once($CFG->libdir.'/gradelib.php');
}
if (!isset($questionnaire->courseid)) {
$questionnaire->courseid = $questionnaire->course;
}
if ($questionnaire->cmidnumber != '') {
$params = array('itemname' => $questionnaire->name, 'idnumber' => $questionnaire->cmidnumber);
} else {
$params = array('itemname' => $questionnaire->name);
}
if ($questionnaire->grade > 0) {
$params['gradetype'] = GRADE_TYPE_VALUE;
$params['grademax'] = $questionnaire->grade;
$params['grademin'] = 0;
} else if ($questionnaire->grade < 0) {
$params['gradetype'] = GRADE_TYPE_SCALE;
$params['scaleid'] = -$questionnaire->grade;
} else if ($questionnaire->grade == 0) { // No Grade..be sure to delete the grade item if it exists.
$grades = null;
$params = array('deleted' => 1);
} else {
$params = null; // Allow text comments only.
}
if ($grades === 'reset') {
$params['reset'] = true;
$grades = null;
}
return grade_update('mod/questionnaire', $questionnaire->courseid, 'mod', 'questionnaire',
$questionnaire->id, 0, $grades, $params);
}
/**
* This function returns if a scale is being used by one questionnaire
* it it has support for grading and scales. Commented code should be
* modified if necessary. See forum, glossary or journal modules
* as reference.
* @param int $questionnaireid
* @param int $scaleid
* @return boolean True if the scale is used by any questionnaire
*
* Function parameters are unused, but API requires them. Suppress PHPMD warning.
*/
function questionnaire_scale_used ($questionnaireid, $scaleid) {
return false;
}
/**
* Checks if scale is being used by any instance of questionnaire
*
* This is used to find out if scale used anywhere
* @param int $scaleid
* @return boolean True if the scale is used by any questionnaire
*
* Function parameters are unused, but API requires them. Suppress PHPMD warning.
*/
function questionnaire_scale_used_anywhere($scaleid) {
return false;
}
/**
* Serves the questionnaire attachments. Implements needed access control ;-)
*
* @param stdClass $course
* @param stdClass $cm
* @param stdClass $context
* @param string $filearea
* @param array $args
* @param bool $forcedownload
* @return bool false if file not found, does not return if found - justsend the file
*
* $forcedownload is unused, but API requires it. Suppress PHPMD warning.
*/
function questionnaire_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
global $DB;
if ($context->contextlevel != CONTEXT_MODULE) {
return false;
}
require_course_login($course, true, $cm);
$fileareas = ['intro', 'info', 'thankbody', 'question', 'feedbacknotes', 'sectionheading', 'feedback'];
if (!in_array($filearea, $fileareas)) {
return false;
}
$componentid = (int)array_shift($args);
if ($filearea == 'question') {
if (!$DB->record_exists('questionnaire_question', ['id' => $componentid])) {
return false;
}
} else if ($filearea == 'sectionheading') {
if (!$DB->record_exists('questionnaire_fb_sections', ['id' => $componentid])) {
return false;
}
} else if ($filearea == 'feedback') {
if (!$DB->record_exists('questionnaire_feedback', ['id' => $componentid])) {
return false;
}
} else {
if (!$DB->record_exists('questionnaire_survey', ['id' => $componentid])) {
return false;
}
}
if (!$DB->record_exists('questionnaire', ['id' => $cm->instance])) {
return false;
}
$fs = get_file_storage();
$relativepath = implode('/', $args);
$fullpath = "/$context->id/mod_questionnaire/$filearea/$componentid/$relativepath";
if (!($file = $fs->get_file_by_hash(sha1($fullpath))) || $file->is_directory()) {
return false;
}
// Finally send the file.
send_stored_file($file, 0, 0, true); // Download MUST be forced - security!
}
/**
* Adds module specific settings to the settings block
*
* @param settings_navigation $settings The settings navigation object
* @param navigation_node $questionnairenode The node to add module settings to
*
* $settings is unused, but API requires it. Suppress PHPMD warning.
*/
function questionnaire_extend_settings_navigation(settings_navigation $settings,
navigation_node $questionnairenode) {
global $PAGE, $DB, $USER, $CFG;
$individualresponse = optional_param('individualresponse', false, PARAM_INT);
$rid = optional_param('rid', false, PARAM_INT); // Response id.
$currentgroupid = optional_param('group', 0, PARAM_INT); // Group id.
require_once($CFG->dirroot.'/mod/questionnaire/questionnaire.class.php');
$context = $PAGE->cm->context;
$cmid = $PAGE->cm->id;
$cm = $PAGE->cm;
$course = $PAGE->course;
if (! $questionnaire = $DB->get_record("questionnaire", array("id" => $cm->instance))) {
throw new \moodle_exception('invalidcoursemodule', 'mod_questionnaire');
}
$courseid = $course->id;
$questionnaire = new questionnaire($course, $cm, 0, $questionnaire);
if ($owner = $DB->get_field('questionnaire_survey', 'courseid', ['id' => $questionnaire->sid])) {
$owner = (trim($owner) == trim($courseid));
} else {
$owner = true;
}
// On view page, currentgroupid is not yet sent as an optional_param, so get it.
$groupmode = groups_get_activity_groupmode($cm, $course);
if ($groupmode > 0 && $currentgroupid == 0) {
$currentgroupid = groups_get_activity_group($questionnaire->cm);
if (!groups_is_member($currentgroupid, $USER->id)) {
$currentgroupid = 0;
}
}
// We want to add these new nodes after the Edit settings node, and before the
// Locally assigned roles node. Of course, both of those are controlled by capabilities.
$keys = $questionnairenode->get_children_key_list();
$beforekey = null;
$i = array_search('modedit', $keys);
if (($i === false) && array_key_exists(0, $keys)) {
$beforekey = $keys[0];
} else if (array_key_exists($i + 1, $keys)) {
$beforekey = $keys[$i + 1];
}
if (has_capability('mod/questionnaire:manage', $context) && $owner) {
$url = '/mod/questionnaire/qsettings.php';
$node = navigation_node::create(get_string('advancedsettings'),
new moodle_url($url, array('id' => $cmid)),
navigation_node::TYPE_SETTING, null, 'advancedsettings',
new pix_icon('t/edit', ''));
$questionnairenode->add_node($node, $beforekey);
}
if (has_capability('mod/questionnaire:editquestions', $context) && $owner) {
$url = '/mod/questionnaire/questions.php';
$node = navigation_node::create(get_string('questions', 'questionnaire'),
new moodle_url($url, array('id' => $cmid)),
navigation_node::TYPE_SETTING, null, 'questions',
new pix_icon('t/edit', ''));
$questionnairenode->add_node($node, $beforekey);
}
if (has_capability('mod/questionnaire:editquestions', $context) && $owner) {
$url = '/mod/questionnaire/feedback.php';
$node = navigation_node::create(get_string('feedback', 'questionnaire'),
new moodle_url($url, array('id' => $cmid)),
navigation_node::TYPE_SETTING, null, 'feedback',
new pix_icon('t/edit', ''));
$questionnairenode->add_node($node, $beforekey);
}
if (has_capability('mod/questionnaire:preview', $context)) {
$url = '/mod/questionnaire/preview.php';
$node = navigation_node::create(get_string('preview_label', 'questionnaire'),
new moodle_url($url, array('id' => $cmid)),
navigation_node::TYPE_SETTING, null, 'preview',
new pix_icon('t/preview', ''));
$questionnairenode->add_node($node, $beforekey);
}
if ($questionnaire->user_can_take($USER->id)) {
$url = '/mod/questionnaire/complete.php';
if ($questionnaire->user_has_saved_response($USER->id)) {
$args = ['id' => $cmid, 'resume' => 1];
$text = get_string('resumesurvey', 'questionnaire');
} else {
$args = ['id' => $cmid];
$text = get_string('answerquestions', 'questionnaire');
}
$node = navigation_node::create($text, new moodle_url($url, $args),
navigation_node::TYPE_SETTING, null, '', new pix_icon('i/info', 'answerquestions'));
$questionnairenode->add_node($node, $beforekey);
}
$usernumresp = $questionnaire->count_submissions($USER->id);
if ($questionnaire->capabilities->readownresponses && ($usernumresp > 0)) {
$url = '/mod/questionnaire/myreport.php';
if ($usernumresp > 1) {
$urlargs = array('instance' => $questionnaire->id, 'userid' => $USER->id,
'byresponse' => 0, 'action' => 'summary', 'group' => $currentgroupid);
$node = navigation_node::create(get_string('yourresponses', 'questionnaire'),
new moodle_url($url, $urlargs), navigation_node::TYPE_SETTING, null, 'yourresponses');
$myreportnode = $questionnairenode->add_node($node, $beforekey);
$urlargs = array('instance' => $questionnaire->id, 'userid' => $USER->id,
'byresponse' => 0, 'action' => 'summary', 'group' => $currentgroupid);
$myreportnode->add(get_string('summary', 'questionnaire'), new moodle_url($url, $urlargs));
$urlargs = array('instance' => $questionnaire->id, 'userid' => $USER->id,
'byresponse' => 1, 'action' => 'vresp', 'group' => $currentgroupid);
$byresponsenode = $myreportnode->add(get_string('viewindividualresponse', 'questionnaire'),
new moodle_url($url, $urlargs));
$urlargs = array('instance' => $questionnaire->id, 'userid' => $USER->id,
'byresponse' => 0, 'action' => 'vall', 'group' => $currentgroupid);
$myreportnode->add(get_string('myresponses', 'questionnaire'), new moodle_url($url, $urlargs));
if ($questionnaire->capabilities->downloadresponses) {
$urlargs = array('instance' => $questionnaire->id, 'user' => $USER->id,
'action' => 'dwnpg', 'group' => $currentgroupid);
$myreportnode->add(get_string('downloadtextformat', 'questionnaire'),
new moodle_url('/mod/questionnaire/report.php', $urlargs));
}
} else {
$urlargs = array('instance' => $questionnaire->id, 'userid' => $USER->id,
'byresponse' => 1, 'action' => 'vresp', 'group' => $currentgroupid);
$node = navigation_node::create(get_string('yourresponse', 'questionnaire'),
new moodle_url($url, $urlargs), navigation_node::TYPE_SETTING, null, 'yourresponse');
$myreportnode = $questionnairenode->add_node($node, $beforekey);
}
}
// If questionnaire is set to separate groups, prevent user who is not member of any group
// and is not a non-editing teacher to view All responses.
if ($questionnaire->can_view_all_responses($usernumresp)) {
$url = '/mod/questionnaire/report.php';
$node = navigation_node::create(get_string('viewallresponses', 'questionnaire'),
new moodle_url($url, array('instance' => $questionnaire->id, 'action' => 'vall')),
navigation_node::TYPE_SETTING, null, 'vall');
$reportnode = $questionnairenode->add_node($node, $beforekey);
if ($questionnaire->capabilities->viewsingleresponse) {
$summarynode = $reportnode->add(get_string('summary', 'questionnaire'),
new moodle_url('/mod/questionnaire/report.php',
array('instance' => $questionnaire->id, 'action' => 'vall')));
} else {
$summarynode = $reportnode;
}
$summarynode->add(get_string('order_default', 'questionnaire'),
new moodle_url('/mod/questionnaire/report.php',
array('instance' => $questionnaire->id, 'action' => 'vall', 'group' => $currentgroupid)));
$summarynode->add(get_string('order_ascending', 'questionnaire'),
new moodle_url('/mod/questionnaire/report.php',
array('instance' => $questionnaire->id, 'action' => 'vallasort', 'group' => $currentgroupid)));
$summarynode->add(get_string('order_descending', 'questionnaire'),
new moodle_url('/mod/questionnaire/report.php',
array('instance' => $questionnaire->id, 'action' => 'vallarsort', 'group' => $currentgroupid)));
if ($questionnaire->capabilities->deleteresponses) {
$summarynode->add(get_string('deleteallresponses', 'questionnaire'),
new moodle_url('/mod/questionnaire/report.php',
array('instance' => $questionnaire->id, 'action' => 'delallresp', 'group' => $currentgroupid)));
}
if ($questionnaire->capabilities->downloadresponses) {
$summarynode->add(get_string('downloadtextformat', 'questionnaire'),
new moodle_url('/mod/questionnaire/report.php',
array('instance' => $questionnaire->id, 'action' => 'dwnpg', 'group' => $currentgroupid)));
}
if ($questionnaire->capabilities->viewsingleresponse) {
$byresponsenode = $reportnode->add(get_string('viewbyresponse', 'questionnaire'),
new moodle_url('/mod/questionnaire/report.php',
array('instance' => $questionnaire->id, 'action' => 'vresp', 'byresponse' => 1, 'group' => $currentgroupid)));
$byresponsenode->add(get_string('view', 'questionnaire'),
new moodle_url('/mod/questionnaire/report.php',
array('instance' => $questionnaire->id, 'action' => 'vresp', 'byresponse' => 1, 'group' => $currentgroupid)));
if ($individualresponse) {
$byresponsenode->add(get_string('deleteresp', 'questionnaire'),
new moodle_url('/mod/questionnaire/report.php',
array('instance' => $questionnaire->id, 'action' => 'dresp', 'byresponse' => 1,
'rid' => $rid, 'group' => $currentgroupid, 'individualresponse' => 1)));
}
}
}
$canviewgroups = true;
$groupmode = groups_get_activity_groupmode($cm, $course);
if ($groupmode == 1) {
$canviewgroups = groups_has_membership($cm, $USER->id);
}
$canviewallgroups = has_capability('moodle/site:accessallgroups', $context);
if ($questionnaire->capabilities->viewsingleresponse && ($canviewallgroups || $canviewgroups)) {
$url = '/mod/questionnaire/show_nonrespondents.php';
$node = navigation_node::create(get_string('show_nonrespondents', 'questionnaire'),
new moodle_url($url, array('id' => $cmid)),
navigation_node::TYPE_SETTING, null, 'nonrespondents');
$questionnairenode->add_node($node, $beforekey);
}
}
// Any other questionnaire functions go here. Each of them must have a name that
// starts with questionnaire_.
/**
* Return the view actions.
* @return string[]
*/
function questionnaire_get_view_actions() {
return array('view', 'view all');
}
/**
* Return the post actions.
* @return string[]
*/
function questionnaire_get_post_actions() {
return array('submit', 'update');
}
/**
* Return the recent activity.
* @param array $activities
* @param int $index
* @param int $timestart
* @param int $courseid
* @param int $cmid
* @param int $userid
* @param int $groupid
* @return mixed|void
*/
function questionnaire_get_recent_mod_activity(&$activities, &$index, $timestart,
$courseid, $cmid, $userid = 0, $groupid = 0) {
global $CFG, $COURSE, $USER, $DB;
require_once($CFG->dirroot . '/mod/questionnaire/locallib.php');
require_once($CFG->dirroot.'/mod/questionnaire/questionnaire.class.php');
if ($COURSE->id == $courseid) {
$course = $COURSE;
} else {
$course = $DB->get_record('course', ['id' => $courseid]);
}
$modinfo = get_fast_modinfo($course);
$cm = $modinfo->cms[$cmid];
$questionnaire = $DB->get_record('questionnaire', ['id' => $cm->instance]);
$questionnaire = new questionnaire($course, $cm, 0, $questionnaire);
$context = context_module::instance($cm->id);
$grader = has_capability('mod/questionnaire:viewsingleresponse', $context);
// If this is a copy of a public questionnaire whose original is located in another course,
// current user (teacher) cannot view responses.
if ($grader) {
// For a public questionnaire, look for the original public questionnaire that it is based on.
if (!$questionnaire->survey_is_public_master()) {
// For a public questionnaire, look for the original public questionnaire that it is based on.
$originalquestionnaire = $DB->get_record('questionnaire',
['sid' => $questionnaire->survey->id, 'course' => $questionnaire->survey->courseid]);
$cmoriginal = get_coursemodule_from_instance("questionnaire", $originalquestionnaire->id,
$questionnaire->survey->courseid);
$contextoriginal = context_course::instance($questionnaire->survey->courseid, MUST_EXIST);
if (!has_capability('mod/questionnaire:viewsingleresponse', $contextoriginal)) {
$tmpactivity = new stdClass();
$tmpactivity->type = 'questionnaire';
$tmpactivity->cmid = $cm->id;
$tmpactivity->cannotview = true;
$tmpactivity->anonymous = false;
$activities[$index++] = $tmpactivity;
return $activities;
}
}
}
if ($userid) {
$userselect = "AND u.id = :userid";
$params['userid'] = $userid;
} else {
$userselect = '';
}
if ($groupid) {
$groupselect = 'AND gm.groupid = :groupid';
$groupjoin = 'JOIN {groups_members} gm ON gm.userid=u.id';
$params['groupid'] = $groupid;
} else {
$groupselect = '';
$groupjoin = '';
}
$params['timestart'] = $timestart;
$params['questionnaireid'] = $questionnaire->id;
$ufields = user_picture::fields('u', null, 'useridagain');
if (!$attempts = $DB->get_records_sql("
SELECT qr.*,
{$ufields}
FROM {questionnaire_response} qr
JOIN {user} u ON u.id = qr.userid
$groupjoin
WHERE qr.submitted > :timestart
AND qr.questionnaireid = :questionnaireid
$userselect
$groupselect
ORDER BY qr.submitted ASC", $params)) {
return;
}
$accessallgroups = has_capability('moodle/site:accessallgroups', $context);
$viewfullnames = has_capability('moodle/site:viewfullnames', $context);
$groupmode = groups_get_activity_groupmode($cm, $course);
$usersgroups = null;
$aname = format_string($cm->name, true);
$userattempts = array();
foreach ($attempts as $attempt) {
if ($questionnaire->respondenttype != 'anonymous') {
if (!isset($userattempts[$attempt->lastname])) {
$userattempts[$attempt->lastname] = 1;
} else {
$userattempts[$attempt->lastname]++;
}
}
if ($attempt->userid != $USER->id) {
if (!$grader) {
// View complete individual responses permission required.
continue;
}
if (($groupmode == SEPARATEGROUPS) && !$accessallgroups) {
if ($usersgroups === null) {
$usersgroups = groups_get_all_groups($course->id,
$attempt->userid, $cm->groupingid);
if (is_array($usersgroups)) {
$usersgroups = array_keys($usersgroups);
} else {
$usersgroups = array();
}
}
if (!array_intersect($usersgroups, $modinfo->groups[$cm->id])) {
continue;
}
}
}
$tmpactivity = new stdClass();
$tmpactivity->type = 'questionnaire';
$tmpactivity->cmid = $cm->id;
$tmpactivity->cminstance = $cm->instance;
// Current user is admin - or teacher enrolled in original public course.
if (isset($cmoriginal)) {
$tmpactivity->cminstance = $cmoriginal->instance;
}
$tmpactivity->cannotview = false;
$tmpactivity->anonymous = false;
$tmpactivity->name = $aname;
$tmpactivity->sectionnum = $cm->sectionnum;
$tmpactivity->timestamp = $attempt->submitted;
$tmpactivity->groupid = $groupid;
if (isset($userattempts[$attempt->lastname])) {
$tmpactivity->nbattempts = $userattempts[$attempt->lastname];
}
$tmpactivity->content = new stdClass();
$tmpactivity->content->attemptid = $attempt->id;
$userfields = explode(',', user_picture::fields());
$tmpactivity->user = new stdClass();
foreach ($userfields as $userfield) {
if ($userfield == 'id') {
$tmpactivity->user->{$userfield} = $attempt->userid;
} else {
if (!empty($attempt->{$userfield})) {
$tmpactivity->user->{$userfield} = $attempt->{$userfield};
} else {
$tmpactivity->user->{$userfield} = null;
}
}
}
if ($questionnaire->respondenttype != 'anonymous') {
$tmpactivity->user->fullname = fullname($attempt, $viewfullnames);
} else {
$tmpactivity->user = '';
unset ($tmpactivity->user);
$tmpactivity->anonymous = true;
}
$activities[$index++] = $tmpactivity;
}
}
/**
* Prints all users who have completed a specified questionnaire since a given time
*
* @param stdClass $activity
* @param int $courseid
* @param string $detail not used but needed for compability
* @param array $modnames
* @return void Output is echo'd
*
* $details and $modenames are unused, but API requires them. Suppress PHPMD warning.
*/
function questionnaire_print_recent_mod_activity($activity, $courseid, $detail, $modnames) {
global $OUTPUT;
// If the questionnaire is "anonymous", then $activity->user won't have been set, so do not display respondent info.
if ($activity->anonymous) {
$stranonymous = ' ('.get_string('anonymous', 'questionnaire').')';
$activity->nbattempts = '';
} else {
$stranonymous = '';
}
// Current user cannot view responses to public questionnaire.
if ($activity->cannotview) {
$strcannotview = get_string('cannotviewpublicresponses', 'questionnaire');
}
echo html_writer::start_tag('div');
echo html_writer::start_tag('span', array('class' => 'clearfix',
'style' => 'margin-top:0px; background-color: white; display: inline-block;'));
if (!$activity->anonymous && !$activity->cannotview) {
echo html_writer::tag('div', $OUTPUT->user_picture($activity->user, array('courseid' => $courseid)),
array('style' => 'float: left; padding-right: 10px;'));
}
if (!$activity->cannotview) {
echo html_writer::start_tag('div');
echo html_writer::start_tag('div');
$urlparams = array('action' => 'vresp', 'instance' => $activity->cminstance,
'group' => $activity->groupid, 'rid' => $activity->content->attemptid, 'individualresponse' => 1);
$context = context_module::instance($activity->cmid);
if (has_capability('mod/questionnaire:viewsingleresponse', $context)) {
$report = 'report.php';
} else {
$report = 'myreport.php';
}
echo html_writer::tag('a', get_string('response', 'questionnaire').' '.$activity->nbattempts.$stranonymous,
array('href' => new moodle_url('/mod/questionnaire/'.$report, $urlparams)));
echo html_writer::end_tag('div');
} else {
echo html_writer::start_tag('div');
echo html_writer::start_tag('div');
echo html_writer::tag('div', $strcannotview);
echo html_writer::end_tag('div');
}