2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * This file is responsible for displaying the 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');
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);
80 $groupingid = $cm->groupingid;
82 if (has_capability('mod/survey:readresponses', $context) or ($groupmode == VISIBLEGROUPS)) {
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);
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&sid=$USER->id&group=$currentgroup&type=student.png");
113 echo $OUTPUT->box(get_string("surveycompletednograph", "survey"));
114 echo $OUTPUT->box(get_string("peoplecompleted", "survey", $numusers));
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();
142 echo "<form method=\"post\" action=\"save.php\" id=\"surveyform\">";
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 global $checklist; // TODO: ugly globals hack for survey_print_*().
156 $checklist = array();
157 foreach ($questions as $question) {
159 if ($question->type >= 0) {
161 $question = survey_translate_question($question);
163 if ($question->multi) {
164 survey_print_multi($question);
166 survey_print_single($question);
171 if (!is_enrolled($context)) {
174 echo $OUTPUT->footer();
178 $checkarray = Array('questions' => Array());
179 if (!empty($checklist)) {
180 foreach ($checklist as $question => $default) {
181 $checkarray['questions'][] = Array('question' => $question, 'default' => $default);
184 $PAGE->requires->data_for_js('surveycheck', $checkarray);
186 'name' => 'mod_survey',
187 'fullpath' => '/mod/survey/survey.js',
188 'requires' => array('yui2-event'),
190 $PAGE->requires->string_for_js('questionsnotanswered', 'survey');
191 $PAGE->requires->js_init_call('M.mod_survey.init', $checkarray, true, $module);
194 echo '<input type="submit" class="btn btn-primary" value="'.get_string("clicktocontinue", "survey").'" />';
198 echo $OUTPUT->footer();