MDL-44182 mod_survey: unified @package use
[moodle.git] / mod / survey / report.php
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.
14 //
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
20  *
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
24  */
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         print_error('invalidcoursemodule');
43     }
45     if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
46         print_error('coursemisconf');
47     }
49     $url = new moodle_url('/mod/survey/report.php', array('id'=>$id));
50     if ($action !== '') {
51         $url->param('action', $action);
52     }
53     if ($qid !== 0) {
54         $url->param('qid', $qid);
55     }
56     if ($student !== 0) {
57         $url->param('student', $student);
58     }
59     if ($notes !== '') {
60         $url->param('notes', $notes);
61     }
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         print_error('invalidsurveyid', 'survey');
72     }
74     if (! $template = $DB->get_record("survey", array("id"=>$survey->template))) {
75         print_error('invalidtmptid', 'survey');
76     }
78     $showscales = ($template->name != 'ciqname');
81     $strreport = get_string("report", "survey");
82     $strsurvey = get_string("modulename", "survey");
83     $strsurveys = get_string("modulenameplural", "survey");
84     $strsummary = get_string("summary", "survey");
85     $strscales = get_string("scales", "survey");
86     $strquestion = get_string("question", "survey");
87     $strquestions = get_string("questions", "survey");
88     $strdownload = get_string("download", "survey");
89     $strallscales = get_string("allscales", "survey");
90     $strallquestions = get_string("allquestions", "survey");
91     $strselectedquestions = get_string("selectedquestions", "survey");
92     $strseemoredetail = get_string("seemoredetail", "survey");
93     $strnotes = get_string("notes", "survey");
95     add_to_log($course->id, "survey", "view report", "report.php?id=$cm->id", "$survey->id", $cm->id);
97     switch ($action) {
98         case 'download':
99             $PAGE->navbar->add(get_string('downloadresults', 'survey'));
100             break;
101         case 'summary':
102         case 'scales':
103         case 'questions':
104             $PAGE->navbar->add($strreport);
105             $PAGE->navbar->add(${'str'.$action});
106             break;
107         case 'students':
108             $PAGE->navbar->add($strreport);
109             $PAGE->navbar->add(get_string('participants'));
110             break;
111         case '':
112             $PAGE->navbar->add($strreport);
113             $PAGE->navbar->add($strsummary);
114             break;
115         default:
116             $PAGE->navbar->add($strreport);
117             break;
118     }
120     $PAGE->set_title("$course->shortname: ".format_string($survey->name));
121     $PAGE->set_heading($course->fullname);
122     echo $OUTPUT->header();
123     echo $OUTPUT->heading($survey->name);
125 /// Check to see if groups are being used in this survey
126     if ($groupmode = groups_get_activity_groupmode($cm)) {   // Groups are being used
127         $menuaction = $action == "student" ? "students" : $action;
128         $currentgroup = groups_get_activity_group($cm, true);
129         groups_print_activity_menu($cm, $CFG->wwwroot . "/mod/survey/report.php?id=$cm->id&amp;action=$menuaction&amp;qid=$qid");
130     } else {
131         $currentgroup = 0;
132     }
134     if ($currentgroup) {
135         $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', $currentgroup, null, false);
136     } else if (!empty($cm->groupingid)) {
137         $groups = groups_get_all_groups($courseid, 0, $cm->groupingid);
138         $groups = array_keys($groups);
139         $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', $groups, null, false);
140     } else {
141         $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', '', null, false);
142         $group = false;
143     }
145     $groupingid = $cm->groupingid;
147     echo $OUTPUT->box_start("generalbox boxaligncenter");
148     if ($showscales) {
149         echo "<a href=\"report.php?action=summary&amp;id=$id\">$strsummary</a>";
150         echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=scales&amp;id=$id\">$strscales</a>";
151         echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=questions&amp;id=$id\">$strquestions</a>";
152         echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=students&amp;id=$id\">".get_string('participants')."</a>";
153         if (has_capability('mod/survey:download', $context)) {
154             echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=download&amp;id=$id\">$strdownload</a>";
155         }
156         if (empty($action)) {
157             $action = "summary";
158         }
159     } else {
160         echo "<a href=\"report.php?action=questions&amp;id=$id\">$strquestions</a>";
161         echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=students&amp;id=$id\">".get_string('participants')."</a>";
162         if (has_capability('mod/survey:download', $context)) {
163             echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=download&amp;id=$id\">$strdownload</a>";
164         }
165         if (empty($action)) {
166             $action = "questions";
167         }
168     }
169     echo $OUTPUT->box_end();
171     echo $OUTPUT->spacer(array('height'=>30, 'width'=>30, 'br'=>true)); // should be done with CSS instead
174 /// Print the menu across the top
176     $virtualscales = false;
178     switch ($action) {
180       case "summary":
181         echo $OUTPUT->heading($strsummary, 3);
183         if (survey_count_responses($survey->id, $currentgroup, $groupingid)) {
184             echo "<div class='reportsummary'><a href=\"report.php?action=scales&amp;id=$id\">";
185             survey_print_graph("id=$id&amp;group=$currentgroup&amp;type=overall.png");
186             echo "</a></div>";
187         } else {
188             echo $OUTPUT->notification(get_string("nobodyyet","survey"));
189         }
190         break;
192       case "scales":
193         echo $OUTPUT->heading($strscales, 3);
195         if (! $results = survey_get_responses($survey->id, $currentgroup, $groupingid) ) {
196             echo $OUTPUT->notification(get_string("nobodyyet","survey"));
198         } else {
200             $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions));
201             $questionorder = explode(",", $survey->questions);
203             foreach ($questionorder as $key => $val) {
204                 $question = $questions[$val];
205                 if ($question->type < 0) {  // We have some virtual scales.  Just show them.
206                     $virtualscales = true;
207                     break;
208                 }
209             }
211             foreach ($questionorder as $key => $val) {
212                 $question = $questions[$val];
213                 if ($question->multi) {
214                     if (!empty($virtualscales) && $question->type > 0) {  // Don't show non-virtual scales if virtual
215                         continue;
216                     }
217                     echo "<p class=\"centerpara\"><a title=\"$strseemoredetail\" href=\"report.php?action=questions&amp;id=$id&amp;qid=$question->multi\">";
218                     survey_print_graph("id=$id&amp;qid=$question->id&amp;group=$currentgroup&amp;type=multiquestion.png");
219                     echo "</a></p><br />";
220                 }
221             }
222         }
224         break;
226       case "questions":
228         if ($qid) {     // just get one multi-question
229             $questions = $DB->get_records_select("survey_questions", "id in ($qid)");
230             $questionorder = explode(",", $qid);
232             if ($scale = $DB->get_records("survey_questions", array("multi"=>$qid))) {
233                 $scale = array_pop($scale);
234                 echo $OUTPUT->heading("$scale->text - $strselectedquestions", 3);
235             } else {
236                 echo $OUTPUT->heading($strselectedquestions, 3);
237             }
239         } else {        // get all top-level questions
240             $questions = $DB->get_records_list("survey_questions", "id", explode(',',$survey->questions));
241             $questionorder = explode(",", $survey->questions);
243             echo $OUTPUT->heading($strallquestions, 3);
244         }
246         if (! $results = survey_get_responses($survey->id, $currentgroup, $groupingid) ) {
247             echo $OUTPUT->notification(get_string("nobodyyet","survey"));
249         } else {
251             foreach ($questionorder as $key => $val) {
252                 $question = $questions[$val];
253                 if ($question->type < 0) {  // We have some virtual scales.  DON'T show them.
254                     $virtualscales = true;
255                     break;
256                 }
257             }
259             foreach ($questionorder as $key => $val) {
260                 $question = $questions[$val];
262                 if ($question->type < 0) {  // We have some virtual scales.  DON'T show them.
263                     continue;
264                 }
265                 $question->text = get_string($question->text, "survey");
267                 if ($question->multi) {
268                     echo $OUTPUT->heading($question->text . ':', 4);
270                     $subquestions = $DB->get_records_list("survey_questions", "id", explode(',', $question->multi));
271                     $subquestionorder = explode(",", $question->multi);
272                     foreach ($subquestionorder as $key => $val) {
273                         $subquestion = $subquestions[$val];
274                         if ($subquestion->type > 0) {
275                             echo "<p class=\"centerpara\">";
276                             echo "<a title=\"$strseemoredetail\" href=\"report.php?action=question&amp;id=$id&amp;qid=$subquestion->id\">";
277                             survey_print_graph("id=$id&amp;qid=$subquestion->id&amp;group=$currentgroup&amp;type=question.png");
278                             echo "</a></p>";
279                         }
280                     }
281                 } else if ($question->type > 0 ) {
282                     echo "<p class=\"centerpara\">";
283                     echo "<a title=\"$strseemoredetail\" href=\"report.php?action=question&amp;id=$id&amp;qid=$question->id\">";
284                     survey_print_graph("id=$id&amp;qid=$question->id&amp;group=$currentgroup&amp;type=question.png");
285                     echo "</a></p>";
287                 } else {
288                     $table = new html_table();
289                     $table->head = array($question->text);
290                     $table->align = array ("left");
292                     $contents = '<table cellpadding="15" width="100%">';
294                     if ($aaa = survey_get_user_answers($survey->id, $question->id, $currentgroup, "sa.time ASC")) {
295                         foreach ($aaa as $a) {
296                             $contents .= "<tr>";
297                             $contents .= '<td class="fullnamecell">'.fullname($a).'</td>';
298                             $contents .= '<td valign="top">'.$a->answer1.'</td>';
299                             $contents .= "</tr>";
300                         }
301                     }
302                     $contents .= "</table>";
304                     $table->data[] = array($contents);
306                     echo html_writer::table($table);
308                     echo $OUTPUT->spacer(array('height'=>30)); // should be done with CSS instead
309                 }
310             }
311         }
313         break;
315       case "question":
316         if (!$question = $DB->get_record("survey_questions", array("id"=>$qid))) {
317             print_error('cannotfindquestion', 'survey');
318         }
319         $question->text = get_string($question->text, "survey");
321         $answers =  explode(",", get_string($question->options, "survey"));
323         echo $OUTPUT->heading("$strquestion: $question->text", 3);
326         $strname = get_string("name", "survey");
327         $strtime = get_string("time", "survey");
328         $stractual = get_string("actual", "survey");
329         $strpreferred = get_string("preferred", "survey");
330         $strdateformat = get_string("strftimedatetime");
332         $table = new html_table();
333         $table->head = array("", $strname, $strtime, $stractual, $strpreferred);
334         $table->align = array ("left", "left", "left", "left", "right");
335         $table->size = array (35, "", "", "", "");
337         if ($aaa = survey_get_user_answers($survey->id, $question->id, $currentgroup)) {
338             foreach ($aaa as $a) {
339                 if ($a->answer1) {
340                     $answer1 =  "$a->answer1 - ".$answers[$a->answer1 - 1];
341                 } else {
342                     $answer1 =  "&nbsp;";
343                 }
344                 if ($a->answer2) {
345                     $answer2 = "$a->answer2 - ".$answers[$a->answer2 - 1];
346                 } else {
347                     $answer2 = "&nbsp;";
348                 }
349                 $table->data[] = array(
350                        $OUTPUT->user_picture($a, array('courseid'=>$course->id)),
351                        "<a href=\"report.php?id=$id&amp;action=student&amp;student=$a->userid\">".fullname($a)."</a>",
352                        userdate($a->time),
353                        $answer1, $answer2);
355             }
356         }
358         echo html_writer::table($table);
360         break;
362       case "students":
364          echo $OUTPUT->heading(get_string("analysisof", "survey", get_string('participants')), 3);
366          if (! $results = survey_get_responses($survey->id, $currentgroup, $groupingid) ) {
367              echo $OUTPUT->notification(get_string("nobodyyet","survey"));
368          } else {
369              survey_print_all_responses($cm->id, $results, $course->id);
370          }
372         break;
374       case "student":
375          if (!$user = $DB->get_record("user", array("id"=>$student))) {
376              print_error('invaliduserid');
377          }
379          echo $OUTPUT->heading(get_string("analysisof", "survey", fullname($user)), 3);
381          if ($notes != '' and confirm_sesskey()) {
382              if (survey_get_analysis($survey->id, $user->id)) {
383                  if (! survey_update_analysis($survey->id, $user->id, $notes)) {
384                      echo $OUTPUT->notification("An error occurred while saving your notes.  Sorry.");
385                  } else {
386                      echo $OUTPUT->notification(get_string("savednotes", "survey"));
387                  }
388              } else {
389                  if (! survey_add_analysis($survey->id, $user->id, $notes)) {
390                      echo $OUTPUT->notification("An error occurred while saving your notes.  Sorry.");
391                  } else {
392                      echo $OUTPUT->notification(get_string("savednotes", "survey"));
393                  }
394              }
395          }
397          echo "<p <p class=\"centerpara\">";
398          echo $OUTPUT->user_picture($user, array('courseid'=>$course->id));
399          echo "</p>";
401          $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions));
402          $questionorder = explode(",", $survey->questions);
404          if ($showscales) {
405              // Print overall summary
406              echo "<p <p class=\"centerpara\">>";
407              survey_print_graph("id=$id&amp;sid=$student&amp;type=student.png");
408              echo "</p>";
410              // Print scales
412              foreach ($questionorder as $key => $val) {
413                  $question = $questions[$val];
414                  if ($question->type < 0) {  // We have some virtual scales.  Just show them.
415                      $virtualscales = true;
416                      break;
417                  }
418              }
420              foreach ($questionorder as $key => $val) {
421                  $question = $questions[$val];
422                  if ($question->multi) {
423                      if ($virtualscales && $question->type > 0) {  // Don't show non-virtual scales if virtual
424                          continue;
425                      }
426                      echo "<p class=\"centerpara\">";
427                      echo "<a title=\"$strseemoredetail\" href=\"report.php?action=questions&amp;id=$id&amp;qid=$question->multi\">";
428                      survey_print_graph("id=$id&amp;qid=$question->id&amp;sid=$student&amp;type=studentmultiquestion.png");
429                      echo "</a></p><br />";
430                  }
431              }
432          }
434          // Print non-scale questions
436          foreach ($questionorder as $key => $val) {
437              $question = $questions[$val];
438              if ($question->type == 0 or $question->type == 1) {
439                  if ($answer = survey_get_user_answer($survey->id, $question->id, $user->id)) {
440                     $table = new html_table();
441                      $table->head = array(get_string($question->text, "survey"));
442                      $table->align = array ("left");
443                      $table->data[] = array(s($answer->answer1)); // no html here, just plain text
444                      echo html_writer::table($table);
445                      echo $OUTPUT->spacer(array('height'=>30));
446                  }
447              }
448          }
450          if ($rs = survey_get_analysis($survey->id, $user->id)) {
451             $notes = $rs->notes;
452          } else {
453             $notes = "";
454          }
455          echo "<hr noshade=\"noshade\" size=\"1\" />";
456          echo "<div class='studentreport'>";
457          echo "<form action=\"report.php\" method=\"post\">";
458          echo "<h3>$strnotes:</h3>";
459          echo "<blockquote>";
460          echo "<textarea name=\"notes\" rows=\"10\" cols=\"60\">";
461          p($notes);
462          echo "</textarea><br />";
463          echo "<input type=\"hidden\" name=\"action\" value=\"student\" />";
464          echo "<input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />";
465          echo "<input type=\"hidden\" name=\"student\" value=\"$student\" />";
466          echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />";
467          echo "<input type=\"submit\" value=\"".get_string("savechanges")."\" />";
468          echo "</blockquote>";
469          echo "</form>";
470          echo "</div>";
473          break;
475       case "download":
476         echo $OUTPUT->heading($strdownload, 3);
478         require_capability('mod/survey:download', $context);
480         echo '<p class="centerpara">'.get_string("downloadinfo", "survey").'</p>';
482         echo $OUTPUT->container_start('reportbuttons');
483         $options = array();
484         $options["id"] = "$cm->id";
485         $options["group"] = $currentgroup;
487         $options["type"] = "ods";
488         echo $OUTPUT->single_button(new moodle_url("download.php", $options), get_string("downloadods"));
490         $options["type"] = "xls";
491         echo $OUTPUT->single_button(new moodle_url("download.php", $options), get_string("downloadexcel"));
493         $options["type"] = "txt";
494         echo $OUTPUT->single_button(new moodle_url("download.php", $options), get_string("downloadtext"));
495         echo $OUTPUT->container_end();
497         break;
499     }
500     echo $OUTPUT->footer();