on-demand release 3.7dev+
[moodle.git] / mod / survey / view.php
blob884feb6c8e1f9a3a1d7f3da7571426f24c24bcba
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 print_error('invalidcoursemodule');
34 if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
35 print_error('coursemisconf');
38 $PAGE->set_url('/mod/survey/view.php', array('id' => $id));
39 require_login($course, false, $cm);
40 $context = context_module::instance($cm->id);
42 require_capability('mod/survey:participate', $context);
44 if (! $survey = $DB->get_record("survey", array("id" => $cm->instance))) {
45 print_error('invalidsurveyid', 'survey');
47 $trimmedintro = trim($survey->intro);
48 if (empty($trimmedintro)) {
49 $tempo = $DB->get_field("survey", "intro", array("id" => $survey->template));
50 $survey->intro = get_string($tempo, "survey");
53 if (! $template = $DB->get_record("survey", array("id" => $survey->template))) {
54 print_error('invalidtmptid', 'survey');
57 $showscales = ($template->name != 'ciqname');
59 // Check the survey hasn't already been filled out.
60 $surveyalreadydone = survey_already_done($survey->id, $USER->id);
61 if ($surveyalreadydone) {
62 // Trigger course_module_viewed event and completion.
63 survey_view($survey, $course, $cm, $context, 'graph');
64 } else {
65 survey_view($survey, $course, $cm, $context, 'form');
68 $strsurvey = get_string("modulename", "survey");
69 $PAGE->set_title($survey->name);
70 $PAGE->set_heading($course->fullname);
71 echo $OUTPUT->header();
72 echo $OUTPUT->heading($survey->name);
74 // Check to see if groups are being used in this survey.
75 if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used.
76 $currentgroup = groups_get_activity_group($cm);
77 } else {
78 $currentgroup = 0;
80 $groupingid = $cm->groupingid;
82 if (has_capability('mod/survey:readresponses', $context) or ($groupmode == VISIBLEGROUPS)) {
83 $currentgroup = 0;
86 if (has_capability('mod/survey:readresponses', $context)) {
87 $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
88 echo "<div class=\"reportlink\"><a href=\"report.php?id=$cm->id\">".
89 get_string("viewsurveyresponses", "survey", $numusers)."</a></div>";
90 } else if (!$cm->visible) {
91 notice(get_string("activityiscurrentlyhidden"));
94 if (!is_enrolled($context)) {
95 echo $OUTPUT->notification(get_string("guestsnotallowed", "survey"));
98 if ($surveyalreadydone) {
100 $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
102 if ($showscales) {
103 // Ensure that graph.php will allow the user to see the graph.
104 if (has_capability('mod/survey:readresponses', $context) || !$groupmode || groups_is_member($currentgroup)) {
106 echo $OUTPUT->box(get_string("surveycompleted", "survey"));
107 echo $OUTPUT->box(get_string("peoplecompleted", "survey", $numusers));
109 echo '<div class="resultgraph">';
110 survey_print_graph("id=$cm->id&amp;sid=$USER->id&amp;group=$currentgroup&amp;type=student.png");
111 echo '</div>';
112 } else {
113 echo $OUTPUT->box(get_string("surveycompletednograph", "survey"));
114 echo $OUTPUT->box(get_string("peoplecompleted", "survey", $numusers));
117 } else {
119 echo $OUTPUT->box(format_module_intro('survey', $survey, $cm->id), 'generalbox', 'intro');
120 echo $OUTPUT->spacer(array('height' => 30, 'width' => 1), true); // Should be done with CSS instead.
122 $questions = survey_get_questions($survey);
123 foreach ($questions as $question) {
125 if ($question->type == 0 or $question->type == 1) {
126 if ($answer = survey_get_user_answer($survey->id, $question->id, $USER->id)) {
127 $table = new html_table();
128 $table->head = array(get_string($question->text, "survey"));
129 $table->align = array ("left");
130 $table->data[] = array(s($answer->answer1));// No html here, just plain text.
131 echo html_writer::table($table);
132 echo $OUTPUT->spacer(array('height' => 30, 'width' => 1), true);
138 echo $OUTPUT->footer();
139 exit;
142 echo "<form method=\"post\" action=\"save.php\" id=\"surveyform\">";
143 echo '<div>';
144 echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />";
145 echo "<input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />";
147 echo $OUTPUT->box(format_module_intro('survey', $survey, $cm->id), 'generalbox boxaligncenter bowidthnormal', 'intro');
148 echo '<div>'. get_string('allquestionrequireanswer', 'survey'). '</div>';
150 // Get all the major questions in order.
151 $questions = survey_get_questions($survey);
153 global $qnum; // TODO: ugly globals hack for survey_print_*().
154 $qnum = 0;
155 foreach ($questions as $question) {
157 if ($question->type >= 0) {
159 $question = survey_translate_question($question);
161 if ($question->multi) {
162 survey_print_multi($question);
163 } else {
164 survey_print_single($question);
169 if (!is_enrolled($context)) {
170 echo '</div>';
171 echo "</form>";
172 echo $OUTPUT->footer();
173 exit;
176 $PAGE->requires->js_call_amd('mod_survey/validation', 'ensureRadiosChosen', array('surveyform'));
178 echo '<br />';
179 echo '<input type="submit" class="btn btn-primary" value="'.get_string("clicktocontinue", "survey").'" />';
180 echo '</div>';
181 echo "</form>";
183 echo $OUTPUT->footer();