MDL-38511 admin: Allow plugins to define custom bulk actions

This commit is contained in:
Brendan Heywood 2020-01-22 18:06:25 +11:00
parent fd126006b0
commit b2eec32733
3 changed files with 154 additions and 48 deletions

View File

@ -1,4 +1,26 @@
<?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/>.
/**
* Bulk user actions
*
* @package core
* @copyright Moodle
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('../../config.php');
require_once($CFG->libdir.'/adminlib.php');
@ -10,28 +32,23 @@ admin_externalpage_setup('userbulk');
if (!isset($SESSION->bulk_users)) {
$SESSION->bulk_users = array();
}
// create the user filter form
// Create the user filter form.
$ufiltering = new user_filtering();
// array of bulk operations
// create the bulk operations form
$action_form = new user_bulk_action_form();
if ($data = $action_form->get_data()) {
// check if an action should be performed and do so
switch ($data->action) {
case 1: redirect($CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk_confirm.php');
case 2: redirect($CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk_message.php');
case 3: redirect($CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk_delete.php');
case 4: redirect($CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk_display.php');
case 5: redirect($CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk_download.php');
case 7: redirect($CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk_forcepasswordchange.php');
case 8: redirect($CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk_cohortadd.php');
// Create the bulk operations form.
$actionform = new user_bulk_action_form();
if ($data = $actionform->get_data()) {
// Check if an action should be performed and do so.
$bulkactions = $actionform->get_actions();
if (array_key_exists($data->action, $bulkactions)) {
redirect($bulkactions[$data->action]->url);
}
}
$user_bulk_form = new user_bulk_form(null, get_selection_data($ufiltering));
$userbulkform = new user_bulk_form(null, get_selection_data($ufiltering));
if ($data = $user_bulk_form->get_data()) {
if ($data = $userbulkform->get_data()) {
if (!empty($data->addall)) {
add_selection_all($ufiltering);
@ -40,7 +57,7 @@ if ($data = $user_bulk_form->get_data()) {
if (in_array(0, $data->ausers)) {
add_selection_all($ufiltering);
} else {
foreach($data->ausers as $userid) {
foreach ($data->ausers as $userid) {
if ($userid == -1) {
continue;
}
@ -52,14 +69,14 @@ if ($data = $user_bulk_form->get_data()) {
}
} else if (!empty($data->removeall)) {
$SESSION->bulk_users= array();
$SESSION->bulk_users = array();
} else if (!empty($data->removesel)) {
if (!empty($data->susers)) {
if (in_array(0, $data->susers)) {
$SESSION->bulk_users= array();
$SESSION->bulk_users = array();
} else {
foreach($data->susers as $userid) {
foreach ($data->susers as $userid) {
if ($userid == -1) {
continue;
}
@ -69,18 +86,17 @@ if ($data = $user_bulk_form->get_data()) {
}
}
// reset the form selections
// Reset the form selections.
unset($_POST);
$user_bulk_form = new user_bulk_form(null, get_selection_data($ufiltering));
$userbulkform = new user_bulk_form(null, get_selection_data($ufiltering));
}
// do output
echo $OUTPUT->header();
$ufiltering->display_add();
$ufiltering->display_active();
$user_bulk_form->display();
$userbulkform->display();
$action_form->display();
$actionform->display();
echo $OUTPUT->footer();

View File

@ -1,34 +1,114 @@
<?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/>.
/**
* Bulk user action forms
*
* @package core
* @copyright Moodle
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/formslib.php');
require_once($CFG->libdir.'/datalib.php');
/**
* Bulk user action form
*
* @copyright Moodle
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class user_bulk_action_form extends moodleform {
function definition() {
/**
* Returns an array of action_link's of all bulk actions available for this user.
*
* @return array of action_link objects
*/
public function get_actions() : array {
global $CFG;
$syscontext = context_system::instance();
$actions = [];
if (has_capability('moodle/user:update', $syscontext)) {
$actions['confirm'] = new action_link(
new moodle_url('/admin/user/user_bulk_confirm.php'),
get_string('confirm'));
}
if (has_capability('moodle/site:readallmessages', $syscontext) && !empty($CFG->messaging)) {
$actions['message'] = new action_link(
new moodle_url('/admin/user/user_bulk_message.php'),
get_string('messageselectadd'));
}
if (has_capability('moodle/user:delete', $syscontext)) {
$actions['delete'] = new action_link(
new moodle_url('/admin/user/user_bulk_delete.php'),
get_string('delete'));
}
$actions['displayonpage'] = new action_link(
new moodle_url('/admin/user/user_bulk_display.php'),
get_string('displayonpage'));
if (has_capability('moodle/user:update', $syscontext)) {
$actions['download'] = new action_link(
new moodle_url('/admin/user/user_bulk_download.php'),
get_string('download', 'admin'));
}
if (has_capability('moodle/user:update', $syscontext)) {
$actions['forcepasswordchange'] = new action_link(
new moodle_url('/admin/user/user_bulk_forcepasswordchange.php'),
get_string('forcepasswordchange'));
}
if (has_capability('moodle/cohort:assign', $syscontext)) {
$actions['addtocohort'] = new action_link(
new moodle_url('/admin/user/user_bulk_cohortadd.php'),
get_string('bulkadd', 'core_cohort'));
}
// Any plugin can append actions to this list by implementing a callback
// <component>_bulk_user_actions() which returns an array of action_link.
// Each new action's key should have a frankenstyle prefix to avoid clashes.
// See MDL-38511 for more details.
$moreactions = get_plugins_with_function('bulk_user_actions', 'lib.php');
foreach ($moreactions as $plugintype => $plugins) {
foreach ($plugins as $pluginfunction) {
$actions += $pluginfunction();
}
}
return $actions;
}
/**
* Form definition
*/
public function definition() {
global $CFG;
$mform =& $this->_form;
$syscontext = context_system::instance();
$actions = array(0=>get_string('choose').'...');
if (has_capability('moodle/user:update', $syscontext)) {
$actions[1] = get_string('confirm');
}
if (has_capability('moodle/site:readallmessages', $syscontext) && !empty($CFG->messaging)) {
$actions[2] = get_string('messageselectadd');
}
if (has_capability('moodle/user:delete', $syscontext)) {
$actions[3] = get_string('delete');
}
$actions[4] = get_string('displayonpage');
if (has_capability('moodle/user:update', $syscontext)) {
$actions[5] = get_string('download', 'admin');
}
if (has_capability('moodle/user:update', $syscontext)) {
$actions[7] = get_string('forcepasswordchange');
}
if (has_capability('moodle/cohort:assign', $syscontext)) {
$actions[8] = get_string('bulkadd', 'core_cohort');
$actions = [0 => get_string('choose') . '...'];
$bulkactions = $this->get_actions();
foreach ($bulkactions as $key => $action) {
$actions[$key] = $action->text;
}
$objs = array();
$objs[] =& $mform->createElement('select', 'action', null, $actions);
@ -37,8 +117,18 @@ class user_bulk_action_form extends moodleform {
}
}
/**
* Bulk user form
*
* @copyright Moodle
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class user_bulk_form extends moodleform {
function definition() {
/**
* Form definition
*/
public function definition() {
$mform =& $this->_form;
$acount =& $this->_customdata['acount'];
@ -92,7 +182,6 @@ class user_bulk_form extends moodleform {
$objs[1] =& $mform->createElement('select', 'susers', get_string('selected', 'bulkusers'), $schoices, 'size="15"');
$objs[1]->setMultiple(true);
$grp =& $mform->addElement('group', 'usersgrp', get_string('users', 'bulkusers'), $objs, ' ', false);
$mform->addHelpButton('usersgrp', 'users', 'bulkusers');

View File

@ -19,6 +19,7 @@ information provided here is intended especially for developers.
- $plugin->incompatible = 36;
incompatible takes a single int corresponding to the first incompatible branch. Any Moodle versions including and
above this will be prevented from installing the plugin, and a message will be given when attempting installation.
* Added the <component>_bulk_user_actions() callback which returns a list of custom action_links objects
=== 3.8 ===
* Add CLI option to notify all cron tasks to stop: admin/cli/cron.php --stop