adding current groupid to grade_export class - soon to be used in plugins
[moodle-pu.git] / mod / survey / report.php
blob8e7ceee832262e32401222cf11ec3afe9b79c0b6
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 $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 = groups_get_activity_groupmode($cm)) { // Groups are being used
80 $menuaction = $action == "student" ? "students" : $action;
81 $currentgroup = groups_get_activity_group($cm, true);
82 groups_print_activity_menu($cm, "report.php?id=$cm->id&amp;action=$menuaction&amp;qid=$qid");
83 } else {
84 $currentgroup = 0;
87 if ($currentgroup) {
88 $users = groups_get_members($currentgroup);
89 } else if (!empty($CFG->enablegroupings) && !empty($cm->groupingid)) {
90 $users = groups_get_grouping_members($cm->groupingid);
91 } else {
92 $users = get_course_users($course->id);
94 $groupingid = $cm->groupingid;
96 print_simple_box_start("center");
97 if ($showscales) {
98 echo "<a href=\"report.php?action=summary&amp;id=$id\">$strsummary</a>";
99 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=scales&amp;id=$id\">$strscales</a>";
100 echo "&nbsp;&nbsp;&nbsp;&nbsp;<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 = "summary";
108 } else {
109 echo "<a href=\"report.php?action=questions&amp;id=$id\">$strquestions</a>";
110 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=students&amp;id=$id\">$course->students</a>";
111 if (has_capability('mod/survey:download', $context)) {
112 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=download&amp;id=$id\">$strdownload</a>";
114 if (empty($action)) {
115 $action = "questions";
118 print_simple_box_end();
120 print_spacer(30,30);
123 /// Print the menu across the top
125 $virtualscales = false;
127 switch ($action) {
129 case "summary":
130 print_heading($strsummary);
132 if (survey_count_responses($survey->id, $currentgroup, $groupingid)) {
133 echo "<div class='reportsummary'><a href=\"report.php?action=scales&amp;id=$id\">";
134 survey_print_graph("id=$id&amp;group=$currentgroup&amp;type=overall.png");
135 echo "</a></div>";
136 } else {
137 notify(get_string("nobodyyet","survey"));
139 break;
141 case "scales":
142 print_heading($strscales);
144 if (! $results = survey_get_responses($survey->id, $currentgroup, $groupingid) ) {
145 notify(get_string("nobodyyet","survey"));
147 } else {
149 $questions = get_records_list("survey_questions", "id", $survey->questions);
150 $questionorder = explode(",", $survey->questions);
152 foreach ($questionorder as $key => $val) {
153 $question = $questions[$val];
154 if ($question->type < 0) { // We have some virtual scales. Just show them.
155 $virtualscales = true;
156 break;
160 foreach ($questionorder as $key => $val) {
161 $question = $questions[$val];
162 if ($question->multi) {
163 if (!empty($virtualscales) && $question->type > 0) { // Don't show non-virtual scales if virtual
164 continue;
166 echo "<p class=\"centerpara\"><a title=\"$strseemoredetail\" href=\"report.php?action=questions&amp;id=$id&amp;qid=$question->multi\">";
167 survey_print_graph("id=$id&amp;qid=$question->id&amp;group=$currentgroup&amp;type=multiquestion.png");
168 echo "</a></p><br />";
173 break;
175 case "questions":
177 if ($qid) { // just get one multi-question
178 $questions = get_records_list("survey_questions", "id", $qid);
179 $questionorder = explode(",", $qid);
181 if ($scale = get_records("survey_questions", "multi", "$qid")) {
182 $scale = array_pop($scale);
183 print_heading("$scale->text - $strselectedquestions");
184 } else {
185 print_heading($strselectedquestions);
188 } else { // get all top-level questions
189 $questions = get_records_list("survey_questions", "id", $survey->questions);
190 $questionorder = explode(",", $survey->questions);
192 print_heading($strallquestions);
195 if (! $results = survey_get_responses($survey->id, $currentgroup, $groupingid) ) {
196 notify(get_string("nobodyyet","survey"));
198 } else {
200 foreach ($questionorder as $key => $val) {
201 $question = $questions[$val];
202 if ($question->type < 0) { // We have some virtual scales. DON'T show them.
203 $virtualscales = true;
204 break;
208 foreach ($questionorder as $key => $val) {
209 $question = $questions[$val];
211 if ($question->type < 0) { // We have some virtual scales. DON'T show them.
212 continue;
214 $question->text = get_string($question->text, "survey");
216 if ($question->multi) {
217 echo "<h3>$question->text:</h3>";
219 $subquestions = get_records_list("survey_questions", "id", $question->multi);
220 $subquestionorder = explode(",", $question->multi);
221 foreach ($subquestionorder as $key => $val) {
222 $subquestion = $subquestions[$val];
223 if ($subquestion->type > 0) {
224 echo "<p class=\"centerpara\">";
225 echo "<a title=\"$strseemoredetail\" href=\"report.php?action=question&amp;id=$id&amp;qid=$subquestion->id\">";
226 survey_print_graph("id=$id&amp;qid=$subquestion->id&amp;group=$currentgroup&amp;type=question.png");
227 echo "</a></p>";
230 } else if ($question->type > 0 ) {
231 echo "<p class=\"centerpara\">";
232 echo "<a title=\"$strseemoredetail\" href=\"report.php?action=question&amp;id=$id&amp;qid=$question->id\">";
233 survey_print_graph("id=$id&amp;qid=$question->id&amp;group=$currentgroup&amp;type=question.png");
234 echo "</a></p>";
236 } else {
237 $table = NULL;
238 $table->head = array($question->text);
239 $table->align = array ("left");
241 $contents = '<table cellpadding="15" width="100%">';
243 if ($aaa = survey_get_user_answers($survey->id, $question->id, $currentgroup, "sa.time ASC")) {
244 foreach ($aaa as $a) {
245 $contents .= "<tr>";
246 $contents .= '<td class="fullnamecell">'.fullname($a).'</td>';
247 $contents .= '<td valign="top">'.$a->answer1.'</td>';
248 $contents .= "</tr>";
251 $contents .= "</table>";
253 $table->data[] = array($contents);
255 print_table($table);
256 print_spacer(30);
261 break;
263 case "question":
264 if (!$question = get_record("survey_questions", "id", $qid)) {
265 error("Question doesn't exist");
267 $question->text = get_string($question->text, "survey");
269 $answers = explode(",", get_string($question->options, "survey"));
271 print_heading("$strquestion: $question->text");
274 $strname = get_string("name", "survey");
275 $strtime = get_string("time", "survey");
276 $stractual = get_string("actual", "survey");
277 $strpreferred = get_string("preferred", "survey");
278 $strdateformat = get_string("strftimedatetime");
280 $table = NULL;
281 $table->head = array("", $strname, $strtime, $stractual, $strpreferred);
282 $table->align = array ("left", "left", "left", "left", "right");
283 $table->size = array (35, "", "", "", "");
285 if ($aaa = survey_get_user_answers($survey->id, $question->id, $currentgroup)) {
286 foreach ($aaa as $a) {
287 if ($a->answer1) {
288 $answer1 = "$a->answer1 - ".$answers[$a->answer1 - 1];
289 } else {
290 $answer1 = "&nbsp;";
292 if ($a->answer2) {
293 $answer2 = "$a->answer2 - ".$answers[$a->answer2 - 1];
294 } else {
295 $answer2 = "&nbsp;";
298 $table->data[] = array(
299 print_user_picture($a->userid, $course->id, $a->picture, false, true, true),
300 "<a href=\"report.php?id=$id&amp;action=student&amp;student=$a->userid\">".fullname($a)."</a>",
301 userdate($a->time),
302 $answer1, $answer2);
307 print_table($table);
309 break;
311 case "students":
313 print_heading(get_string("analysisof", "survey", "$course->students"));
315 if (! $results = survey_get_responses($survey->id, $currentgroup, $groupingid) ) {
316 notify(get_string("nobodyyet","survey"));
317 } else {
318 survey_print_all_responses($cm->id, $results, $course->id);
321 break;
323 case "student":
324 if (!$user = get_record("user", "id", $student)) {
325 error("Student doesn't exist");
328 print_heading(get_string("analysisof", "survey", fullname($user)));
330 if ($notes != '' and confirm_sesskey()) {
331 if (survey_get_analysis($survey->id, $user->id)) {
332 if (! survey_update_analysis($survey->id, $user->id, $notes)) {
333 notify("An error occurred while saving your notes. Sorry.");
334 } else {
335 notify(get_string("savednotes", "survey"));
337 } else {
338 if (! survey_add_analysis($survey->id, $user->id, $notes)) {
339 notify("An error occurred while saving your notes. Sorry.");
340 } else {
341 notify(get_string("savednotes", "survey"));
346 echo "<p <p class=\"centerpara\">";
347 print_user_picture($user->id, $course->id, $user->picture, true);
348 echo "</p>";
350 $questions = get_records_list("survey_questions", "id", $survey->questions);
351 $questionorder = explode(",", $survey->questions);
353 if ($showscales) {
354 // Print overall summary
355 echo "<p <p class=\"centerpara\">>";
356 survey_print_graph("id=$id&amp;sid=$student&amp;type=student.png");
357 echo "</p>";
359 // Print scales
361 foreach ($questionorder as $key => $val) {
362 $question = $questions[$val];
363 if ($question->type < 0) { // We have some virtual scales. Just show them.
364 $virtualscales = true;
365 break;
369 foreach ($questionorder as $key => $val) {
370 $question = $questions[$val];
371 if ($question->multi) {
372 if ($virtualscales && $question->type > 0) { // Don't show non-virtual scales if virtual
373 continue;
375 echo "<p class=\"centerpara\">";
376 echo "<a title=\"$strseemoredetail\" href=\"report.php?action=questions&amp;id=$id&amp;qid=$question->multi\">";
377 survey_print_graph("id=$id&amp;qid=$question->id&amp;sid=$student&amp;type=studentmultiquestion.png");
378 echo "</a></p><br />";
383 // Print non-scale questions
385 foreach ($questionorder as $key => $val) {
386 $question = $questions[$val];
387 if ($question->type == 0 or $question->type == 1) {
388 if ($answer = survey_get_user_answer($survey->id, $question->id, $user->id)) {
389 $table = NULL;
390 $table->head = array(get_string($question->text, "survey"));
391 $table->align = array ("left");
392 $table->data[] = array(s($answer->answer1)); // no html here, just plain text
393 print_table($table);
394 print_spacer(30);
399 if ($rs = survey_get_analysis($survey->id, $user->id)) {
400 $notes = $rs->notes;
401 } else {
402 $notes = "";
404 echo "<hr noshade=\"noshade\" size=\"1\" />";
405 echo "<div class='studentreport'>";
406 echo "<form action=\"report.php\" method=\"post\">";
407 echo "<h3>$strnotes:</h3>";
408 echo "<blockquote>";
409 echo "<textarea name=\"notes\" rows=\"10\" cols=\"60\">";
410 p($notes);
411 echo "</textarea><br />";
412 echo "<input type=\"hidden\" name=\"action\" value=\"student\" />";
413 echo "<input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />";
414 echo "<input type=\"hidden\" name=\"student\" value=\"$student\" />";
415 echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />";
416 echo "<input type=\"submit\" value=\"".get_string("savechanges")."\" />";
417 echo "</blockquote>";
418 echo "</form>";
419 echo "</div>";
422 break;
424 case "download":
425 print_heading($strdownload);
427 require_capability('mod/survey:download', $context);
429 echo '<p class="centerpara">'.get_string("downloadinfo", "survey").'</p>';
431 echo '<div class="reportbuttons">';
432 $optons = array();
433 $options["id"] = "$cm->id";
434 $options["group"] = $currentgroup;
436 $options["type"] = "ods";
437 print_single_button("download.php", $options, get_string("downloadods"));
439 $options["type"] = "xls";
440 print_single_button("download.php", $options, get_string("downloadexcel"));
442 $options["type"] = "txt";
443 print_single_button("download.php", $options, get_string("downloadtext"));
444 echo '</div>';
446 break;
449 print_footer($course);