2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * A bulk operation for the self enrolment plugin to delete selected users enrolments.
21 * @copyright 2018 Farhan Karmali
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
28 * A bulk operation for the self enrolment plugin to delete selected users enrolments.
31 * @copyright 2018 Farhan Karmali
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class enrol_self_deleteselectedusers_operation extends enrol_bulk_enrolment_operation {
37 * Returns the title to display for this bulk operation.
41 public function get_identifier() {
42 return 'deleteselectedusers';
46 * Returns the identifier for this bulk operation. This is the key used when the plugin
47 * returns an array containing all of the bulk operations it supports.
51 public function get_title() {
52 return get_string('deleteselectedusers', 'enrol_manual');
56 * Returns a enrol_bulk_enrolment_operation extension form to be used
57 * in collecting required information for this operation to be processed.
59 * @param string|moodle_url|null $defaultaction
60 * @param mixed $defaultcustomdata
61 * @return enrol_manual_editselectedusers_form
63 public function get_form($defaultaction = null, $defaultcustomdata = null) {
64 if (!array($defaultcustomdata)) {
65 $defaultcustomdata = array();
67 $defaultcustomdata['title'] = $this->get_title();
68 $defaultcustomdata['message'] = get_string('confirmbulkdeleteenrolment', 'enrol_self');
69 $defaultcustomdata['button'] = get_string('unenrolusers', 'enrol_self');
71 return new enrol_self_deleteselectedusers_form($defaultaction, $defaultcustomdata);
75 * Processes the bulk operation request for the given userids with the provided properties.
77 * @param course_enrolment_manager $manager
79 * @param stdClass $properties The data returned by the form.
81 public function process(course_enrolment_manager $manager, array $users, stdClass $properties) {
82 if (!has_capability("enrol/self:unenrol", $manager->get_context())) {
86 foreach ($users as $user) {
87 foreach ($user->enrolments as $enrolment) {
88 $plugin = $enrolment->enrolmentplugin;
89 $instance = $enrolment->enrolmentinstance;
90 if ($plugin->allow_unenrol_user($instance, $enrolment)) {
91 $plugin->unenrol_user($instance, $user->id);