Skip to content

Commit

Permalink
MDL-12643 course: fix capabilities used for bulk participant actions.
Browse files Browse the repository at this point in the history
Previously the `moodle/course:bulkmessaging` capability controlled
access to the entire bulk actions menu for course participants,
rather than just those actions related to messaging.
  • Loading branch information
paulholden committed Jul 18, 2022
1 parent d9632ca commit c7d7c83
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 90 deletions.
24 changes: 11 additions & 13 deletions user/classes/table/participants.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,17 @@ public function out($pagesize, $useinitialsbar, $downloadhelpbutton = '') {
$headers = [];
$columns = [];

$bulkoperations = has_capability('moodle/course:bulkmessaging', $this->context);
if ($bulkoperations) {
$mastercheckbox = new \core\output\checkbox_toggleall('participants-table', true, [
'id' => 'select-all-participants',
'name' => 'select-all-participants',
'label' => get_string('selectall'),
'labelclasses' => 'sr-only',
'classes' => 'm-1',
'checked' => false,
]);
$headers[] = $OUTPUT->render($mastercheckbox);
$columns[] = 'select';
}
// At the very least, the user viewing this table will be able to use bulk actions to export it, so add 'select' column.
$mastercheckbox = new \core\output\checkbox_toggleall('participants-table', true, [
'id' => 'select-all-participants',
'name' => 'select-all-participants',
'label' => get_string('selectall'),
'labelclasses' => 'sr-only',
'classes' => 'm-1',
'checked' => false,
]);
$headers[] = $OUTPUT->render($mastercheckbox);
$columns[] = 'select';

$headers[] = get_string('fullname');
$columns[] = 'fullname';
Expand Down
151 changes: 74 additions & 77 deletions user/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@
// Trigger events.
user_list_view($course, $context);

$bulkoperations = has_capability('moodle/course:bulkmessaging', $context);

$PAGE->set_title("$course->shortname: ".get_string('participants'));
$PAGE->set_heading($course->fullname);
$PAGE->set_pagetype('course-view-participants');
Expand Down Expand Up @@ -209,90 +207,89 @@
'uniqueid' => $participanttable->uniqueid,
];

if ($bulkoperations) {
echo '<br /><div class="buttons"><div class="form-inline">';

echo html_writer::start_tag('div', array('class' => 'btn-group'));

if ($participanttable->get_page_size() < $participanttable->totalrows) {
// Select all users, refresh table showing all users and mark them all selected.
$label = get_string('selectalluserswithcount', 'moodle', $participanttable->totalrows);
echo html_writer::empty_tag('input', [
'type' => 'button',
'id' => 'checkall',
'class' => 'btn btn-secondary',
'value' => $label,
'data-target-page-size' => TABLE_SHOW_ALL_PAGE_SIZE,
]);
}
echo html_writer::end_tag('div');
$displaylist = array();
if (!empty($CFG->messaging) && has_all_capabilities(['moodle/site:sendmessage', 'moodle/course:bulkmessaging'], $context)) {
$displaylist['#messageselect'] = get_string('messageselectadd');
}
if (!empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context) && $context->id != $frontpagectx->id) {
$displaylist['#addgroupnote'] = get_string('addnewnote', 'notes');
echo '<br /><div class="buttons"><div class="form-inline">';

echo html_writer::start_tag('div', array('class' => 'btn-group'));

if ($participanttable->get_page_size() < $participanttable->totalrows) {
// Select all users, refresh table showing all users and mark them all selected.
$label = get_string('selectalluserswithcount', 'moodle', $participanttable->totalrows);
echo html_writer::empty_tag('input', [
'type' => 'button',
'id' => 'checkall',
'class' => 'btn btn-secondary',
'value' => $label,
'data-target-page-size' => TABLE_SHOW_ALL_PAGE_SIZE,
]);
}
echo html_writer::end_tag('div');
$displaylist = array();
if (!empty($CFG->messaging) && has_all_capabilities(['moodle/site:sendmessage', 'moodle/course:bulkmessaging'], $context)) {
$displaylist['#messageselect'] = get_string('messageselectadd');
}
if (!empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context) && $context->id != $frontpagectx->id) {
$displaylist['#addgroupnote'] = get_string('addnewnote', 'notes');
}

$params = ['operation' => 'download_participants'];

$downloadoptions = [];
$formats = core_plugin_manager::instance()->get_plugins_of_type('dataformat');
foreach ($formats as $format) {
if ($format->is_enabled()) {
$params = ['operation' => 'download_participants', 'dataformat' => $format->name];
$url = new moodle_url('bulkchange.php', $params);
$downloadoptions[$url->out(false)] = get_string('dataformat', $format->component);
}
}

