MDL-44182 mod_survey: unified @package use
[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 *
da9bddbe 21 * @package mod_survey
a5cb6242 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));
cdbea7ee 40 require_login($course, false, $cm);
1c969116 41 $context = context_module::instance($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
05b7cd7a 58// Update 'viewed' state if required by completion system
59require_once($CFG->libdir . '/completionlib.php');
60$completion = new completion_info($course);
61$completion->set_module_viewed($cm);
62
7c8c335f 63 $showscales = ($template->name != 'ciqname');
98899fc0 64
bfbef967 65 $strsurvey = get_string("modulename", "survey");
8c6ca696 66 $PAGE->set_title($survey->name);
5617e8c9 67 $PAGE->set_heading($course->fullname);
207e24b8 68 echo $OUTPUT->header();
5def24cf 69 echo $OUTPUT->heading($survey->name);
f9903ed0 70
a9ccbf60 71/// Check to see if groups are being used in this survey
07aa2057 72 if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used
73 $currentgroup = groups_get_activity_group($cm);
a9ccbf60 74 } else {
75 $currentgroup = 0;
76 }
07aa2057 77 $groupingid = $cm->groupingid;
8411d0aa 78
79 if (has_capability('mod/survey:readresponses', $context) or ($groupmode == VISIBLEGROUPS)) {
bee7ee38 80 $currentgroup = 0;
a9ccbf60 81 }
8411d0aa 82
0468976c 83 if (has_capability('mod/survey:readresponses', $context)) {
07aa2057 84 $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
7f3f41ec 85 echo "<div class=\"reportlink\"><a href=\"report.php?id=$cm->id\">".
86 get_string("viewsurveyresponses", "survey", $numusers)."</a></div>";
3a4c2fd0 87 } else if (!$cm->visible) {
88 notice(get_string("activityiscurrentlyhidden"));
f9903ed0 89 }
90
4f0c2d00 91 if (!is_enrolled($context)) {
8b1b0382 92 echo $OUTPUT->notification(get_string("guestsnotallowed", "survey"));
813ec7ad 93 }
94
f9903ed0 95
96// Check the survey hasn't already been filled out.
97
98 if (survey_already_done($survey->id, $USER->id)) {
7c8c335f 99
037f99aa 100 add_to_log($course->id, "survey", "view graph", "view.php?id=$cm->id", $survey->id, $cm->id);
07aa2057 101 $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
7c8c335f 102
103 if ($showscales) {
5def24cf
RW
104 echo $OUTPUT->box(get_string("surveycompleted", "survey"));
105 echo $OUTPUT->box(get_string("peoplecompleted", "survey", $numusers));
795b56b3 106 echo '<div class="resultgraph">';
839f2456 107 survey_print_graph("id=$cm->id&amp;sid=$USER->id&amp;group=$currentgroup&amp;type=student.png");
795b56b3 108 echo '</div>';
7c8c335f 109
110 } else {
f14a311e 111
8411d0aa 112 echo $OUTPUT->box(format_module_intro('survey', $survey, $cm->id), 'generalbox', 'intro');
32cfd597 113 echo $OUTPUT->spacer(array('height'=>30, 'width'=>1), true); // should be done with CSS instead
f14a311e 114
44e1b7d7 115 $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions));
7c8c335f 116 $questionorder = explode(",", $survey->questions);
117 foreach ($questionorder as $key => $val) {
118 $question = $questions[$val];
119 if ($question->type == 0 or $question->type == 1) {
120 if ($answer = survey_get_user_answer($survey->id, $question->id, $USER->id)) {
02f78a26 121 $table = new html_table();
7c8c335f 122 $table->head = array(get_string($question->text, "survey"));
123 $table->align = array ("left");
8f414ab8 124 $table->data[] = array(s($answer->answer1));//no html here, just plain text
16be8974 125 echo html_writer::table($table);
487ee886 126 echo $OUTPUT->spacer(array('height'=>30, 'width'=>1), true);
7c8c335f 127 }
128 }
129 }
130 }
ec81373f 131
641f16ba 132 echo $OUTPUT->footer();
f9903ed0 133 exit;
134 }
135
136// Start the survey form
037f99aa 137 add_to_log($course->id, "survey", "view form", "view.php?id=$cm->id", $survey->id, $cm->id);
f9903ed0 138
a33bbfdd 139 echo "<form method=\"post\" action=\"save.php\" id=\"surveyform\">";
795b56b3 140 echo '<div>';
d1290cec 141 echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />";
c2dcbfe3 142 echo "<input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />";
f9903ed0 143
8b1b0382 144 echo $OUTPUT->box(format_module_intro('survey', $survey, $cm->id), 'generalbox boxaligncenter bowidthnormal', 'intro');
6438c34f 145 echo '<div>'. get_string('allquestionrequireanswer', 'survey'). '</div>';
f9903ed0 146
147// Get all the major questions and their proper order
44e1b7d7 148 if (! $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions))) {
83b10e57 149 print_error('cannotfindquestion', 'survey');
f9903ed0 150 }
151 $questionorder = explode( ",", $survey->questions);
152
153// Cycle through all the questions in order and print them
154
0a9d1b3b
PS
155 global $qnum; //TODO: ugly globals hack for survey_print_*()
156 global $checklist; //TODO: ugly globals hack for survey_print_*()
f9903ed0 157 $qnum = 0;
25e0783d 158 $checklist = array();
f9903ed0 159 foreach ($questionorder as $key => $val) {
160 $question = $questions["$val"];
161 $question->id = $val;
ec81373f 162
b1f50577 163 if ($question->type >= 0) {
164
165 if ($question->text) {
166 $question->text = get_string($question->text, "survey");
167 }
168
169 if ($question->shorttext) {
170 $question->shorttext = get_string($question->shorttext, "survey");
171 }
172
f762c448 173 if ($question->intro) {
174 $question->intro = get_string($question->intro, "survey");
175 }
b1f50577 176
f762c448 177 if ($question->options) {
178 $question->options = get_string($question->options, "survey");
179 }
180
b1f50577 181 if ($question->multi) {
182 survey_print_multi($question);
183 } else {
184 survey_print_single($question);
185 }
f9903ed0 186 }
187 }
188
4f0c2d00 189 if (!is_enrolled($context)) {
8411d0aa 190 echo '</div>';
813ec7ad 191 echo "</form>";
641f16ba 192 echo $OUTPUT->footer();
813ec7ad 193 exit;
194 }
195
74ef4c2e 196 $checkarray = Array('questions'=>Array());
197 if (!empty($checklist)) {
198 foreach ($checklist as $question => $default) {
199 $checkarray['questions'][] = Array('question'=>$question, 'default'=>$default);
3a4c2fd0 200 }
f9903ed0 201 }
74ef4c2e 202 $PAGE->requires->data_for_js('surveycheck', $checkarray);
2de3515f
PS
203 $module = array(
204 'name' => 'mod_survey',
205 'fullpath' => '/mod/survey/survey.js',
206 'requires' => array('yui2-event'),
207 );
74ef4c2e 208 $PAGE->requires->string_for_js('questionsnotanswered', 'survey');
2de3515f 209 $PAGE->requires->js_init_call('M.mod_survey.init', $checkarray, true, $module);
f9903ed0 210
74ef4c2e 211 echo '<br />';
212 echo '<input type="submit" value="'.get_string("clicktocontinue", "survey").'" />';
213 echo '</div>';
214 echo "</form>";
f9903ed0 215
641f16ba 216 echo $OUTPUT->footer();
f9903ed0 217
e5dd8e3b 218