Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   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.
   8  //
   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.
  13  //
  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/>.
  16  
  17  /**
  18   * A bulk operation for the self enrolment plugin to delete selected users enrolments.
  19   *
  20   * @package enrol_self
  21   * @copyright 2018 Farhan Karmali
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * A bulk operation for the self enrolment plugin to delete selected users enrolments.
  29   *
  30   * @package enrol_self
  31   * @copyright 2018 Farhan Karmali
  32   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class enrol_self_deleteselectedusers_operation extends enrol_bulk_enrolment_operation {
  35  
  36      /**
  37       * Returns the title to display for this bulk operation.
  38       *
  39       * @return string
  40       */
  41      public function get_identifier() {
  42          return 'deleteselectedusers';
  43      }
  44  
  45      /**
  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.
  48       *
  49       * @return string
  50       */
  51      public function get_title() {
  52          return get_string('deleteselectedusers', 'enrol_self');
  53      }
  54  
  55      /**
  56       * Returns a enrol_bulk_enrolment_operation extension form to be used
  57       * in collecting required information for this operation to be processed.
  58       *
  59       * @param string|moodle_url|null $defaultaction
  60       * @param mixed $defaultcustomdata
  61       * @return enrol_self_deleteselectedusers_form
  62       */
  63      public function get_form($defaultaction = null, $defaultcustomdata = null) {
  64          if (!array($defaultcustomdata)) {
  65              $defaultcustomdata = array();
  66          }
  67          $defaultcustomdata['title'] = $this->get_title();
  68          $defaultcustomdata['message'] = get_string('confirmbulkdeleteenrolment', 'enrol_self');
  69          $defaultcustomdata['button'] = get_string('unenrolusers', 'enrol_self');
  70  
  71          return new enrol_self_deleteselectedusers_form($defaultaction, $defaultcustomdata);
  72      }
  73  
  74      /**
  75       * Processes the bulk operation request for the given userids with the provided properties.
  76       *
  77       * @param course_enrolment_manager $manager
  78       * @param array $users
  79       * @param stdClass $properties The data returned by the form.
  80       */
  81      public function process(course_enrolment_manager $manager, array $users, stdClass $properties) {
  82          if (!has_capability("enrol/self:unenrol", $manager->get_context())) {
  83              return false;
  84          }
  85  
  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);
  92                  }
  93              }
  94          }
  95  
  96          return true;
  97      }
  98  }