$params = ['operation' => 'download_participants'];
if (!empty($downloadoptions)) {
$displaylist[] = [get_string('downloadas', 'table') => $downloadoptions];
}

if ($context->id != $frontpagectx->id) {
$instances = $manager->get_enrolment_instances();
$plugins = $manager->get_enrolment_plugins(false);
foreach ($instances as $key => $instance) {
if (!isset($plugins[$instance->enrol])) {
// Weird, some broken stuff in plugin.
continue;
}
$plugin = $plugins[$instance->enrol];
$bulkoperations = $plugin->get_bulk_operations($manager);

$downloadoptions = [];
$formats = core_plugin_manager::instance()->get_plugins_of_type('dataformat');
foreach ($formats as $format) {
if ($format->is_enabled()) {
$params = ['operation' => 'download_participants', 'dataformat' => $format->name];
$pluginoptions = [];
foreach ($bulkoperations as $key => $bulkoperation) {
$params = ['plugin' => $plugin->get_name(), 'operation' => $key];
$url = new moodle_url('bulkchange.php', $params);
$downloadoptions[$url->out(false)] = get_string('dataformat', $format->component);
$pluginoptions[$url->out(false)] = $bulkoperation->get_title();
}
if (!empty($pluginoptions)) {
$name = get_string('pluginname', 'enrol_' . $plugin->get_name());
$displaylist[] = [$name => $pluginoptions];
}
}
}

if (!empty($downloadoptions)) {
$displaylist[] = [get_string('downloadas', 'table') => $downloadoptions];
}
$selectactionparams = array(
'id' => 'formactionid',
'class' => 'ml-2',
'data-action' => 'toggle',
'data-togglegroup' => 'participants-table',
'data-toggle' => 'action',
'disabled' => 'disabled'
);
$label = html_writer::tag('label', get_string("withselectedusers"),
['for' => 'formactionid', 'class' => 'col-form-label d-inline']);
$select = html_writer::select($displaylist, 'formaction', '', ['' => 'choosedots'], $selectactionparams);
echo html_writer::tag('div', $label . $select);

if ($context->id != $frontpagectx->id) {
$instances = $manager->get_enrolment_instances();
$plugins = $manager->get_enrolment_plugins(false);
foreach ($instances as $key => $instance) {
if (!isset($plugins[$instance->enrol])) {
// Weird, some broken stuff in plugin.
continue;
}
$plugin = $plugins[$instance->enrol];
$bulkoperations = $plugin->get_bulk_operations($manager);

$pluginoptions = [];
foreach ($bulkoperations as $key => $bulkoperation) {
$params = ['plugin' => $plugin->get_name(), 'operation' => $key];
$url = new moodle_url('bulkchange.php', $params);
$pluginoptions[$url->out(false)] = $bulkoperation->get_title();
}
if (!empty($pluginoptions)) {
$name = get_string('pluginname', 'enrol_' . $plugin->get_name());
$displaylist[] = [$name => $pluginoptions];
}
}
}
echo '<input type="hidden" name="id" value="' . $course->id . '" />';
echo '<div class="d-none" data-region="state-help-icon">' . $OUTPUT->help_icon('publishstate', 'notes') . '</div>';
echo '</div></div></div>';

$bulkoptions->noteStateNames = note_get_state_names();

$selectactionparams = array(
'id' => 'formactionid',
'class' => 'ml-2',
'data-action' => 'toggle',
'data-togglegroup' => 'participants-table',
'data-toggle' => 'action',
'disabled' => 'disabled'
);
$label = html_writer::tag('label', get_string("withselectedusers"),
['for' => 'formactionid', 'class' => 'col-form-label d-inline']);
$select = html_writer::select($displaylist, 'formaction', '', ['' => 'choosedots'], $selectactionparams);
echo html_writer::tag('div', $label . $select);

echo '<input type="hidden" name="id" value="' . $course->id . '" />';
echo '<div class="d-none" data-region="state-help-icon">' . $OUTPUT->help_icon('publishstate', 'notes') . '</div>';
echo '</div></div></div>';

$bulkoptions->noteStateNames = note_get_state_names();
}
echo '</form>';

$PAGE->requires->js_call_amd('core_user/participants', 'init', [$bulkoptions]);
Expand Down

0 comments on commit c7d7c83

Please sign in to comment.