|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00030 defined('MOODLE_INTERNAL') || die(); 00031 00032 require_once("$CFG->libdir/externallib.php"); 00033 00037 class core_enrol_external extends external_api { 00038 00043 public static function get_users_courses_parameters() { 00044 return new external_function_parameters( 00045 array( 00046 'userid' => new external_value(PARAM_INT, 'user id'), 00047 ) 00048 ); 00049 } 00050 00059 public static function get_users_courses($userid) { 00060 global $USER, $DB; 00061 00062 // Do basic automatic PARAM checks on incoming data, using params description 00063 // If any problems are found then exceptions are thrown with helpful error messages 00064 $params = self::validate_parameters(self::get_users_courses_parameters(), array('userid'=>$userid)); 00065 00066 $courses = enrol_get_users_courses($params['userid'], true, 'id, shortname, fullname, idnumber, visible'); 00067 $result = array(); 00068 00069 foreach ($courses as $course) { 00070 $context = get_context_instance(CONTEXT_COURSE, $course->id); 00071 try { 00072 self::validate_context($context); 00073 } catch (Exception $e) { 00074 // current user can not access this course, sorry we can not disclose who is enrolled in this course! 00075 continue; 00076 } 00077 00078 if ($userid != $USER->id and !has_capability('moodle/course:viewparticipants', $context)) { 00079 // we need capability to view participants 00080 continue; 00081 } 00082 00083 list($enrolledsqlselect, $enrolledparams) = get_enrolled_sql($context); 00084 $enrolledsql = "SELECT COUNT(*) FROM ($enrolledsqlselect) AS enrolleduserids"; 00085 $enrolledusercount = $DB->count_records_sql($enrolledsql, $enrolledparams); 00086 00087 $result[] = array('id'=>$course->id, 'shortname'=>$course->shortname, 'fullname'=>$course->fullname, 'idnumber'=>$course->idnumber,'visible'=>$course->visible, 'enrolledusercount'=>$enrolledusercount); 00088 } 00089 00090 return $result; 00091 } 00092 00097 public static function get_users_courses_returns() { 00098 return new external_multiple_structure( 00099 new external_single_structure( 00100 array( 00101 'id' => new external_value(PARAM_INT, 'id of course'), 00102 'shortname' => new external_value(PARAM_RAW, 'short name of course'), 00103 'fullname' => new external_value(PARAM_RAW, 'long name of course'), 00104 'enrolledusercount' => new external_value(PARAM_INT, 'Number of enrolled users in this course'), 00105 'idnumber' => new external_value(PARAM_RAW, 'id number of course'), 00106 'visible' => new external_value(PARAM_INT, '1 means visible, 0 means hidden course'), 00107 ) 00108 ) 00109 ); 00110 } 00111 00116 public static function get_enrolled_users_parameters() { 00117 return new external_function_parameters( 00118 array( 00119 'courseid' => new external_value(PARAM_INT, 'course id'), 00120 'options' => new external_multiple_structure( 00121 new external_single_structure( 00122 array( 00123 'name' => new external_value(PARAM_ALPHANUMEXT, 'option name'), 00124 'value' => new external_value(PARAM_RAW, 'option value') 00125 ) 00126 ), 'method options', VALUE_DEFAULT, array()), 00127 ) 00128 ); 00129 } 00130 00140 public static function get_enrolled_users($courseid, $options) { 00141 global $CFG, $USER, $DB; 00142 require_once($CFG->dirroot . "/user/lib.php"); 00143 00144 $params = self::validate_parameters( 00145 self::get_enrolled_users_parameters(), 00146 array( 00147 'courseid'=>$courseid, 00148 'options'=>$options 00149 ) 00150 ); 00151 $withcapability = ''; 00152 $groupid = 0; 00153 $onlyactive = false; 00154 $userfields = array(); 00155 foreach ($options as $option) { 00156 switch ($option['name']) { 00157 case 'withcapability': 00158 $withcapability = $option['value']; 00159 break; 00160 case 'groupid': 00161 $groupid = (int)$option['value']; 00162 break; 00163 case 'onlyactive': 00164 $onlyactive = !empty($option['value']); 00165 break; 00166 case 'userfields': 00167 $thefields = explode(',', $option['value']); 00168 foreach ($thefields as $f) { 00169 $userfields[] = clean_param($f, PARAM_ALPHANUMEXT); 00170 } 00171 break; 00172 } 00173 } 00174 00175 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST); 00176 $coursecontext = get_context_instance(CONTEXT_COURSE, $courseid); 00177 if ($courseid == SITEID) { 00178 $context = get_system_context(); 00179 } else { 00180 $context = $coursecontext; 00181 } 00182 try { 00183 self::validate_context($context); 00184 } catch (Exception $e) { 00185 $exceptionparam = new stdClass(); 00186 $exceptionparam->message = $e->getMessage(); 00187 $exceptionparam->courseid = $params['courseid']; 00188 throw new moodle_exception(get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam)); 00189 } 00190 00191 if ($courseid == SITEID) { 00192 require_capability('moodle/site:viewparticipants', $context); 00193 } else { 00194 require_capability('moodle/course:viewparticipants', $context); 00195 } 00196 // to overwrite this parameter, you need role:review capability 00197 if ($withcapability) { 00198 require_capability('moodle/role:review', $coursecontext); 00199 } 00200 // need accessallgroups capability if you want to overwrite this option 00201 if (!empty($groupid) && groups_is_member($groupid)) { 00202 require_capability('moodle/site:accessallgroups', $coursecontext); 00203 } 00204 // to overwrite this option, you need course:enrolereview permission 00205 if ($onlyactive) { 00206 require_capability('moodle/course:enrolreview', $coursecontext); 00207 } 00208 00209 list($enrolledsql, $enrolledparams) = get_enrolled_sql($coursecontext, $withcapability, $groupid, $onlyactive); 00210 list($ctxselect, $ctxjoin) = context_instance_preload_sql('u.id', CONTEXT_USER, 'ctx'); 00211 $sqlparams['courseid'] = $courseid; 00212 $sql = "SELECT u.* $ctxselect 00213 FROM {user} u $ctxjoin 00214 WHERE u.id IN ($enrolledsql) 00215 ORDER BY u.id ASC"; 00216 $enrolledusers = $DB->get_recordset_sql($sql, $enrolledparams); 00217 $users = array(); 00218 foreach ($enrolledusers as $user) { 00219 context_instance_preload($user); 00220 if ($userdetails = user_get_user_details($user, $course, $userfields)) { 00221 $users[] = $userdetails; 00222 } 00223 } 00224 $enrolledusers->close(); 00225 00226 return $users; 00227 } 00228 00233 public static function get_enrolled_users_returns() { 00234 return new external_multiple_structure( 00235 new external_single_structure( 00236 array( 00237 'id' => new external_value(PARAM_NUMBER, 'ID of the user'), 00238 'username' => new external_value(PARAM_RAW, 'Username policy is defined in Moodle security config', VALUE_OPTIONAL), 00239 'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user', VALUE_OPTIONAL), 00240 'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user', VALUE_OPTIONAL), 00241 'fullname' => new external_value(PARAM_NOTAGS, 'The fullname of the user'), 00242 'email' => new external_value(PARAM_TEXT, 'An email address - allow email as root@localhost', VALUE_OPTIONAL), 00243 'address' => new external_value(PARAM_MULTILANG, 'Postal address', VALUE_OPTIONAL), 00244 'phone1' => new external_value(PARAM_NOTAGS, 'Phone 1', VALUE_OPTIONAL), 00245 'phone2' => new external_value(PARAM_NOTAGS, 'Phone 2', VALUE_OPTIONAL), 00246 'icq' => new external_value(PARAM_NOTAGS, 'icq number', VALUE_OPTIONAL), 00247 'skype' => new external_value(PARAM_NOTAGS, 'skype id', VALUE_OPTIONAL), 00248 'yahoo' => new external_value(PARAM_NOTAGS, 'yahoo id', VALUE_OPTIONAL), 00249 'aim' => new external_value(PARAM_NOTAGS, 'aim id', VALUE_OPTIONAL), 00250 'msn' => new external_value(PARAM_NOTAGS, 'msn number', VALUE_OPTIONAL), 00251 'department' => new external_value(PARAM_TEXT, 'department', VALUE_OPTIONAL), 00252 'institution' => new external_value(PARAM_TEXT, 'institution', VALUE_OPTIONAL), 00253 'interests' => new external_value(PARAM_TEXT, 'user interests (separated by commas)', VALUE_OPTIONAL), 00254 'firstaccess' => new external_value(PARAM_INT, 'first access to the site (0 if never)', VALUE_OPTIONAL), 00255 'lastaccess' => new external_value(PARAM_INT, 'last access to the site (0 if never)', VALUE_OPTIONAL), 00256 'description' => new external_value(PARAM_RAW, 'User profile description', VALUE_OPTIONAL), 00257 'descriptionformat' => new external_value(PARAM_INT, 'User profile description format', VALUE_OPTIONAL), 00258 'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL), 00259 'url' => new external_value(PARAM_URL, 'URL of the user', VALUE_OPTIONAL), 00260 'country' => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL), 00261 'profileimageurlsmall' => new external_value(PARAM_URL, 'User image profile URL - small version', VALUE_OPTIONAL), 00262 'profileimageurl' => new external_value(PARAM_URL, 'User image profile URL - big version', VALUE_OPTIONAL), 00263 'customfields' => new external_multiple_structure( 00264 new external_single_structure( 00265 array( 00266 'type' => new external_value(PARAM_ALPHANUMEXT, 'The type of the custom field - text field, checkbox...'), 00267 'value' => new external_value(PARAM_RAW, 'The value of the custom field'), 00268 'name' => new external_value(PARAM_RAW, 'The name of the custom field'), 00269 'shortname' => new external_value(PARAM_RAW, 'The shortname of the custom field - to be able to build the field class in the code'), 00270 ) 00271 ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL), 00272 'groups' => new external_multiple_structure( 00273 new external_single_structure( 00274 array( 00275 'id' => new external_value(PARAM_INT, 'group id'), 00276 'name' => new external_value(PARAM_RAW, 'group name'), 00277 'description' => new external_value(PARAM_RAW, 'group description'), 00278 ) 00279 ), 'user groups', VALUE_OPTIONAL), 00280 'roles' => new external_multiple_structure( 00281 new external_single_structure( 00282 array( 00283 'roleid' => new external_value(PARAM_INT, 'role id'), 00284 'name' => new external_value(PARAM_RAW, 'role name'), 00285 'shortname' => new external_value(PARAM_ALPHANUMEXT, 'role shortname'), 00286 'sortorder' => new external_value(PARAM_INT, 'role sortorder') 00287 ) 00288 ), 'user roles', VALUE_OPTIONAL), 00289 'preferences' => new external_multiple_structure( 00290 new external_single_structure( 00291 array( 00292 'name' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preferences'), 00293 'value' => new external_value(PARAM_RAW, 'The value of the custom field'), 00294 ) 00295 ), 'User preferences', VALUE_OPTIONAL), 00296 'enrolledcourses' => new external_multiple_structure( 00297 new external_single_structure( 00298 array( 00299 'id' => new external_value(PARAM_INT, 'Id of the course'), 00300 'fullname' => new external_value(PARAM_RAW, 'Fullname of the course'), 00301 'shortname' => new external_value(PARAM_RAW, 'Shortname of the course') 00302 ) 00303 ), 'Courses where the user is enrolled - limited by which courses the user is able to see', VALUE_OPTIONAL) 00304 ) 00305 ) 00306 ); 00307 } 00308 00309 } 00310 00314 class core_role_external extends external_api { 00315 00320 public static function assign_roles_parameters() { 00321 return new external_function_parameters( 00322 array( 00323 'assignments' => new external_multiple_structure( 00324 new external_single_structure( 00325 array( 00326 'roleid' => new external_value(PARAM_INT, 'Role to assign to the user'), 00327 'userid' => new external_value(PARAM_INT, 'The user that is going to be assigned'), 00328 'contextid' => new external_value(PARAM_INT, 'The context to assign the user role in'), 00329 ) 00330 ) 00331 ) 00332 ) 00333 ); 00334 } 00335 00342 public static function assign_roles($assignments) { 00343 global $DB; 00344 00345 // Do basic automatic PARAM checks on incoming data, using params description 00346 // If any problems are found then exceptions are thrown with helpful error messages 00347 $params = self::validate_parameters(self::assign_roles_parameters(), array('assignments'=>$assignments)); 00348 00349 $transaction = $DB->start_delegated_transaction(); 00350 00351 foreach ($params['assignments'] as $assignment) { 00352 // Ensure the current user is allowed to run this function in the enrolment context 00353 $context = get_context_instance_by_id($assignment['contextid']); 00354 self::validate_context($context); 00355 require_capability('moodle/role:assign', $context); 00356 00357 // throw an exception if user is not able to assign the role in this context 00358 $roles = get_assignable_roles($context, ROLENAME_SHORT); 00359 00360 if (!key_exists($assignment['roleid'], $roles)) { 00361 throw new invalid_parameter_exception('Can not assign roleid='.$assignment['roleid'].' in contextid='.$assignment['contextid']); 00362 } 00363 00364 role_assign($assignment['roleid'], $assignment['userid'], $assignment['contextid']); 00365 } 00366 00367 $transaction->allow_commit(); 00368 } 00369 00374 public static function assign_roles_returns() { 00375 return null; 00376 } 00377 00378 00383 public static function unassign_roles_parameters() { 00384 return new external_function_parameters( 00385 array( 00386 'unassignments' => new external_multiple_structure( 00387 new external_single_structure( 00388 array( 00389 'roleid' => new external_value(PARAM_INT, 'Role to assign to the user'), 00390 'userid' => new external_value(PARAM_INT, 'The user that is going to be assigned'), 00391 'contextid' => new external_value(PARAM_INT, 'The context to unassign the user role from'), 00392 ) 00393 ) 00394 ) 00395 ) 00396 ); 00397 } 00398 00405 public static function unassign_roles($unassignments) { 00406 global $DB; 00407 00408 // Do basic automatic PARAM checks on incoming data, using params description 00409 // If any problems are found then exceptions are thrown with helpful error messages 00410 $params = self::validate_parameters(self::unassign_roles_parameters(), array('unassignments'=>$unassignments)); 00411 00412 $transaction = $DB->start_delegated_transaction(); 00413 00414 foreach ($params['unassignments'] as $unassignment) { 00415 // Ensure the current user is allowed to run this function in the unassignment context 00416 $context = get_context_instance_by_id($unassignment['contextid']); 00417 self::validate_context($context); 00418 require_capability('moodle/role:assign', $context); 00419 00420 // throw an exception if user is not able to unassign the role in this context 00421 $roles = get_assignable_roles($context, ROLENAME_SHORT); 00422 if (!key_exists($unassignment['roleid'], $roles)) { 00423 throw new invalid_parameter_exception('Can not unassign roleid='.$unassignment['roleid'].' in contextid='.$unassignment['contextid']); 00424 } 00425 00426 role_unassign($unassignment['roleid'], $unassignment['userid'], $unassignment['contextid']); 00427 } 00428 00429 $transaction->allow_commit(); 00430 } 00431 00436 public static function unassign_roles_returns() { 00437 return null; 00438 } 00439 } 00440 00441 00446 class moodle_enrol_external extends external_api { 00447 00448 00454 public static function get_enrolled_users_parameters() { 00455 return new external_function_parameters( 00456 array( 00457 'courseid' => new external_value(PARAM_INT, 'Course id'), 00458 'withcapability' => new external_value(PARAM_CAPABILITY, 'User should have this capability', VALUE_DEFAULT, null), 00459 'groupid' => new external_value(PARAM_INT, 'Group id, null means all groups', VALUE_DEFAULT, null), 00460 'onlyactive' => new external_value(PARAM_INT, 'True means only active, false means all participants', VALUE_DEFAULT, 0), 00461 ) 00462 ); 00463 } 00464 00474 public static function get_enrolled_users($courseid, $withcapability = null, $groupid = null, $onlyactive = false) { 00475 global $DB, $CFG, $USER; 00476 00477 // Do basic automatic PARAM checks on incoming data, using params description 00478 // If any problems are found then exceptions are thrown with helpful error messages 00479 $params = self::validate_parameters(self::get_enrolled_users_parameters(), array( 00480 'courseid'=>$courseid, 00481 'withcapability'=>$withcapability, 00482 'groupid'=>$groupid, 00483 'onlyactive'=>$onlyactive) 00484 ); 00485 00486 $coursecontext = get_context_instance(CONTEXT_COURSE, $params['courseid']); 00487 if ($courseid == SITEID) { 00488 $context = get_context_instance(CONTEXT_SYSTEM); 00489 } else { 00490 $context = $coursecontext; 00491 } 00492 00493 try { 00494 self::validate_context($context); 00495 } catch (Exception $e) { 00496 $exceptionparam = new stdClass(); 00497 $exceptionparam->message = $e->getMessage(); 00498 $exceptionparam->courseid = $params['courseid']; 00499 throw new moodle_exception(get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam)); 00500 } 00501 00502 if ($courseid == SITEID) { 00503 require_capability('moodle/site:viewparticipants', $context); 00504 } else { 00505 require_capability('moodle/course:viewparticipants', $context); 00506 } 00507 00508 if ($withcapability) { 00509 require_capability('moodle/role:review', $coursecontext); 00510 } 00511 if ($groupid && groups_is_member($groupid)) { 00512 require_capability('moodle/site:accessallgroups', $coursecontext); 00513 } 00514 if ($onlyactive) { 00515 require_capability('moodle/course:enrolreview', $coursecontext); 00516 } 00517 00518 list($sqlparams, $params) = get_enrolled_sql($coursecontext, $withcapability, $groupid, $onlyactive); 00519 $sql = "SELECT ue.userid, e.courseid, u.firstname, u.lastname, u.username, c.id as usercontextid 00520 FROM {user_enrolments} ue 00521 JOIN {enrol} e ON (e.id = ue.enrolid) 00522 JOIN {user} u ON (ue.userid = u.id) 00523 JOIN {context} c ON (u.id = c.instanceid AND contextlevel = " . CONTEXT_USER . ") 00524 WHERE e.courseid = :courseid AND ue.userid IN ($sqlparams) 00525 GROUP BY ue.userid, e.courseid, u.firstname, u.lastname, u.username, c.id"; 00526 $params['courseid'] = $courseid; 00527 $enrolledusers = $DB->get_records_sql($sql, $params); 00528 $result = array(); 00529 $isadmin = is_siteadmin($USER); 00530 $canviewfullnames = has_capability('moodle/site:viewfullnames', $context); 00531 foreach ($enrolledusers as $enrolleduser) { 00532 $profilimgurl = moodle_url::make_pluginfile_url($enrolleduser->usercontextid, 'user', 'icon', NULL, '/', 'f1'); 00533 $profilimgurlsmall = moodle_url::make_pluginfile_url($enrolleduser->usercontextid, 'user', 'icon', NULL, '/', 'f2'); 00534 $resultuser = array( 00535 'courseid' => $enrolleduser->courseid, 00536 'userid' => $enrolleduser->userid, 00537 'fullname' => fullname($enrolleduser), 00538 'profileimgurl' => $profilimgurl->out(false), 00539 'profileimgurlsmall' => $profilimgurlsmall->out(false) 00540 ); 00541 // check if we can return username 00542 if ($isadmin) { 00543 $resultuser['username'] = $enrolleduser->username; 00544 } 00545 // check if we can return first and last name 00546 if ($isadmin or $canviewfullnames) { 00547 $resultuser['firstname'] = $enrolleduser->firstname; 00548 $resultuser['lastname'] = $enrolleduser->lastname; 00549 } 00550 $result[] = $resultuser; 00551 } 00552 00553 return $result; 00554 } 00555 00561 public static function get_enrolled_users_returns() { 00562 return new external_multiple_structure( 00563 new external_single_structure( 00564 array( 00565 'courseid' => new external_value(PARAM_INT, 'id of course'), 00566 'userid' => new external_value(PARAM_INT, 'id of user'), 00567 'firstname' => new external_value(PARAM_RAW, 'first name of user', VALUE_OPTIONAL), 00568 'lastname' => new external_value(PARAM_RAW, 'last name of user', VALUE_OPTIONAL), 00569 'fullname' => new external_value(PARAM_RAW, 'fullname of user'), 00570 'username' => new external_value(PARAM_RAW, 'username of user', VALUE_OPTIONAL), 00571 'profileimgurl' => new external_value(PARAM_URL, 'url of the profile image'), 00572 'profileimgurlsmall' => new external_value(PARAM_URL, 'url of the profile image (small version)') 00573 ) 00574 ) 00575 ); 00576 } 00577 00583 public static function get_users_courses_parameters() { 00584 return core_enrol_external::get_users_courses_parameters(); 00585 } 00586 00595 public static function get_users_courses($userid) { 00596 return core_enrol_external::get_users_courses($userid); 00597 } 00598 00604 public static function get_users_courses_returns() { 00605 return core_enrol_external::get_users_courses_returns(); 00606 } 00607 00608 00614 public static function role_assign_parameters() { 00615 return core_role_external::assign_roles_parameters(); 00616 } 00617 00624 public static function role_assign($assignments) { 00625 return core_role_external::assign_roles($assignments); 00626 } 00627 00633 public static function role_assign_returns() { 00634 return core_role_external::assign_roles_returns(); 00635 } 00636 00637 00643 public static function role_unassign_parameters() { 00644 return core_role_external::unassign_roles_parameters(); 00645 } 00646 00653 public static function role_unassign($unassignments) { 00654 return core_role_external::unassign_roles($unassignments); 00655 } 00656 00662 public static function role_unassign_returns() { 00663 return core_role_external::unassign_roles_returns(); 00664 } 00665 }