whitespace
[moodle.git] / mod / survey / view.php
CommitLineData
a5cb6242 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
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.
9//
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.
14//
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/>.
17
18/**
19 * This file is responsible for displaying the survey
20 *
21 * @package mod-survey
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
f9903ed0 25
b0e3a925 26 require_once("../../config.php");
27 require_once("lib.php");
f9903ed0 28
00592b80 29 $id = required_param('id', PARAM_INT); // Course Module ID
f9903ed0 30
f9d5371b 31 if (! $cm = get_coursemodule_from_id('survey', $id)) {
83b10e57 32 print_error('invalidcoursemodule');
f9903ed0 33 }
34
deb3a60d 35 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
83b10e57 36 print_error('coursemisconf');
f9903ed0 37 }
38
a6855934 39 $PAGE->set_url('/mod/survey/view.php', array('id'=>$id));
ec81373f 40 require_login($course->id, false, $cm);
bbbf2d40 41 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
0468976c 42
43 require_capability('mod/survey:participate', $context);
f9903ed0 44
deb3a60d 45 if (! $survey = $DB->get_record("survey", array("id"=>$cm->instance))) {
83b10e57 46 print_error('invalidsurveyid', 'survey');
f9903ed0 47 }
e0ca19a1 48 $trimmedintro = trim($survey->intro);
49 if (empty($trimmedintro)) {
deb3a60d 50 $tempo = $DB->get_field("survey", "intro", array("id"=>$survey->template));
4d7a4019 51 $survey->intro = get_string($tempo, "survey");
52 }
7c8c335f 53
deb3a60d 54 if (! $template = $DB->get_record("survey", array("id"=>$survey->template))) {
83b10e57 55 print_error('invalidtmptid', 'survey');
7c8c335f 56 }
57
58 $showscales = ($template->name != 'ciqname');
98899fc0 59
bfbef967 60 $strsurvey = get_string("modulename", "survey");
207e24b8 61 $PAGE->set_title(format_string($survey->name));
5617e8c9 62 $PAGE->set_heading($course->fullname);
207e24b8 63 echo $OUTPUT->header();
f9903ed0 64
a9ccbf60 65/// Check to see if groups are being used in this survey
07aa2057 66 if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used
67 $currentgroup = groups_get_activity_group($cm);
a9ccbf60 68 } else {
69 $currentgroup = 0;
70 }
07aa2057 71 $groupingid = $cm->groupingid;
8411d0aa 72
73 if (has_capability('mod/survey:readresponses', $context) or ($groupmode == VISIBLEGROUPS)) {
bee7ee38 74 $currentgroup = 0;
a9ccbf60 75 }
8411d0aa 76
0468976c 77 if (has_capability('mod/survey:readresponses', $context)) {
07aa2057 78 $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
7f3f41ec 79 echo "<div class=\"reportlink\"><a href=\"report.php?id=$cm->id\">".
80 get_string("viewsurveyresponses", "survey", $numusers)."</a></div>";
3a4c2fd0 81 } else if (!$cm->visible) {
82 notice(get_string("activityiscurrentlyhidden"));
f9903ed0 83 }
84
4f0c2d00 85 if (!is_enrolled($context)) {
8b1b0382 86 echo $OUTPUT->notification(get_string("guestsnotallowed", "survey"));
813ec7ad 87 }
88
f9903ed0 89
90// Check the survey hasn't already been filled out.
91
92 if (survey_already_done($survey->id, $USER->id)) {
7c8c335f 93
037f99aa 94 add_to_log($course->id, "survey", "view graph", "view.php?id=$cm->id", $survey->id, $cm->id);
07aa2057 95 $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
7c8c335f 96
97 if ($showscales) {
8d849394 98 echo $OUTPUT->heading(get_string("surveycompleted", "survey"));
99 echo $OUTPUT->heading(get_string("peoplecompleted", "survey", $numusers));
795b56b3 100 echo '<div class="resultgraph">';
839f2456 101 survey_print_graph("id=$cm->id&amp;sid=$USER->id&amp;group=$currentgroup&amp;type=student.png");
795b56b3 102 echo '</div>';
7c8c335f 103
104 } else {
f14a311e 105
8411d0aa 106 echo $OUTPUT->box(format_module_intro('survey', $survey, $cm->id), 'generalbox', 'intro');
1ba862ec 107 echo $OUTPUT->spacer(array('height'=>30, 'width'=>1, 'br'=>true)); // should be done with CSS instead
f14a311e 108
44e1b7d7 109 $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions));
7c8c335f 110 $questionorder = explode(",", $survey->questions);
111 foreach ($questionorder as $key => $val) {
112 $question = $questions[$val];
113 if ($question->type == 0 or $question->type == 1) {
114 if ($answer = survey_get_user_answer($survey->id, $question->id, $USER->id)) {
02f78a26 115 $table = new html_table();
7c8c335f 116 $table->head = array(get_string($question->text, "survey"));
117 $table->align = array ("left");
8f414ab8 118 $table->data[] = array(s($answer->answer1));//no html here, just plain text
16be8974 119 echo html_writer::table($table);
2cfba1a8 120 echo $OUTPUT->spacer(clone($spacer)) . '<br />';
7c8c335f 121 }
122 }
123 }
124 }
ec81373f 125
641f16ba 126 echo $OUTPUT->footer();
f9903ed0 127 exit;
128 }
129
130// Start the survey form
037f99aa 131 add_to_log($course->id, "survey", "view form", "view.php?id=$cm->id", $survey->id, $cm->id);
f9903ed0 132
a33bbfdd 133 echo "<form method=\"post\" action=\"save.php\" id=\"surveyform\">";
795b56b3 134 echo '<div>';
d1290cec 135 echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />";
c2dcbfe3 136 echo "<input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />";
f9903ed0 137
8b1b0382 138 echo $OUTPUT->box(format_module_intro('survey', $survey, $cm->id), 'generalbox boxaligncenter bowidthnormal', 'intro');
6438c34f 139 echo '<div>'. get_string('allquestionrequireanswer', 'survey'). '</div>';
f9903ed0 140
141// Get all the major questions and their proper order
44e1b7d7 142 if (! $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions))) {
83b10e57 143 print_error('cannotfindquestion', 'survey');
f9903ed0 144 }
145 $questionorder = explode( ",", $survey->questions);
146
147// Cycle through all the questions in order and print them
148
0a9d1b3b
PS
149 global $qnum; //TODO: ugly globals hack for survey_print_*()
150 global $checklist; //TODO: ugly globals hack for survey_print_*()
f9903ed0 151 $qnum = 0;
25e0783d 152 $checklist = array();
f9903ed0 153 foreach ($questionorder as $key => $val) {
154 $question = $questions["$val"];
155 $question->id = $val;
ec81373f 156
b1f50577 157 if ($question->type >= 0) {
158
159 if ($question->text) {
160 $question->text = get_string($question->text, "survey");
161 }
162
163 if ($question->shorttext) {
164 $question->shorttext = get_string($question->shorttext, "survey");
165 }
166
f762c448 167 if ($question->intro) {
168 $question->intro = get_string($question->intro, "survey");
169 }
b1f50577 170
f762c448 171 if ($question->options) {
172 $question->options = get_string($question->options, "survey");
173 }
174
b1f50577 175 if ($question->multi) {
176 survey_print_multi($question);
177 } else {
178 survey_print_single($question);
179 }
f9903ed0 180 }
181 }
182
4f0c2d00 183 if (!is_enrolled($context)) {
8411d0aa 184 echo '</div>';
813ec7ad 185 echo "</form>";
641f16ba 186 echo $OUTPUT->footer();
813ec7ad 187 exit;
188 }
189
74ef4c2e 190 $checkarray = Array('questions'=>Array());
191 if (!empty($checklist)) {
192 foreach ($checklist as $question => $default) {
193 $checkarray['questions'][] = Array('question'=>$question, 'default'=>$default);
3a4c2fd0 194 }
f9903ed0 195 }
9dec75db 196 $PAGE->requires->js('/mod/survey/survey.js');
74ef4c2e 197 $PAGE->requires->data_for_js('surveycheck', $checkarray);
198 $PAGE->requires->string_for_js('questionsnotanswered', 'survey');
199 $PAGE->requires->js_function_call('survey_attach_onsubmit');
f9903ed0 200
74ef4c2e 201 echo '<br />';
202 echo '<input type="submit" value="'.get_string("clicktocontinue", "survey").'" />';
203 echo '</div>';
204 echo "</form>";
f9903ed0 205
641f16ba 206 echo $OUTPUT->footer();
f9903ed0 207
e5dd8e3b 208