Dan's fix from MDL-7263
[moodle.git] / mod / survey / report.php
blob4e1b968f45b8a6d193e99e85fa5411c91d50610f
1 <?php // $Id$
3 require_once("../../config.php");
4 require_once("lib.php");
6 // Check that all the parameters have been provided.
8 $id = required_param('id', PARAM_INT); // Course Module ID
9 $action = optional_param('action', '', PARAM_ALPHA); // What to look at
10 $qid = optional_param('qid', 0, PARAM_RAW); // Question IDs comma-separated list
11 $student = optional_param('student', 0, PARAM_INT); // Student ID
12 $notes = optional_param('notes', '', PARAM_RAW); // Save teachers notes
14 $qids = explode(',', $qid);
15 $qids = clean_param($qids, PARAM_INT);
16 $qid = implode (',', $qids);
18 if (! $cm = get_coursemodule_from_id('survey', $id)) {
19 error("Course Module ID was incorrect");
22 if (! $course = get_record("course", "id", $cm->course)) {
23 error("Course is misconfigured");
26 require_login($course->id, false);
28 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
30 require_capability('mod/survey:readresponses', $context);
32 if (! $survey = get_record("survey", "id", $cm->instance)) {
33 error("Survey ID was incorrect");
36 if (! $template = get_record("survey", "id", $survey->template)) {
37 error("Template ID was incorrect");
40 $showscales = ($template->name != 'ciqname');
43 $strreport = get_string("report", "survey");
44 $strsurvey = get_string("modulename", "survey");
45 $strsurveys = get_string("modulenameplural", "survey");
46 $strsummary = get_string("summary", "survey");
47 $strscales = get_string("scales", "survey");
48 $strquestion = get_string("question", "survey");
49 $strquestions = get_string("questions", "survey");
50 $strdownload = get_string("download", "survey");
51 $strallscales = get_string("allscales", "survey");
52 $strallquestions = get_string("allquestions", "survey");
53 $strselectedquestions = get_string("selectedquestions", "survey");
54 $strseemoredetail = get_string("seemoredetail", "survey");
55 $strnotes = get_string("notes", "survey");
57 add_to_log($course->id, "survey", "view report", "report.php?id=$cm->id", "$survey->id", $cm->id);
59 if ($course->category) {
60 $navigation = "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> ->
61 <a href=\"index.php?id=$course->id\">$strsurveys</a> ->
62 <a href=\"view.php?id=$cm->id\">".format_string($survey->name,true)."</a> -> ";
63 } else {
64 $navigation = "<a href=\"index.php?id=$course->id\">$strsurveys</a> ->
65 <a href=\"view.php?id=$cm->id\">".format_string($survey->name,true)."</a> -> ";
68 print_header("$course->shortname: ".format_string($survey->name), "$course->fullname", "$navigation $strreport",
69 "", "", true,
70 update_module_button($cm->id, $course->id, $strsurvey), navmenu($course, $cm));
72 /// Check to see if groups are being used in this survey
73 if ($groupmode = groupmode($course, $cm)) { // Groups are being used
74 $menuaction = $action == "student" ? "students" : $action;
75 $currentgroup = setup_and_print_groups($course, $groupmode,
76 "report.php?id=$cm->id&amp;action=$menuaction&amp;qid=$qid");
77 } else {
78 $currentgroup = 0;
81 if ($currentgroup) {
82 $users = get_group_users($currentgroup);
83 } else {
84 $users = get_course_users($course->id);
87 print_simple_box_start("center");
88 if ($showscales) {
89 echo "<a href=\"report.php?action=summary&amp;id=$id\">$strsummary</a>";
90 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=scales&amp;id=$id\">$strscales</a>";
91 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=questions&amp;id=$id\">$strquestions</a>";
92 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=students&amp;id=$id\">$course->students</a>";
93 if (has_capability('mod/survey:download', $context)) {
94 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=download&amp;id=$id\">$strdownload</a>";
96 if (empty($action)) {
97 $action = "summary";
99 } else {
100 echo "<a href=\"report.php?action=questions&amp;id=$id\">$strquestions</a>";
101 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=students&amp;id=$id\">$course->students</a>";
102 if (has_capability('mod/survey:download', $context)) {
103 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=download&amp;id=$id\">$strdownload</a>";
105 if (empty($action)) {
106 $action = "questions";
109 print_simple_box_end();
111 print_spacer(30,30);
114 /// Print the menu across the top
116 $virtualscales = false;
118 switch ($action) {
120 case "summary":
121 print_heading($strsummary);
123 if (survey_count_responses($survey->id, $currentgroup)) {
124 echo "<center><a href=\"report.php?action=scales&amp;id=$id\">";
125 survey_print_graph("id=$id&amp;group=$currentgroup&amp;type=overall.png");
126 echo "</a></center>";
127 } else {
128 notify(get_string("nobodyyet","survey"));
130 break;
132 case "scales":
133 print_heading($strscales);
135 if (! $results = survey_get_responses($survey->id, $currentgroup) ) {
136 notify(get_string("nobodyyet","survey"));
138 } else {
140 $questions = get_records_list("survey_questions", "id", $survey->questions);
141 $questionorder = explode(",", $survey->questions);
143 foreach ($questionorder as $key => $val) {
144 $question = $questions[$val];
145 if ($question->type < 0) { // We have some virtual scales. Just show them.
146 $virtualscales = true;
147 break;
151 foreach ($questionorder as $key => $val) {
152 $question = $questions[$val];
153 if ($question->multi) {
154 if (!empty($virtualscales) && $question->type > 0) { // Don't show non-virtual scales if virtual
155 continue;
157 echo "<p align=\"center\"><a title=\"$strseemoredetail\" href=\"report.php?action=questions&amp;id=$id&amp;qid=$question->multi\">";
158 survey_print_graph("id=$id&amp;qid=$question->id&amp;group=$currentgroup&amp;type=multiquestion.png");
159 echo "</a></p><br />";
164 break;
166 case "questions":
168 if ($qid) { // just get one multi-question
169 $questions = get_records_list("survey_questions", "id", $qid);
170 $questionorder = explode(",", $qid);
172 if ($scale = get_records("survey_questions", "multi", "$qid")) {
173 $scale = array_pop($scale);
174 print_heading("$scale->text - $strselectedquestions");
175 } else {
176 print_heading($strselectedquestions);
179 } else { // get all top-level questions
180 $questions = get_records_list("survey_questions", "id", $survey->questions);
181 $questionorder = explode(",", $survey->questions);
183 print_heading($strallquestions);
186 if (! $results = survey_get_responses($survey->id, $currentgroup) ) {
187 notify(get_string("nobodyyet","survey"));
189 } else {
191 foreach ($questionorder as $key => $val) {
192 $question = $questions[$val];
193 if ($question->type < 0) { // We have some virtual scales. DON'T show them.
194 $virtualscales = true;
195 break;
199 foreach ($questionorder as $key => $val) {
200 $question = $questions[$val];
202 if ($question->type < 0) { // We have some virtual scales. DON'T show them.
203 continue;
205 $question->text = get_string($question->text, "survey");
207 if ($question->multi) {
208 echo "<h3>$question->text:</h3>";
210 $subquestions = get_records_list("survey_questions", "id", $question->multi);
211 $subquestionorder = explode(",", $question->multi);
212 foreach ($subquestionorder as $key => $val) {
213 $subquestion = $subquestions[$val];
214 if ($subquestion->type > 0) {
215 echo "<p align=\"center\">";
216 echo "<a title=\"$strseemoredetail\" href=\"report.php?action=question&amp;id=$id&amp;qid=$subquestion->id\">";
217 survey_print_graph("id=$id&amp;qid=$subquestion->id&amp;group=$currentgroup&amp;type=question.png");
218 echo "</a></p>";
221 } else if ($question->type > 0 ) {
222 echo "<p align=\"center\">";
223 echo "<a title=\"$strseemoredetail\" href=\"report.php?action=question&amp;id=$id&amp;qid=$question->id\">";
224 survey_print_graph("id=$id&amp;qid=$question->id&amp;group=$currentgroup&amp;type=question.png");
225 echo "</a></p>";
227 } else {
228 $table = NULL;
229 $table->head = array($question->text);
230 $table->align = array ("left");
232 $contents = '<table cellpadding="15" width="100%">';
234 if ($aaa = survey_get_user_answers($survey->id, $question->id, $currentgroup, "sa.time ASC")) {
235 foreach ($aaa as $a) {
236 $contents .= "<tr>";
237 $contents .= '<td nowrap="nowrap" width="10%" valign="top">'.fullname($a).'</td>';
238 $contents .= '<td valign="top">'.$a->answer1.'</td>';
239 $contents .= "</tr>";
242 $contents .= "</table>";
244 $table->data[] = array($contents);
246 print_table($table);
247 print_spacer(30);
252 break;
254 case "question":
255 if (!$question = get_record("survey_questions", "id", $qid)) {
256 error("Question doesn't exist");
258 $question->text = get_string($question->text, "survey");
260 $answers = explode(",", get_string($question->options, "survey"));
262 print_heading("$strquestion: $question->text");
265 $strname = get_string("name", "survey");
266 $strtime = get_string("time", "survey");
267 $stractual = get_string("actual", "survey");
268 $strpreferred = get_string("preferred", "survey");
269 $strdateformat = get_string("strftimedatetime");
271 $table = NULL;
272 $table->head = array("", $strname, $strtime, $stractual, $strpreferred);
273 $table->align = array ("left", "left", "left", "left", "right");
274 $table->size = array (35, "", "", "", "");
276 if ($aaa = survey_get_user_answers($survey->id, $question->id, $currentgroup)) {
277 foreach ($aaa as $a) {
278 if ($a->answer1) {
279 $answer1 = "$a->answer1 - ".$answers[$a->answer1 - 1];
280 } else {
281 $answer1 = "&nbsp;";
283 if ($a->answer2) {
284 $answer2 = "$a->answer2 - ".$answers[$a->answer2 - 1];
285 } else {
286 $answer2 = "&nbsp;";
289 $table->data[] = array(
290 print_user_picture($a->userid, $course->id, $a->picture, false, true, true),
291 "<a href=\"report.php?id=$id&amp;action=student&amp;student=$a->userid\">".fullname($a)."</a>",
292 userdate($a->time),
293 $answer1, $answer2);
298 print_table($table);
300 break;
302 case "students":
304 print_heading(get_string("analysisof", "survey", "$course->students"));
306 if (! $results = survey_get_responses($survey->id, $currentgroup) ) {
307 notify(get_string("nobodyyet","survey"));
308 } else {
309 survey_print_all_responses($cm->id, $results, $course->id);
312 break;
314 case "student":
315 if (!$user = get_record("user", "id", $student)) {
316 error("Student doesn't exist");
319 print_heading(get_string("analysisof", "survey", fullname($user)));
321 if ($notes != '' and confirm_sesskey()) {
322 if (survey_get_analysis($survey->id, $user->id)) {
323 if (! survey_update_analysis($survey->id, $user->id, $notes)) {
324 notify("An error occurred while saving your notes. Sorry.");
325 } else {
326 notify(get_string("savednotes", "survey"));
328 } else {
329 if (! survey_add_analysis($survey->id, $user->id, $notes)) {
330 notify("An error occurred while saving your notes. Sorry.");
331 } else {
332 notify(get_string("savednotes", "survey"));
337 echo "<p align=\"center\">";
338 print_user_picture($user->id, $course->id, $user->picture, true);
339 echo "</p>";
341 $questions = get_records_list("survey_questions", "id", $survey->questions);
342 $questionorder = explode(",", $survey->questions);
344 if ($showscales) {
345 // Print overall summary
346 echo "<p align=\"center\">";
347 survey_print_graph("id=$id&amp;sid=$student&amp;type=student.png");
348 echo "</p>";
350 // Print scales
352 foreach ($questionorder as $key => $val) {
353 $question = $questions[$val];
354 if ($question->type < 0) { // We have some virtual scales. Just show them.
355 $virtualscales = true;
356 break;
360 foreach ($questionorder as $key => $val) {
361 $question = $questions[$val];
362 if ($question->multi) {
363 if ($virtualscales && $question->type > 0) { // Don't show non-virtual scales if virtual
364 continue;
366 echo "<p align=\"center\">";
367 echo "<a title=\"$strseemoredetail\" href=\"report.php?action=questions&amp;id=$id&amp;qid=$question->multi\">";
368 survey_print_graph("id=$id&amp;qid=$question->id&amp;sid=$student&amp;type=studentmultiquestion.png");
369 echo "</a></p><br />";
374 // Print non-scale questions
376 foreach ($questionorder as $key => $val) {
377 $question = $questions[$val];
378 if ($question->type == 0 or $question->type == 1) {
379 if ($answer = survey_get_user_answer($survey->id, $question->id, $user->id)) {
380 $table = NULL;
381 $table->head = array(get_string($question->text, "survey"));
382 $table->align = array ("left");
383 $table->data[] = array(s($answer->answer1)); // no html here, just plain text
384 print_table($table);
385 print_spacer(30);
390 if ($rs = survey_get_analysis($survey->id, $user->id)) {
391 $notes = $rs->notes;
392 } else {
393 $notes = "";
395 echo "<hr noshade=\"noshade\" size=\"1\" />";
396 echo "<center>";
397 echo "<form action=\"report.php\" method=\"post\" name=\"form\">";
398 echo "<h3>$strnotes:</h3>";
399 echo "<blockquote>";
400 echo "<textarea name=\"notes\" rows=\"10\" cols=\"60\">";
401 p($notes);
402 echo "</textarea><br />";
403 echo "<input type=\"hidden\" name=\"action\" value=\"student\" />";
404 echo "<input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />";
405 echo "<input type=\"hidden\" name=\"student\" value=\"$student\" />";
406 echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />";
407 echo "<input type=\"submit\" value=\"".get_string("savechanges")."\" />";
408 echo "</blockquote>";
409 echo "</form>";
410 echo "</center>";
413 break;
415 case "download":
416 print_heading($strdownload);
418 require_capability('mod/survey:download', $context);
420 echo '<p align="center">'.get_string("downloadinfo", "survey").'</p>';
422 echo '<center>';
423 $options["id"] = "$cm->id";
424 $options["type"] = "xls";
425 $options["group"] = $currentgroup;
426 print_single_button("download.php", $options, get_string("downloadexcel", "survey"));
428 $options["type"] = "txt";
429 print_single_button("download.php", $options, get_string("downloadtext", "survey"));
430 echo '</center>';
432 break;
435 print_footer($course);