Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 402] [Versions 400 and 403]

   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   * Self enrol plugin external functions
  19   *
  20   * @package    enrol_self
  21   * @copyright  2013 Rajesh Taneja <rajesh@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  require_once("$CFG->libdir/externallib.php");
  28  
  29  /**
  30   * Self enrolment external functions.
  31   *
  32   * @package   enrol_self
  33   * @copyright 2012 Rajesh Taneja <rajesh@moodle.com>
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   * @since     Moodle 2.6
  36   */
  37  class enrol_self_external extends external_api {
  38  
  39      /**
  40       * Returns description of get_instance_info() parameters.
  41       *
  42       * @return external_function_parameters
  43       */
  44      public static function get_instance_info_parameters() {
  45          return new external_function_parameters(
  46                  array('instanceid' => new external_value(PARAM_INT, 'instance id of self enrolment plugin.'))
  47              );
  48      }
  49  
  50      /**
  51       * Return self-enrolment instance information.
  52       *
  53       * @param int $instanceid instance id of self enrolment plugin.
  54       * @return array instance information.
  55       * @throws moodle_exception
  56       */
  57      public static function get_instance_info($instanceid) {
  58          global $DB, $CFG;
  59  
  60          require_once($CFG->libdir . '/enrollib.php');
  61  
  62          $params = self::validate_parameters(self::get_instance_info_parameters(), array('instanceid' => $instanceid));
  63  
  64          // Retrieve self enrolment plugin.
  65          $enrolplugin = enrol_get_plugin('self');
  66          if (empty($enrolplugin)) {
  67              throw new moodle_exception('invaliddata', 'error');
  68          }
  69  
  70          self::validate_context(context_system::instance());
  71  
  72          $enrolinstance = $DB->get_record('enrol', array('id' => $params['instanceid']), '*', MUST_EXIST);
  73          $course = $DB->get_record('course', array('id' => $enrolinstance->courseid), '*', MUST_EXIST);
  74          if (!core_course_category::can_view_course_info($course) && !can_access_course($course)) {
  75              throw new moodle_exception('coursehidden');
  76          }
  77  
  78          $instanceinfo = (array) $enrolplugin->get_enrol_info($enrolinstance);
  79          if (isset($instanceinfo['requiredparam']->enrolpassword)) {
  80              $instanceinfo['enrolpassword'] = $instanceinfo['requiredparam']->enrolpassword;
  81          }
  82          unset($instanceinfo->requiredparam);
  83  
  84          return $instanceinfo;
  85      }
  86  
  87      /**
  88       * Returns description of get_instance_info() result value.
  89       *
  90       * @return external_description
  91       */
  92      public static function get_instance_info_returns() {
  93          return new external_single_structure(
  94              array(
  95                  'id' => new external_value(PARAM_INT, 'id of course enrolment instance'),
  96                  'courseid' => new external_value(PARAM_INT, 'id of course'),
  97                  'type' => new external_value(PARAM_PLUGIN, 'type of enrolment plugin'),
  98                  'name' => new external_value(PARAM_RAW, 'name of enrolment plugin'),
  99                  'status' => new external_value(PARAM_RAW, 'status of enrolment plugin'),
 100                  'enrolpassword' => new external_value(PARAM_RAW, 'password required for enrolment', VALUE_OPTIONAL),
 101              )
 102          );
 103      }
 104  
 105      /**
 106       * Returns description of method parameters
 107       *
 108       * @return external_function_parameters
 109       * @since Moodle 3.0
 110       */
 111      public static function enrol_user_parameters() {
 112          return new external_function_parameters(
 113              array(
 114                  'courseid' => new external_value(PARAM_INT, 'Id of the course'),
 115                  'password' => new external_value(PARAM_RAW, 'Enrolment key', VALUE_DEFAULT, ''),
 116                  'instanceid' => new external_value(PARAM_INT, 'Instance id of self enrolment plugin.', VALUE_DEFAULT, 0)
 117              )
 118          );
 119      }
 120  
 121      /**
 122       * Self enrol the current user in the given course.
 123       *
 124       * @param int $courseid id of course
 125       * @param string $password enrolment key
 126       * @param int $instanceid instance id of self enrolment plugin
 127       * @return array of warnings and status result
 128       * @since Moodle 3.0
 129       * @throws moodle_exception
 130       */
 131      public static function enrol_user($courseid, $password = '', $instanceid = 0) {
 132          global $CFG;
 133  
 134          require_once($CFG->libdir . '/enrollib.php');
 135  
 136          $params = self::validate_parameters(self::enrol_user_parameters(),
 137                                              array(
 138                                                  'courseid' => $courseid,
 139                                                  'password' => $password,
 140                                                  'instanceid' => $instanceid
 141                                              ));
 142  
 143          $warnings = array();
 144  
 145          $course = get_course($params['courseid']);
 146          $context = context_course::instance($course->id);
 147          self::validate_context(context_system::instance());
 148  
 149          if (!core_course_category::can_view_course_info($course)) {
 150              throw new moodle_exception('coursehidden');
 151          }
 152  
 153          // Retrieve the self enrolment plugin.
 154          $enrol = enrol_get_plugin('self');
 155          if (empty($enrol)) {
 156              throw new moodle_exception('canntenrol', 'enrol_self');
 157          }
 158  
 159          // We can expect multiple self-enrolment instances.
 160          $instances = array();
 161          $enrolinstances = enrol_get_instances($course->id, true);
 162          foreach ($enrolinstances as $courseenrolinstance) {
 163              if ($courseenrolinstance->enrol == "self") {
 164                  // Instance specified.
 165                  if (!empty($params['instanceid'])) {
 166                      if ($courseenrolinstance->id == $params['instanceid']) {
 167                          $instances[] = $courseenrolinstance;
 168                          break;
 169                      }
 170                  } else {
 171                      $instances[] = $courseenrolinstance;
 172                  }
 173  
 174              }
 175          }
 176          if (empty($instances)) {
 177              throw new moodle_exception('canntenrol', 'enrol_self');
 178          }
 179  
 180          // Try to enrol the user in the instance/s.
 181          $enrolled = false;
 182          foreach ($instances as $instance) {
 183              $enrolstatus = $enrol->can_self_enrol($instance);
 184              if ($enrolstatus === true) {
 185                  if ($instance->password and $params['password'] !== $instance->password) {
 186  
 187                      // Check if we are using group enrolment keys.
 188                      if ($instance->customint1) {
 189                          require_once($CFG->dirroot . "/enrol/self/locallib.php");
 190  
 191                          if (!enrol_self_check_group_enrolment_key($course->id, $params['password'])) {
 192                              $warnings[] = array(
 193                                  'item' => 'instance',
 194                                  'itemid' => $instance->id,
 195                                  'warningcode' => '2',
 196                                  'message' => get_string('passwordinvalid', 'enrol_self')
 197                              );
 198                              continue;
 199                          }
 200                      } else {
 201                          if ($enrol->get_config('showhint')) {
 202                              $hint = core_text::substr($instance->password, 0, 1);
 203                              $warnings[] = array(
 204                                  'item' => 'instance',
 205                                  'itemid' => $instance->id,
 206                                  'warningcode' => '3',
 207                                  'message' => s(get_string('passwordinvalidhint', 'enrol_self', $hint)) // message is PARAM_TEXT.
 208                              );
 209                              continue;
 210                          } else {
 211                              $warnings[] = array(
 212                                  'item' => 'instance',
 213                                  'itemid' => $instance->id,
 214                                  'warningcode' => '4',
 215                                  'message' => get_string('passwordinvalid', 'enrol_self')
 216                              );
 217                              continue;
 218                          }
 219                      }
 220                  }
 221  
 222                  // Do the enrolment.
 223                  $data = array('enrolpassword' => $params['password']);
 224                  $enrol->enrol_self($instance, (object) $data);
 225                  $enrolled = true;
 226                  break;
 227              } else {
 228                  $warnings[] = array(
 229                      'item' => 'instance',
 230                      'itemid' => $instance->id,
 231                      'warningcode' => '1',
 232                      'message' => $enrolstatus
 233                  );
 234              }
 235          }
 236  
 237          $result = array();
 238          $result['status'] = $enrolled;
 239          $result['warnings'] = $warnings;
 240          return $result;
 241      }
 242  
 243      /**
 244       * Returns description of method result value
 245       *
 246       * @return external_description
 247       * @since Moodle 3.0
 248       */
 249      public static function enrol_user_returns() {
 250          return new external_single_structure(
 251              array(
 252                  'status' => new external_value(PARAM_BOOL, 'status: true if the user is enrolled, false otherwise'),
 253                  'warnings' => new external_warnings()
 254              )
 255          );
 256      }
 257  }