1 |
efrain |
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/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* This page shows results of a questionnaire to a student.
|
|
|
19 |
*
|
|
|
20 |
* @package mod_questionnaire
|
|
|
21 |
* @copyright 2016 Mike Churchward (mike.churchward@poetgroup.org)
|
|
|
22 |
* @author Mike Churchward
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*
|
|
|
25 |
*/
|
|
|
26 |
require_once("../../config.php");
|
|
|
27 |
require_once($CFG->dirroot.'/mod/questionnaire/questionnaire.class.php');
|
|
|
28 |
|
|
|
29 |
$instance = required_param('instance', PARAM_INT); // Questionnaire ID.
|
|
|
30 |
$userid = optional_param('user', $USER->id, PARAM_INT);
|
|
|
31 |
$rid = optional_param('rid', null, PARAM_INT);
|
|
|
32 |
$byresponse = optional_param('byresponse', 0, PARAM_INT);
|
|
|
33 |
$action = optional_param('action', 'summary', PARAM_ALPHA);
|
|
|
34 |
$currentgroupid = optional_param('group', 0, PARAM_INT); // Groupid.
|
|
|
35 |
|
|
|
36 |
if (! $questionnaire = $DB->get_record("questionnaire", array("id" => $instance))) {
|
|
|
37 |
throw new \moodle_exception('incorrectquestionnaire', 'mod_questionnaire');
|
|
|
38 |
}
|
|
|
39 |
if (! $course = $DB->get_record("course", array("id" => $questionnaire->course))) {
|
|
|
40 |
throw new \moodle_exception('coursemisconf', 'mod_questionnaire');
|
|
|
41 |
}
|
|
|
42 |
if (! $cm = get_coursemodule_from_instance("questionnaire", $questionnaire->id, $course->id)) {
|
|
|
43 |
throw new \moodle_exception('invalidcoursemodule', 'mod_questionnaire');
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
require_course_login($course, true, $cm);
|
|
|
47 |
$context = context_module::instance($cm->id);
|
|
|
48 |
$questionnaire->canviewallgroups = has_capability('moodle/site:accessallgroups', $context);
|
|
|
49 |
// Should never happen, unless called directly by a snoop...
|
|
|
50 |
if ( !has_capability('mod/questionnaire:readownresponses', $context)
|
|
|
51 |
|| $userid != $USER->id) {
|
|
|
52 |
throw new \moodle_exception('nopermissions', 'mod_questionnaire');
|
|
|
53 |
}
|
|
|
54 |
$url = new moodle_url($CFG->wwwroot.'/mod/questionnaire/myreport.php', array('instance' => $instance));
|
|
|
55 |
if (isset($userid)) {
|
|
|
56 |
$url->param('userid', $userid);
|
|
|
57 |
}
|
|
|
58 |
if (isset($byresponse)) {
|
|
|
59 |
$url->param('byresponse', $byresponse);
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
if (isset($currentgroupid)) {
|
|
|
63 |
$url->param('group', $currentgroupid);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
if (isset($action)) {
|
|
|
67 |
$url->param('action', $action);
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
$PAGE->set_url($url);
|
|
|
71 |
$PAGE->set_context($context);
|
|
|
72 |
$PAGE->set_title(get_string('questionnairereport', 'questionnaire'));
|
|
|
73 |
$PAGE->set_heading(format_string($course->fullname));
|
|
|
74 |
|
|
|
75 |
$questionnaire = new questionnaire($course, $cm, 0, $questionnaire);
|
|
|
76 |
// Add renderer and page objects to the questionnaire object for display use.
|
|
|
77 |
$questionnaire->add_renderer($PAGE->get_renderer('mod_questionnaire'));
|
|
|
78 |
$questionnaire->add_page(new \mod_questionnaire\output\reportpage());
|
|
|
79 |
|
|
|
80 |
$sid = $questionnaire->survey->id;
|
|
|
81 |
$courseid = $course->id;
|
|
|
82 |
|
|
|
83 |
// Tab setup.
|
|
|
84 |
if (!isset($SESSION->questionnaire)) {
|
|
|
85 |
$SESSION->questionnaire = new stdClass();
|
|
|
86 |
}
|
|
|
87 |
$SESSION->questionnaire->current_tab = 'myreport';
|
|
|
88 |
|
|
|
89 |
switch ($action) {
|
|
|
90 |
case 'summary':
|
|
|
91 |
if (empty($questionnaire->survey)) {
|
|
|
92 |
throw new \moodle_exception('surveynotexists', 'mod_questionnaire');
|
|
|
93 |
}
|
|
|
94 |
$SESSION->questionnaire->current_tab = 'mysummary';
|
|
|
95 |
$resps = $questionnaire->get_responses($userid);
|
|
|
96 |
$rids = array_keys($resps);
|
|
|
97 |
if (count($resps) > 1) {
|
|
|
98 |
$titletext = get_string('myresponsetitle', 'questionnaire', count($resps));
|
|
|
99 |
} else {
|
|
|
100 |
$titletext = get_string('yourresponse', 'questionnaire');
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
// Print the page header.
|
|
|
104 |
echo $questionnaire->renderer->header();
|
|
|
105 |
|
|
|
106 |
// Print the tabs.
|
|
|
107 |
include('tabs.php');
|
|
|
108 |
|
|
|
109 |
$questionnaire->page->add_to_page('myheaders', $titletext);
|
|
|
110 |
$questionnaire->survey_results($rids, $USER->id);
|
|
|
111 |
|
|
|
112 |
echo $questionnaire->renderer->render($questionnaire->page);
|
|
|
113 |
|
|
|
114 |
// Finish the page.
|
|
|
115 |
echo $questionnaire->renderer->footer($course);
|
|
|
116 |
break;
|
|
|
117 |
|
|
|
118 |
case 'vall':
|
|
|
119 |
if (empty($questionnaire->survey)) {
|
|
|
120 |
throw new \moodle_exception('surveynotexists', 'mod_questionnaire');
|
|
|
121 |
}
|
|
|
122 |
$SESSION->questionnaire->current_tab = 'myvall';
|
|
|
123 |
$questionnaire->add_user_responses($userid);
|
|
|
124 |
$titletext = get_string('myresponses', 'questionnaire');
|
|
|
125 |
|
|
|
126 |
// Print the page header.
|
|
|
127 |
echo $questionnaire->renderer->header();
|
|
|
128 |
|
|
|
129 |
// Print the tabs.
|
|
|
130 |
include('tabs.php');
|
|
|
131 |
|
|
|
132 |
$questionnaire->page->add_to_page('myheaders', $titletext);
|
|
|
133 |
$questionnaire->view_all_responses();
|
|
|
134 |
echo $questionnaire->renderer->render($questionnaire->page);
|
|
|
135 |
// Finish the page.
|
|
|
136 |
echo $questionnaire->renderer->footer($course);
|
|
|
137 |
break;
|
|
|
138 |
|
|
|
139 |
case 'vresp':
|
|
|
140 |
if (empty($questionnaire->survey)) {
|
|
|
141 |
throw new \moodle_exception('surveynotexists', 'mod_questionnaire');
|
|
|
142 |
}
|
|
|
143 |
$SESSION->questionnaire->current_tab = 'mybyresponse';
|
|
|
144 |
$usergraph = get_config('questionnaire', 'usergraph');
|
|
|
145 |
if ($usergraph) {
|
|
|
146 |
$charttype = $questionnaire->survey->chart_type;
|
|
|
147 |
if ($charttype) {
|
|
|
148 |
$PAGE->requires->js('/mod/questionnaire/javascript/RGraph/RGraph.common.core.js');
|
|
|
149 |
|
|
|
150 |
switch ($charttype) {
|
|
|
151 |
case 'bipolar':
|
|
|
152 |
$PAGE->requires->js('/mod/questionnaire/javascript/RGraph/RGraph.bipolar.js');
|
|
|
153 |
break;
|
|
|
154 |
case 'hbar':
|
|
|
155 |
$PAGE->requires->js('/mod/questionnaire/javascript/RGraph/RGraph.hbar.js');
|
|
|
156 |
break;
|
|
|
157 |
case 'radar':
|
|
|
158 |
$PAGE->requires->js('/mod/questionnaire/javascript/RGraph/RGraph.radar.js');
|
|
|
159 |
break;
|
|
|
160 |
case 'rose':
|
|
|
161 |
$PAGE->requires->js('/mod/questionnaire/javascript/RGraph/RGraph.rose.js');
|
|
|
162 |
break;
|
|
|
163 |
case 'vprogress':
|
|
|
164 |
$PAGE->requires->js('/mod/questionnaire/javascript/RGraph/RGraph.vprogress.js');
|
|
|
165 |
break;
|
|
|
166 |
}
|
|
|
167 |
}
|
|
|
168 |
}
|
|
|
169 |
$resps = $questionnaire->get_responses($userid);
|
|
|
170 |
|
|
|
171 |
// All participants.
|
|
|
172 |
$respsallparticipants = $questionnaire->get_responses();
|
|
|
173 |
|
|
|
174 |
$respsuser = $questionnaire->get_responses($userid);
|
|
|
175 |
|
|
|
176 |
$SESSION->questionnaire->numrespsallparticipants = count($respsallparticipants);
|
|
|
177 |
$SESSION->questionnaire->numselectedresps = $SESSION->questionnaire->numrespsallparticipants;
|
|
|
178 |
$iscurrentgroupmember = false;
|
|
|
179 |
|
|
|
180 |
// Available group modes (0 = no groups; 1 = separate groups; 2 = visible groups).
|
|
|
181 |
$groupmode = groups_get_activity_groupmode($cm, $course);
|
|
|
182 |
if ($groupmode > 0) {
|
|
|
183 |
// Check if current user is member of any group.
|
|
|
184 |
$usergroups = groups_get_user_groups($courseid, $userid);
|
|
|
185 |
$isgroupmember = count($usergroups[0]) > 0;
|
|
|
186 |
// Check if current user is member of current group.
|
|
|
187 |
$iscurrentgroupmember = groups_is_member($currentgroupid, $userid);
|
|
|
188 |
|
|
|
189 |
if ($groupmode == 1) {
|
|
|
190 |
$questionnairegroups = groups_get_all_groups($course->id, $userid);
|
|
|
191 |
}
|
|
|
192 |
if ($groupmode == 2 || $questionnaire->canviewallgroups) {
|
|
|
193 |
$questionnairegroups = groups_get_all_groups($course->id);
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
if (!empty($questionnairegroups)) {
|
|
|
197 |
$groupscount = count($questionnairegroups);
|
|
|
198 |
foreach ($questionnairegroups as $key) {
|
|
|
199 |
$firstgroupid = $key->id;
|
|
|
200 |
break;
|
|
|
201 |
}
|
|
|
202 |
if ($groupscount === 0 && $groupmode == 1) {
|
|
|
203 |
$currentgroupid = 0;
|
|
|
204 |
}
|
|
|
205 |
if ($groupmode == 1 && !$questionnaire->canviewallgroups && $currentgroupid == 0) {
|
|
|
206 |
$currentgroupid = $firstgroupid;
|
|
|
207 |
}
|
|
|
208 |
// If currentgroup is All Participants, current user is of course member of that "group"!
|
|
|
209 |
if ($currentgroupid == 0) {
|
|
|
210 |
$iscurrentgroupmember = true;
|
|
|
211 |
}
|
|
|
212 |
// Current group members.
|
|
|
213 |
$currentgroupresps = $questionnaire->get_responses(false, $currentgroupid);
|
|
|
214 |
|
|
|
215 |
} else {
|
|
|
216 |
// Groupmode = separate groups but user is not member of any group
|
|
|
217 |
// and does not have moodle/site:accessallgroups capability -> refuse view responses.
|
|
|
218 |
if (!$questionnaire->canviewallgroups) {
|
|
|
219 |
$currentgroupid = 0;
|
|
|
220 |
}
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
if ($currentgroupid > 0) {
|
|
|
224 |
$groupname = get_string('group').' <strong>'.groups_get_group_name($currentgroupid).'</strong>';
|
|
|
225 |
} else {
|
|
|
226 |
$groupname = '<strong>'.get_string('allparticipants').'</strong>';
|
|
|
227 |
}
|
|
|
228 |
}
|
|
|
229 |
|
|
|
230 |
$rids = array_keys($resps);
|
|
|
231 |
if (!$rid) {
|
|
|
232 |
// If more than one response for this respondent, display most recent response.
|
|
|
233 |
$rid = end($rids);
|
|
|
234 |
}
|
|
|
235 |
$numresp = count($rids);
|
|
|
236 |
if ($numresp > 1) {
|
|
|
237 |
$titletext = get_string('myresponsetitle', 'questionnaire', $numresp);
|
|
|
238 |
} else {
|
|
|
239 |
$titletext = get_string('yourresponse', 'questionnaire');
|
|
|
240 |
}
|
|
|
241 |
|
|
|
242 |
$compare = false;
|
|
|
243 |
// Print the page header.
|
|
|
244 |
echo $questionnaire->renderer->header();
|
|
|
245 |
|
|
|
246 |
// Print the tabs.
|
|
|
247 |
include('tabs.php');
|
|
|
248 |
$questionnaire->page->add_to_page('myheaders', $titletext);
|
|
|
249 |
|
|
|
250 |
if (count($resps) > 1) {
|
|
|
251 |
$userresps = $resps;
|
|
|
252 |
$questionnaire->survey_results_navbar_student ($rid, $userid, $instance, $userresps);
|
|
|
253 |
}
|
|
|
254 |
$resps = array();
|
|
|
255 |
// Determine here which "global" responses should get displayed for comparison with current user.
|
|
|
256 |
// Current user is viewing his own group's results.
|
|
|
257 |
if (isset($currentgroupresps)) {
|
|
|
258 |
$resps = $currentgroupresps;
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
// Current user is viewing another group's results so we must add their own results to that group's results.
|
|
|
262 |
|
|
|
263 |
if (!$iscurrentgroupmember) {
|
|
|
264 |
$resps += $respsuser;
|
|
|
265 |
}
|
|
|
266 |
// No groups.
|
|
|
267 |
if ($groupmode == 0 || $currentgroupid == 0) {
|
|
|
268 |
$resps = $respsallparticipants;
|
|
|
269 |
}
|
|
|
270 |
$compare = true;
|
|
|
271 |
$questionnaire->view_response($rid, null, $resps, $compare, $iscurrentgroupmember, false, $currentgroupid);
|
|
|
272 |
// Finish the page.
|
|
|
273 |
echo $questionnaire->renderer->render($questionnaire->page);
|
|
|
274 |
echo $questionnaire->renderer->footer($course);
|
|
|
275 |
break;
|
|
|
276 |
|
|
|
277 |
case get_string('return', 'questionnaire'):
|
|
|
278 |
default:
|
|
|
279 |
redirect('view.php?id='.$cm->id);
|
|
|
280 |
}
|