MDL-80880 quiz: change display of previous attempts summary
[moodle.git] / mod / survey / view.php
blob078530887b8a75f60f62ba7e1b1cf8dd8346089b
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * This file is responsible for displaying the survey
20 * @package mod_survey
21 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once("../../config.php");
26 require_once("lib.php");
28 $id = required_param('id', PARAM_INT); // Course Module ID.
30 if (! $cm = get_coursemodule_from_id('survey', $id)) {
31 throw new \moodle_exception('invalidcoursemodule');
34 $cm = cm_info::create($cm);
36 if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
37 throw new \moodle_exception('coursemisconf');
40 $PAGE->set_url('/mod/survey/view.php', array('id' => $id));
41 require_login($course, false, $cm);
42 $context = context_module::instance($cm->id);
44 require_capability('mod/survey:participate', $context);
46 if (! $survey = $DB->get_record("survey", array("id" => $cm->instance))) {
47 throw new \moodle_exception('invalidsurveyid', 'survey');
50 if (! $template = $DB->get_record("survey", array("id" => $survey->template))) {
51 throw new \moodle_exception('invalidtmptid', 'survey');
54 $showscales = ($template->name != 'ciqname');
56 // Check the survey hasn't already been filled out.
57 $surveyalreadydone = survey_already_done($survey->id, $USER->id);
58 if ($surveyalreadydone) {
59 // Trigger course_module_viewed event and completion.
60 survey_view($survey, $course, $cm, $context, 'graph');
61 } else {
62 survey_view($survey, $course, $cm, $context, 'form');
65 $strsurvey = get_string("modulename", "survey");
66 $PAGE->set_title($survey->name);
67 $PAGE->set_heading($course->fullname);
68 // No need to show the description if the survey is done and a graph page is to be shown.
69 if ($surveyalreadydone && $showscales) {
70 $PAGE->activityheader->set_description('');
71 } else {
72 // If the survey has empty description, display the default one.
73 $trimmedintro = trim($survey->intro);
74 if (empty($trimmedintro)) {
75 $tempo = $DB->get_field("survey", "intro", array("id" => $survey->template));
76 $PAGE->activityheader->set_description(get_string($tempo, "survey"));
79 $PAGE->add_body_class('limitedwidth');
81 echo $OUTPUT->header();
83 // Check to see if groups are being used in this survey.
84 if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used.
85 $currentgroup = groups_get_activity_group($cm);
86 } else {
87 $currentgroup = 0;
89 $groupingid = $cm->groupingid;
91 if (has_capability('mod/survey:readresponses', $context) or ($groupmode == VISIBLEGROUPS)) {
92 $currentgroup = 0;
95 if (!$cm->visible) {
96 notice(get_string("activityiscurrentlyhidden"));
99 if (!is_enrolled($context)) {
100 echo $OUTPUT->notification(get_string("guestsnotallowed", "survey"));
103 if ($surveyalreadydone) {
104 $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
105 if ($showscales) {
106 // Ensure that graph.php will allow the user to see the graph.
107 if (has_capability('mod/survey:readresponses', $context) || !$groupmode || groups_is_member($currentgroup)) {
109 echo $OUTPUT->box(get_string("surveycompleted", "survey"));
110 echo $OUTPUT->box(get_string("peoplecompleted", "survey", $numusers));
112 echo '<div class="resultgraph">';
113 survey_print_graph("id=$cm->id&amp;sid=$USER->id&amp;group=$currentgroup&amp;type=student.png");
114 echo '</div>';
115 } else {
116 echo $OUTPUT->box(get_string("surveycompletednograph", "survey"));
117 echo $OUTPUT->box(get_string("peoplecompleted", "survey", $numusers));
120 } else {
122 echo $OUTPUT->spacer(array('height' => 30, 'width' => 1), true); // Should be done with CSS instead.
124 $questions = survey_get_questions($survey);
125 foreach ($questions as $question) {
127 if ($question->type == 0 or $question->type == 1) {
128 if ($answer = survey_get_user_answer($survey->id, $question->id, $USER->id)) {
129 $table = new html_table();
130 $table->head = array(get_string($question->text, "survey"));
131 $table->align = array ("left");
132 $table->data[] = array(s($answer->answer1));// No html here, just plain text.
133 echo html_writer::table($table);
134 echo $OUTPUT->spacer(array('height' => 30, 'width' => 1), true);
140 echo $OUTPUT->footer();
141 exit;
144 echo "<form method=\"post\" action=\"save.php\" id=\"surveyform\">";
145 echo '<div>';
146 echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />";
147 echo "<input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />";
149 echo '<div>'. get_string('allquestionrequireanswer', 'survey'). '</div>';
151 // Get all the major questions in order.
152 $questions = survey_get_questions($survey);
154 global $qnum; // TODO: ugly globals hack for survey_print_*().
155 $qnum = 0;
156 foreach ($questions as $question) {
158 if ($question->type >= 0) {
160 $question = survey_translate_question($question);
162 if ($question->multi) {
163 survey_print_multi($question);
164 } else {
165 survey_print_single($question);
170 if (!is_enrolled($context)) {
171 echo '</div>';
172 echo "</form>";
173 echo $OUTPUT->footer();
174 exit;
177 $PAGE->requires->js_call_amd('mod_survey/validation', 'ensureRadiosChosen', array('surveyform'));
179 echo '<br />';
180 echo '<input type="submit" class="btn btn-primary" value="'. get_string("submit"). '" />';
181 echo '</div>';
182 echo "</form>";
184 echo $OUTPUT->footer();