|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00028 defined('MOODLE_INTERNAL') || die(); 00029 00030 require_once("$CFG->libdir/externallib.php"); 00031 00035 class enrol_manual_external extends external_api { 00036 00041 public static function enrol_users_parameters() { 00042 return new external_function_parameters( 00043 array( 00044 'enrolments' => new external_multiple_structure( 00045 new external_single_structure( 00046 array( 00047 'roleid' => new external_value(PARAM_INT, 'Role to assign to the user'), 00048 'userid' => new external_value(PARAM_INT, 'The user that is going to be enrolled'), 00049 'courseid' => new external_value(PARAM_INT, 'The course to enrol the user role in'), 00050 'timestart' => new external_value(PARAM_INT, 'Timestamp when the enrolment start', VALUE_OPTIONAL), 00051 'timeend' => new external_value(PARAM_INT, 'Timestamp when the enrolment end', VALUE_OPTIONAL), 00052 'suspend' => new external_value(PARAM_INT, 'set to 1 to suspend the enrolment', VALUE_OPTIONAL) 00053 ) 00054 ) 00055 ) 00056 ) 00057 ); 00058 } 00059 00066 public static function enrol_users($enrolments) { 00067 global $DB, $CFG; 00068 00069 require_once($CFG->libdir . '/enrollib.php'); 00070 00071 $params = self::validate_parameters(self::enrol_users_parameters(), 00072 array('enrolments' => $enrolments)); 00073 00074 $transaction = $DB->start_delegated_transaction(); //rollback all enrolment if an error occurs 00075 //(except if the DB doesn't support it) 00076 00077 //retrieve the manual enrolment plugin 00078 $enrol = enrol_get_plugin('manual'); 00079 if (empty($enrol)) { 00080 throw new moodle_exception('manualpluginnotinstalled', 'enrol_manual'); 00081 } 00082 00083 foreach ($params['enrolments'] as $enrolment) { 00084 // Ensure the current user is allowed to run this function in the enrolment context 00085 $context = get_context_instance(CONTEXT_COURSE, $enrolment['courseid']); 00086 self::validate_context($context); 00087 00088 //check that the user has the permission to manual enrol 00089 require_capability('enrol/manual:enrol', $context); 00090 00091 //throw an exception if user is not able to assign the role 00092 $roles = get_assignable_roles($context); 00093 if (!key_exists($enrolment['roleid'], $roles)) { 00094 $errorparams = new stdClass(); 00095 $errorparams->roleid = $enrolment['roleid']; 00096 $errorparams->courseid = $enrolment['courseid']; 00097 $errorparams->userid = $enrolment['userid']; 00098 throw new moodle_exception('wsusercannotassign', 'enrol_manual', '', $errorparams); 00099 } 00100 00101 //check manual enrolment plugin instance is enabled/exist 00102 $enrolinstances = enrol_get_instances($enrolment['courseid'], true); 00103 foreach ($enrolinstances as $courseenrolinstance) { 00104 if ($courseenrolinstance->enrol == "manual") { 00105 $instance = $courseenrolinstance; 00106 break; 00107 } 00108 } 00109 if (empty($instance)) { 00110 $errorparams = new stdClass(); 00111 $errorparams->courseid = $enrolment['courseid']; 00112 throw new moodle_exception('wsnoinstance', 'enrol_manual', $errorparams); 00113 } 00114 00115 //check that the plugin accept enrolment (it should always the case, it's hard coded in the plugin) 00116 if (!$enrol->allow_enrol($instance)) { 00117 $errorparams = new stdClass(); 00118 $errorparams->roleid = $enrolment['roleid']; 00119 $errorparams->courseid = $enrolment['courseid']; 00120 $errorparams->userid = $enrolment['userid']; 00121 throw new moodle_exception('wscannotenrol', 'enrol_manual', '', $errorparams); 00122 } 00123 00124 //finally proceed the enrolment 00125 $enrolment['timestart'] = isset($enrolment['timestart']) ? $enrolment['timestart'] : 0; 00126 $enrolment['timeend'] = isset($enrolment['timeend']) ? $enrolment['timeend'] : 0; 00127 $enrolment['status'] = (isset($enrolment['suspend']) && !empty($enrolment['suspend'])) ? 00128 ENROL_USER_SUSPENDED : ENROL_USER_ACTIVE; 00129 00130 $enrol->enrol_user($instance, $enrolment['userid'], $enrolment['roleid'], 00131 $enrolment['timestart'], $enrolment['timeend'], $enrolment['status']); 00132 00133 } 00134 00135 $transaction->allow_commit(); 00136 } 00137 00142 public static function enrol_users_returns() { 00143 return null; 00144 } 00145 00146 } 00147 00152 class moodle_enrol_manual_external extends external_api { 00153 00159 public static function manual_enrol_users_parameters() { 00160 return enrol_manual_external::enrol_users_parameters(); 00161 } 00162 00170 public static function manual_enrol_users($enrolments) { 00171 return enrol_manual_external::enrol_users($enrolments); 00172 } 00173 00179 public static function manual_enrol_users_returns() { 00180 return enrol_manual_external::enrol_users_returns(); 00181 } 00182 00183 }