-
Notifications
You must be signed in to change notification settings - Fork 1
/
view.php
184 lines (147 loc) · 6.13 KB
/
view.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file is responsible for displaying the survey
*
* @package mod_survey
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once("../../config.php");
require_once("lib.php");
$id = required_param('id', PARAM_INT); // Course Module ID.
if (! $cm = get_coursemodule_from_id('survey', $id)) {
throw new \moodle_exception('invalidcoursemodule');
}
$cm = cm_info::create($cm);
if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
throw new \moodle_exception('coursemisconf');
}
$PAGE->set_url('/mod/survey/view.php', array('id' => $id));
require_login($course, false, $cm);
$context = context_module::instance($cm->id);
require_capability('mod/survey:participate', $context);
if (! $survey = $DB->get_record("survey", array("id" => $cm->instance))) {
throw new \moodle_exception('invalidsurveyid', 'survey');
}
if (! $template = $DB->get_record("survey", array("id" => $survey->template))) {
throw new \moodle_exception('invalidtmptid', 'survey');
}
$showscales = ($template->name != 'ciqname');
// Check the survey hasn't already been filled out.
$surveyalreadydone = survey_already_done($survey->id, $USER->id);
if ($surveyalreadydone) {
// Trigger course_module_viewed event and completion.
survey_view($survey, $course, $cm, $context, 'graph');
} else {
survey_view($survey, $course, $cm, $context, 'form');
}
$strsurvey = get_string("modulename", "survey");
$PAGE->set_title($survey->name);
$PAGE->set_heading($course->fullname);
// No need to show the description if the survey is done and a graph page is to be shown.
if ($surveyalreadydone && $showscales) {
$PAGE->activityheader->set_description('');
} else {
// If the survey has empty description, display the default one.
$trimmedintro = trim($survey->intro);
if (empty($trimmedintro)) {
$tempo = $DB->get_field("survey", "intro", array("id" => $survey->template));
$PAGE->activityheader->set_description(get_string($tempo, "survey"));
}
}
$PAGE->add_body_class('limitedwidth');
echo $OUTPUT->header();
// Check to see if groups are being used in this survey.
if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used.
$currentgroup = groups_get_activity_group($cm);
} else {
$currentgroup = 0;
}
$groupingid = $cm->groupingid;
if (has_capability('mod/survey:readresponses', $context) or ($groupmode == VISIBLEGROUPS)) {
$currentgroup = 0;
}
if (!$cm->visible) {
notice(get_string("activityiscurrentlyhidden"));
}
if (!is_enrolled($context)) {
echo $OUTPUT->notification(get_string("guestsnotallowed", "survey"));
}
if ($surveyalreadydone) {
$numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
if ($showscales) {
// Ensure that graph.php will allow the user to see the graph.
if (has_capability('mod/survey:readresponses', $context) || !$groupmode || groups_is_member($currentgroup)) {
echo $OUTPUT->box(get_string("surveycompleted", "survey"));
echo $OUTPUT->box(get_string("peoplecompleted", "survey", $numusers));
echo '<div class="resultgraph">';
survey_print_graph("id=$cm->id&sid=$USER->id&group=$currentgroup&type=student.png");
echo '</div>';
} else {
echo $OUTPUT->box(get_string("surveycompletednograph", "survey"));
echo $OUTPUT->box(get_string("peoplecompleted", "survey", $numusers));
}
} else {
echo $OUTPUT->spacer(array('height' => 30, 'width' => 1), true); // Should be done with CSS instead.
$questions = survey_get_questions($survey);
foreach ($questions as $question) {
if ($question->type == 0 or $question->type == 1) {
if ($answer = survey_get_user_answer($survey->id, $question->id, $USER->id)) {
$table = new html_table();
$table->head = array(get_string($question->text, "survey"));
$table->align = array ("left");
$table->data[] = array(s($answer->answer1));// No html here, just plain text.
echo html_writer::table($table);
echo $OUTPUT->spacer(array('height' => 30, 'width' => 1), true);
}
}
}
}
echo $OUTPUT->footer();
exit;
}
echo "<form method=\"post\" action=\"save.php\" id=\"surveyform\">";
echo '<div>';
echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />";
echo "<input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />";
echo '<div>'. get_string('allquestionrequireanswer', 'survey'). '</div>';
// Get all the major questions in order.
$questions = survey_get_questions($survey);
global $qnum; // TODO: ugly globals hack for survey_print_*().
$qnum = 0;
foreach ($questions as $question) {
if ($question->type >= 0) {
$question = survey_translate_question($question);
if ($question->multi) {
survey_print_multi($question);
} else {
survey_print_single($question);
}
}
}
if (!is_enrolled($context)) {
echo '</div>';
echo "</form>";
echo $OUTPUT->footer();
exit;
}
$PAGE->requires->js_call_amd('mod_survey/validation', 'ensureRadiosChosen', array('surveyform'));
echo '<br />';
echo '<input type="submit" class="btn btn-primary" value="'. get_string("submit"). '" />';
echo '</div>';
echo "</form>";
echo $OUTPUT->footer();