3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * This file is responsible for producing the survey reports
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once("../../config.php");
27 require_once("lib.php");
29 // Check that all the parameters have been provided.
31 $id = required_param('id', PARAM_INT
); // Course Module ID
32 $action = optional_param('action', '', PARAM_ALPHA
); // What to look at
33 $qid = optional_param('qid', 0, PARAM_RAW
); // Question IDs comma-separated list
34 $student = optional_param('student', 0, PARAM_INT
); // Student ID
35 $notes = optional_param('notes', '', PARAM_RAW
); // Save teachers notes
37 $qids = explode(',', $qid);
38 $qids = clean_param_array($qids, PARAM_INT
);
39 $qid = implode (',', $qids);
41 if (! $cm = get_coursemodule_from_id('survey', $id)) {
42 print_error('invalidcoursemodule');
45 if (! $course = $DB->get_record("course", array("id"=>$cm->course
))) {
46 print_error('coursemisconf');
49 $url = new moodle_url('/mod/survey/report.php', array('id'=>$id));
51 $url->param('action', $action);
54 $url->param('qid', $qid);
57 $url->param('student', $student);
60 $url->param('notes', $notes);
64 require_login($course, false, $cm);
66 $context = context_module
::instance($cm->id
);
68 require_capability('mod/survey:readresponses', $context);
70 if (! $survey = $DB->get_record("survey", array("id"=>$cm->instance
))) {
71 print_error('invalidsurveyid', 'survey');
74 if (! $template = $DB->get_record("survey", array("id"=>$survey->template
))) {
75 print_error('invalidtmptid', 'survey');
78 $showscales = ($template->name
!= 'ciqname');
81 $strreport = get_string("report", "survey");
82 $strsurvey = get_string("modulename", "survey");
83 $strsurveys = get_string("modulenameplural", "survey");
84 $strsummary = get_string("summary", "survey");
85 $strscales = get_string("scales", "survey");
86 $strquestion = get_string("question", "survey");
87 $strquestions = get_string("questions", "survey");
88 $strdownload = get_string("download", "survey");
89 $strallscales = get_string("allscales", "survey");
90 $strallquestions = get_string("allquestions", "survey");
91 $strselectedquestions = get_string("selectedquestions", "survey");
92 $strseemoredetail = get_string("seemoredetail", "survey");
93 $strnotes = get_string("notes", "survey");
95 add_to_log($course->id
, "survey", "view report", "report.php?id=$cm->id", "$survey->id", $cm->id
);
99 $PAGE->navbar
->add(get_string('downloadresults', 'survey'));
104 $PAGE->navbar
->add($strreport);
105 $PAGE->navbar
->add($
{'str'.$action});
108 $PAGE->navbar
->add($strreport);
109 $PAGE->navbar
->add(get_string('participants'));
112 $PAGE->navbar
->add($strreport);
113 $PAGE->navbar
->add($strsummary);
116 $PAGE->navbar
->add($strreport);
120 $PAGE->set_title("$course->shortname: ".format_string($survey->name
));
121 $PAGE->set_heading($course->fullname
);
122 echo $OUTPUT->header();
124 /// Check to see if groups are being used in this survey
125 if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used
126 $menuaction = $action == "student" ?
"students" : $action;
127 $currentgroup = groups_get_activity_group($cm, true);
128 groups_print_activity_menu($cm, $CFG->wwwroot
. "/mod/survey/report.php?id=$cm->id&action=$menuaction&qid=$qid");
134 $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', $currentgroup, null, false);
135 } else if (!empty($cm->groupingid
)) {
136 $groups = groups_get_all_groups($courseid, 0, $cm->groupingid
);
137 $groups = array_keys($groups);
138 $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', $groups, null, false);
140 $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', '', null, false);
144 $groupingid = $cm->groupingid
;
146 echo $OUTPUT->box_start("generalbox boxaligncenter");
148 echo "<a href=\"report.php?action=summary&id=$id\">$strsummary</a>";
149 echo " <a href=\"report.php?action=scales&id=$id\">$strscales</a>";
150 echo " <a href=\"report.php?action=questions&id=$id\">$strquestions</a>";
151 echo " <a href=\"report.php?action=students&id=$id\">".get_string('participants')."</a>";
152 if (has_capability('mod/survey:download', $context)) {
153 echo " <a href=\"report.php?action=download&id=$id\">$strdownload</a>";
155 if (empty($action)) {
159 echo "<a href=\"report.php?action=questions&id=$id\">$strquestions</a>";
160 echo " <a href=\"report.php?action=students&id=$id\">".get_string('participants')."</a>";
161 if (has_capability('mod/survey:download', $context)) {
162 echo " <a href=\"report.php?action=download&id=$id\">$strdownload</a>";
164 if (empty($action)) {
165 $action = "questions";
168 echo $OUTPUT->box_end();
170 echo $OUTPUT->spacer(array('height'=>30, 'width'=>30, 'br'=>true)); // should be done with CSS instead
173 /// Print the menu across the top
175 $virtualscales = false;
180 echo $OUTPUT->heading($strsummary);
182 if (survey_count_responses($survey->id
, $currentgroup, $groupingid)) {
183 echo "<div class='reportsummary'><a href=\"report.php?action=scales&id=$id\">";
184 survey_print_graph("id=$id&group=$currentgroup&type=overall.png");
187 echo $OUTPUT->notification(get_string("nobodyyet","survey"));
192 echo $OUTPUT->heading($strscales);
194 if (! $results = survey_get_responses($survey->id
, $currentgroup, $groupingid) ) {
195 echo $OUTPUT->notification(get_string("nobodyyet","survey"));
199 $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions
));
200 $questionorder = explode(",", $survey->questions
);
202 foreach ($questionorder as $key => $val) {
203 $question = $questions[$val];
204 if ($question->type
< 0) { // We have some virtual scales. Just show them.
205 $virtualscales = true;
210 foreach ($questionorder as $key => $val) {
211 $question = $questions[$val];
212 if ($question->multi
) {
213 if (!empty($virtualscales) && $question->type
> 0) { // Don't show non-virtual scales if virtual
216 echo "<p class=\"centerpara\"><a title=\"$strseemoredetail\" href=\"report.php?action=questions&id=$id&qid=$question->multi\">";
217 survey_print_graph("id=$id&qid=$question->id&group=$currentgroup&type=multiquestion.png");
218 echo "</a></p><br />";
227 if ($qid) { // just get one multi-question
228 $questions = $DB->get_records_select("survey_questions", "id in ($qid)");
229 $questionorder = explode(",", $qid);
231 if ($scale = $DB->get_records("survey_questions", array("multi"=>$qid))) {
232 $scale = array_pop($scale);
233 echo $OUTPUT->heading("$scale->text - $strselectedquestions");
235 echo $OUTPUT->heading($strselectedquestions);
238 } else { // get all top-level questions
239 $questions = $DB->get_records_list("survey_questions", "id", explode(',',$survey->questions
));
240 $questionorder = explode(",", $survey->questions
);
242 echo $OUTPUT->heading($strallquestions);
245 if (! $results = survey_get_responses($survey->id
, $currentgroup, $groupingid) ) {
246 echo $OUTPUT->notification(get_string("nobodyyet","survey"));
250 foreach ($questionorder as $key => $val) {
251 $question = $questions[$val];
252 if ($question->type
< 0) { // We have some virtual scales. DON'T show them.
253 $virtualscales = true;
258 foreach ($questionorder as $key => $val) {
259 $question = $questions[$val];
261 if ($question->type
< 0) { // We have some virtual scales. DON'T show them.
264 $question->text
= get_string($question->text
, "survey");
266 if ($question->multi
) {
267 echo "<h3>$question->text:</h3>";
269 $subquestions = $DB->get_records_list("survey_questions", "id", explode(',', $question->multi
));
270 $subquestionorder = explode(",", $question->multi
);
271 foreach ($subquestionorder as $key => $val) {
272 $subquestion = $subquestions[$val];
273 if ($subquestion->type
> 0) {
274 echo "<p class=\"centerpara\">";
275 echo "<a title=\"$strseemoredetail\" href=\"report.php?action=question&id=$id&qid=$subquestion->id\">";
276 survey_print_graph("id=$id&qid=$subquestion->id&group=$currentgroup&type=question.png");
280 } else if ($question->type
> 0 ) {
281 echo "<p class=\"centerpara\">";
282 echo "<a title=\"$strseemoredetail\" href=\"report.php?action=question&id=$id&qid=$question->id\">";
283 survey_print_graph("id=$id&qid=$question->id&group=$currentgroup&type=question.png");
287 $table = new html_table();
288 $table->head
= array($question->text
);
289 $table->align
= array ("left");
291 $contents = '<table cellpadding="15" width="100%">';
293 if ($aaa = survey_get_user_answers($survey->id
, $question->id
, $currentgroup, "sa.time ASC")) {
294 foreach ($aaa as $a) {
296 $contents .= '<td class="fullnamecell">'.fullname($a).'</td>';
297 $contents .= '<td valign="top">'.$a->answer1
.'</td>';
298 $contents .= "</tr>";
301 $contents .= "</table>";
303 $table->data
[] = array($contents);
305 echo html_writer
::table($table);
307 echo $OUTPUT->spacer(array('height'=>30)); // should be done with CSS instead
315 if (!$question = $DB->get_record("survey_questions", array("id"=>$qid))) {
316 print_error('cannotfindquestion', 'survey');
318 $question->text
= get_string($question->text
, "survey");
320 $answers = explode(",", get_string($question->options
, "survey"));
322 echo $OUTPUT->heading("$strquestion: $question->text");
325 $strname = get_string("name", "survey");
326 $strtime = get_string("time", "survey");
327 $stractual = get_string("actual", "survey");
328 $strpreferred = get_string("preferred", "survey");
329 $strdateformat = get_string("strftimedatetime");
331 $table = new html_table();
332 $table->head
= array("", $strname, $strtime, $stractual, $strpreferred);
333 $table->align
= array ("left", "left", "left", "left", "right");
334 $table->size
= array (35, "", "", "", "");
336 if ($aaa = survey_get_user_answers($survey->id
, $question->id
, $currentgroup)) {
337 foreach ($aaa as $a) {
339 $answer1 = "$a->answer1 - ".$answers[$a->answer1
- 1];
344 $answer2 = "$a->answer2 - ".$answers[$a->answer2
- 1];
348 $table->data
[] = array(
349 $OUTPUT->user_picture($a, array('courseid'=>$course->id
)),
350 "<a href=\"report.php?id=$id&action=student&student=$a->userid\">".fullname($a)."</a>",
357 echo html_writer
::table($table);
363 echo $OUTPUT->heading(get_string("analysisof", "survey", get_string('participants')));
365 if (! $results = survey_get_responses($survey->id
, $currentgroup, $groupingid) ) {
366 echo $OUTPUT->notification(get_string("nobodyyet","survey"));
368 survey_print_all_responses($cm->id
, $results, $course->id
);
374 if (!$user = $DB->get_record("user", array("id"=>$student))) {
375 print_error('invaliduserid');
378 echo $OUTPUT->heading(get_string("analysisof", "survey", fullname($user)));
380 if ($notes != '' and confirm_sesskey()) {
381 if (survey_get_analysis($survey->id
, $user->id
)) {
382 if (! survey_update_analysis($survey->id
, $user->id
, $notes)) {
383 echo $OUTPUT->notification("An error occurred while saving your notes. Sorry.");
385 echo $OUTPUT->notification(get_string("savednotes", "survey"));
388 if (! survey_add_analysis($survey->id
, $user->id
, $notes)) {
389 echo $OUTPUT->notification("An error occurred while saving your notes. Sorry.");
391 echo $OUTPUT->notification(get_string("savednotes", "survey"));
396 echo "<p <p class=\"centerpara\">";
397 echo $OUTPUT->user_picture($user, array('courseid'=>$course->id
));
400 $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions
));
401 $questionorder = explode(",", $survey->questions
);
404 // Print overall summary
405 echo "<p <p class=\"centerpara\">>";
406 survey_print_graph("id=$id&sid=$student&type=student.png");
411 foreach ($questionorder as $key => $val) {
412 $question = $questions[$val];
413 if ($question->type
< 0) { // We have some virtual scales. Just show them.
414 $virtualscales = true;
419 foreach ($questionorder as $key => $val) {
420 $question = $questions[$val];
421 if ($question->multi
) {
422 if ($virtualscales && $question->type
> 0) { // Don't show non-virtual scales if virtual
425 echo "<p class=\"centerpara\">";
426 echo "<a title=\"$strseemoredetail\" href=\"report.php?action=questions&id=$id&qid=$question->multi\">";
427 survey_print_graph("id=$id&qid=$question->id&sid=$student&type=studentmultiquestion.png");
428 echo "</a></p><br />";
433 // Print non-scale questions
435 foreach ($questionorder as $key => $val) {
436 $question = $questions[$val];
437 if ($question->type
== 0 or $question->type
== 1) {
438 if ($answer = survey_get_user_answer($survey->id
, $question->id
, $user->id
)) {
439 $table = new html_table();
440 $table->head
= array(get_string($question->text
, "survey"));
441 $table->align
= array ("left");
442 $table->data
[] = array(s($answer->answer1
)); // no html here, just plain text
443 echo html_writer
::table($table);
444 echo $OUTPUT->spacer(array('height'=>30));
449 if ($rs = survey_get_analysis($survey->id
, $user->id
)) {
454 echo "<hr noshade=\"noshade\" size=\"1\" />";
455 echo "<div class='studentreport'>";
456 echo "<form action=\"report.php\" method=\"post\">";
457 echo "<h3>$strnotes:</h3>";
459 echo "<textarea name=\"notes\" rows=\"10\" cols=\"60\">";
461 echo "</textarea><br />";
462 echo "<input type=\"hidden\" name=\"action\" value=\"student\" />";
463 echo "<input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />";
464 echo "<input type=\"hidden\" name=\"student\" value=\"$student\" />";
465 echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />";
466 echo "<input type=\"submit\" value=\"".get_string("savechanges")."\" />";
467 echo "</blockquote>";
475 echo $OUTPUT->heading($strdownload);
477 require_capability('mod/survey:download', $context);
479 echo '<p class="centerpara">'.get_string("downloadinfo", "survey").'</p>';
481 echo $OUTPUT->container_start('reportbuttons');
483 $options["id"] = "$cm->id";
484 $options["group"] = $currentgroup;
486 $options["type"] = "ods";
487 echo $OUTPUT->single_button(new moodle_url("download.php", $options), get_string("downloadods"));
489 $options["type"] = "xls";
490 echo $OUTPUT->single_button(new moodle_url("download.php", $options), get_string("downloadexcel"));
492 $options["type"] = "txt";
493 echo $OUTPUT->single_button(new moodle_url("download.php", $options), get_string("downloadtext"));
494 echo $OUTPUT->container_end();
499 echo $OUTPUT->footer();