forked from deraadt/Moodle-block_progress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverview.php
291 lines (261 loc) · 9.83 KB
/
overview.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
281
282
283
284
285
286
287
288
289
290
291
<?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/>.
/**
* Progress Bar block overview page
*
* @package contrib
* @subpackage block_progress
* @copyright 2010 Michael de Raadt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// Include required files.
require_once(dirname(__FILE__) . '/../../config.php');
require_once($CFG->dirroot.'/blocks/progress/lib.php');
require_once($CFG->libdir.'/tablelib.php');
// Gather form data.
$id = required_param('progressbarid', PARAM_INT);
$courseid = required_param('courseid', PARAM_INT);
// Determine course and context.
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
if (class_exists('context_course')) {
$context = context_course::instance($courseid);
} else {
$context = get_context_instance(CONTEXT_COURSE, $courseid);
}
// Get specific block config and context.
$progressblock = $DB->get_record('block_instances', array('id' => $id), '*', MUST_EXIST);
$progressconfig = unserialize(base64_decode($progressblock->configdata));
if (class_exists('context_block')) {
$progressblockcontext = context_block::instance($id);
} else {
$progressblockcontext = get_context_instance(CONTEXT_BLOCK, $courseid);
}
// Set up page parameters.
$PAGE->set_course($course);
$PAGE->requires->css('/blocks/progress/styles.css');
$PAGE->set_url('/blocks/progress/overview.php', array('progressbarid' => $id, 'courseid' => $courseid));
$PAGE->set_context($context);
$title = get_string('overview', 'block_progress');
$PAGE->set_title($title);
$PAGE->set_heading($title);
$PAGE->navbar->add($title);
$PAGE->set_pagelayout('standard');
// Check user is logged in and capable of grading.
require_login($course, false);
require_capability('block/progress:overview', $progressblockcontext);
// Start page output.
echo $OUTPUT->header();
echo $OUTPUT->heading($title, 2);
echo $OUTPUT->container_start('block_progress');
// Get the modules to check progress on.
$modules = block_progress_modules_in_use();
if (empty($modules)) {
echo get_string('no_events_config_message', 'block_progress');
echo $OUTPUT->container_end();
echo $OUTPUT->footer();
die();
}
// Check if activities/resources have been selected in config.
$events = block_progress_event_information($progressconfig, $modules);
if ($events == null) {
echo get_string('no_events_message', 'block_progress');
echo $OUTPUT->container_end();
echo $OUTPUT->footer();
die();
}
if (empty($events)) {
echo get_string('no_visible_events_message', 'block_progress');
echo $OUTPUT->container_end();
echo $OUTPUT->footer();
die();
}
$numevents = count($events);
// Determine if a role has been selected.
$sql = "SELECT DISTINCT r.id, r.name
FROM {role} r, {role_assignments} a
WHERE a.contextid = :contextid
AND r.id = a.roleid
AND r.shortname = :shortname";
$params = array('contextid' => $context->id, 'shortname' => 'student');
$studentrole = $DB->get_record_sql($sql, $params);
if ($studentrole) {
$studentroleid = $studentrole->id;
} else {
$studentroleid = 0;
}
$roleselected = optional_param('role', $studentroleid, PARAM_INT);
$rolewhere = $roleselected != 0 ? "AND a.roleid = $roleselected" : '';
// Output group selector if there are groups in the course.
echo $OUTPUT->container_start('progressoverviewmenus');
$groupuserid = 0;
if (!has_capability('moodle/site:accessallgroups', $context)) {
$groupuserid = $USER->id;
}
$groups = groups_get_all_groups($course->id);
if (!empty($groups)) {
$course->groupmode = 1;
groups_print_course_menu($course, $PAGE->url);
}
// Output the roles menu.
$sql = "SELECT DISTINCT r.id, r.name, r.shortname
FROM {role} r, {role_assignments} a
WHERE a.contextid = :contextid
AND r.id = a.roleid";
$params = array('contextid' => $context->id);
$roles = role_fix_names($DB->get_records_sql($sql, $params), $context);
$rolestodisplay = array(0 => get_string('allparticipants'));
foreach ($roles as $role) {
$rolestodisplay[$role->id] = $role->localname;
}
echo ' '.get_string('role');
echo $OUTPUT->single_select($PAGE->url, 'role', $rolestodisplay, $roleselected);
echo $OUTPUT->container_end();
// Apply group restrictions.
$params = array();
$groupwhere = '';
$groupsfrom = '';
$groupselected = groups_get_course_group($course);
if ($groupselected && $groupselected != 0) {
$groupsfrom = ', {groups_members} g ';
$groupwhere = " AND g.groupid = :groupselected AND g.userid = u.id";
$params['groupselected'] = $groupselected;
}
// Get the list of users enrolled in the course.
$picturefields = user_picture::fields('u');
$sql = "SELECT DISTINCT $picturefields, u.lastaccess
FROM {user} u, {role_assignments} a $groupsfrom
WHERE a.contextid = :contextid
AND a.userid = u.id
$rolewhere
$groupwhere";
$params['contextid'] = $context->id;
$users = array_values($DB->get_records_sql($sql, $params));
$numberofusers = count($users);
// Setup submissions table.
$table = new flexible_table('mod-block-progress-overview');
$tablecolumns = array('picture', 'fullname', 'lastonline', 'progressbar', 'progress');
$table->define_columns($tablecolumns);
$tableheaders = array(
'',
get_string('fullname'),
get_string('lastonline', 'block_progress'),
get_string('progressbar', 'block_progress'),
get_string('progress', 'block_progress')
);
$table->define_headers($tableheaders);
$table->sortable(true);
$table->set_attribute('class', 'generalbox');
$table->column_style_all('padding', '5px 10px');
$table->column_style_all('text-align', 'left');
$table->column_style_all('vertical-align', 'middle');
$table->column_style('progressbar', 'width', '200px');
$table->column_style('progress', 'text-align', 'center');
$table->no_sorting('picture');
$table->no_sorting('progressbar');
$table->define_baseurl($PAGE->url);
$table->setup();
// Build table of progress bars as they are marked.
for ($i = 0; $i < $numberofusers; $i++) {
$picture = $OUTPUT->user_picture($users[$i], array('course' => $course->id));
$name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$users[$i]->id.'&course='.
$course->id.'">'.fullname($users[$i]).'</a>';
if ($users[$i]->lastaccess == 0) {
$lastonline = get_string('never');
} else {
$lastonline = userdate($users[$i]->lastaccess);
}
$attempts = block_progress_attempts($modules, $progressconfig, $events, $users[$i]->id, $course->id);
$progressbar = block_progress_bar($modules, $progressconfig, $events, $users[$i]->id, $course->id, $attempts,
true);
$progressvalue = block_progress_percentage($events, $attempts);
$progress = $progressvalue.'%';
$rows[] = array(
'firstname' => $users[$i]->firstname,
'lastname' => strtoupper($users[$i]->lastname),
'picture' => $picture,
'fullname' => $name,
'lastonlinetime' => $users[$i]->lastaccess,
'lastonline' => $lastonline,
'progressbar' => $progressbar,
'progressvalue' => $progressvalue,
'progress' => $progress
);
}
// Build the table content and output.
if (!$sort = $table->get_sql_sort()) {
$sort = 'lastname DESC';
}
if ($numberofusers > 0) {
usort($rows, 'block_progress_compare_rows');
foreach ($rows as $row) {
$table->add_data(array($row['picture'], $row['fullname'], $row['lastonline'],
$row['progressbar'], $row['progress']));
}
}
$table->print_initials_bar();
$table->print_html();
echo $OUTPUT->container_end();
// Organise access to JS.
$jsmodule = array(
'name' => 'block_progress',
'fullpath' => '/blocks/progress/module.js',
'requires' => array(),
'strings' => array(
array('time_expected', 'block_progress'),
),
);
$arguments = array($CFG->wwwroot, array_keys($modules));
$PAGE->requires->js_init_call('M.block_progress.init', $arguments, false, $jsmodule);
echo $OUTPUT->footer();
/**
* Compares two table row elements for ordering.
*
* @param mixed $a element containing name, online time and progress info
* @param mixed $b element containing name, online time and progress info
* @return order of pair expressed as -1, 0, or 1
*/
function block_progress_compare_rows($a, $b) {
global $sort;
// Process each of the one or two orders.
$orders = explode(',', $sort);
foreach ($orders as $order) {
// Extract the order information.
$orderelements = explode(' ', trim($order));
$aspect = $orderelements[0];
$ascdesc = $orderelements[1];
// Compensate for presented vs actual.
switch ($aspect) {
case 'name':
$aspect = 'lastname';
break;
case 'lastonline':
$aspect = 'lastonlinetime';
break;
case 'progress':
$aspect = 'progressvalue';
break;
}
// Check of order can be established.
if ($a[$aspect] < $b[$aspect]) {
return $ascdesc == 'ASC'?1:-1;
}
if ($a[$aspect] > $b[$aspect]) {
return $ascdesc == 'ASC'?-1:1;
}
}
// If previous ordering fails, consider values equal.
return 0;
}