Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / survey / report.php
bloba585b73f546a836b1e47fbd28cae6da20e3c512c
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, $cm);
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 $navigation = build_navigation($strreport, $cm);
69 print_header("$course->shortname: ".format_string($survey->name), $course->fullname, $navigation,
70 "", "", true,
71 update_module_button($cm->id, $course->id, $strsurvey), navmenu($course, $cm));
73 /// Check to see if groups are being used in this survey
74 if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used
75 $menuaction = $action == "student" ? "students" : $action;
76 $currentgroup = groups_get_activity_group($cm, true);
77 groups_print_activity_menu($cm, "report.php?id=$cm->id&amp;action=$menuaction&amp;qid=$qid");
78 } else {
79 $currentgroup = 0;
82 if ($currentgroup) {
83 $users = groups_get_members($currentgroup);
84 } else if (!empty($CFG->enablegroupings) && !empty($cm->groupingid)) {
85 $users = groups_get_grouping_members($cm->groupingid);
86 } else {
87 $users = get_course_users($course->id);
89 $groupingid = $cm->groupingid;
91 print_simple_box_start("center");
92 if ($showscales) {
93 echo "<a href=\"report.php?action=summary&amp;id=$id\">$strsummary</a>";
94 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=scales&amp;id=$id\">$strscales</a>";
95 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=questions&amp;id=$id\">$strquestions</a>";
96 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=students&amp;id=$id\">$course->students</a>";
97 if (has_capability('mod/survey:download', $context)) {
98 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=download&amp;id=$id\">$strdownload</a>";
100 if (empty($action)) {
101 $action = "summary";
103 } else {
104 echo "<a href=\"report.php?action=questions&amp;id=$id\">$strquestions</a>";
105 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=students&amp;id=$id\">$course->students</a>";
106 if (has_capability('mod/survey:download', $context)) {
107 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=download&amp;id=$id\">$strdownload</a>";
109 if (empty($action)) {
110 $action = "questions";
113 print_simple_box_end();
115 print_spacer(30,30);
118 /// Print the menu across the top
120 $virtualscales = false;
122 switch ($action) {
124 case "summary":
125 print_heading($strsummary);
127 if (survey_count_responses($survey->id, $currentgroup, $groupingid)) {
128 echo "<div class='reportsummary'><a href=\"report.php?action=scales&amp;id=$id\">";
129 survey_print_graph("id=$id&amp;group=$currentgroup&amp;type=overall.png");
130 echo "</a></div>";
131 } else {
132 notify(get_string("nobodyyet","survey"));
134 break;
136 case "scales":
137 print_heading($strscales);
139 if (! $results = survey_get_responses($survey->id, $currentgroup, $groupingid) ) {
140 notify(get_string("nobodyyet","survey"));
142 } else {
144 $questions = get_records_list("survey_questions", "id", $survey->questions);
145 $questionorder = explode(",", $survey->questions);
147 foreach ($questionorder as $key => $val) {
148 $question = $questions[$val];
149 if ($question->type < 0) { // We have some virtual scales. Just show them.
150 $virtualscales = true;
151 break;
155 foreach ($questionorder as $key => $val) {
156 $question = $questions[$val];
157 if ($question->multi) {
158 if (!empty($virtualscales) && $question->type > 0) { // Don't show non-virtual scales if virtual
159 continue;
161 echo "<p class=\"centerpara\"><a title=\"$strseemoredetail\" href=\"report.php?action=questions&amp;id=$id&amp;qid=$question->multi\">";
162 survey_print_graph("id=$id&amp;qid=$question->id&amp;group=$currentgroup&amp;type=multiquestion.png");
163 echo "</a></p><br />";
168 break;
170 case "questions":
172 if ($qid) { // just get one multi-question
173 $questions = get_records_list("survey_questions", "id", $qid);
174 $questionorder = explode(",", $qid);
176 if ($scale = get_records("survey_questions", "multi", "$qid")) {
177 $scale = array_pop($scale);
178 print_heading("$scale->text - $strselectedquestions");
179 } else {
180 print_heading($strselectedquestions);
183 } else { // get all top-level questions
184 $questions = get_records_list("survey_questions", "id", $survey->questions);
185 $questionorder = explode(",", $survey->questions);
187 print_heading($strallquestions);
190 if (! $results = survey_get_responses($survey->id, $currentgroup, $groupingid) ) {
191 notify(get_string("nobodyyet","survey"));
193 } else {
195 foreach ($questionorder as $key => $val) {
196 $question = $questions[$val];
197 if ($question->type < 0) { // We have some virtual scales. DON'T show them.
198 $virtualscales = true;
199 break;
203 foreach ($questionorder as $key => $val) {
204 $question = $questions[$val];
206 if ($question->type < 0) { // We have some virtual scales. DON'T show them.
207 continue;
209 $question->text = get_string($question->text, "survey");
211 if ($question->multi) {
212 echo "<h3>$question->text:</h3>";
214 $subquestions = get_records_list("survey_questions", "id", $question->multi);
215 $subquestionorder = explode(",", $question->multi);
216 foreach ($subquestionorder as $key => $val) {
217 $subquestion = $subquestions[$val];
218 if ($subquestion->type > 0) {
219 echo "<p class=\"centerpara\">";
220 echo "<a title=\"$strseemoredetail\" href=\"report.php?action=question&amp;id=$id&amp;qid=$subquestion->id\">";
221 survey_print_graph("id=$id&amp;qid=$subquestion->id&amp;group=$currentgroup&amp;type=question.png");
222 echo "</a></p>";
225 } else if ($question->type > 0 ) {
226 echo "<p class=\"centerpara\">";
227 echo "<a title=\"$strseemoredetail\" href=\"report.php?action=question&amp;id=$id&amp;qid=$question->id\">";
228 survey_print_graph("id=$id&amp;qid=$question->id&amp;group=$currentgroup&amp;type=question.png");
229 echo "</a></p>";
231 } else {
232 $table = NULL;
233 $table->head = array($question->text);
234 $table->align = array ("left");
236 $contents = '<table cellpadding="15" width="100%">';
238 if ($aaa = survey_get_user_answers($survey->id, $question->id, $currentgroup, "sa.time ASC")) {
239 foreach ($aaa as $a) {
240 $contents .= "<tr>";
241 $contents .= '<td class="fullnamecell">'.fullname($a).'</td>';
242 $contents .= '<td valign="top">'.$a->answer1.'</td>';
243 $contents .= "</tr>";
246 $contents .= "</table>";
248 $table->data[] = array($contents);
250 print_table($table);
251 print_spacer(30);
256 break;
258 case "question":
259 if (!$question = get_record("survey_questions", "id", $qid)) {
260 error("Question doesn't exist");
262 $question->text = get_string($question->text, "survey");
264 $answers = explode(",", get_string($question->options, "survey"));
266 print_heading("$strquestion: $question->text");
269 $strname = get_string("name", "survey");
270 $strtime = get_string("time", "survey");
271 $stractual = get_string("actual", "survey");
272 $strpreferred = get_string("preferred", "survey");
273 $strdateformat = get_string("strftimedatetime");
275 $table = NULL;
276 $table->head = array("", $strname, $strtime, $stractual, $strpreferred);
277 $table->align = array ("left", "left", "left", "left", "right");
278 $table->size = array (35, "", "", "", "");
280 if ($aaa = survey_get_user_answers($survey->id, $question->id, $currentgroup)) {
281 foreach ($aaa as $a) {
282 if ($a->answer1) {
283 $answer1 = "$a->answer1 - ".$answers[$a->answer1 - 1];
284 } else {
285 $answer1 = "&nbsp;";
287 if ($a->answer2) {
288 $answer2 = "$a->answer2 - ".$answers[$a->answer2 - 1];
289 } else {
290 $answer2 = "&nbsp;";
293 $table->data[] = array(
294 print_user_picture($a->userid, $course->id, $a->picture, false, true, true),
295 "<a href=\"report.php?id=$id&amp;action=student&amp;student=$a->userid\">".fullname($a)."</a>",
296 userdate($a->time),
297 $answer1, $answer2);
302 print_table($table);
304 break;
306 case "students":
308 print_heading(get_string("analysisof", "survey", "$course->students"));
310 if (! $results = survey_get_responses($survey->id, $currentgroup, $groupingid) ) {
311 notify(get_string("nobodyyet","survey"));
312 } else {
313 survey_print_all_responses($cm->id, $results, $course->id);
316 break;
318 case "student":
319 if (!$user = get_record("user", "id", $student)) {
320 error("Student doesn't exist");
323 print_heading(get_string("analysisof", "survey", fullname($user)));
325 if ($notes != '' and confirm_sesskey()) {
326 if (survey_get_analysis($survey->id, $user->id)) {
327 if (! survey_update_analysis($survey->id, $user->id, $notes)) {
328 notify("An error occurred while saving your notes. Sorry.");
329 } else {
330 notify(get_string("savednotes", "survey"));
332 } else {
333 if (! survey_add_analysis($survey->id, $user->id, $notes)) {
334 notify("An error occurred while saving your notes. Sorry.");
335 } else {
336 notify(get_string("savednotes", "survey"));
341 echo "<p <p class=\"centerpara\">";
342 print_user_picture($user->id, $course->id, $user->picture, true);
343 echo "</p>";
345 $questions = get_records_list("survey_questions", "id", $survey->questions);
346 $questionorder = explode(",", $survey->questions);
348 if ($showscales) {
349 // Print overall summary
350 echo "<p <p class=\"centerpara\">>";
351 survey_print_graph("id=$id&amp;sid=$student&amp;type=student.png");
352 echo "</p>";
354 // Print scales
356 foreach ($questionorder as $key => $val) {
357 $question = $questions[$val];
358 if ($question->type < 0) { // We have some virtual scales. Just show them.
359 $virtualscales = true;
360 break;
364 foreach ($questionorder as $key => $val) {
365 $question = $questions[$val];
366 if ($question->multi) {
367 if ($virtualscales && $question->type > 0) { // Don't show non-virtual scales if virtual
368 continue;
370 echo "<p class=\"centerpara\">";
371 echo "<a title=\"$strseemoredetail\" href=\"report.php?action=questions&amp;id=$id&amp;qid=$question->multi\">";
372 survey_print_graph("id=$id&amp;qid=$question->id&amp;sid=$student&amp;type=studentmultiquestion.png");
373 echo "</a></p><br />";
378 // Print non-scale questions
380 foreach ($questionorder as $key => $val) {
381 $question = $questions[$val];
382 if ($question->type == 0 or $question->type == 1) {
383 if ($answer = survey_get_user_answer($survey->id, $question->id, $user->id)) {
384 $table = NULL;
385 $table->head = array(get_string($question->text, "survey"));
386 $table->align = array ("left");
387 $table->data[] = array(s($answer->answer1)); // no html here, just plain text
388 print_table($table);
389 print_spacer(30);
394 if ($rs = survey_get_analysis($survey->id, $user->id)) {
395 $notes = $rs->notes;
396 } else {
397 $notes = "";
399 echo "<hr noshade=\"noshade\" size=\"1\" />";
400 echo "<div class='studentreport'>";
401 echo "<form action=\"report.php\" method=\"post\">";
402 echo "<h3>$strnotes:</h3>";
403 echo "<blockquote>";
404 echo "<textarea name=\"notes\" rows=\"10\" cols=\"60\">";
405 p($notes);
406 echo "</textarea><br />";
407 echo "<input type=\"hidden\" name=\"action\" value=\"student\" />";
408 echo "<input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />";
409 echo "<input type=\"hidden\" name=\"student\" value=\"$student\" />";
410 echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />";
411 echo "<input type=\"submit\" value=\"".get_string("savechanges")."\" />";
412 echo "</blockquote>";
413 echo "</form>";
414 echo "</div>";
417 break;
419 case "download":
420 print_heading($strdownload);
422 require_capability('mod/survey:download', $context);
424 echo '<p class="centerpara">'.get_string("downloadinfo", "survey").'</p>';
426 echo '<div class="reportbuttons">';
427 $optons = array();
428 $options["id"] = "$cm->id";
429 $options["group"] = $currentgroup;
431 $options["type"] = "ods";
432 print_single_button("download.php", $options, get_string("downloadods"));
434 $options["type"] = "xls";
435 print_single_button("download.php", $options, get_string("downloadexcel"));
437 $options["type"] = "txt";
438 print_single_button("download.php", $options, get_string("downloadtext"));
439 echo '</div>';
441 break;
444 print_footer($course);