From ebaf979c94de3a4b0945485af4c076998e05f7dd Mon Sep 17 00:00:00 2001 From: Farhan Karmali Date: Fri, 2 Mar 2018 00:52:36 +0530 Subject: [PATCH] MDL-60811 enrol_self: Add bulk action for self enrollment on users page --- enrol/self/bulkchangeforms.php | 0 .../self/classes/deleteselectedusers_form.php | 37 ++++ .../classes/deleteselectedusers_operation.php | 99 +++++++++++ enrol/self/classes/editselectedusers_form.php | 37 ++++ .../classes/editselectedusers_operation.php | 163 ++++++++++++++++++ enrol/self/lang/en/enrol_self.php | 4 + enrol/self/lib.php | 19 ++ enrol/self/locallib.php | 1 + 8 files changed, 360 insertions(+) create mode 100644 enrol/self/bulkchangeforms.php create mode 100644 enrol/self/classes/deleteselectedusers_form.php create mode 100644 enrol/self/classes/deleteselectedusers_operation.php create mode 100644 enrol/self/classes/editselectedusers_form.php create mode 100644 enrol/self/classes/editselectedusers_operation.php diff --git a/enrol/self/bulkchangeforms.php b/enrol/self/bulkchangeforms.php new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/enrol/self/classes/deleteselectedusers_form.php b/enrol/self/classes/deleteselectedusers_form.php new file mode 100644 index 0000000000000..4ebc7ed34f3cf --- /dev/null +++ b/enrol/self/classes/deleteselectedusers_form.php @@ -0,0 +1,37 @@ +. + +/** + * The form to confirm the intention to bulk delete users enrolments. + * + * @package enrol_self + * @copyright 2018 Farhan Karmali + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +require_once("$CFG->dirroot/enrol/bulkchange_forms.php"); + +/** + * The form to confirm the intention to bulk delete users enrolments. + * + * @package enrol_self + * @copyright 2018 Farhan Karmali + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class enrol_self_deleteselectedusers_form extends enrol_bulk_enrolment_confirm_form { +} \ No newline at end of file diff --git a/enrol/self/classes/deleteselectedusers_operation.php b/enrol/self/classes/deleteselectedusers_operation.php new file mode 100644 index 0000000000000..ec9ca87bc9d0a --- /dev/null +++ b/enrol/self/classes/deleteselectedusers_operation.php @@ -0,0 +1,99 @@ +. + +/** + * A bulk operation for the self enrolment plugin to delete selected users enrolments. + * + * @package enrol_self + * @copyright 2018 Farhan Karmali + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * A bulk operation for the self enrolment plugin to delete selected users enrolments. + * + * @package enrol_self + * @copyright 2018 Farhan Karmali + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class enrol_self_deleteselectedusers_operation extends enrol_bulk_enrolment_operation { + + /** + * Returns the title to display for this bulk operation. + * + * @return string + */ + public function get_identifier() { + return 'deleteselectedusers'; + } + + /** + * Returns the identifier for this bulk operation. This is the key used when the plugin + * returns an array containing all of the bulk operations it supports. + * + * @return string + */ + public function get_title() { + return get_string('deleteselectedusers', 'enrol_manual'); + } + + /** + * Returns a enrol_bulk_enrolment_operation extension form to be used + * in collecting required information for this operation to be processed. + * + * @param string|moodle_url|null $defaultaction + * @param mixed $defaultcustomdata + * @return enrol_manual_editselectedusers_form + */ + public function get_form($defaultaction = null, $defaultcustomdata = null) { + global $CFG; + if (!array($defaultcustomdata)) { + $defaultcustomdata = array(); + } + $defaultcustomdata['title'] = $this->get_title(); + $defaultcustomdata['message'] = get_string('confirmbulkdeleteenrolment', 'enrol_self'); + $defaultcustomdata['button'] = get_string('unenrolusers', 'enrol_self'); + return new enrol_self_deleteselectedusers_form($defaultaction, $defaultcustomdata); + } + + /** + * Processes the bulk operation request for the given userids with the provided properties. + * + * @param course_enrolment_manager $manager + * @param array $users + * @param stdClass $properties The data returned by the form. + */ + public function process(course_enrolment_manager $manager, array $users, stdClass $properties) { + global $DB; + + if (!has_capability("enrol/self:unenrol", $manager->get_context())) { + return false; + } + foreach ($users as $user) { + foreach ($user->enrolments as $enrolment) { + $plugin = $enrolment->enrolmentplugin; + $instance = $enrolment->enrolmentinstance; + if ($plugin->allow_unenrol_user($instance, $enrolment)) { + $plugin->unenrol_user($instance, $user->id); + } + } + } + return true; + } +} + diff --git a/enrol/self/classes/editselectedusers_form.php b/enrol/self/classes/editselectedusers_form.php new file mode 100644 index 0000000000000..7bc82f4997745 --- /dev/null +++ b/enrol/self/classes/editselectedusers_form.php @@ -0,0 +1,37 @@ +. + +/** + * The form to collect required information when bulk editing users enrolments. + * + * @package enrol_self + * @copyright 2018 Farhan Karmali + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +require_once("$CFG->dirroot/enrol/bulkchange_forms.php"); + +/** + * The form to collect required information when bulk editing users enrolments. + * + * @package enrol_self + * @copyright 2018 Farhan Karmali + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class enrol_self_editselectedusers_form extends enrol_bulk_enrolment_change_form { +} diff --git a/enrol/self/classes/editselectedusers_operation.php b/enrol/self/classes/editselectedusers_operation.php new file mode 100644 index 0000000000000..360749c08fac3 --- /dev/null +++ b/enrol/self/classes/editselectedusers_operation.php @@ -0,0 +1,163 @@ +. + +/** + * A bulk operation for the manual enrolment plugin to edit selected users. + * + * @package enrol_self + * @copyright 2018 Farhan Karmali + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * A bulk operation for the manual enrolment plugin to edit selected users. + * + * @package enrol_self + * @copyright 2018 Farhan Karmali + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class enrol_self_editselectedusers_operation extends enrol_bulk_enrolment_operation { + + /** + * Returns the title to display for this bulk operation. + * + * @return string + */ + public function get_title() { + return get_string('editselectedusers', 'enrol_self'); + } + + /** + * Returns the identifier for this bulk operation. This is the key used when the plugin + * returns an array containing all of the bulk operations it supports. + */ + public function get_identifier() { + return 'editselectedusers'; + } + + /** + * Processes the bulk operation request for the given userids with the provided properties. + * + * @param course_enrolment_manager $manager + * @param array $users + * @param stdClass $properties The data returned by the form. + */ + public function process(course_enrolment_manager $manager, array $users, stdClass $properties) { + global $DB, $USER; + + if (!has_capability("enrol/self:manage", $manager->get_context())) { + return false; + } + + // Get all of the user enrolment id's. + $ueids = array(); + $instances = array(); + foreach ($users as $user) { + foreach ($user->enrolments as $enrolment) { + $ueids[] = $enrolment->id; + if (!array_key_exists($enrolment->id, $instances)) { + $instances[$enrolment->id] = $enrolment; + } + } + } + + // Check that each instance is manageable by the current user. + foreach ($instances as $instance) { + if (!$this->plugin->allow_manage($instance)) { + return false; + } + } + + // Collect the known properties. + $status = $properties->status; + $timestart = $properties->timestart; + $timeend = $properties->timeend; + + list($ueidsql, $params) = $DB->get_in_or_equal($ueids, SQL_PARAMS_NAMED); + + $updatesql = array(); + if ($status == ENROL_USER_ACTIVE || $status == ENROL_USER_SUSPENDED) { + $updatesql[] = 'status = :status'; + $params['status'] = (int)$status; + } + if (!empty($timestart)) { + $updatesql[] = 'timestart = :timestart'; + $params['timestart'] = (int)$timestart; + } + if (!empty($timeend)) { + $updatesql[] = 'timeend = :timeend'; + $params['timeend'] = (int)$timeend; + } + if (empty($updatesql)) { + return true; + } + + // Update the modifierid. + $updatesql[] = 'modifierid = :modifierid'; + $params['modifierid'] = (int)$USER->id; + + // Update the time modified. + $updatesql[] = 'timemodified = :timemodified'; + $params['timemodified'] = time(); + + // Build the SQL statement. + $updatesql = join(', ', $updatesql); + $sql = "UPDATE {user_enrolments} + SET $updatesql + WHERE id $ueidsql"; + + if ($DB->execute($sql, $params)) { + foreach ($users as $user) { + foreach ($user->enrolments as $enrolment) { + $enrolment->courseid = $enrolment->enrolmentinstance->courseid; + $enrolment->enrol = 'self'; + // Trigger event. + $event = \core\event\user_enrolment_updated::create( + array( + 'objectid' => $enrolment->id, + 'courseid' => $enrolment->courseid, + 'context' => context_course::instance($enrolment->courseid), + 'relateduserid' => $user->id, + 'other' => array('enrol' => 'self') + ) + ); + $event->trigger(); + } + } + // Delete cached course contacts for this course because they may be affected. + cache::make('core', 'coursecontacts')->delete($manager->get_context()->instanceid); + return true; + } + + return false; + } + + /** + * Returns a enrol_bulk_enrolment_operation extension form to be used + * in collecting required information for this operation to be processed. + * + * @param string|moodle_url|null $defaultaction + * @param mixed $defaultcustomdata + * @return enrol_self_editselectedusers_form + */ + public function get_form($defaultaction = null, $defaultcustomdata = null) { + global $CFG; + return new enrol_self_editselectedusers_form($defaultaction, $defaultcustomdata); + } +} + diff --git a/enrol/self/lang/en/enrol_self.php b/enrol/self/lang/en/enrol_self.php index 5f4e5e1a39e28..9cfb17a81f757 100644 --- a/enrol/self/lang/en/enrol_self.php +++ b/enrol/self/lang/en/enrol_self.php @@ -28,6 +28,7 @@ $string['cohortnonmemberinfo'] = 'Only members of cohort \'{$a}\' can self-enrol.'; $string['cohortonly'] = 'Only cohort members'; $string['cohortonly_help'] = 'Self enrolment may be restricted to members of a specified cohort only. Note that changing this setting has no effect on existing enrolments.'; +$string['confirmbulkdeleteenrolment'] = 'Are you sure you want to delete these users enrolments?'; $string['customwelcomemessage'] = 'Custom welcome message'; $string['customwelcomemessage_help'] = 'A custom welcome message may be added as plain text or Moodle-auto format, including HTML tags and multi-lang tags. @@ -39,6 +40,8 @@ * User fullname {$a->fullname}'; $string['defaultrole'] = 'Default role assignment'; $string['defaultrole_desc'] = 'Select role which should be assigned to users during self enrolment'; +$string['deleteselectedusers'] = 'Delete selected user enrolments'; +$string['editselectedusers'] = 'Edit selected user enrolments'; $string['enrolenddate'] = 'End date'; $string['enrolenddate_help'] = 'If enabled, users can enrol themselves until this date only.'; $string['enrolenddaterror'] = 'Enrolment end date cannot be earlier than start date'; @@ -107,6 +110,7 @@ $string['unenrol'] = 'Unenrol user'; $string['unenrolselfconfirm'] = 'Do you really want to unenrol yourself from course "{$a}"?'; $string['unenroluser'] = 'Do you really want to unenrol "{$a->user}" from course "{$a->course}"?'; +$string['unenrolusers'] = 'Unenrol users'; $string['usepasswordpolicy'] = 'Use password policy'; $string['usepasswordpolicy_desc'] = 'Use standard password policy for enrolment keys.'; $string['welcometocourse'] = 'Welcome to {$a}'; diff --git a/enrol/self/lib.php b/enrol/self/lib.php index 98e9583558c3c..e521c2ef929bd 100644 --- a/enrol/self/lib.php +++ b/enrol/self/lib.php @@ -694,6 +694,25 @@ protected function get_longtimenosee_options() { return $options; } + /** + * The self enrollment plugin has several bulk operations that can be performed. + * @param course_enrolment_manager $manager + * @return array + */ + public function get_bulk_operations(course_enrolment_manager $manager) { + global $CFG; + require_once($CFG->dirroot.'/enrol/self/locallib.php'); + $context = $manager->get_context(); + $bulkoperations = array(); + if (has_capability("enrol/self:manage", $context)) { + $bulkoperations['editselectedusers'] = new enrol_self_editselectedusers_operation($manager, $this); + } + if (has_capability("enrol/self:unenrol", $context)) { + $bulkoperations['deleteselectedusers'] = new enrol_self_deleteselectedusers_operation($manager, $this); + } + return $bulkoperations; + } + /** * Add elements to the edit instance form. * diff --git a/enrol/self/locallib.php b/enrol/self/locallib.php index dc17af1f68299..850555e2990a7 100644 --- a/enrol/self/locallib.php +++ b/enrol/self/locallib.php @@ -25,6 +25,7 @@ defined('MOODLE_INTERNAL') || die(); require_once("$CFG->libdir/formslib.php"); +require_once($CFG->dirroot . '/enrol/locallib.php'); /** * Check if the given password match a group enrolment key in the specified course.