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.

Differences Between: [Versions 401 and 402] [Versions 401 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   * Guest enrolment method external API
  19   *
  20   * @package    enrol_guest
  21   * @category   external
  22   * @copyright  2015 Juan Leyva <juan@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @since      Moodle 3.1
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die;
  28  
  29  require_once($CFG->libdir . '/externallib.php');
  30  require_once($CFG->libdir . '/enrollib.php');
  31  
  32  /**
  33   * Guest enrolment method external API
  34   *
  35   * @package    enrol_guest
  36   * @category   external
  37   * @copyright  2015 Juan Leyva <juan@moodle.com>
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   * @since      Moodle 3.1
  40   */
  41  class enrol_guest_external extends external_api {
  42  
  43      /**
  44       * Returns description of get_instance_info() parameters.
  45       *
  46       * @return external_function_parameters
  47       * @since Moodle 3.1
  48       */
  49      public static function get_instance_info_parameters() {
  50          return new external_function_parameters(
  51                  array('instanceid' => new external_value(PARAM_INT, 'Instance id of guest enrolment plugin.'))
  52              );
  53      }
  54  
  55      /**
  56       * Return guest enrolment instance information.
  57       *
  58       * @param int $instanceid instance id of guest enrolment plugin.
  59       * @return array warnings and instance information.
  60       * @since Moodle 3.1
  61       */
  62      public static function get_instance_info($instanceid) {
  63          global $DB;
  64  
  65          $params = self::validate_parameters(self::get_instance_info_parameters(), array('instanceid' => $instanceid));
  66          $warnings = array();
  67  
  68          // Retrieve guest enrolment plugin.
  69          $enrolplugin = enrol_get_plugin('guest');
  70          if (empty($enrolplugin)) {
  71              throw new moodle_exception('invaliddata', 'error');
  72          }
  73  
  74          self::validate_context(context_system::instance());
  75          $enrolinstance = $DB->get_record('enrol', array('id' => $params['instanceid']), '*', MUST_EXIST);
  76  
  77          $course = $DB->get_record('course', array('id' => $enrolinstance->courseid), '*', MUST_EXIST);
  78          if (!core_course_category::can_view_course_info($course) && !can_access_course($course)) {
  79              throw new moodle_exception('coursehidden');
  80          }
  81  
  82          $instanceinfo = $enrolplugin->get_enrol_info($enrolinstance);
  83          // Specific instance information.
  84          $instanceinfo->passwordrequired = $instanceinfo->requiredparam->passwordrequired;
  85  
  86          unset($instanceinfo->requiredparam);
  87  
  88          $result = array();
  89          $result['instanceinfo'] = $instanceinfo;
  90          $result['warnings'] = $warnings;
  91          return $result;
  92      }
  93  
  94      /**
  95       * Returns description of get_instance_info() result value.
  96       *
  97       * @return external_description
  98       * @since Moodle 3.1
  99       */
 100      public static function get_instance_info_returns() {
 101          return new external_single_structure(
 102              array(
 103                  'instanceinfo' => new external_single_structure(
 104                      array(
 105                          'id' => new external_value(PARAM_INT, 'Id of course enrolment instance'),
 106                          'courseid' => new external_value(PARAM_INT, 'Id of course'),
 107                          'type' => new external_value(PARAM_PLUGIN, 'Type of enrolment plugin'),
 108                          'name' => new external_value(PARAM_RAW, 'Name of enrolment plugin'),
 109                          'status' => new external_value(PARAM_BOOL, 'Is the enrolment enabled?'),
 110                          'passwordrequired' => new external_value(PARAM_BOOL, 'Is a password required?'),
 111                      )
 112                  ),
 113                  'warnings' => new external_warnings()
 114              )
 115          );
 116      }
 117  
 118  }