MDL-56846 mod_survey: Fix styles in boost
[moodle.git] / mod / survey / view.php
1 <?php
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/>.
17 /**
18  * This file is responsible for displaying the survey
19  *
20  * @package   mod_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
23  */
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');
32 }
34 if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
35     print_error('coursemisconf');
36 }
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');
46 }
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");
51 }
53 if (! $template = $DB->get_record("survey", array("id" => $survey->template))) {
54     print_error('invalidtmptid', 'survey');
55 }
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');
64 } else {
65     survey_view($survey, $course, $cm, $context, 'form');
66 }
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);
77 } else {
78     $currentgroup = 0;
79 }
80 $groupingid = $cm->groupingid;
82 if (has_capability('mod/survey:readresponses', $context) or ($groupmode == VISIBLEGROUPS)) {
83     $currentgroup = 0;
84 }
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"));
92 }
94 if (!is_enrolled($context)) {
95     echo $OUTPUT->notification(get_string("guestsnotallowed", "survey"));
96 }
98 if ($surveyalreadydone) {
100     $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
102     if ($showscales) {
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&amp;sid=$USER->id&amp;group=$currentgroup&amp;type=student.png");
111             echo '</div>';
112         } else {
113             echo $OUTPUT->box(get_string("surveycompletednograph", "survey"));
114             echo $OUTPUT->box(get_string("peoplecompleted", "survey", $numusers));
115         }
117     } else {
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);
133                 }
134             }
135         }
136     }
138     echo $OUTPUT->footer();
139     exit;
142 echo "<form method=\"post\" action=\"save.php\" id=\"surveyform\">";
143 echo '<div>';
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_*().
155 $qnum = 0;
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);
165         } else {
166             survey_print_single($question);
167         }
168     }
171 if (!is_enrolled($context)) {
172     echo '</div>';
173     echo "</form>";
174     echo $OUTPUT->footer();
175     exit;
178 $checkarray = Array('questions' => Array());
179 if (!empty($checklist)) {
180     foreach ($checklist as $question => $default) {
181         $checkarray['questions'][] = Array('question' => $question, 'default' => $default);
182     }
184 $PAGE->requires->data_for_js('surveycheck', $checkarray);
185 $module = array(
186     'name'      => 'mod_survey',
187     'fullpath'  => '/mod/survey/survey.js',
188     'requires'  => array('yui2-event'),
189 );
190 $PAGE->requires->string_for_js('questionsnotanswered', 'survey');
191 $PAGE->requires->js_init_call('M.mod_survey.init', $checkarray, true, $module);
193 echo '<br />';
194 echo '<input type="submit" class="btn btn-primary" value="'.get_string("clicktocontinue", "survey").'" />';
195 echo '</div>';
196 echo "</form>";
198 echo $OUTPUT->footer();