/
overview.php
258 lines (223 loc) · 9.4 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
<?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/>.
/**
* Completion Progress block overview page
*
* @package block_completion_progress
* @copyright 2018 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.'/notes/lib.php');
require_once($CFG->libdir.'/tablelib.php');
use block_completion_progress\completion_progress;
/**
* Default number of participants per page.
*/
const DEFAULT_PAGE_SIZE = 20;
/**
* An impractically high number of participants indicating 'all' are to be shown.
*/
const SHOW_ALL_PAGE_SIZE = 5000;
// Gather form data.
$id = required_param('instanceid', PARAM_INT);
$courseid = required_param('courseid', PARAM_INT);
$page = optional_param('page', 0, PARAM_INT); // Which page to show.
$perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT); // How many per page.
$group = optional_param('group', 0, PARAM_ALPHANUMEXT); // Group selected.
$role = optional_param('role', null, PARAM_INT);
$download = optional_param('download', '', PARAM_ALPHA);
// Determine course and context.
$course = $DB->get_record('course', ['id' => $courseid], '*', MUST_EXIST);
$context = context_course::instance($courseid);
// Get specific block config and context.
$block = $DB->get_record('block_instances', ['id' => $id], '*', MUST_EXIST);
$blockcontext = context_block::instance($id);
$notesallowed = !empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context);
$messagingallowed = !empty($CFG->messaging) && has_capability('moodle/site:sendmessage', $context);
$bulkoperations = has_capability('moodle/course:bulkmessaging', $context) && ($notesallowed || $messagingallowed);
// Set up page parameters.
$PAGE->set_course($course);
$PAGE->set_url(
'/blocks/completion_progress/overview.php',
[
'instanceid' => $id,
'courseid' => $courseid,
'page' => $page,
'perpage' => $perpage,
'group' => $group,
'role' => $role,
]
);
$PAGE->set_context($context);
$title = get_string('overview', 'block_completion_progress');
$PAGE->set_title($title);
$PAGE->set_heading($title);
$PAGE->navbar->add($title);
$PAGE->set_pagelayout('report');
$cachevalue = debugging() ? -1 : (int)get_config('block_completion_progress', 'cachevalue');
$PAGE->requires->css('/blocks/completion_progress/css.php?v=' . $cachevalue);
// Check user is logged in and capable of accessing the Overview.
require_login($course, false);
require_capability('block/completion_progress:overview', $blockcontext);
$progress = (new completion_progress($course))
->for_overview()
->for_block_instance($block);
// Prepare a group selector if there are groups in the course.
$groupids = [];
$groupoptions = [];
if (has_capability('moodle/site:accessallgroups', $context)) {
$allgroups = groups_get_all_groups($course->id, 0);
$allgroupings = groups_get_all_groupings($course->id);
if ($allgroups) {
$groupoptions[0] = get_string('allparticipants');
}
} else {
$allgroups = groups_get_all_groups($course->id, $USER->id);
$allgroupings = [];
$groupids = array_keys($allgroups);
}
foreach ($allgroups as $rec) {
if ($group == $rec->id) {
$groupids = [ $rec->id ]; // Selected filter.
}
$groupoptions[$rec->id] = format_string($rec->name);
}
foreach ($allgroupings as $rec) {
if ($group === "g{$rec->id}") { // Selected grouping.
$groupids = array_keys(groups_get_all_groups($course->id, 0, $rec->id));
}
$groupoptions["g{$rec->id}"] = format_string($rec->name);
}
if (!$groupids) {
$group = 0;
$PAGE->set_url($PAGE->url, ['group' => $group]);
}
// Prepare the roles menu.
$sql = "SELECT DISTINCT r.id, r.name, r.shortname, r.archetype, r.sortorder
FROM {role} r, {role_assignments} ra
WHERE ra.contextid = :contextid
AND r.id = ra.roleid
ORDER BY r.sortorder";
$params = ['contextid' => $context->id];
$roles = role_fix_names($DB->get_records_sql($sql, $params), $context);
$roleoptions = [0 => get_string('allparticipants')];
foreach ($roles as $rec) {
if ($role === null && $rec->archetype === 'student') {
$role = $rec->id; // First student role is the default.
$PAGE->set_url($PAGE->url, ['role' => $role]);
}
$roleoptions[$rec->id] = $rec->localname;
}
// Setup the overview table.
$table = new block_completion_progress\table\overview($progress, $groupids, $role, $bulkoperations);
$table->define_baseurl($PAGE->url);
$table->show_download_buttons_at([]); // We'll output them ourselves.
$table->is_downloading($download, 'completion_progress-' . $COURSE->shortname);
$table->setup();
if ($download) {
$table->query_db($perpage);
$table->start_output();
$table->build_table();
$table->finish_output();
exit;
}
$output = $PAGE->get_renderer('block_completion_progress');
// Start page output.
echo $output->header();
echo $output->heading($title, 2);
echo $output->container_start('block_completion_progress');
// Check if activities/resources have been selected in config.
if (!$progress->has_activities()) {
echo get_string('no_activities_message', 'block_completion_progress');
echo $output->container_end();
echo $output->footer();
die();
}
echo $output->container_start('progressoverviewmenus');
if ($groupoptions) {
echo $output->single_select($PAGE->url, 'group', $groupoptions, $group,
['' => 'choosedots'], null, ['label' => s(get_string('groupsgroupings', 'group'))]);
}
if ($roleoptions) {
echo $output->single_select($PAGE->url, 'role', $roleoptions, $role,
['' => 'choosedots'], null, ['label' => get_string('role')]);
}
echo $output->container_end();
// Form for messaging selected participants.
$formattributes = ['action' => $CFG->wwwroot.'/user/action_redir.php', 'method' => 'post', 'id' => 'participantsform'];
$formattributes['data-course-id'] = $course->id;
$formattributes['data-table-unique-id'] = 'block-completion_progress-overview-' . $course->id;
echo html_writer::start_tag('form', $formattributes);
echo html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()]);
echo html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'returnto', 'value' => s($PAGE->url->out(false))]);
// Imitate a 3.9 dynamic table enough to fool the core_user/participants JS code, until
// next time it changes again.
$tabledivattributes = [
'data-region' => 'core_table/dynamic',
'data-table-uniqueid' => $formattributes['data-table-unique-id'],
];
echo html_writer::start_div('', $tabledivattributes);
// Render the overview table.
$table->query_db($perpage);
$table->start_output();
$table->build_table();
$table->finish_output();
echo html_writer::end_div(); // Closes the 3.9 imitation table wrapper.
// Output paging controls.
if ($table->totalrows > $perpage || $perpage == SHOW_ALL_PAGE_SIZE) {
$perpageurl = new moodle_url($PAGE->url, ['page' => 0]);
if ($perpage < SHOW_ALL_PAGE_SIZE) {
$perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
echo $output->container(html_writer::link($perpageurl,
get_string('showall', '', $table->totalrows)), [], 'showall');
} else {
$perpageurl->param('perpage', DEFAULT_PAGE_SIZE);
echo $output->container(html_writer::link($perpageurl,
get_string('showperpage', '', DEFAULT_PAGE_SIZE)), [], 'showall');
}
}
if ($bulkoperations) {
echo '<br /><div class="form-inline m-1">';
$displaylist = [];
if ($messagingallowed) {
$displaylist['#messageselect'] = get_string('messageselectadd');
}
if ($notesallowed) {
$displaylist['#addgroupnote'] = get_string('addnewnote', 'notes');
}
echo html_writer::tag('label', get_string("withselectedusers"), ['for' => 'formactionid']);
echo html_writer::select($displaylist, 'formaction', '', ['' => 'choosedots'], ['id' => 'formactionid']);
echo '<input type="hidden" name="id" value="'.$course->id.'" />';
echo '<noscript style="display:inline">';
echo '<div><input type="submit" value="'.get_string('ok').'" /></div>';
echo '</noscript>';
echo '</div>';
$options = new stdClass();
$options->noteStateNames = note_get_state_names();
$options->uniqueid = $formattributes['data-table-unique-id'];
echo '<div class="d-none" data-region="state-help-icon">' . $output->help_icon('publishstate', 'notes') . '</div>';
$PAGE->requires->js_call_amd('block_completion_progress/overview', 'init', [$options]);
}
echo html_writer::end_tag('form');
echo $table->download_buttons();
// Organise access to JS for progress bars.
$PAGE->requires->js_call_amd('block_completion_progress/progressbar', 'init', [
'instances' => [$block->id],
]);
echo $output->container_end();
echo $output->footer();