Function name change ... hope it doesn't break any third-party code,
[moodle.git] / mod / survey / report.php
blobb183d80e0dedd14f1f8f58dd104834dde660b67d
1 <?PHP // $Id$
3 require_once("../../config.php");
4 require_once("lib.php");
6 // Check that all the parameters have been provided.
8 require_variable($id); // Course Module ID
9 optional_variable($action, ""); // What to look at
10 optional_variable($qid, "0"); // Question id
12 if (! $cm = get_record("course_modules", "id", $id)) {
13 error("Course Module ID was incorrect");
16 if (! $course = get_record("course", "id", $cm->course)) {
17 error("Course is misconfigured");
20 require_login($course->id);
22 if (!isteacher($course->id)) {
23 error("Sorry, only teachers can see this.");
26 if (! $survey = get_record("survey", "id", $cm->instance)) {
27 error("Survey ID was incorrect");
30 if (! $template = get_record("survey", "id", $survey->template)) {
31 error("Template ID was incorrect");
34 $showscales = ($template->name != 'ciqname');
37 $strreport = get_string("report", "survey");
38 $strsurvey = get_string("modulename", "survey");
39 $strsurveys = get_string("modulenameplural", "survey");
40 $strsummary = get_string("summary", "survey");
41 $strscales = get_string("scales", "survey");
42 $strquestion = get_string("question", "survey");
43 $strquestions = get_string("questions", "survey");
44 $strdownload = get_string("download", "survey");
45 $strallscales = get_string("allscales", "survey");
46 $strallquestions = get_string("allquestions", "survey");
47 $strselectedquestions = get_string("selectedquestions", "survey");
48 $strseemoredetail = get_string("seemoredetail", "survey");
49 $strnotes = get_string("notes", "survey");
51 add_to_log($course->id, "survey", "view report", "report.php?id=$cm->id", "$survey->id", $cm->id);
53 if ($course->category) {
54 $navigation = "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> ->
55 <a href=\"index.php?id=$course->id\">$strsurveys</a> ->
56 <a href=\"view.php?id=$cm->id\">$survey->name</a> -> ";
57 } else {
58 $navigation = "<a href=\"index.php?id=$course->id\">$strsurveys</a> ->
59 <a href=\"view.php?id=$cm->id\">$survey->name</a> -> ";
62 print_header("$course->shortname: $survey->name", "$course->fullname", "$navigation $strreport",
63 "", "", true,
64 update_module_button($cm->id, $course->id, $strsurvey), navmenu($course, $cm));
66 /// Check to see if groups are being used in this survey
67 if ($groupmode = groupmode($course, $cm)) { // Groups are being used
68 $menuaction = $action == "student" ? "students" : $action;
69 $currentgroup = setup_and_print_groups($course, $groupmode,
70 "report.php?id=$cm->id&action=$menuaction&qid=$qid");
71 } else {
72 $currentgroup = 0;
75 if ($currentgroup) {
76 $users = get_group_users($currentgroup);
77 } else {
78 $users = get_course_users($course->id);
81 print_simple_box_start("center");
82 if ($showscales) {
83 echo "<a href=\"report.php?action=summary&id=$id\">$strsummary</a>";
84 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=scales&id=$id\">$strscales</a>";
85 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=questions&id=$id\">$strquestions</a>";
86 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=students&id=$id\">$course->students</a>";
87 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=download&id=$id\">$strdownload</a>";
88 if (empty($action)) {
89 $action = "summary";
91 } else {
92 echo "<a href=\"report.php?action=questions&id=$id\">$strquestions</a>";
93 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=students&id=$id\">$course->students</a>";
94 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=download&id=$id\">$strdownload</a>";
95 if (empty($action)) {
96 $action = "questions";
99 print_simple_box_end();
101 print_spacer(30,30);
104 /// Print the menu across the top
106 switch ($action) {
108 case "summary":
109 print_heading($strsummary);
111 if (survey_count_responses($survey->id, $currentgroup)) {
112 echo "<p align=center><a href=\"report.php?action=scales&id=$id\">";
113 survey_print_graph("id=$id&group=$currentgroup&type=overall.png");
114 echo "</a>";
115 } else {
116 echo "<p align=center>".get_string("nobodyyet","survey")."</p>";
118 break;
120 case "scales":
121 print_heading($strscales);
123 $questions = get_records_list("survey_questions", "id", $survey->questions);
124 $questionorder = explode(",", $survey->questions);
126 foreach ($questionorder as $key => $val) {
127 $question = $questions[$val];
128 if ($question->type < 0) { // We have some virtual scales. Just show them.
129 $virtualscales = true;
130 break;
134 foreach ($questionorder as $key => $val) {
135 $question = $questions[$val];
136 if ($question->multi) {
137 if ($virtualscales && $question->type > 0) { // Don't show non-virtual scales if virtual
138 continue;
140 echo "<p align=center><a title=\"$strseemoredetail\" href=report.php?action=questions&id=$id&qid=$question->multi>";
141 survey_print_graph("id=$id&qid=$question->id&group=$currentgroup&type=multiquestion.png");
142 echo "</a></p><br>";
146 break;
148 case "questions":
150 if ($qid) { // just get one multi-question
151 $questions = get_records_list("survey_questions", "id", $qid);
152 $questionorder = explode(",", $qid);
154 if ($scale = get_records("survey_questions", "multi", "$qid")) {
155 $scale = array_pop($scale);
156 print_heading("$scale->text - $strselectedquestions");
157 } else {
158 print_heading($strselectedquestions);
161 } else { // get all top-level questions
162 $questions = get_records_list("survey_questions", "id", $survey->questions);
163 $questionorder = explode(",", $survey->questions);
165 print_heading($strallquestions);
168 foreach ($questionorder as $key => $val) {
169 $question = $questions[$val];
170 if ($question->type < 0) { // We have some virtual scales. DON'T show them.
171 $virtualscales = true;
172 break;
176 foreach ($questionorder as $key => $val) {
177 $question = $questions[$val];
179 if ($question->type < 0) { // We have some virtual scales. DON'T show them.
180 continue;
182 $question->text = get_string($question->text, "survey");
184 if ($question->multi) {
185 echo "<h3>$question->text:</h3>";
187 $subquestions = get_records_list("survey_questions", "id", $question->multi);
188 $subquestionorder = explode(",", $question->multi);
189 foreach ($subquestionorder as $key => $val) {
190 $subquestion = $subquestions[$val];
191 if ($subquestion->type > 0) {
192 echo "<p align=center>";
193 echo "<a title=\"$strseemoredetail\" href=\"report.php?action=question&id=$id&qid=$subquestion->id\">";
194 survey_print_graph("id=$id&qid=$subquestion->id&group=$currentgroup&type=question.png");
195 echo "</a></p>";
198 } else if ($question->type > 0 ) {
199 echo "<p align=center>";
200 echo "<a title=\"$strseemoredetail\" href=\"report.php?action=question&id=$id&qid=$question->id\">";
201 survey_print_graph("id=$id&qid=$question->id&group=$currentgroup&type=question.png");
202 echo "</a></p>";
204 } else {
205 $table = NULL;
206 $table->head = array($question->text);
207 $table->align = array ("left");
209 $contents = '<table cellpadding="15" width="100%">';
211 if ($aaa = survey_get_user_answers($survey->id, $question->id, $currentgroup, "sa.time ASC")) {
212 foreach ($aaa as $a) {
213 $contents .= "<tr>";
214 $contents .= '<td nowrap="nowrap" width="10%" valign="top">'.fullname($a).'</td>';
215 $contents .= '<td valign="top">'.$a->answer1.'</td>';
216 $contents .= "</tr>";
219 $contents .= "</table>";
221 $table->data[] = array($contents);
223 print_table($table);
224 print_spacer(30);
228 break;
230 case "question":
231 if (!$question = get_record("survey_questions", "id", $qid)) {
232 error("Question doesn't exist");
234 $question->text = get_string($question->text, "survey");
236 $answers = explode(",", get_string($question->options, "survey"));
238 print_heading("$strquestion: $question->text");
241 $strname = get_string("name", "survey");
242 $strtime = get_string("time", "survey");
243 $stractual = get_string("actual", "survey");
244 $strpreferred = get_string("preferred", "survey");
245 $strdateformat = get_string("strftimedatetime");
247 $table = NULL;
248 $table->head = array("", $strname, $strtime, $stractual, $strpreferred);
249 $table->align = array ("left", "left", "left", "left", "right");
250 $table->size = array (35, "", "", "", "");
252 if ($aaa = survey_get_user_answers($survey->id, $question->id, $currentgroup)) {
253 foreach ($aaa as $a) {
254 if ($a->answer1) {
255 $answer1 = "$a->answer1 - ".$answers[$a->answer1 - 1];
256 } else {
257 $answer1 = "&nbsp;";
259 if ($a->answer2) {
260 $answer2 = "$a->answer2 - ".$answers[$a->answer2 - 1];
261 } else {
262 $answer2 = "&nbsp;";
265 $table->data[] = array(
266 print_user_picture($a->userid, $course->id, $a->picture, false, true, true),
267 "<a href=\"report.php?id=$id&action=student&student=$a->userid\">".fullname($a)."</a>",
268 userdate($a->time),
269 $answer1, $answer2);
274 print_table($table);
276 break;
278 case "students":
280 print_heading(get_string("analysisof", "survey", "$course->students"));
282 if (! $results = survey_get_responses($survey->id, $currentgroup) ) {
283 notify(get_string("nobodyyet","survey"));
284 } else {
285 survey_print_all_responses($cm->id, $results, $course->id);
288 break;
290 case "student":
291 if (!$user = get_record("user", "id", $student)) {
292 error("Student doesn't exist");
295 print_heading(get_string("analysisof", "survey", fullname($user)));
297 if (isset($notes)) {
298 if (survey_get_analysis($survey->id, $user->id)) {
299 if (! survey_update_analysis($survey->id, $user->id, $notes)) {
300 notify("An error occurred while saving your notes. Sorry.");
301 } else {
302 notify(get_string("savednotes", "survey"));
304 } else {
305 if (! survey_add_analysis($survey->id, $user->id, $notes)) {
306 notify("An error occurred while saving your notes. Sorry.");
307 } else {
308 notify(get_string("savednotes", "survey"));
313 echo "<p align=center>";
314 print_user_picture($user->id, $course->id, $user->picture, true);
315 echo "</p>";
317 $questions = get_records_list("survey_questions", "id", $survey->questions);
318 $questionorder = explode(",", $survey->questions);
320 if ($showscales) {
321 // Print overall summary
322 echo "<p align=center>";
323 survey_print_graph("id=$id&sid=$student&type=student.png");
324 echo "</p>";
326 // Print scales
328 foreach ($questionorder as $key => $val) {
329 $question = $questions[$val];
330 if ($question->type < 0) { // We have some virtual scales. Just show them.
331 $virtualscales = true;
332 break;
336 foreach ($questionorder as $key => $val) {
337 $question = $questions[$val];
338 if ($question->multi) {
339 if ($virtualscales && $question->type > 0) { // Don't show non-virtual scales if virtual
340 continue;
342 echo "<p align=center>";
343 echo "<a title=\"$strseemoredetail\" href=report.php?action=questions&id=$id&qid=$question->multi>";
344 survey_print_graph("id=$id&qid=$question->id&sid=$student&type=studentmultiquestion.png");
345 echo "</a></p><br>";
350 // Print non-scale questions
352 foreach ($questionorder as $key => $val) {
353 $question = $questions[$val];
354 if ($question->type == 0 or $question->type == 1) {
355 if ($answer = survey_get_user_answer($survey->id, $question->id, $user->id)) {
356 $table = NULL;
357 $table->head = array(get_string($question->text, "survey"));
358 $table->align = array ("left");
359 $table->data[] = array("$answer->answer1");
360 print_table($table);
361 print_spacer(30);
366 if ($rs = survey_get_analysis($survey->id, $user->id)) {
367 $notes = $rs->notes;
368 } else {
369 $notes = "";
371 echo "<hr noshade size=1>";
372 echo "<center>";
373 echo "<form action=report.php method=post name=form>";
374 echo "<h3>$strnotes:</h3>";
375 echo "<blockquote>";
376 echo "<textarea name=notes rows=10 cols=60>";
377 p($notes);
378 echo "</textarea><br>";
379 echo "<input type=hidden name=action value=student>";
380 echo "<input type=hidden name=student value=$student>";
381 echo "<input type=hidden name=id value=$cm->id>";
382 echo "<input type=submit value=\"".get_string("savechanges")."\">";
383 echo "</blockquote>";
384 echo "</form>";
385 echo "</center>";
388 break;
390 case "download":
391 print_heading($strdownload);
393 echo '<p align="center">'.get_string("downloadinfo", "survey").'</p>';
395 echo '<center>';
396 $options["id"] = "$cm->id";
397 $options["type"] = "xls";
398 $options["group"] = $currentgroup;
399 print_single_button("download.php", $options, get_string("downloadexcel", "survey"));
401 $options["type"] = "txt";
402 print_single_button("download.php", $options, get_string("downloadtext", "survey"));
403 echo '</center>';
405 break;
408 print_footer($course);