MDL-78962 core/loadingicon: remove jQuery requirement in the API
[moodle.git] / mod / survey / report.php
blobf4042cc9a918bbf26623f81de540ea03e8d43236
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * This file is responsible for producing the survey reports
21 * @package mod_survey
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once("../../config.php");
27 require_once("lib.php");
29 // Check that all the parameters have been provided.
31 $id = required_param('id', PARAM_INT); // Course Module ID.
32 $action = optional_param('action', '', PARAM_ALPHA); // What to look at.
33 $qid = optional_param('qid', 0, PARAM_RAW); // Question IDs comma-separated list.
34 $student = optional_param('student', 0, PARAM_INT); // Student ID.
35 $notes = optional_param('notes', '', PARAM_RAW); // Save teachers notes.
37 $qids = explode(',', $qid);
38 $qids = clean_param_array($qids, PARAM_INT);
39 $qid = implode(',', $qids);
41 if (!$cm = get_coursemodule_from_id('survey', $id)) {
42 throw new moodle_exception('invalidcoursemodule');
45 if (!$course = $DB->get_record("course", array("id" => $cm->course))) {
46 throw new moodle_exception('coursemisconf');
49 $url = new moodle_url('/mod/survey/report.php', array('id' => $id));
50 if ($action !== '') {
51 $url->param('action', $action);
53 if ($qid !== 0) {
54 $url->param('qid', $qid);
56 if ($student !== 0) {
57 $url->param('student', $student);
59 if ($notes !== '') {
60 $url->param('notes', $notes);
62 $PAGE->set_url($url);
64 require_login($course, false, $cm);
66 $context = context_module::instance($cm->id);
68 require_capability('mod/survey:readresponses', $context);
70 if (!$survey = $DB->get_record("survey", array("id" => $cm->instance))) {
71 throw new moodle_exception('invalidsurveyid', 'survey');
74 if (!$template = $DB->get_record("survey", array("id" => $survey->template))) {
75 throw new moodle_exception('invalidtmptid', 'survey');
78 $showscales = ($template->name != 'ciqname');
80 $strreport = get_string("report", "survey");
81 $strsurvey = get_string("modulename", "survey");
82 $strsurveys = get_string("modulenameplural", "survey");
83 $strsummary = get_string("summary", "survey");
84 $strscales = get_string("scales", "survey");
85 $strquestion = get_string("question", "survey");
86 $strquestions = get_string("questions", "survey");
87 $strdownload = get_string("download", "survey");
88 $strallscales = get_string("allscales", "survey");
89 $strselectedquestions = get_string("selectedquestions", "survey");
90 $strseemoredetail = get_string("seemoredetail", "survey");
91 $strnotes = get_string("notes", "survey");
93 $PAGE->set_title("$course->shortname: " . format_string($survey->name));
94 $PAGE->set_heading($course->fullname);
95 $PAGE->activityheader->set_attrs([
96 'hidecompletion' => true,
97 'description' => ''
98 ]);
100 // Activate the secondary nav tab.
101 navigation_node::override_active_url(new moodle_url('/mod/survey/report.php', ['id' => $id, 'action' => 'summary']));
103 $actionbar = new \mod_survey\output\actionbar($id, $action, $url);
104 echo $OUTPUT->header();
105 $renderer = $PAGE->get_renderer('mod_survey');
106 echo $renderer->response_actionbar($actionbar);
108 // Check to see if groups are being used in this survey.
109 if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used.
110 $menuaction = $action == "student" ? "students" : $action;
111 $currentgroup = groups_get_activity_group($cm, true);
112 $groupsactivitymenu = groups_print_activity_menu($cm, new moodle_url('/mod/survey/report.php',
113 ['id' => $cm->id, 'action' => $menuaction, 'qid' => $qid]), true);
114 } else {
115 $currentgroup = 0;
116 $groupsactivitymenu = null;
119 $params = array(
120 'objectid' => $survey->id,
121 'context' => $context,
122 'courseid' => $course->id,
123 'relateduserid' => $student,
124 'other' => array('action' => $action, 'groupid' => $currentgroup)
126 $event = \mod_survey\event\report_viewed::create($params);
127 $event->trigger();
129 if ($currentgroup) {
130 $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', $currentgroup, null, false);
131 } else if (!empty($cm->groupingid)) {
132 $groups = groups_get_all_groups($courseid, 0, $cm->groupingid);
133 $groups = array_keys($groups);
134 $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', $groups, null, false);
135 } else {
136 $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', '', null, false);
137 $group = false;
140 $groupingid = $cm->groupingid;
142 // Print the menu across the top.
144 $virtualscales = false;
146 switch ($action) {
148 case "summary":
149 // If survey type is Critical incidents then we don't show summary report.
150 if ($survey->template == SURVEY_CIQ) {
151 throw new moodle_exception('cannotviewreport');
153 echo $OUTPUT->heading($strsummary, 3);
155 if ($groupsactivitymenu) {
156 echo html_writer::div($groupsactivitymenu, 'mb-2');
159 if (survey_count_responses($survey->id, $currentgroup, $groupingid)) {
160 echo "<div class='reportsummary'><a href=\"report.php?action=scales&amp;id=$id\">";
161 survey_print_graph("id=$id&amp;group=$currentgroup&amp;type=overall.png");
162 echo "</a></div>";
163 } else {
164 echo $OUTPUT->notification(get_string("nobodyyet", "survey"), 'info', false);
166 break;
168 case "scales":
169 // If survey type is Critical incidents then we don't show scales report.
170 if ($survey->template == SURVEY_CIQ) {
171 throw new moodle_exception('cannotviewreport');
173 echo $OUTPUT->heading($strscales, 3);
175 if ($groupsactivitymenu) {
176 echo html_writer::div($groupsactivitymenu, 'mb-2');
179 if (!$results = survey_get_responses($survey->id, $currentgroup, $groupingid)) {
180 echo $OUTPUT->notification(get_string("nobodyyet", "survey"), 'info', false);
182 } else {
184 $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions));
185 $questionorder = explode(",", $survey->questions);
187 foreach ($questionorder as $key => $val) {
188 $question = $questions[$val];
189 if ($question->type < 0) { // We have some virtual scales. Just show them.
190 $virtualscales = true;
191 break;
195 foreach ($questionorder as $key => $val) {
196 $question = $questions[$val];
197 if ($question->multi) {
198 if (!empty($virtualscales) && $question->type > 0) { // Don't show non-virtual scales if virtual.
199 continue;
201 echo "<p class=\"centerpara\"><a title=\"$strseemoredetail\" href=\"report.php?action=questions&amp;id=$id&amp;qid=$question->multi\">";
202 survey_print_graph("id=$id&amp;qid=$question->id&amp;group=$currentgroup&amp;type=multiquestion.png");
203 echo "</a></p><br />";
208 break;
210 case "questions":
212 if ($qid) { // Just get one multi-question.
213 $questions = $DB->get_records_select("survey_questions", "id in ($qid)");
214 $questionorder = explode(",", $qid);
216 if ($scale = $DB->get_records("survey_questions", array("multi" => $qid))) {
217 $scale = array_pop($scale);
218 echo $OUTPUT->heading("$scale->text - $strselectedquestions", 3);
219 } else {
220 echo $OUTPUT->heading($strselectedquestions, 3);
223 } else { // Get all top-level questions.
224 $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions));
225 $questionorder = explode(",", $survey->questions);
227 echo $OUTPUT->heading($strquestions, 3);
230 if ($groupsactivitymenu) {
231 echo html_writer::div($groupsactivitymenu, 'mb-2');
234 if (!$results = survey_get_responses($survey->id, $currentgroup, $groupingid)) {
235 echo $OUTPUT->notification(get_string("nobodyyet", "survey"), 'info', false);
237 } else {
239 foreach ($questionorder as $key => $val) {
240 $question = $questions[$val];
241 if ($question->type < 0) { // We have some virtual scales. DON'T show them.
242 $virtualscales = true;
243 break;
247 foreach ($questionorder as $key => $val) {
248 $question = $questions[$val];
250 if ($question->type < 0) { // We have some virtual scales. DON'T show them.
251 continue;
253 $question->text = get_string($question->text, "survey");
255 if ($question->multi) {
256 echo $OUTPUT->heading($question->text . ':', 4);
258 $subquestions = survey_get_subquestions($question);
259 foreach ($subquestions as $subquestion) {
260 if ($subquestion->type > 0) {
261 echo "<p class=\"centerpara\">";
262 echo "<a title=\"$strseemoredetail\" href=\"report.php?action=question&amp;id=$id&amp;qid=$subquestion->id\">";
263 survey_print_graph("id=$id&amp;qid=$subquestion->id&amp;group=$currentgroup&amp;type=question.png");
264 echo "</a></p>";
267 } else if ($question->type > 0) {
268 echo "<p class=\"centerpara\">";
269 echo "<a title=\"$strseemoredetail\" href=\"report.php?action=question&amp;id=$id&amp;qid=$question->id\">";
270 survey_print_graph("id=$id&amp;qid=$question->id&amp;group=$currentgroup&amp;type=question.png");
271 echo "</a></p>";
273 } else {
274 $table = new html_table();
275 $table->head = array($question->text);
276 $table->align = array("left");
278 $contents = '<table cellpadding="15" width="100%">';
280 if ($aaa = survey_get_user_answers($survey->id, $question->id, $currentgroup, "sa.time ASC")) {
281 foreach ($aaa as $a) {
282 $contents .= "<tr>";
283 $contents .= '<td class="fullnamecell">' . fullname($a) . '</td>';
284 $contents .= '<td valign="top">' . s($a->answer1) . '</td>';
285 $contents .= "</tr>";
288 $contents .= "</table>";
290 $table->data[] = array($contents);
292 echo html_writer::table($table);
294 echo $OUTPUT->spacer(array('height' => 30)); // Should be done with CSS instead.
299 break;
301 case "question":
302 if (!$question = $DB->get_record("survey_questions", array("id" => $qid))) {
303 throw new \moodle_exception('cannotfindquestion', 'survey');
305 $question->text = get_string($question->text, "survey");
307 $answers = explode(",", get_string($question->options, "survey"));
309 echo $OUTPUT->heading("$strquestion: $question->text", 3);
311 if ($groupsactivitymenu) {
312 echo html_writer::div($groupsactivitymenu, 'mb-2');
315 $strname = get_string("name", "survey");
316 $strtime = get_string("time", "survey");
317 $stractual = get_string("actual", "survey");
318 $strpreferred = get_string("preferred", "survey");
319 $strdateformat = get_string("strftimedatetime");
321 $table = new html_table();
322 $table->head = array("", $strname, $strtime, $stractual, $strpreferred);
323 $table->align = array("left", "left", "left", "left", "right");
324 $table->size = array(35, "", "", "", "");
326 if ($aaa = survey_get_user_answers($survey->id, $question->id, $currentgroup)) {
327 foreach ($aaa as $a) {
328 if ($a->answer1) {
329 $answer1 = "$a->answer1 - " . $answers[$a->answer1 - 1];
330 } else {
331 $answer1 = "&nbsp;";
333 if ($a->answer2) {
334 $answer2 = "$a->answer2 - " . $answers[$a->answer2 - 1];
335 } else {
336 $answer2 = "&nbsp;";
338 $table->data[] = array(
339 $OUTPUT->user_picture($a, array('courseid' => $course->id)),
340 "<a href=\"report.php?id=$id&amp;action=student&amp;student=$a->userid\">" . fullname($a) . "</a>",
341 userdate($a->time),
342 s($answer1), s($answer2));
347 echo html_writer::table($table);
349 break;
351 case "students":
353 echo $OUTPUT->heading(get_string("analysisof", "survey", get_string('participants')), 3);
355 if ($groupsactivitymenu) {
356 echo html_writer::div($groupsactivitymenu, 'mb-2');
359 if (!$results = survey_get_responses($survey->id, $currentgroup, $groupingid)) {
360 echo $OUTPUT->notification(get_string("nobodyyet", "survey"), 'info', false);
361 } else {
362 survey_print_all_responses($cm->id, $results, $course->id);
365 break;
367 case "student":
368 if (!$user = $DB->get_record("user", array("id" => $student))) {
369 throw new moodle_exception('invaliduserid');
372 echo $OUTPUT->heading(get_string("analysisof", "survey", fullname($user)), 3);
374 if ($groupsactivitymenu) {
375 echo html_writer::div($groupsactivitymenu, 'mb-2');
378 if ($notes != '' and confirm_sesskey()) {
379 if (survey_get_analysis($survey->id, $user->id)) {
380 if (!survey_update_analysis($survey->id, $user->id, $notes)) {
381 echo $OUTPUT->notification(get_string("errorunabletosavenotes", "survey"), "notifyproblem");
382 } else {
383 echo $OUTPUT->notification(get_string("savednotes", "survey"), "notifysuccess");
385 } else {
386 if (!survey_add_analysis($survey->id, $user->id, $notes)) {
387 echo $OUTPUT->notification(get_string("errorunabletosavenotes", "survey"), "notifyproblem");
388 } else {
389 echo $OUTPUT->notification(get_string("savednotes", "survey"), "notifysuccess");
394 echo "<p class=\"centerpara\">";
395 echo $OUTPUT->user_picture($user, array('courseid' => $course->id));
396 echo "</p>";
398 $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions));
399 $questionorder = explode(",", $survey->questions);
401 if ($showscales) {
402 // Print overall summary.
403 echo "<p class=\"centerpara\">";
404 survey_print_graph("id=$id&amp;sid=$student&amp;type=student.png");
405 echo "</p>";
407 // Print scales.
409 foreach ($questionorder as $key => $val) {
410 $question = $questions[$val];
411 if ($question->type < 0) { // We have some virtual scales. Just show them.
412 $virtualscales = true;
413 break;
417 foreach ($questionorder as $key => $val) {
418 $question = $questions[$val];
419 if ($question->multi) {
420 if ($virtualscales && $question->type > 0) { // Don't show non-virtual scales if virtual.
421 continue;
423 echo "<p class=\"centerpara\">";
424 echo "<a title=\"$strseemoredetail\" href=\"report.php?action=questions&amp;id=$id&amp;qid=$question->multi\">";
425 survey_print_graph("id=$id&amp;qid=$question->id&amp;sid=$student&amp;type=studentmultiquestion.png");
426 echo "</a></p><br />";
431 // Print non-scale questions.
433 foreach ($questionorder as $key => $val) {
434 $question = $questions[$val];
435 if ($question->type == 0 or $question->type == 1) {
436 if ($answer = survey_get_user_answer($survey->id, $question->id, $user->id)) {
437 $table = new html_table();
438 $table->head = array(get_string($question->text, "survey"));
439 $table->align = array("left");
440 if (!empty($question->options) && $answer->answer1 > 0) {
441 $answers = explode(',', get_string($question->options, 'survey'));
442 if ($answer->answer1 <= count($answers)) {
443 $table->data[] = array(s($answers[$answer->answer1 - 1])); // No html here, just plain text.
444 } else {
445 $table->data[] = array(s($answer->answer1)); // No html here, just plain text.
447 } else {
448 $table->data[] = array(s($answer->answer1)); // No html here, just plain text.
450 echo html_writer::table($table);
451 echo $OUTPUT->spacer(array('height' => 30));
456 if ($rs = survey_get_analysis($survey->id, $user->id)) {
457 $notes = $rs->notes;
458 } else {
459 $notes = "";
461 echo "<hr noshade=\"noshade\" size=\"1\" />";
462 echo "<div class='studentreport'>";
463 echo "<form action=\"report.php\" method=\"post\">";
464 echo "<h3>$strnotes:</h3>";
465 echo "<blockquote>";
466 echo "<textarea class=\"form-control\" name=\"notes\" rows=\"10\" cols=\"60\">";
467 p($notes);
468 echo "</textarea><br />";
469 echo "<input type=\"hidden\" name=\"action\" value=\"student\" />";
470 echo "<input type=\"hidden\" name=\"sesskey\" value=\"" . sesskey() . "\" />";
471 echo "<input type=\"hidden\" name=\"student\" value=\"$student\" />";
472 echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />";
473 echo "<input type=\"submit\" class=\"btn btn-primary\" value=\"" . get_string("savechanges") . "\" />";
474 echo "</blockquote>";
475 echo "</form>";
476 echo "</div>";
478 break;
480 case "download":
481 echo $OUTPUT->heading($strdownload, 3);
483 if ($groupsactivitymenu) {
484 echo html_writer::div($groupsactivitymenu, 'mb-2');
487 require_capability('mod/survey:download', $context);
489 $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
490 if ($numusers > 0) {
491 echo html_writer::tag('p', get_string("downloadinfo", "survey"), array('class' => 'centerpara'));
493 echo $OUTPUT->container_start('reportbuttons');
494 $options = array();
495 $options["id"] = "$cm->id";
496 $options["group"] = $currentgroup;
498 $options["type"] = "ods";
499 echo $OUTPUT->single_button(new moodle_url("download.php", $options), get_string("downloadods"));
501 $options["type"] = "xls";
502 echo $OUTPUT->single_button(new moodle_url("download.php", $options), get_string("downloadexcel"));
504 $options["type"] = "txt";
505 echo $OUTPUT->single_button(new moodle_url("download.php", $options), get_string("downloadtext"));
506 echo $OUTPUT->container_end();
508 } else {
509 echo $OUTPUT->notification(get_string("nobodyyet", "survey"), 'info', false);
512 break;
514 default:
515 throw new moodle_exception('cannotviewreport');
518 echo $OUTPUT->footer();