Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / mod / survey / report.php
blob35d5283850cf99d135f59fd7952a07f8db6a57b1
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 $navlinks = array();
69 $navlinks[] = array('name' => $strsurveys, 'link' => "index.php?id=$course->id", 'type' => 'activity');
70 $navlinks[] = array('name' => format_string($survey->name), 'link' => "view.php?id=$cm->id", 'type' => 'activityinstance');
71 $navlinks[] = array('name' => $strreport, 'link' => '', 'type' => 'title');
72 $navigation = build_navigation($navlinks);
74 print_header("$course->shortname: ".format_string($survey->name), $course->fullname, $navigation,
75 "", "", true,
76 update_module_button($cm->id, $course->id, $strsurvey), navmenu($course, $cm));
78 /// Check to see if groups are being used in this survey
79 if ($groupmode = groupmode($course, $cm)) { // Groups are being used
80 $menuaction = $action == "student" ? "students" : $action;
81 $currentgroup = setup_and_print_groups($course, $groupmode,
82 "report.php?id=$cm->id&amp;action=$menuaction&amp;qid=$qid");
83 } else {
84 $currentgroup = 0;
87 if ($currentgroup) {
88 $users = get_group_users($currentgroup);
89 } else {
90 $users = get_course_users($course->id);
93 print_simple_box_start("center");
94 if ($showscales) {
95 echo "<a href=\"report.php?action=summary&amp;id=$id\">$strsummary</a>";
96 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=scales&amp;id=$id\">$strscales</a>";
97 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=questions&amp;id=$id\">$strquestions</a>";
98 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=students&amp;id=$id\">$course->students</a>";
99 if (has_capability('mod/survey:download', $context)) {
100 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=download&amp;id=$id\">$strdownload</a>";
102 if (empty($action)) {
103 $action = "summary";
105 } else {
106 echo "<a href=\"report.php?action=questions&amp;id=$id\">$strquestions</a>";
107 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=students&amp;id=$id\">$course->students</a>";
108 if (has_capability('mod/survey:download', $context)) {
109 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=download&amp;id=$id\">$strdownload</a>";
111 if (empty($action)) {
112 $action = "questions";
115 print_simple_box_end();
117 print_spacer(30,30);
120 /// Print the menu across the top
122 $virtualscales = false;
124 switch ($action) {
126 case "summary":
127 print_heading($strsummary);
129 if (survey_count_responses($survey->id, $currentgroup)) {
130 echo "<div class='reportsummary'><a href=\"report.php?action=scales&amp;id=$id\">";
131 survey_print_graph("id=$id&amp;group=$currentgroup&amp;type=overall.png");
132 echo "</a></div>";
133 } else {
134 notify(get_string("nobodyyet","survey"));
136 break;
138 case "scales":
139 print_heading($strscales);
141 if (! $results = survey_get_responses($survey->id, $currentgroup) ) {
142 notify(get_string("nobodyyet","survey"));
144 } else {
146 $questions = get_records_list("survey_questions", "id", $survey->questions);
147 $questionorder = explode(",", $survey->questions);
149 foreach ($questionorder as $key => $val) {
150 $question = $questions[$val];
151 if ($question->type < 0) { // We have some virtual scales. Just show them.
152 $virtualscales = true;
153 break;
157 foreach ($questionorder as $key => $val) {
158 $question = $questions[$val];
159 if ($question->multi) {
160 if (!empty($virtualscales) && $question->type > 0) { // Don't show non-virtual scales if virtual
161 continue;
163 echo "<p class=\"centerpara\"><a title=\"$strseemoredetail\" href=\"report.php?action=questions&amp;id=$id&amp;qid=$question->multi\">";
164 survey_print_graph("id=$id&amp;qid=$question->id&amp;group=$currentgroup&amp;type=multiquestion.png");
165 echo "</a></p><br />";
170 break;
172 case "questions":
174 if ($qid) { // just get one multi-question
175 $questions = get_records_list("survey_questions", "id", $qid);
176 $questionorder = explode(",", $qid);
178 if ($scale = get_records("survey_questions", "multi", "$qid")) {
179 $scale = array_pop($scale);
180 print_heading("$scale->text - $strselectedquestions");
181 } else {
182 print_heading($strselectedquestions);
185 } else { // get all top-level questions
186 $questions = get_records_list("survey_questions", "id", $survey->questions);
187 $questionorder = explode(",", $survey->questions);
189 print_heading($strallquestions);
192 if (! $results = survey_get_responses($survey->id, $currentgroup) ) {
193 notify(get_string("nobodyyet","survey"));
195 } else {
197 foreach ($questionorder as $key => $val) {
198 $question = $questions[$val];
199 if ($question->type < 0) { // We have some virtual scales. DON'T show them.
200 $virtualscales = true;
201 break;
205 foreach ($questionorder as $key => $val) {
206 $question = $questions[$val];
208 if ($question->type < 0) { // We have some virtual scales. DON'T show them.
209 continue;
211 $question->text = get_string($question->text, "survey");
213 if ($question->multi) {
214 echo "<h3>$question->text:</h3>";
216 $subquestions = get_records_list("survey_questions", "id", $question->multi);
217 $subquestionorder = explode(",", $question->multi);
218 foreach ($subquestionorder as $key => $val) {
219 $subquestion = $subquestions[$val];
220 if ($subquestion->type > 0) {
221 echo "<p class=\"centerpara\">";
222 echo "<a title=\"$strseemoredetail\" href=\"report.php?action=question&amp;id=$id&amp;qid=$subquestion->id\">";
223 survey_print_graph("id=$id&amp;qid=$subquestion->id&amp;group=$currentgroup&amp;type=question.png");
224 echo "</a></p>";
227 } else if ($question->type > 0 ) {
228 echo "<p class=\"centerpara\">";
229 echo "<a title=\"$strseemoredetail\" href=\"report.php?action=question&amp;id=$id&amp;qid=$question->id\">";
230 survey_print_graph("id=$id&amp;qid=$question->id&amp;group=$currentgroup&amp;type=question.png");
231 echo "</a></p>";
233 } else {
234 $table = NULL;
235 $table->head = array($question->text);
236 $table->align = array ("left");
238 $contents = '<table cellpadding="15" width="100%">';
240 if ($aaa = survey_get_user_answers($survey->id, $question->id, $currentgroup, "sa.time ASC")) {
241 foreach ($aaa as $a) {
242 $contents .= "<tr>";
243 $contents .= '<td class="fullnamecell">'.fullname($a).'</td>';
244 $contents .= '<td valign="top">'.$a->answer1.'</td>';
245 $contents .= "</tr>";
248 $contents .= "</table>";
250 $table->data[] = array($contents);
252 print_table($table);
253 print_spacer(30);
258 break;
260 case "question":
261 if (!$question = get_record("survey_questions", "id", $qid)) {
262 error("Question doesn't exist");
264 $question->text = get_string($question->text, "survey");
266 $answers = explode(",", get_string($question->options, "survey"));
268 print_heading("$strquestion: $question->text");
271 $strname = get_string("name", "survey");
272 $strtime = get_string("time", "survey");
273 $stractual = get_string("actual", "survey");
274 $strpreferred = get_string("preferred", "survey");
275 $strdateformat = get_string("strftimedatetime");
277 $table = NULL;
278 $table->head = array("", $strname, $strtime, $stractual, $strpreferred);
279 $table->align = array ("left", "left", "left", "left", "right");
280 $table->size = array (35, "", "", "", "");
282 if ($aaa = survey_get_user_answers($survey->id, $question->id, $currentgroup)) {
283 foreach ($aaa as $a) {
284 if ($a->answer1) {
285 $answer1 = "$a->answer1 - ".$answers[$a->answer1 - 1];
286 } else {
287 $answer1 = "&nbsp;";
289 if ($a->answer2) {
290 $answer2 = "$a->answer2 - ".$answers[$a->answer2 - 1];
291 } else {
292 $answer2 = "&nbsp;";
295 $table->data[] = array(
296 print_user_picture($a->userid, $course->id, $a->picture, false, true, true),
297 "<a href=\"report.php?id=$id&amp;action=student&amp;student=$a->userid\">".fullname($a)."</a>",
298 userdate($a->time),
299 $answer1, $answer2);
304 print_table($table);
306 break;
308 case "students":
310 print_heading(get_string("analysisof", "survey", "$course->students"));
312 if (! $results = survey_get_responses($survey->id, $currentgroup) ) {
313 notify(get_string("nobodyyet","survey"));
314 } else {
315 survey_print_all_responses($cm->id, $results, $course->id);
318 break;
320 case "student":
321 if (!$user = get_record("user", "id", $student)) {
322 error("Student doesn't exist");
325 print_heading(get_string("analysisof", "survey", fullname($user)));
327 if ($notes != '' and confirm_sesskey()) {
328 if (survey_get_analysis($survey->id, $user->id)) {
329 if (! survey_update_analysis($survey->id, $user->id, $notes)) {
330 notify("An error occurred while saving your notes. Sorry.");
331 } else {
332 notify(get_string("savednotes", "survey"));
334 } else {
335 if (! survey_add_analysis($survey->id, $user->id, $notes)) {
336 notify("An error occurred while saving your notes. Sorry.");
337 } else {
338 notify(get_string("savednotes", "survey"));
343 echo "<p <p class=\"centerpara\">";
344 print_user_picture($user->id, $course->id, $user->picture, true);
345 echo "</p>";
347 $questions = get_records_list("survey_questions", "id", $survey->questions);
348 $questionorder = explode(",", $survey->questions);
350 if ($showscales) {
351 // Print overall summary
352 echo "<p <p class=\"centerpara\">>";
353 survey_print_graph("id=$id&amp;sid=$student&amp;type=student.png");
354 echo "</p>";
356 // Print scales
358 foreach ($questionorder as $key => $val) {
359 $question = $questions[$val];
360 if ($question->type < 0) { // We have some virtual scales. Just show them.
361 $virtualscales = true;
362 break;
366 foreach ($questionorder as $key => $val) {
367 $question = $questions[$val];
368 if ($question->multi) {
369 if ($virtualscales && $question->type > 0) { // Don't show non-virtual scales if virtual
370 continue;
372 echo "<p class=\"centerpara\">";
373 echo "<a title=\"$strseemoredetail\" href=\"report.php?action=questions&amp;id=$id&amp;qid=$question->multi\">";
374 survey_print_graph("id=$id&amp;qid=$question->id&amp;sid=$student&amp;type=studentmultiquestion.png");
375 echo "</a></p><br />";
380 // Print non-scale questions
382 foreach ($questionorder as $key => $val) {
383 $question = $questions[$val];
384 if ($question->type == 0 or $question->type == 1) {
385 if ($answer = survey_get_user_answer($survey->id, $question->id, $user->id)) {
386 $table = NULL;
387 $table->head = array(get_string($question->text, "survey"));
388 $table->align = array ("left");
389 $table->data[] = array(s($answer->answer1)); // no html here, just plain text
390 print_table($table);
391 print_spacer(30);
396 if ($rs = survey_get_analysis($survey->id, $user->id)) {
397 $notes = $rs->notes;
398 } else {
399 $notes = "";
401 echo "<hr noshade=\"noshade\" size=\"1\" />";
402 echo "<div class='studentreport'>";
403 echo "<form action=\"report.php\" method=\"post\">";
404 echo "<h3>$strnotes:</h3>";
405 echo "<blockquote>";
406 echo "<textarea name=\"notes\" rows=\"10\" cols=\"60\">";
407 p($notes);
408 echo "</textarea><br />";
409 echo "<input type=\"hidden\" name=\"action\" value=\"student\" />";
410 echo "<input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />";
411 echo "<input type=\"hidden\" name=\"student\" value=\"$student\" />";
412 echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />";
413 echo "<input type=\"submit\" value=\"".get_string("savechanges")."\" />";
414 echo "</blockquote>";
415 echo "</form>";
416 echo "</div>";
419 break;
421 case "download":
422 print_heading($strdownload);
424 require_capability('mod/survey:download', $context);
426 echo '<p class="centerpara">'.get_string("downloadinfo", "survey").'</p>';
428 echo '<div class="reportbuttons">';
429 $optons = array();
430 $options["id"] = "$cm->id";
431 $options["group"] = $currentgroup;
433 $options["type"] = "ods";
434 print_single_button("download.php", $options, get_string("downloadods"));
436 $options["type"] = "xls";
437 print_single_button("download.php", $options, get_string("downloadexcel"));
439 $options["type"] = "txt";
440 print_single_button("download.php", $options, get_string("downloadtext"));
441 echo '</div>';
443 break;
446 print_footer($course);