[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/enrol/self/ -> externallib.php (source)

   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          $context = context_course::instance($course->id);
  75          if (!$course->visible and !has_capability('moodle/course:viewhiddencourses', $context)) {
  76              throw new moodle_exception('coursehidden');
  77          }
  78  
  79          $instanceinfo = (array) $enrolplugin->get_enrol_info($enrolinstance);
  80          if (isset($instanceinfo['requiredparam']->enrolpassword)) {
  81              $instanceinfo['enrolpassword'] = $instanceinfo['requiredparam']->enrolpassword;
  82          }
  83          unset($instanceinfo->requiredparam);
  84  
  85          return $instanceinfo;
  86      }
  87  
  88      /**
  89       * Returns description of get_instance_info() result value.
  90       *
  91       * @return external_description
  92       */
  93      public static function get_instance_info_returns() {
  94          return new external_single_structure(
  95              array(
  96                  'id' => new external_value(PARAM_INT, 'id of course enrolment instance'),
  97                  'courseid' => new external_value(PARAM_INT, 'id of course'),
  98                  'type' => new external_value(PARAM_PLUGIN, 'type of enrolment plugin'),
  99                  'name' => new external_value(PARAM_RAW, 'name of enrolment plugin'),
 100                  'status' => new external_value(PARAM_RAW, 'status of enrolment plugin'),
 101                  'enrolpassword' => new external_value(PARAM_RAW, 'password required for enrolment', VALUE_OPTIONAL),
 102              )
 103          );
 104      }
 105  
 106      /**
 107       * Returns description of method parameters
 108       *
 109       * @return external_function_parameters
 110       * @since Moodle 3.0
 111       */
 112      public static function enrol_user_parameters() {
 113          return new external_function_parameters(
 114              array(
 115                  'courseid' => new external_value(PARAM_INT, 'Id of the course'),
 116                  'password' => new external_value(PARAM_RAW, 'Enrolment key', VALUE_DEFAULT, ''),
 117                  'instanceid' => new external_value(PARAM_INT, 'Instance id of self enrolment plugin.', VALUE_DEFAULT, 0)
 118              )
 119          );
 120      }
 121  
 122      /**
 123       * Self enrol the current user in the given course.
 124       *
 125       * @param int $courseid id of course
 126       * @param string $password enrolment key
 127       * @param int $instanceid instance id of self enrolment plugin
 128       * @return array of warnings and status result
 129       * @since Moodle 3.0
 130       * @throws moodle_exception
 131       */
 132      public static function enrol_user($courseid, $password = '', $instanceid = 0) {
 133          global $CFG;
 134  
 135          require_once($CFG->libdir . '/enrollib.php');
 136  
 137          $params = self::validate_parameters(self::enrol_user_parameters(),
 138                                              array(
 139                                                  'courseid' => $courseid,
 140                                                  'password' => $password,
 141                                                  'instanceid' => $instanceid
 142                                              ));
 143  
 144          $warnings = array();
 145  
 146          $course = get_course($params['courseid']);
 147          $context = context_course::instance($course->id);
 148          self::validate_context(context_system::instance());
 149  
 150          if (!$course->visible and !has_capability('moodle/course:viewhiddencourses', $context)) {
 151              throw new moodle_exception('coursehidden');
 152          }
 153  
 154          // Retrieve the self enrolment plugin.
 155          $enrol = enrol_get_plugin('self');
 156          if (empty($enrol)) {
 157              throw new moodle_exception('canntenrol', 'enrol_self');
 158          }
 159  
 160          // We can expect multiple self-enrolment instances.
 161          $instances = array();
 162          $enrolinstances = enrol_get_instances($course->id, true);
 163          foreach ($enrolinstances as $courseenrolinstance) {
 164              if ($courseenrolinstance->enrol == "self") {
 165                  // Instance specified.
 166                  if (!empty($params['instanceid'])) {
 167                      if ($courseenrolinstance->id == $params['instanceid']) {
 168                          $instances[] = $courseenrolinstance;
 169                          break;
 170                      }
 171                  } else {
 172                      $instances[] = $courseenrolinstance;
 173                  }
 174  
 175              }
 176          }
 177          if (empty($instances)) {
 178              throw new moodle_exception('canntenrol', 'enrol_self');
 179          }
 180  
 181          // Try to enrol the user in the instance/s.
 182          $enrolled = false;
 183          foreach ($instances as $instance) {
 184              $enrolstatus = $enrol->can_self_enrol($instance);
 185              if ($enrolstatus === true) {
 186                  if ($instance->password and $params['password'] !== $instance->password) {
 187  
 188                      // Check if we are using group enrolment keys.
 189                      if ($instance->customint1) {
 190                          require_once($CFG->dirroot . "/enrol/self/locallib.php");
 191  
 192                          if (!enrol_self_check_group_enrolment_key($course->id, $params['password'])) {
 193                              $warnings[] = array(
 194                                  'item' => 'instance',
 195                                  'itemid' => $instance->id,
 196                                  'warningcode' => '2',
 197                                  'message' => get_string('passwordinvalid', 'enrol_self')
 198                              );
 199                              continue;
 200                          }
 201                      } else {
 202                          if ($enrol->get_config('showhint')) {
 203                              $hint = core_text::substr($instance->password, 0, 1);
 204                              $warnings[] = array(
 205                                  'item' => 'instance',
 206                                  'itemid' => $instance->id,
 207                                  'warningcode' => '3',
 208                                  'message' => s(get_string('passwordinvalidhint', 'enrol_self', $hint)) // message is PARAM_TEXT.
 209                              );
 210                              continue;
 211                          } else {
 212                              $warnings[] = array(
 213                                  'item' => 'instance',
 214                                  'itemid' => $instance->id,
 215                                  'warningcode' => '4',
 216                                  'message' => get_string('passwordinvalid', 'enrol_self')
 217                              );
 218                              continue;
 219                          }
 220                      }
 221                  }
 222  
 223                  // Do the enrolment.
 224                  $data = array('enrolpassword' => $params['password']);
 225                  $enrol->enrol_self($instance, (object) $data);
 226                  $enrolled = true;
 227                  break;
 228              } else {
 229                  $warnings[] = array(
 230                      'item' => 'instance',
 231                      'itemid' => $instance->id,
 232                      'warningcode' => '1',
 233                      'message' => $enrolstatus
 234                  );
 235              }
 236          }
 237  
 238          $result = array();
 239          $result['status'] = $enrolled;
 240          $result['warnings'] = $warnings;
 241          return $result;
 242      }
 243  
 244      /**
 245       * Returns description of method result value
 246       *
 247       * @return external_description
 248       * @since Moodle 3.0
 249       */
 250      public static function enrol_user_returns() {
 251          return new external_single_structure(
 252              array(
 253                  'status' => new external_value(PARAM_BOOL, 'status: true if the user is enrolled, false otherwise'),
 254                  'warnings' => new external_warnings()
 255              )
 256          );
 257      }
 258  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1