Details
-
Type: Improvement
-
Status: Open
-
Priority: Minor
-
Resolution:Unresolved
-
Affects Version/s:2.0.2
-
Fix Version/s:None
-
Component/s:Web Services
-
Labels:
None
enrol_user(array $user, array $course, array $role)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
enrol_user(array('username' => 'chrisf') , array('shortname' => 'PHP 101'), array('shortname' => 'student') ); enrol_user(array('idnumber' => '0123456'), array('idnumber' => '2010-2012-PHP101'), array('id' => '3') ); enrol_user(array('email' => 'chrisf@example.com'), array('fullname' => 'Introduction to PHP programming'), array('name' => 'Student') ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
moodleDir/enrol/manual/externallib.php /** * Returns description of method parameters * @return external_function_parameters */ public static function manual_enrol_users_by_name_parameters() { return new external_function_parameters( array( 'enrolments' => new external_multiple_structure( new external_single_structure( array( 'roleid' => new external_value(PARAM_INT, 'Role to assign to the user'), 'username' => new external_value(PARAM_INT, 'The user that is going to be enrolled'), 'coursename' => new external_value(PARAM_INT, 'The course to enrol the user role in'), 'timestart' => new external_value(PARAM_INT, 'Timestamp when the enrolment start', VALUE_OPTIONAL), 'timeend' => new external_value(PARAM_INT, 'Timestamp when the enrolment end', VALUE_OPTIONAL), 'suspend' => new external_value(PARAM_INT, 'set to 1 to suspend the enrolment', VALUE_OPTIONAL) ) ) ) ) ); } /** * Enrolment of users * Function throw an exception at the first error encountered. * @param array $enrolments An array of user enrolment * @return null */ public static function manual_enrol_users_by_name($enrolments) { global $DB, $CFG; require_once($CFG->libdir . '/enrollib.php'); $params = self::validate_parameters(self::manual_enrol_users_parameters(), array('enrolments' => $enrolments)); $transaction = $DB->start_delegated_transaction(); //rollback all enrolment if an error occurs //(except if the DB doesn't support it) //retrieve the manual enrolment plugin $enrol = enrol_get_plugin('manual'); if (empty($enrol)) { throw new moodle_exception('manualpluginnotinstalled', 'enrol_manual'); } foreach ($params['enrolments'] as $enrolment) { // Ensure the current user is allowed to run this function in the enrolment context $context = get_context_instance(CONTEXT_COURSE, $enrolment['courseid']); self::validate_context($context); //check that the user has the permission to manual enrol require_capability('enrol/manual:enrol', $context); //throw an exception if user is not able to assign the role $roles = get_assignable_roles($context); if (!key_exists($enrolment['roleid'], $roles)) { $errorparams = new stdClass(); $errorparams->roleid = $enrolment['roleid']; $errorparams->coursename = $enrolment['coursename']; $errorparams->username = $enrolment['username']; throw new moodle_exception('wsusercannotassign', 'enrol_manual', '', $errorparams); } //convert coursename to courseid $courses = $DB->get_records_list('course', 'shortname', $enrolment['coursename']); if(count($courses) < 1){ //error no returned courses $errorparams = new stdClass(); $errorparams->roleid = $enrolment['roleid']; $errorparams->coursename = $enrolment['shortname']; throw new moodle_exception('wsnocourse', 'enrol_manual', '', $errorparams); } $enrolment['courseid'] = $courses[0]->id; //convert username to userid $users = user_get_users_by_username($enrolment['username']); if(count($users) < 1){ //error no returned users $errorparams = new stdClass(); $errorparams->roleid = $enrolment['roleid']; $errorparams->username = $enrolment['username']; throw new moodle_exception('wsnouser', 'enrol_manual', '', $errorparams); } $enrolment['userid'] = $users[0]->id; //check manual enrolment plugin instance is enabled/exist $enrolinstances = enrol_get_instances($enrolment['courseid'], true); foreach ($enrolinstances as $courseenrolinstance) { if ($courseenrolinstance->enrol == "manual") { $instance = $courseenrolinstance; break; } } if (empty($instance)) { $errorparams = new stdClass(); $errorparams->courseid = $enrolment['courseid']; throw new moodle_exception('wsnoinstance', 'enrol_manual', $errorparams); } //check that the plugin accept enrolment (it should always the case, it's hard coded in the plugin) if (!$enrol->allow_enrol($instance)) { $errorparams = new stdClass(); $errorparams->roleid = $enrolment['roleid']; $errorparams->courseid = $enrolment['courseid']; $errorparams->userid = $enrolment['userid']; throw new moodle_exception('wscannotenrol', 'enrol_manual', '', $errorparams); } //finally proceed the enrolment $enrolment['timestart'] = isset($enrolment['timestart']) ? $enrolment['timestart'] : 0; $enrolment['timeend'] = isset($enrolment['timeend']) ? $enrolment['timeend'] : 0; $enrolment['status'] = (isset($enrolment['suspend']) && !empty($enrolment['suspend'])) ? ENROL_USER_SUSPENDED : ENROL_USER_ACTIVE; $enrol->enrol_user($instance, $enrolment['userid'], $enrolment['roleid'], $enrolment['timestart'], $enrolment['timeend'], $enrolment['status']); } $transaction->allow_commit(); } /** * Returns description of method result value * @return external_description */ public static function manual_enrol_users_by_name_returns() { return null; } |
moodleDir/enrol/manual/externallib.php
Besides this chunk I also added in some extra error messages:
moodleDir/enrol/manual/lang/en/enrol_manual.php
1 2 |
$string['wsnouser'] = 'Cannot find a user by username ({$a->username}).'; $string['wsnocourse'] = 'Cannot find a course by shortname ({$a->shortname}).'; |