MDL-37961 webservice: PARAM_BOOL with PARAM_DEFAULT accepts boolean default
[moodle.git] / mod / survey / view.php
blob8c450e073f47162b493810a429010f192bdab684
1 <?php
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.
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/>.
18 /**
19 * This file is responsible for displaying the survey
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
26 require_once("../../config.php");
27 require_once("lib.php");
29 $id = required_param('id', PARAM_INT); // Course Module ID
31 if (! $cm = get_coursemodule_from_id('survey', $id)) {
32 print_error('invalidcoursemodule');
35 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
36 print_error('coursemisconf');
39 $PAGE->set_url('/mod/survey/view.php', array('id'=>$id));
40 require_login($course, false, $cm);
41 $context = context_module::instance($cm->id);
43 require_capability('mod/survey:participate', $context);
45 if (! $survey = $DB->get_record("survey", array("id"=>$cm->instance))) {
46 print_error('invalidsurveyid', 'survey');
48 $trimmedintro = trim($survey->intro);
49 if (empty($trimmedintro)) {
50 $tempo = $DB->get_field("survey", "intro", array("id"=>$survey->template));
51 $survey->intro = get_string($tempo, "survey");
54 if (! $template = $DB->get_record("survey", array("id"=>$survey->template))) {
55 print_error('invalidtmptid', 'survey');
58 // Update 'viewed' state if required by completion system
59 require_once($CFG->libdir . '/completionlib.php');
60 $completion = new completion_info($course);
61 $completion->set_module_viewed($cm);
63 $showscales = ($template->name != 'ciqname');
65 $strsurvey = get_string("modulename", "survey");
66 $PAGE->set_title(format_string($survey->name));
67 $PAGE->set_heading($course->fullname);
68 echo $OUTPUT->header();
70 /// Check to see if groups are being used in this survey
71 if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used
72 $currentgroup = groups_get_activity_group($cm);
73 } else {
74 $currentgroup = 0;
76 $groupingid = $cm->groupingid;
78 if (has_capability('mod/survey:readresponses', $context) or ($groupmode == VISIBLEGROUPS)) {
79 $currentgroup = 0;
82 if (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"));
90 if (!is_enrolled($context)) {
91 echo $OUTPUT->notification(get_string("guestsnotallowed", "survey"));
95 // Check the survey hasn't already been filled out.
97 if (survey_already_done($survey->id, $USER->id)) {
99 add_to_log($course->id, "survey", "view graph", "view.php?id=$cm->id", $survey->id, $cm->id);
100 $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
102 if ($showscales) {
103 echo $OUTPUT->heading(get_string("surveycompleted", "survey"));
104 echo $OUTPUT->heading(get_string("peoplecompleted", "survey", $numusers));
105 echo '<div class="resultgraph">';
106 survey_print_graph("id=$cm->id&amp;sid=$USER->id&amp;group=$currentgroup&amp;type=student.png");
107 echo '</div>';
109 } else {
111 echo $OUTPUT->box(format_module_intro('survey', $survey, $cm->id), 'generalbox', 'intro');
112 echo $OUTPUT->spacer(array('height'=>30, 'width'=>1), true); // should be done with CSS instead
114 $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions));
115 $questionorder = explode(",", $survey->questions);
116 foreach ($questionorder as $key => $val) {
117 $question = $questions[$val];
118 if ($question->type == 0 or $question->type == 1) {
119 if ($answer = survey_get_user_answer($survey->id, $question->id, $USER->id)) {
120 $table = new html_table();
121 $table->head = array(get_string($question->text, "survey"));
122 $table->align = array ("left");
123 $table->data[] = array(s($answer->answer1));//no html here, just plain text
124 echo html_writer::table($table);
125 echo $OUTPUT->spacer(array('height'=>30, 'width'=>1), true);
131 echo $OUTPUT->footer();
132 exit;
135 // Start the survey form
136 add_to_log($course->id, "survey", "view form", "view.php?id=$cm->id", $survey->id, $cm->id);
138 echo "<form method=\"post\" action=\"save.php\" id=\"surveyform\">";
139 echo '<div>';
140 echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />";
141 echo "<input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />";
143 echo $OUTPUT->box(format_module_intro('survey', $survey, $cm->id), 'generalbox boxaligncenter bowidthnormal', 'intro');
144 echo '<div>'. get_string('allquestionrequireanswer', 'survey'). '</div>';
146 // Get all the major questions and their proper order
147 if (! $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions))) {
148 print_error('cannotfindquestion', 'survey');
150 $questionorder = explode( ",", $survey->questions);
152 // Cycle through all the questions in order and print them
154 global $qnum; //TODO: ugly globals hack for survey_print_*()
155 global $checklist; //TODO: ugly globals hack for survey_print_*()
156 $qnum = 0;
157 $checklist = array();
158 foreach ($questionorder as $key => $val) {
159 $question = $questions["$val"];
160 $question->id = $val;
162 if ($question->type >= 0) {
164 if ($question->text) {
165 $question->text = get_string($question->text, "survey");
168 if ($question->shorttext) {
169 $question->shorttext = get_string($question->shorttext, "survey");
172 if ($question->intro) {
173 $question->intro = get_string($question->intro, "survey");
176 if ($question->options) {
177 $question->options = get_string($question->options, "survey");
180 if ($question->multi) {
181 survey_print_multi($question);
182 } else {
183 survey_print_single($question);
188 if (!is_enrolled($context)) {
189 echo '</div>';
190 echo "</form>";
191 echo $OUTPUT->footer();
192 exit;
195 $checkarray = Array('questions'=>Array());
196 if (!empty($checklist)) {
197 foreach ($checklist as $question => $default) {
198 $checkarray['questions'][] = Array('question'=>$question, 'default'=>$default);
201 $PAGE->requires->data_for_js('surveycheck', $checkarray);
202 $module = array(
203 'name' => 'mod_survey',
204 'fullpath' => '/mod/survey/survey.js',
205 'requires' => array('yui2-event'),
207 $PAGE->requires->string_for_js('questionsnotanswered', 'survey');
208 $PAGE->requires->js_init_call('M.mod_survey.init', $checkarray, true, $module);
210 echo '<br />';
211 echo '<input type="submit" value="'.get_string("clicktocontinue", "survey").'" />';
212 echo '</div>';
213 echo "</form>";
215 echo $OUTPUT->footer();