forked from PoetOS/moodle-mod_questionnaire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myreport.php
280 lines (243 loc) · 10.9 KB
/
myreport.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?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 page shows results of a questionnaire to a student.
*
* @package mod_questionnaire
* @copyright 2016 Mike Churchward ([email protected])
* @author Mike Churchward
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
*/
require_once("../../config.php");
require_once($CFG->dirroot.'/mod/questionnaire/questionnaire.class.php');
$instance = required_param('instance', PARAM_INT); // Questionnaire ID.
$userid = optional_param('user', $USER->id, PARAM_INT);
$rid = optional_param('rid', null, PARAM_INT);
$byresponse = optional_param('byresponse', 0, PARAM_INT);
$action = optional_param('action', 'summary', PARAM_ALPHA);
$currentgroupid = optional_param('group', 0, PARAM_INT); // Groupid.
if (! $questionnaire = $DB->get_record("questionnaire", array("id" => $instance))) {
throw new \moodle_exception('incorrectquestionnaire', 'mod_questionnaire');
}
if (! $course = $DB->get_record("course", array("id" => $questionnaire->course))) {
throw new \moodle_exception('coursemisconf', 'mod_questionnaire');
}
if (! $cm = get_coursemodule_from_instance("questionnaire", $questionnaire->id, $course->id)) {
throw new \moodle_exception('invalidcoursemodule', 'mod_questionnaire');
}
require_course_login($course, true, $cm);
$context = context_module::instance($cm->id);
$questionnaire->canviewallgroups = has_capability('moodle/site:accessallgroups', $context);
// Should never happen, unless called directly by a snoop...
if ( !has_capability('mod/questionnaire:readownresponses', $context)
|| $userid != $USER->id) {
throw new \moodle_exception('nopermissions', 'mod_questionnaire');
}
$url = new moodle_url($CFG->wwwroot.'/mod/questionnaire/myreport.php', array('instance' => $instance));
if (isset($userid)) {
$url->param('userid', $userid);
}
if (isset($byresponse)) {
$url->param('byresponse', $byresponse);
}
if (isset($currentgroupid)) {
$url->param('group', $currentgroupid);
}
if (isset($action)) {
$url->param('action', $action);
}
$PAGE->set_url($url);
$PAGE->set_context($context);
$PAGE->set_title(get_string('questionnairereport', 'questionnaire'));
$PAGE->set_heading(format_string($course->fullname));
$questionnaire = new questionnaire($course, $cm, 0, $questionnaire);
// Add renderer and page objects to the questionnaire object for display use.
$questionnaire->add_renderer($PAGE->get_renderer('mod_questionnaire'));
$questionnaire->add_page(new \mod_questionnaire\output\reportpage());
$sid = $questionnaire->survey->id;
$courseid = $course->id;
// Tab setup.
if (!isset($SESSION->questionnaire)) {
$SESSION->questionnaire = new stdClass();
}
$SESSION->questionnaire->current_tab = 'myreport';
switch ($action) {
case 'summary':
if (empty($questionnaire->survey)) {
throw new \moodle_exception('surveynotexists', 'mod_questionnaire');
}
$SESSION->questionnaire->current_tab = 'mysummary';
$resps = $questionnaire->get_responses($userid);
$rids = array_keys($resps);
if (count($resps) > 1) {
$titletext = get_string('myresponsetitle', 'questionnaire', count($resps));
} else {
$titletext = get_string('yourresponse', 'questionnaire');
}
// Print the page header.
echo $questionnaire->renderer->header();
// Print the tabs.
include('tabs.php');
$questionnaire->page->add_to_page('myheaders', $titletext);
$questionnaire->survey_results($rids, $USER->id);
echo $questionnaire->renderer->render($questionnaire->page);
// Finish the page.
echo $questionnaire->renderer->footer($course);
break;
case 'vall':
if (empty($questionnaire->survey)) {
throw new \moodle_exception('surveynotexists', 'mod_questionnaire');
}
$SESSION->questionnaire->current_tab = 'myvall';
$questionnaire->add_user_responses($userid);
$titletext = get_string('myresponses', 'questionnaire');
// Print the page header.
echo $questionnaire->renderer->header();
// Print the tabs.
include('tabs.php');
$questionnaire->page->add_to_page('myheaders', $titletext);
$questionnaire->view_all_responses();
echo $questionnaire->renderer->render($questionnaire->page);
// Finish the page.
echo $questionnaire->renderer->footer($course);
break;
case 'vresp':
if (empty($questionnaire->survey)) {
throw new \moodle_exception('surveynotexists', 'mod_questionnaire');
}
$SESSION->questionnaire->current_tab = 'mybyresponse';
$usergraph = get_config('questionnaire', 'usergraph');
if ($usergraph) {
$charttype = $questionnaire->survey->chart_type;
if ($charttype) {
$PAGE->requires->js('/mod/questionnaire/javascript/RGraph/RGraph.common.core.js');
switch ($charttype) {
case 'bipolar':
$PAGE->requires->js('/mod/questionnaire/javascript/RGraph/RGraph.bipolar.js');
break;
case 'hbar':
$PAGE->requires->js('/mod/questionnaire/javascript/RGraph/RGraph.hbar.js');
break;
case 'radar':
$PAGE->requires->js('/mod/questionnaire/javascript/RGraph/RGraph.radar.js');
break;
case 'rose':
$PAGE->requires->js('/mod/questionnaire/javascript/RGraph/RGraph.rose.js');
break;
case 'vprogress':
$PAGE->requires->js('/mod/questionnaire/javascript/RGraph/RGraph.vprogress.js');
break;
}
}
}
$resps = $questionnaire->get_responses($userid);
// All participants.
$respsallparticipants = $questionnaire->get_responses();
$respsuser = $questionnaire->get_responses($userid);
$SESSION->questionnaire->numrespsallparticipants = count($respsallparticipants);
$SESSION->questionnaire->numselectedresps = $SESSION->questionnaire->numrespsallparticipants;
$iscurrentgroupmember = false;
// Available group modes (0 = no groups; 1 = separate groups; 2 = visible groups).
$groupmode = groups_get_activity_groupmode($cm, $course);
if ($groupmode > 0) {
// Check if current user is member of any group.
$usergroups = groups_get_user_groups($courseid, $userid);
$isgroupmember = count($usergroups[0]) > 0;
// Check if current user is member of current group.
$iscurrentgroupmember = groups_is_member($currentgroupid, $userid);
if ($groupmode == 1) {
$questionnairegroups = groups_get_all_groups($course->id, $userid);
}
if ($groupmode == 2 || $questionnaire->canviewallgroups) {
$questionnairegroups = groups_get_all_groups($course->id);
}
if (!empty($questionnairegroups)) {
$groupscount = count($questionnairegroups);
foreach ($questionnairegroups as $key) {
$firstgroupid = $key->id;
break;
}
if ($groupscount === 0 && $groupmode == 1) {
$currentgroupid = 0;
}
if ($groupmode == 1 && !$questionnaire->canviewallgroups && $currentgroupid == 0) {
$currentgroupid = $firstgroupid;
}
// If currentgroup is All Participants, current user is of course member of that "group"!
if ($currentgroupid == 0) {
$iscurrentgroupmember = true;
}
// Current group members.
$currentgroupresps = $questionnaire->get_responses(false, $currentgroupid);
} else {
// Groupmode = separate groups but user is not member of any group
// and does not have moodle/site:accessallgroups capability -> refuse view responses.
if (!$questionnaire->canviewallgroups) {
$currentgroupid = 0;
}
}
if ($currentgroupid > 0) {
$groupname = get_string('group').' <strong>'.groups_get_group_name($currentgroupid).'</strong>';
} else {
$groupname = '<strong>'.get_string('allparticipants').'</strong>';
}
}
$rids = array_keys($resps);
if (!$rid) {
// If more than one response for this respondent, display most recent response.
$rid = end($rids);
}
$numresp = count($rids);
if ($numresp > 1) {
$titletext = get_string('myresponsetitle', 'questionnaire', $numresp);
} else {
$titletext = get_string('yourresponse', 'questionnaire');
}
$compare = false;
// Print the page header.
echo $questionnaire->renderer->header();
// Print the tabs.
include('tabs.php');
$questionnaire->page->add_to_page('myheaders', $titletext);
if (count($resps) > 1) {
$userresps = $resps;
$questionnaire->survey_results_navbar_student ($rid, $userid, $instance, $userresps);
}
$resps = array();
// Determine here which "global" responses should get displayed for comparison with current user.
// Current user is viewing his own group's results.
if (isset($currentgroupresps)) {
$resps = $currentgroupresps;
}
// Current user is viewing another group's results so we must add their own results to that group's results.
if (!$iscurrentgroupmember) {
$resps += $respsuser;
}
// No groups.
if ($groupmode == 0 || $currentgroupid == 0) {
$resps = $respsallparticipants;
}
$compare = true;
$questionnaire->view_response($rid, null, $resps, $compare, $iscurrentgroupmember, false, $currentgroupid);
// Finish the page.
echo $questionnaire->renderer->render($questionnaire->page);
echo $questionnaire->renderer->footer($course);
break;
case get_string('return', 'questionnaire'):
default:
redirect('view.php?id='.$cm->id);
}