Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.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 manual enrolment plugin to edit selected users.
  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 manual enrolment plugin to edit selected users.
  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_editselectedusers_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_title() {
  42          return get_string('editselectedusers', 'enrol_self');
  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      public function get_identifier() {
  50          return 'editselectedusers';
  51      }
  52  
  53      /**
  54       * Processes the bulk operation request for the given userids with the provided properties.
  55       *
  56       * @param course_enrolment_manager $manager
  57       * @param array $users
  58       * @param stdClass $properties The data returned by the form.
  59       */
  60      public function process(course_enrolment_manager $manager, array $users, stdClass $properties) {
  61          global $DB, $USER;
  62  
  63          if (!has_capability("enrol/self:manage", $manager->get_context())) {
  64              return false;
  65          }
  66  
  67          // Get all of the user enrolment id's.
  68          $ueids = array();
  69          $instances = array();
  70          foreach ($users as $user) {
  71              foreach ($user->enrolments as $enrolment) {
  72                  $ueids[] = $enrolment->id;
  73                  if (!array_key_exists($enrolment->id, $instances)) {
  74                      $instances[$enrolment->id] = $enrolment;
  75                  }
  76              }
  77          }
  78  
  79          // Check that each instance is manageable by the current user.
  80          foreach ($instances as $instance) {
  81              if (!$this->plugin->allow_manage($instance)) {
  82                  return false;
  83              }
  84          }
  85  
  86          // Collect the known properties.
  87          $status = $properties->status;
  88          $timestart = $properties->timestart;
  89          $timeend = $properties->timeend;
  90  
  91          list($ueidsql, $params) = $DB->get_in_or_equal($ueids, SQL_PARAMS_NAMED);
  92  
  93          $updatesql = array();
  94          if ($status == ENROL_USER_ACTIVE || $status == ENROL_USER_SUSPENDED) {
  95              $updatesql[] = 'status = :status';
  96              $params['status'] = (int)$status;
  97          }
  98          if (!empty($timestart)) {
  99              $updatesql[] = 'timestart = :timestart';
 100              $params['timestart'] = (int)$timestart;
 101          }
 102          if (!empty($timeend)) {
 103              $updatesql[] = 'timeend = :timeend';
 104              $params['timeend'] = (int)$timeend;
 105          }
 106          if (empty($updatesql)) {
 107              return true;
 108          }
 109  
 110          // Update the modifierid.
 111          $updatesql[] = 'modifierid = :modifierid';
 112          $params['modifierid'] = (int)$USER->id;
 113  
 114          // Update the time modified.
 115          $updatesql[] = 'timemodified = :timemodified';
 116          $params['timemodified'] = time();
 117  
 118          // Build the SQL statement.
 119          $updatesql = join(', ', $updatesql);
 120          $sql = "UPDATE {user_enrolments}
 121                     SET $updatesql
 122                   WHERE id $ueidsql";
 123  
 124          if ($DB->execute($sql, $params)) {
 125              foreach ($users as $user) {
 126                  foreach ($user->enrolments as $enrolment) {
 127                      $enrolment->courseid  = $enrolment->enrolmentinstance->courseid;
 128                      $enrolment->enrol     = 'self';
 129                      // Trigger event.
 130                      $event = \core\event\user_enrolment_updated::create(
 131                          array(
 132                              'objectid' => $enrolment->id,
 133                              'courseid' => $enrolment->courseid,
 134                              'context' => context_course::instance($enrolment->courseid),
 135                              'relateduserid' => $user->id,
 136                              'other' => array('enrol' => 'self')
 137                          )
 138                      );
 139                      $event->trigger();
 140                  }
 141              }
 142              // Delete cached course contacts for this course because they may be affected.
 143              cache::make('core', 'coursecontacts')->delete($manager->get_context()->instanceid);
 144              return true;
 145          }
 146  
 147          return false;
 148      }
 149  
 150      /**
 151       * Returns a enrol_bulk_enrolment_operation extension form to be used
 152       * in collecting required information for this operation to be processed.
 153       *
 154       * @param string|moodle_url|null $defaultaction
 155       * @param mixed $defaultcustomdata
 156       * @return enrol_self_editselectedusers_form
 157       */
 158      public function get_form($defaultaction = null, $defaultcustomdata = null) {
 159          return new enrol_self_editselectedusers_form($defaultaction, $defaultcustomdata);
 160      }
 161  }