MDL-50015 mod_imscp: New WS mod_imscp_view_imscp
[moodle.git] / mod / survey / view.php
CommitLineData
a5cb6242 1<?php
a5cb6242 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.
13//
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/>.
16
17/**
18 * This file is responsible for displaying the survey
19 *
da9bddbe 20 * @package mod_survey
a5cb6242 21 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
f9903ed0 24
64d45daf
AD
25require_once("../../config.php");
26require_once("lib.php");
f9903ed0 27
64d45daf 28$id = required_param('id', PARAM_INT); // Course Module ID.
f9903ed0 29
64d45daf
AD
30if (! $cm = get_coursemodule_from_id('survey', $id)) {
31 print_error('invalidcoursemodule');
32}
f9903ed0 33
64d45daf
AD
34if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
35 print_error('coursemisconf');
36}
f9903ed0 37
64d45daf
AD
38$PAGE->set_url('/mod/survey/view.php', array('id' => $id));
39require_login($course, false, $cm);
40$context = context_module::instance($cm->id);
0468976c 41
64d45daf 42require_capability('mod/survey:participate', $context);
f9903ed0 43
64d45daf
AD
44if (! $survey = $DB->get_record("survey", array("id" => $cm->instance))) {
45 print_error('invalidsurveyid', 'survey');
46}
47$trimmedintro = trim($survey->intro);
48if (empty($trimmedintro)) {
49 $tempo = $DB->get_field("survey", "intro", array("id" => $survey->template));
50 $survey->intro = get_string($tempo, "survey");
51}
7c8c335f 52
64d45daf
AD
53if (! $template = $DB->get_record("survey", array("id" => $survey->template))) {
54 print_error('invalidtmptid', 'survey');
55}
7c8c335f 56
64d45daf 57// Update 'viewed' state if required by completion system.
05b7cd7a 58require_once($CFG->libdir . '/completionlib.php');
59$completion = new completion_info($course);
60$completion->set_module_viewed($cm);
61
64d45daf 62$showscales = ($template->name != 'ciqname');
98899fc0 63
64d45daf
AD
64$strsurvey = get_string("modulename", "survey");
65$PAGE->set_title($survey->name);
66$PAGE->set_heading($course->fullname);
67echo $OUTPUT->header();
68echo $OUTPUT->heading($survey->name);
f9903ed0 69
64d45daf
AD
70// Check to see if groups are being used in this survey.
71if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used.
72 $currentgroup = groups_get_activity_group($cm);
73} else {
74 $currentgroup = 0;
75}
76$groupingid = $cm->groupingid;
8411d0aa 77
64d45daf
AD
78if (has_capability('mod/survey:readresponses', $context) or ($groupmode == VISIBLEGROUPS)) {
79 $currentgroup = 0;
80}
8411d0aa 81
64d45daf
AD
82if (has_capability('mod/survey:readresponses', $context)) {
83 $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
84 echo "<div class=\"reportlink\"><a href=\"report.php?id=$cm->id\">".
85 get_string("viewsurveyresponses", "survey", $numusers)."</a></div>";
86} else if (!$cm->visible) {
87 notice(get_string("activityiscurrentlyhidden"));
88}
f9903ed0 89
64d45daf
AD
90if (!is_enrolled($context)) {
91 echo $OUTPUT->notification(get_string("guestsnotallowed", "survey"));
92}
813ec7ad 93
f9903ed0 94
64d45daf 95// Check the survey hasn't already been filled out.
7c8c335f 96
64d45daf 97if (survey_already_done($survey->id, $USER->id)) {
9e176aa2
RT
98 $params = array(
99 'objectid' => $survey->id,
100 'context' => $context,
101 'courseid' => $course->id,
64d45daf 102 'other' => array('viewed' => 'graph')
9e176aa2
RT
103 );
104 $event = \mod_survey\event\course_module_viewed::create($params);
105 $event->trigger();
64d45daf 106 $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
f9903ed0 107
64d45daf
AD
108 if ($showscales) {
109 // Ensure that graph.php will allow the user to see the graph.
110 if (has_capability('mod/survey:readresponses', $context) || !$groupmode || groups_is_member($currentgroup)) {
f9903ed0 111
64d45daf
AD
112 echo $OUTPUT->box(get_string("surveycompleted", "survey"));
113 echo $OUTPUT->box(get_string("peoplecompleted", "survey", $numusers));
ec81373f 114
64d45daf
AD
115 echo '<div class="resultgraph">';
116 survey_print_graph("id=$cm->id&amp;sid=$USER->id&amp;group=$currentgroup&amp;type=student.png");
117 echo '</div>';
118 } else {
119 echo $OUTPUT->box(get_string("surveycompletednograph", "survey"));
120 echo $OUTPUT->box(get_string("peoplecompleted", "survey", $numusers));
121 }
b1f50577 122
64d45daf 123 } else {
b1f50577 124
64d45daf
AD
125 echo $OUTPUT->box(format_module_intro('survey', $survey, $cm->id), 'generalbox', 'intro');
126 echo $OUTPUT->spacer(array('height' => 30, 'width' => 1), true); // Should be done with CSS instead.
127
128 $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions));
129 $questionorder = explode(",", $survey->questions);
130 foreach ($questionorder as $key => $val) {
131 $question = $questions[$val];
132 if ($question->type == 0 or $question->type == 1) {
133 if ($answer = survey_get_user_answer($survey->id, $question->id, $USER->id)) {
134 $table = new html_table();
135 $table->head = array(get_string($question->text, "survey"));
136 $table->align = array ("left");
137 $table->data[] = array(s($answer->answer1));// No html here, just plain text.
138 echo html_writer::table($table);
139 echo $OUTPUT->spacer(array('height' => 30, 'width' => 1), true);
140 }
b1f50577 141 }
64d45daf
AD
142 }
143 }
b1f50577 144
64d45daf
AD
145 echo $OUTPUT->footer();
146 exit;
147}
148
149// Start the survey form.
150$params = array(
151 'objectid' => $survey->id,
152 'context' => $context,
153 'courseid' => $course->id,
154 'other' => array('viewed' => 'form')
155);
156$event = \mod_survey\event\course_module_viewed::create($params);
157$event->trigger();
158
159echo "<form method=\"post\" action=\"save.php\" id=\"surveyform\">";
160echo '<div>';
161echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />";
162echo "<input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />";
163
164echo $OUTPUT->box(format_module_intro('survey', $survey, $cm->id), 'generalbox boxaligncenter bowidthnormal', 'intro');
165echo '<div>'. get_string('allquestionrequireanswer', 'survey'). '</div>';
166
167// Get all the major questions and their proper order.
168if (! $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions))) {
169 print_error('cannotfindquestion', 'survey');
170}
171$questionorder = explode( ",", $survey->questions);
172
173// Cycle through all the questions in order and print them.
174
175global $qnum; // TODO: ugly globals hack for survey_print_*().
176global $checklist; // TODO: ugly globals hack for survey_print_*().
177$qnum = 0;
178$checklist = array();
179foreach ($questionorder as $key => $val) {
180 $question = $questions["$val"];
181 $question->id = $val;
182
183 if ($question->type >= 0) {
184
185 if ($question->text) {
186 $question->text = get_string($question->text, "survey");
187 }
b1f50577 188
64d45daf
AD
189 if ($question->shorttext) {
190 $question->shorttext = get_string($question->shorttext, "survey");
191 }
f762c448 192
64d45daf
AD
193 if ($question->intro) {
194 $question->intro = get_string($question->intro, "survey");
f9903ed0 195 }
f9903ed0 196
64d45daf
AD
197 if ($question->options) {
198 $question->options = get_string($question->options, "survey");
199 }
813ec7ad 200
64d45daf
AD
201 if ($question->multi) {
202 survey_print_multi($question);
203 } else {
204 survey_print_single($question);
205 }
f9903ed0 206 }
64d45daf 207}
f9903ed0 208
64d45daf 209if (!is_enrolled($context)) {
74ef4c2e 210 echo '</div>';
211 echo "</form>";
641f16ba 212 echo $OUTPUT->footer();
64d45daf
AD
213 exit;
214}
215
216$checkarray = Array('questions' => Array());
217if (!empty($checklist)) {
218 foreach ($checklist as $question => $default) {
219 $checkarray['questions'][] = Array('question' => $question, 'default' => $default);
220 }
221}
222$PAGE->requires->data_for_js('surveycheck', $checkarray);
223$module = array(
224 'name' => 'mod_survey',
225 'fullpath' => '/mod/survey/survey.js',
226 'requires' => array('yui2-event'),
227);
228$PAGE->requires->string_for_js('questionsnotanswered', 'survey');
229$PAGE->requires->js_init_call('M.mod_survey.init', $checkarray, true, $module);
230
231echo '<br />';
232echo '<input type="submit" value="'.get_string("clicktocontinue", "survey").'" />';
233echo '</div>';
234echo "</form>";
235
236echo $OUTPUT->footer();
f9903ed0 237
e5dd8e3b 238