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