MDL-59564 users: Add bulk enrolment change

Add bulk change enrolments form to the participants page.
This commit is contained in:
Damyon Wiese
2017-07-31 09:29:37 +08:00
parent d509f80c48
commit 689ccae5f3
2 changed files with 136 additions and 2 deletions
+120 -2
View File
@@ -24,16 +24,18 @@
require_once("../config.php");
$formaction = required_param('formaction', PARAM_FILE);
$formaction = required_param('formaction', PARAM_LOCALURL);
$id = required_param('id', PARAM_INT);
$PAGE->set_url('/user/action_redir.php', array('formaction' => $formaction, 'id' => $id));
list($formaction) = explode('?', $formaction, 2);
// Add every page will be redirected by this script.
$actions = array(
'messageselect.php',
'addnote.php',
'groupaddnote.php',
'bulkchange.php'
);
if (array_search($formaction, $actions) === false) {
@@ -44,4 +46,120 @@ if (!confirm_sesskey()) {
print_error('confirmsesskeybad');
}
require_once($formaction);
if ($formaction == 'bulkchange.php') {
// Backwards compatibility for enrolment plugins bulk change functionality.
// This awful code is adapting from the participant page with it's param names and values
// to the values expected by the bulk enrolment changes forms.
$formaction = required_param('formaction', PARAM_URL);
require_once($CFG->dirroot . '/enrol/locallib.php');
$url = new moodle_url($formaction);
// Get the enrolment plugin type and bulk action from the url.
$plugin = $url->param('plugin');
$operationname = $url->param('operation');
$course = $DB->get_record('course', array('id' => $id), '*', MUST_EXIST);
$context = context_course::instance($id);
$PAGE->set_context($context);
$instances = enrol_get_instances($course->id, false);
$instance = false;
foreach ($instances as $oneinstance) {
if ($oneinstance->enrol == $plugin) {
$instance = $oneinstance;
break;
}
}
if (!$instance) {
print_error('errorwithbulkoperation', 'enrol');
}
$manager = new course_enrolment_manager($PAGE, $course, $instance->id);
$plugins = $manager->get_enrolment_plugins();
if (!isset($plugins[$plugin])) {
print_error('errorwithbulkoperation', 'enrol');
}
$plugin = $plugins[$plugin];
$operations = $plugin->get_bulk_operations($manager);
if (!isset($operations[$operationname])) {
print_error('errorwithbulkoperation', 'enrol');
}
$operation = $operations[$operationname];
$userids = optional_param_array('userid', array(), PARAM_INT);
$default = new moodle_url('/user/index.php', ['id' => $course->id]);
$returnurl = new moodle_url(optional_param('returnto', $default, PARAM_URL));
if (empty($userids)) {
$userids = optional_param_array('bulkuser', array(), PARAM_INT);
}
if (empty($userids)) {
// The first time list hack.
if (empty($userids) and $post = data_submitted()) {
foreach ($post as $k => $v) {
if (preg_match('/^user(\d+)$/', $k, $m)) {
$userids[] = $m[1];
}
}
}
}
if (empty($userids)) {
redirect($returnurl, get_string('noselectedusers', 'bulkusers'));
}
$users = $manager->get_users_enrolments($userids);
// We may have users from any kind of enrolment, we need to filter for the enrolment plugin matching the bulk action.
$matchesplugin = function($user) use ($plugin) {
foreach ($user->enrolments as $enrolment) {
if ($enrolment->enrolmentplugin->get_name() == $plugin->get_name()) {
return true;
}
}
return false;
};
$users = array_filter($users, $matchesplugin);
if (empty($users)) {
redirect($returnurl, get_string('noselectedusers', 'bulkusers'));
}
// Get the form for the bulk operation.
$mform = $operation->get_form($PAGE->url, array('users' => $users));
// If the mform is false then attempt an immediate process. This may be an immediate action that
// doesn't require user input OR confirmation.... who know what but maybe one day.
if ($mform === false) {
if ($operation->process($manager, $users, new stdClass)) {
redirect($returnurl);
} else {
print_error('errorwithbulkoperation', 'enrol');
}
}
// Check if the bulk operation has been cancelled.
if ($mform->is_cancelled()) {
redirect($returnurl);
}
if ($mform->is_submitted() && $mform->is_validated() && confirm_sesskey()) {
if ($operation->process($manager, $users, $mform->get_data())) {
redirect($returnurl);
}
}
$pagetitle = get_string('bulkuseroperation', 'enrol');
$PAGE->set_title($pagetitle);
$PAGE->set_heading($pagetitle);
echo $OUTPUT->header();
echo $OUTPUT->heading($operation->get_title());
$mform->display();
echo $OUTPUT->footer();
exit();
} else {
require_once($formaction);
}
+16
View File
@@ -363,6 +363,22 @@ if ($bulkoperations) {
$displaylist['groupaddnote.php'] = get_string('groupaddnewnote', 'notes');
}
$plugins = $manager->get_enrolment_plugins();
foreach ($plugins as $plugin) {
$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 $OUTPUT->help_icon('withselectedusers');
echo html_writer::tag('label', get_string("withselectedusers"), array('for' => 'formactionid'));
echo html_writer::select($displaylist, 'formaction', '', array('' => 'choosedots'), array('id' => 'formactionid'));