2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Self enrol plugin external functions
21 * @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
27 require_once("$CFG->libdir/externallib.php");
30 * Self enrolment external functions.
33 * @copyright 2012 Rajesh Taneja <rajesh@moodle.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class enrol_self_external
extends external_api
{
40 * Returns description of get_instance_info() parameters.
42 * @return external_function_parameters
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.'))
51 * Return self-enrolment instance information.
53 * @param int $instanceid instance id of self enrolment plugin.
54 * @return array instance information.
55 * @throws moodle_exception
57 public static function get_instance_info($instanceid) {
60 require_once($CFG->libdir
. '/enrollib.php');
62 $params = self
::validate_parameters(self
::get_instance_info_parameters(), array('instanceid' => $instanceid));
64 // Retrieve self enrolment plugin.
65 $enrolplugin = enrol_get_plugin('self');
66 if (empty($enrolplugin)) {
67 throw new moodle_exception('invaliddata', 'error');
70 self
::validate_context(context_system
::instance());
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');
78 $instanceinfo = (array) $enrolplugin->get_enrol_info($enrolinstance);
79 if (isset($instanceinfo['requiredparam']->enrolpassword
)) {
80 $instanceinfo['enrolpassword'] = $instanceinfo['requiredparam']->enrolpassword
;
82 unset($instanceinfo->requiredparam
);
88 * Returns description of get_instance_info() result value.
90 * @return external_description
92 public static function get_instance_info_returns() {
93 return new external_single_structure(
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
),
106 * Returns description of method parameters
108 * @return external_function_parameters
111 public static function enrol_user_parameters() {
112 return new external_function_parameters(
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)
122 * Self enrol the current user in the given course.
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
129 * @throws moodle_exception
131 public static function enrol_user($courseid, $password = '', $instanceid = 0) {
134 require_once($CFG->libdir
. '/enrollib.php');
136 $params = self
::validate_parameters(self
::enrol_user_parameters(),
138 'courseid' => $courseid,
139 'password' => $password,
140 'instanceid' => $instanceid
145 $course = get_course($params['courseid']);
146 $context = context_course
::instance($course->id
);
147 self
::validate_context(context_system
::instance());
149 if (!core_course_category
::can_view_course_info($course)) {
150 throw new moodle_exception('coursehidden');
153 // Retrieve the self enrolment plugin.
154 $enrol = enrol_get_plugin('self');
156 throw new moodle_exception('canntenrol', 'enrol_self');
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;
171 $instances[] = $courseenrolinstance;
176 if (empty($instances)) {
177 throw new moodle_exception('canntenrol', 'enrol_self');
180 // Try to enrol the user in the instance/s.
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
) {
187 // Check if we are using group enrolment keys.
188 if ($instance->customint1
) {
189 require_once($CFG->dirroot
. "/enrol/self/locallib.php");
191 if (!enrol_self_check_group_enrolment_key($course->id
, $params['password'])) {
193 'item' => 'instance',
194 'itemid' => $instance->id
,
195 'warningcode' => '2',
196 'message' => get_string('passwordinvalid', 'enrol_self')
201 if ($enrol->get_config('showhint')) {
202 $hint = core_text
::substr($instance->password
, 0, 1);
204 'item' => 'instance',
205 'itemid' => $instance->id
,
206 'warningcode' => '3',
207 'message' => s(get_string('passwordinvalidhint', 'enrol_self', $hint)) // message is PARAM_TEXT.
212 'item' => 'instance',
213 'itemid' => $instance->id
,
214 'warningcode' => '4',
215 'message' => get_string('passwordinvalid', 'enrol_self')
223 $data = array('enrolpassword' => $params['password']);
224 $enrol->enrol_self($instance, (object) $data);
229 'item' => 'instance',
230 'itemid' => $instance->id
,
231 'warningcode' => '1',
232 'message' => $enrolstatus
238 $result['status'] = $enrolled;
239 $result['warnings'] = $warnings;
244 * Returns description of method result value
246 * @return external_description
249 public static function enrol_user_returns() {
250 return new external_single_structure(
252 'status' => new external_value(PARAM_BOOL
, 'status: true if the user is enrolled, false otherwise'),
253 'warnings' => new external_warnings()