Fixed problem with cmi.interactions.n.id duplicated ids
[moodle-linuxchix.git] / mod / survey / report.php
blobcced689e574bb846706f5d3a5b35f8387a98c781
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->id != SITEID) {
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 $crumbs[] = array('name' => $strsurveys, 'link' => "index.php?id=$course->id", 'type' => 'activity');
69 $crumbs[] = array('name' => format_string($survey->name), 'link' => "view.php?id=$cm->id", 'type' => 'activityinstance');
70 $crumbs[] = array('name' => $strreport, 'link' => '', 'type' => 'title');
71 $navigation = build_navigation($crumbs);
73 print_header("$course->shortname: ".format_string($survey->name), $course->fullname, $navigation,
74 "", "", true,
75 update_module_button($cm->id, $course->id, $strsurvey), navmenu($course, $cm));
77 /// Check to see if groups are being used in this survey
78 if ($groupmode = groupmode($course, $cm)) { // Groups are being used
79 $menuaction = $action == "student" ? "students" : $action;
80 $currentgroup = setup_and_print_groups($course, $groupmode,
81 "report.php?id=$cm->id&amp;action=$menuaction&amp;qid=$qid");
82 } else {
83 $currentgroup = 0;
86 if ($currentgroup) {
87 $users = get_group_users($currentgroup);
88 } else {
89 $users = get_course_users($course->id);
92 print_simple_box_start("center");
93 if ($showscales) {
94 echo "<a href=\"report.php?action=summary&amp;id=$id\">$strsummary</a>";
95 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=scales&amp;id=$id\">$strscales</a>";
96 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=questions&amp;id=$id\">$strquestions</a>";
97 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=students&amp;id=$id\">$course->students</a>";
98 if (has_capability('mod/survey:download', $context)) {
99 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=download&amp;id=$id\">$strdownload</a>";
101 if (empty($action)) {
102 $action = "summary";
104 } else {
105 echo "<a href=\"report.php?action=questions&amp;id=$id\">$strquestions</a>";
106 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=students&amp;id=$id\">$course->students</a>";
107 if (has_capability('mod/survey:download', $context)) {
108 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=download&amp;id=$id\">$strdownload</a>";
110 if (empty($action)) {
111 $action = "questions";
114 print_simple_box_end();
116 print_spacer(30,30);
119 /// Print the menu across the top
121 $virtualscales = false;
123 switch ($action) {
125 case "summary":
126 print_heading($strsummary);
128 if (survey_count_responses($survey->id, $currentgroup)) {
129 echo "<div class='reportsummary'><a href=\"report.php?action=scales&amp;id=$id\">";
130 survey_print_graph("id=$id&amp;group=$currentgroup&amp;type=overall.png");
131 echo "</a></div>";
132 } else {
133 notify(get_string("nobodyyet","survey"));
135 break;
137 case "scales":
138 print_heading($strscales);
140 if (! $results = survey_get_responses($survey->id, $currentgroup) ) {
141 notify(get_string("nobodyyet","survey"));
143 } else {
145 $questions = get_records_list("survey_questions", "id", $survey->questions);
146 $questionorder = explode(",", $survey->questions);
148 foreach ($questionorder as $key => $val) {
149 $question = $questions[$val];
150 if ($question->type < 0) { // We have some virtual scales. Just show them.
151 $virtualscales = true;
152 break;
156 foreach ($questionorder as $key => $val) {
157 $question = $questions[$val];
158 if ($question->multi) {
159 if (!empty($virtualscales) && $question->type > 0) { // Don't show non-virtual scales if virtual
160 continue;
162 echo "<p class=\"centerpara\"><a title=\"$strseemoredetail\" href=\"report.php?action=questions&amp;id=$id&amp;qid=$question->multi\">";
163 survey_print_graph("id=$id&amp;qid=$question->id&amp;group=$currentgroup&amp;type=multiquestion.png");
164 echo "</a></p><br />";
169 break;
171 case "questions":
173 if ($qid) { // just get one multi-question
174 $questions = get_records_list("survey_questions", "id", $qid);
175 $questionorder = explode(",", $qid);
177 if ($scale = get_records("survey_questions", "multi", "$qid")) {
178 $scale = array_pop($scale);
179 print_heading("$scale->text - $strselectedquestions");
180 } else {
181 print_heading($strselectedquestions);
184 } else { // get all top-level questions
185 $questions = get_records_list("survey_questions", "id", $survey->questions);
186 $questionorder = explode(",", $survey->questions);
188 print_heading($strallquestions);
191 if (! $results = survey_get_responses($survey->id, $currentgroup) ) {
192 notify(get_string("nobodyyet","survey"));
194 } else {
196 foreach ($questionorder as $key => $val) {
197 $question = $questions[$val];
198 if ($question->type < 0) { // We have some virtual scales. DON'T show them.
199 $virtualscales = true;
200 break;
204 foreach ($questionorder as $key => $val) {
205 $question = $questions[$val];
207 if ($question->type < 0) { // We have some virtual scales. DON'T show them.
208 continue;
210 $question->text = get_string($question->text, "survey");
212 if ($question->multi) {
213 echo "<h3>$question->text:</h3>";
215 $subquestions = get_records_list("survey_questions", "id", $question->multi);
216 $subquestionorder = explode(",", $question->multi);
217 foreach ($subquestionorder as $key => $val) {
218 $subquestion = $subquestions[$val];
219 if ($subquestion->type > 0) {
220 echo "<p class=\"centerpara\">";
221 echo "<a title=\"$strseemoredetail\" href=\"report.php?action=question&amp;id=$id&amp;qid=$subquestion->id\">";
222 survey_print_graph("id=$id&amp;qid=$subquestion->id&amp;group=$currentgroup&amp;type=question.png");
223 echo "</a></p>";
226 } else if ($question->type > 0 ) {
227 echo "<p class=\"centerpara\">";
228 echo "<a title=\"$strseemoredetail\" href=\"report.php?action=question&amp;id=$id&amp;qid=$question->id\">";
229 survey_print_graph("id=$id&amp;qid=$question->id&amp;group=$currentgroup&amp;type=question.png");
230 echo "</a></p>";
232 } else {
233 $table = NULL;
234 $table->head = array($question->text);
235 $table->align = array ("left");
237 $contents = '<table cellpadding="15" width="100%">';
239 if ($aaa = survey_get_user_answers($survey->id, $question->id, $currentgroup, "sa.time ASC")) {
240 foreach ($aaa as $a) {
241 $contents .= "<tr>";
242 $contents .= '<td class="fullnamecell">'.fullname($a).'</td>';
243 $contents .= '<td valign="top">'.$a->answer1.'</td>';
244 $contents .= "</tr>";
247 $contents .= "</table>";
249 $table->data[] = array($contents);
251 print_table($table);
252 print_spacer(30);
257 break;
259 case "question":
260 if (!$question = get_record("survey_questions", "id", $qid)) {
261 error("Question doesn't exist");
263 $question->text = get_string($question->text, "survey");
265 $answers = explode(",", get_string($question->options, "survey"));
267 print_heading("$strquestion: $question->text");
270 $strname = get_string("name", "survey");
271 $strtime = get_string("time", "survey");
272 $stractual = get_string("actual", "survey");
273 $strpreferred = get_string("preferred", "survey");
274 $strdateformat = get_string("strftimedatetime");
276 $table = NULL;
277 $table->head = array("", $strname, $strtime, $stractual, $strpreferred);
278 $table->align = array ("left", "left", "left", "left", "right");
279 $table->size = array (35, "", "", "", "");
281 if ($aaa = survey_get_user_answers($survey->id, $question->id, $currentgroup)) {
282 foreach ($aaa as $a) {
283 if ($a->answer1) {
284 $answer1 = "$a->answer1 - ".$answers[$a->answer1 - 1];
285 } else {
286 $answer1 = "&nbsp;";
288 if ($a->answer2) {
289 $answer2 = "$a->answer2 - ".$answers[$a->answer2 - 1];
290 } else {
291 $answer2 = "&nbsp;";
294 $table->data[] = array(
295 print_user_picture($a->userid, $course->id, $a->picture, false, true, true),
296 "<a href=\"report.php?id=$id&amp;action=student&amp;student=$a->userid\">".fullname($a)."</a>",
297 userdate($a->time),
298 $answer1, $answer2);
303 print_table($table);
305 break;
307 case "students":
309 print_heading(get_string("analysisof", "survey", "$course->students"));
311 if (! $results = survey_get_responses($survey->id, $currentgroup) ) {
312 notify(get_string("nobodyyet","survey"));
313 } else {
314 survey_print_all_responses($cm->id, $results, $course->id);
317 break;
319 case "student":
320 if (!$user = get_record("user", "id", $student)) {
321 error("Student doesn't exist");
324 print_heading(get_string("analysisof", "survey", fullname($user)));
326 if ($notes != '' and confirm_sesskey()) {
327 if (survey_get_analysis($survey->id, $user->id)) {
328 if (! survey_update_analysis($survey->id, $user->id, $notes)) {
329 notify("An error occurred while saving your notes. Sorry.");
330 } else {
331 notify(get_string("savednotes", "survey"));
333 } else {
334 if (! survey_add_analysis($survey->id, $user->id, $notes)) {
335 notify("An error occurred while saving your notes. Sorry.");
336 } else {
337 notify(get_string("savednotes", "survey"));
342 echo "<p <p class=\"centerpara\">";
343 print_user_picture($user->id, $course->id, $user->picture, true);
344 echo "</p>";
346 $questions = get_records_list("survey_questions", "id", $survey->questions);
347 $questionorder = explode(",", $survey->questions);
349 if ($showscales) {
350 // Print overall summary
351 echo "<p <p class=\"centerpara\">>";
352 survey_print_graph("id=$id&amp;sid=$student&amp;type=student.png");
353 echo "</p>";
355 // Print scales
357 foreach ($questionorder as $key => $val) {
358 $question = $questions[$val];
359 if ($question->type < 0) { // We have some virtual scales. Just show them.
360 $virtualscales = true;
361 break;
365 foreach ($questionorder as $key => $val) {
366 $question = $questions[$val];
367 if ($question->multi) {
368 if ($virtualscales && $question->type > 0) { // Don't show non-virtual scales if virtual
369 continue;
371 echo "<p class=\"centerpara\">";
372 echo "<a title=\"$strseemoredetail\" href=\"report.php?action=questions&amp;id=$id&amp;qid=$question->multi\">";
373 survey_print_graph("id=$id&amp;qid=$question->id&amp;sid=$student&amp;type=studentmultiquestion.png");
374 echo "</a></p><br />";
379 // Print non-scale questions
381 foreach ($questionorder as $key => $val) {
382 $question = $questions[$val];
383 if ($question->type == 0 or $question->type == 1) {
384 if ($answer = survey_get_user_answer($survey->id, $question->id, $user->id)) {
385 $table = NULL;
386 $table->head = array(get_string($question->text, "survey"));
387 $table->align = array ("left");
388 $table->data[] = array(s($answer->answer1)); // no html here, just plain text
389 print_table($table);
390 print_spacer(30);
395 if ($rs = survey_get_analysis($survey->id, $user->id)) {
396 $notes = $rs->notes;
397 } else {
398 $notes = "";
400 echo "<hr noshade=\"noshade\" size=\"1\" />";
401 echo "<div class='studentreport'>";
402 echo "<form action=\"report.php\" method=\"post\">";
403 echo "<h3>$strnotes:</h3>";
404 echo "<blockquote>";
405 echo "<textarea name=\"notes\" rows=\"10\" cols=\"60\">";
406 p($notes);
407 echo "</textarea><br />";
408 echo "<input type=\"hidden\" name=\"action\" value=\"student\" />";
409 echo "<input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />";
410 echo "<input type=\"hidden\" name=\"student\" value=\"$student\" />";
411 echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />";
412 echo "<input type=\"submit\" value=\"".get_string("savechanges")."\" />";
413 echo "</blockquote>";
414 echo "</form>";
415 echo "</div>";
418 break;
420 case "download":
421 print_heading($strdownload);
423 require_capability('mod/survey:download', $context);
425 echo '<p class="centerpara">'.get_string("downloadinfo", "survey").'</p>';
427 echo '<div class="reportbuttons">';
428 $optons = array();
429 $options["id"] = "$cm->id";
430 $options["group"] = $currentgroup;
432 $options["type"] = "ods";
433 print_single_button("download.php", $options, get_string("downloadods"));
435 $options["type"] = "xls";
436 print_single_button("download.php", $options, get_string("downloadexcel"));
438 $options["type"] = "txt";
439 print_single_button("download.php", $options, get_string("downloadtext"));
440 echo '</div>';
442 break;
445 print_footer($course);