[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 18 /** 19 * External course participation api. 20 * 21 * This api is mostly read only, the actual enrol and unenrol 22 * support is in each enrol plugin. 23 * 24 * @package core_enrol 25 * @category external 26 * @copyright 2010 Jerome Mouneyrac 27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 28 */ 29 30 defined('MOODLE_INTERNAL') || die(); 31 32 require_once("$CFG->libdir/externallib.php"); 33 34 /** 35 * Enrol external functions 36 * 37 * @package core_enrol 38 * @category external 39 * @copyright 2011 Jerome Mouneyrac 40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 41 * @since Moodle 2.2 42 */ 43 class core_enrol_external extends external_api { 44 45 /** 46 * Returns description of method parameters 47 * 48 * @return external_function_parameters 49 * @since Moodle 2.4 50 */ 51 public static function get_enrolled_users_with_capability_parameters() { 52 return new external_function_parameters( 53 array ( 54 'coursecapabilities' => new external_multiple_structure( 55 new external_single_structure( 56 array ( 57 'courseid' => new external_value(PARAM_INT, 'Course ID number in the Moodle course table'), 58 'capabilities' => new external_multiple_structure( 59 new external_value(PARAM_CAPABILITY, 'Capability name, such as mod/forum:viewdiscussion')), 60 ) 61 ) 62 , 'course id and associated capability name'), 63 'options' => new external_multiple_structure( 64 new external_single_structure( 65 array( 66 'name' => new external_value(PARAM_ALPHANUMEXT, 'option name'), 67 'value' => new external_value(PARAM_RAW, 'option value') 68 ) 69 ), 'Option names: 70 * groupid (integer) return only users in this group id. Requires \'moodle/site:accessallgroups\' . 71 * onlyactive (integer) only users with active enrolments. Requires \'moodle/course:enrolreview\' . 72 * userfields (\'string, string, ...\') return only the values of these user fields. 73 * limitfrom (integer) sql limit from. 74 * limitnumber (integer) max number of users per course and capability.', VALUE_DEFAULT, array()) 75 ) 76 ); 77 } 78 79 /** 80 * Return users that have the capabilities for each course specified. For each course and capability specified, 81 * a list of the users that are enrolled in the course and have that capability are returned. 82 * 83 * @param array $coursecapabilities array of course ids and associated capability names {courseid, {capabilities}} 84 * @return array An array of arrays describing users for each associated courseid and capability 85 * @since Moodle 2.4 86 */ 87 public static function get_enrolled_users_with_capability($coursecapabilities, $options) { 88 global $CFG, $DB; 89 require_once($CFG->dirroot . "/user/lib.php"); 90 91 if (empty($coursecapabilities)) { 92 throw new invalid_parameter_exception('Parameter can not be empty'); 93 } 94 $params = self::validate_parameters(self::get_enrolled_users_with_capability_parameters(), 95 array ('coursecapabilities' => $coursecapabilities, 'options'=>$options)); 96 $result = array(); 97 $userlist = array(); 98 $groupid = 0; 99 $onlyactive = false; 100 $userfields = array(); 101 $limitfrom = 0; 102 $limitnumber = 0; 103 foreach ($params['options'] as $option) { 104 switch ($option['name']) { 105 case 'groupid': 106 $groupid = (int)$option['value']; 107 break; 108 case 'onlyactive': 109 $onlyactive = !empty($option['value']); 110 break; 111 case 'userfields': 112 $thefields = explode(',', $option['value']); 113 foreach ($thefields as $f) { 114 $userfields[] = clean_param($f, PARAM_ALPHANUMEXT); 115 } 116 break; 117 case 'limitfrom' : 118 $limitfrom = clean_param($option['value'], PARAM_INT); 119 break; 120 case 'limitnumber' : 121 $limitnumber = clean_param($option['value'], PARAM_INT); 122 break; 123 } 124 } 125 126 foreach ($params['coursecapabilities'] as $coursecapability) { 127 $courseid = $coursecapability['courseid']; 128 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST); 129 $coursecontext = context_course::instance($courseid); 130 if (!$coursecontext) { 131 throw new moodle_exception('cannotfindcourse', 'error', '', null, 132 'The course id ' . $courseid . ' doesn\'t exist.'); 133 } 134 if ($courseid == SITEID) { 135 $context = context_system::instance(); 136 } else { 137 $context = $coursecontext; 138 } 139 try { 140 self::validate_context($context); 141 } catch (Exception $e) { 142 $exceptionparam = new stdClass(); 143 $exceptionparam->message = $e->getMessage(); 144 $exceptionparam->courseid = $params['courseid']; 145 throw new moodle_exception(get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam)); 146 } 147 148 if ($courseid == SITEID) { 149 require_capability('moodle/site:viewparticipants', $context); 150 } else { 151 require_capability('moodle/course:viewparticipants', $context); 152 } 153 // The accessallgroups capability is needed to use this option. 154 if (!empty($groupid) && groups_is_member($groupid)) { 155 require_capability('moodle/site:accessallgroups', $coursecontext); 156 } 157 // The course:enrolereview capability is needed to use this option. 158 if ($onlyactive) { 159 require_capability('moodle/course:enrolreview', $coursecontext); 160 } 161 162 // To see the permissions of others role:review capability is required. 163 require_capability('moodle/role:review', $coursecontext); 164 foreach ($coursecapability['capabilities'] as $capability) { 165 $courseusers['courseid'] = $courseid; 166 $courseusers['capability'] = $capability; 167 168 list($enrolledsql, $enrolledparams) = get_enrolled_sql($coursecontext, $capability, $groupid, $onlyactive); 169 170 $sql = "SELECT u.* FROM {user} u WHERE u.id IN ($enrolledsql) ORDER BY u.id ASC"; 171 172 $enrolledusers = $DB->get_recordset_sql($sql, $enrolledparams, $limitfrom, $limitnumber); 173 $users = array(); 174 foreach ($enrolledusers as $courseuser) { 175 if ($userdetails = user_get_user_details($courseuser, $course, $userfields)) { 176 $users[] = $userdetails; 177 } 178 } 179 $enrolledusers->close(); 180 $courseusers['users'] = $users; 181 $result[] = $courseusers; 182 } 183 } 184 return $result; 185 } 186 187 /** 188 * Returns description of method result value 189 * 190 * @return external_multiple_structure 191 * @since Moodle 2.4 192 */ 193 public static function get_enrolled_users_with_capability_returns() { 194 return new external_multiple_structure( new external_single_structure ( 195 array ( 196 'courseid' => new external_value(PARAM_INT, 'Course ID number in the Moodle course table'), 197 'capability' => new external_value(PARAM_CAPABILITY, 'Capability name'), 198 'users' => new external_multiple_structure( 199 new external_single_structure( 200 array( 201 'id' => new external_value(PARAM_INT, 'ID of the user'), 202 'username' => new external_value(PARAM_RAW, 'Username', VALUE_OPTIONAL), 203 'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user', VALUE_OPTIONAL), 204 'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user', VALUE_OPTIONAL), 205 'fullname' => new external_value(PARAM_NOTAGS, 'The fullname of the user'), 206 'email' => new external_value(PARAM_TEXT, 'Email address', VALUE_OPTIONAL), 207 'address' => new external_value(PARAM_MULTILANG, 'Postal address', VALUE_OPTIONAL), 208 'phone1' => new external_value(PARAM_NOTAGS, 'Phone 1', VALUE_OPTIONAL), 209 'phone2' => new external_value(PARAM_NOTAGS, 'Phone 2', VALUE_OPTIONAL), 210 'icq' => new external_value(PARAM_NOTAGS, 'icq number', VALUE_OPTIONAL), 211 'skype' => new external_value(PARAM_NOTAGS, 'skype id', VALUE_OPTIONAL), 212 'yahoo' => new external_value(PARAM_NOTAGS, 'yahoo id', VALUE_OPTIONAL), 213 'aim' => new external_value(PARAM_NOTAGS, 'aim id', VALUE_OPTIONAL), 214 'msn' => new external_value(PARAM_NOTAGS, 'msn number', VALUE_OPTIONAL), 215 'department' => new external_value(PARAM_TEXT, 'department', VALUE_OPTIONAL), 216 'institution' => new external_value(PARAM_TEXT, 'institution', VALUE_OPTIONAL), 217 'interests' => new external_value(PARAM_TEXT, 'user interests (separated by commas)', VALUE_OPTIONAL), 218 'firstaccess' => new external_value(PARAM_INT, 'first access to the site (0 if never)', VALUE_OPTIONAL), 219 'lastaccess' => new external_value(PARAM_INT, 'last access to the site (0 if never)', VALUE_OPTIONAL), 220 'description' => new external_value(PARAM_RAW, 'User profile description', VALUE_OPTIONAL), 221 'descriptionformat' => new external_value(PARAM_INT, 'User profile description format', VALUE_OPTIONAL), 222 'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL), 223 'url' => new external_value(PARAM_URL, 'URL of the user', VALUE_OPTIONAL), 224 'country' => new external_value(PARAM_ALPHA, 'Country code of the user, such as AU or CZ', VALUE_OPTIONAL), 225 'profileimageurlsmall' => new external_value(PARAM_URL, 'User image profile URL - small', VALUE_OPTIONAL), 226 'profileimageurl' => new external_value(PARAM_URL, 'User image profile URL - big', VALUE_OPTIONAL), 227 'customfields' => new external_multiple_structure( 228 new external_single_structure( 229 array( 230 'type' => new external_value(PARAM_ALPHANUMEXT, 'The type of the custom field'), 231 'value' => new external_value(PARAM_RAW, 'The value of the custom field'), 232 'name' => new external_value(PARAM_RAW, 'The name of the custom field'), 233 'shortname' => new external_value(PARAM_RAW, 'The shortname of the custom field'), 234 ) 235 ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL), 236 'groups' => new external_multiple_structure( 237 new external_single_structure( 238 array( 239 'id' => new external_value(PARAM_INT, 'group id'), 240 'name' => new external_value(PARAM_RAW, 'group name'), 241 'description' => new external_value(PARAM_RAW, 'group description'), 242 ) 243 ), 'user groups', VALUE_OPTIONAL), 244 'roles' => new external_multiple_structure( 245 new external_single_structure( 246 array( 247 'roleid' => new external_value(PARAM_INT, 'role id'), 248 'name' => new external_value(PARAM_RAW, 'role name'), 249 'shortname' => new external_value(PARAM_ALPHANUMEXT, 'role shortname'), 250 'sortorder' => new external_value(PARAM_INT, 'role sortorder') 251 ) 252 ), 'user roles', VALUE_OPTIONAL), 253 'preferences' => new external_multiple_structure( 254 new external_single_structure( 255 array( 256 'name' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preferences'), 257 'value' => new external_value(PARAM_RAW, 'The value of the custom field'), 258 ) 259 ), 'User preferences', VALUE_OPTIONAL), 260 'enrolledcourses' => new external_multiple_structure( 261 new external_single_structure( 262 array( 263 'id' => new external_value(PARAM_INT, 'Id of the course'), 264 'fullname' => new external_value(PARAM_RAW, 'Fullname of the course'), 265 'shortname' => new external_value(PARAM_RAW, 'Shortname of the course') 266 ) 267 ), 'Courses where the user is enrolled - limited by which courses the user is able to see', VALUE_OPTIONAL) 268 ) 269 ), 'List of users that are enrolled in the course and have the specified capability'), 270 ) 271 ) 272 ); 273 } 274 275 /** 276 * Returns description of method parameters 277 * 278 * @return external_function_parameters 279 */ 280 public static function get_users_courses_parameters() { 281 return new external_function_parameters( 282 array( 283 'userid' => new external_value(PARAM_INT, 'user id'), 284 ) 285 ); 286 } 287 288 /** 289 * Get list of courses user is enrolled in (only active enrolments are returned). 290 * Please note the current user must be able to access the course, otherwise the course is not included. 291 * 292 * @param int $userid 293 * @return array of courses 294 */ 295 public static function get_users_courses($userid) { 296 global $USER, $DB; 297 298 // Do basic automatic PARAM checks on incoming data, using params description 299 // If any problems are found then exceptions are thrown with helpful error messages 300 $params = self::validate_parameters(self::get_users_courses_parameters(), array('userid'=>$userid)); 301 302 $courses = enrol_get_users_courses($params['userid'], true, 'id, shortname, fullname, idnumber, visible'); 303 $result = array(); 304 305 foreach ($courses as $course) { 306 $context = context_course::instance($course->id, IGNORE_MISSING); 307 try { 308 self::validate_context($context); 309 } catch (Exception $e) { 310 // current user can not access this course, sorry we can not disclose who is enrolled in this course! 311 continue; 312 } 313 314 if ($userid != $USER->id and !has_capability('moodle/course:viewparticipants', $context)) { 315 // we need capability to view participants 316 continue; 317 } 318 319 list($enrolledsqlselect, $enrolledparams) = get_enrolled_sql($context); 320 $enrolledsql = "SELECT COUNT('x') FROM ($enrolledsqlselect) enrolleduserids"; 321 $enrolledusercount = $DB->count_records_sql($enrolledsql, $enrolledparams); 322 323 $result[] = array('id'=>$course->id, 'shortname'=>$course->shortname, 'fullname'=>$course->fullname, 'idnumber'=>$course->idnumber,'visible'=>$course->visible, 'enrolledusercount'=>$enrolledusercount); 324 } 325 326 return $result; 327 } 328 329 /** 330 * Returns description of method result value 331 * 332 * @return external_description 333 */ 334 public static function get_users_courses_returns() { 335 return new external_multiple_structure( 336 new external_single_structure( 337 array( 338 'id' => new external_value(PARAM_INT, 'id of course'), 339 'shortname' => new external_value(PARAM_RAW, 'short name of course'), 340 'fullname' => new external_value(PARAM_RAW, 'long name of course'), 341 'enrolledusercount' => new external_value(PARAM_INT, 'Number of enrolled users in this course'), 342 'idnumber' => new external_value(PARAM_RAW, 'id number of course'), 343 'visible' => new external_value(PARAM_INT, '1 means visible, 0 means hidden course'), 344 ) 345 ) 346 ); 347 } 348 349 /** 350 * Returns description of method parameters 351 * 352 * @return external_function_parameters 353 */ 354 public static function get_enrolled_users_parameters() { 355 return new external_function_parameters( 356 array( 357 'courseid' => new external_value(PARAM_INT, 'course id'), 358 'options' => new external_multiple_structure( 359 new external_single_structure( 360 array( 361 'name' => new external_value(PARAM_ALPHANUMEXT, 'option name'), 362 'value' => new external_value(PARAM_RAW, 'option value') 363 ) 364 ), 'Option names: 365 * withcapability (string) return only users with this capability. This option requires \'moodle/role:review\' on the course context. 366 * groupid (integer) return only users in this group id. This option requires \'moodle/site:accessallgroups\' on the course context. 367 * onlyactive (integer) return only users with active enrolments and matching time restrictions. This option requires \'moodle/course:enrolreview\' on the course context. 368 * userfields (\'string, string, ...\') return only the values of these user fields. 369 * limitfrom (integer) sql limit from. 370 * limitnumber (integer) maximum number of returned users.', VALUE_DEFAULT, array()), 371 ) 372 ); 373 } 374 375 /** 376 * Get course participants details 377 * 378 * @param int $courseid course id 379 * @param array $options options { 380 * 'name' => option name 381 * 'value' => option value 382 * } 383 * @return array An array of users 384 */ 385 public static function get_enrolled_users($courseid, $options = array()) { 386 global $CFG, $USER, $DB; 387 require_once($CFG->dirroot . "/user/lib.php"); 388 389 $params = self::validate_parameters( 390 self::get_enrolled_users_parameters(), 391 array( 392 'courseid'=>$courseid, 393 'options'=>$options 394 ) 395 ); 396 $withcapability = ''; 397 $groupid = 0; 398 $onlyactive = false; 399 $userfields = array(); 400 $limitfrom = 0; 401 $limitnumber = 0; 402 foreach ($options as $option) { 403 switch ($option['name']) { 404 case 'withcapability': 405 $withcapability = $option['value']; 406 break; 407 case 'groupid': 408 $groupid = (int)$option['value']; 409 break; 410 case 'onlyactive': 411 $onlyactive = !empty($option['value']); 412 break; 413 case 'userfields': 414 $thefields = explode(',', $option['value']); 415 foreach ($thefields as $f) { 416 $userfields[] = clean_param($f, PARAM_ALPHANUMEXT); 417 } 418 break; 419 case 'limitfrom' : 420 $limitfrom = clean_param($option['value'], PARAM_INT); 421 break; 422 case 'limitnumber' : 423 $limitnumber = clean_param($option['value'], PARAM_INT); 424 break; 425 } 426 } 427 428 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST); 429 $coursecontext = context_course::instance($courseid, IGNORE_MISSING); 430 if ($courseid == SITEID) { 431 $context = context_system::instance(); 432 } else { 433 $context = $coursecontext; 434 } 435 try { 436 self::validate_context($context); 437 } catch (Exception $e) { 438 $exceptionparam = new stdClass(); 439 $exceptionparam->message = $e->getMessage(); 440 $exceptionparam->courseid = $params['courseid']; 441 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam); 442 } 443 444 if ($courseid == SITEID) { 445 require_capability('moodle/site:viewparticipants', $context); 446 } else { 447 require_capability('moodle/course:viewparticipants', $context); 448 } 449 // to overwrite this parameter, you need role:review capability 450 if ($withcapability) { 451 require_capability('moodle/role:review', $coursecontext); 452 } 453 // need accessallgroups capability if you want to overwrite this option 454 if (!empty($groupid) && groups_is_member($groupid)) { 455 require_capability('moodle/site:accessallgroups', $coursecontext); 456 } 457 // to overwrite this option, you need course:enrolereview permission 458 if ($onlyactive) { 459 require_capability('moodle/course:enrolreview', $coursecontext); 460 } 461 462 list($enrolledsql, $enrolledparams) = get_enrolled_sql($coursecontext, $withcapability, $groupid, $onlyactive); 463 $ctxselect = ', ' . context_helper::get_preload_record_columns_sql('ctx'); 464 $ctxjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel)"; 465 $enrolledparams['contextlevel'] = CONTEXT_USER; 466 $sql = "SELECT u.* $ctxselect 467 FROM {user} u $ctxjoin 468 WHERE u.id IN ($enrolledsql) 469 ORDER BY u.id ASC"; 470 $enrolledusers = $DB->get_recordset_sql($sql, $enrolledparams, $limitfrom, $limitnumber); 471 $users = array(); 472 foreach ($enrolledusers as $user) { 473 context_helper::preload_from_record($user); 474 if ($userdetails = user_get_user_details($user, $course, $userfields)) { 475 $users[] = $userdetails; 476 } 477 } 478 $enrolledusers->close(); 479 480 return $users; 481 } 482 483 /** 484 * Returns description of method result value 485 * 486 * @return external_description 487 */ 488 public static function get_enrolled_users_returns() { 489 return new external_multiple_structure( 490 new external_single_structure( 491 array( 492 'id' => new external_value(PARAM_INT, 'ID of the user'), 493 'username' => new external_value(PARAM_RAW, 'Username policy is defined in Moodle security config', VALUE_OPTIONAL), 494 'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user', VALUE_OPTIONAL), 495 'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user', VALUE_OPTIONAL), 496 'fullname' => new external_value(PARAM_NOTAGS, 'The fullname of the user'), 497 'email' => new external_value(PARAM_TEXT, 'An email address - allow email as root@localhost', VALUE_OPTIONAL), 498 'address' => new external_value(PARAM_TEXT, 'Postal address', VALUE_OPTIONAL), 499 'phone1' => new external_value(PARAM_NOTAGS, 'Phone 1', VALUE_OPTIONAL), 500 'phone2' => new external_value(PARAM_NOTAGS, 'Phone 2', VALUE_OPTIONAL), 501 'icq' => new external_value(PARAM_NOTAGS, 'icq number', VALUE_OPTIONAL), 502 'skype' => new external_value(PARAM_NOTAGS, 'skype id', VALUE_OPTIONAL), 503 'yahoo' => new external_value(PARAM_NOTAGS, 'yahoo id', VALUE_OPTIONAL), 504 'aim' => new external_value(PARAM_NOTAGS, 'aim id', VALUE_OPTIONAL), 505 'msn' => new external_value(PARAM_NOTAGS, 'msn number', VALUE_OPTIONAL), 506 'department' => new external_value(PARAM_TEXT, 'department', VALUE_OPTIONAL), 507 'institution' => new external_value(PARAM_TEXT, 'institution', VALUE_OPTIONAL), 508 'idnumber' => new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution', VALUE_OPTIONAL), 509 'interests' => new external_value(PARAM_TEXT, 'user interests (separated by commas)', VALUE_OPTIONAL), 510 'firstaccess' => new external_value(PARAM_INT, 'first access to the site (0 if never)', VALUE_OPTIONAL), 511 'lastaccess' => new external_value(PARAM_INT, 'last access to the site (0 if never)', VALUE_OPTIONAL), 512 'description' => new external_value(PARAM_RAW, 'User profile description', VALUE_OPTIONAL), 513 'descriptionformat' => new external_format_value('description', VALUE_OPTIONAL), 514 'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL), 515 'url' => new external_value(PARAM_URL, 'URL of the user', VALUE_OPTIONAL), 516 'country' => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL), 517 'profileimageurlsmall' => new external_value(PARAM_URL, 'User image profile URL - small version', VALUE_OPTIONAL), 518 'profileimageurl' => new external_value(PARAM_URL, 'User image profile URL - big version', VALUE_OPTIONAL), 519 'customfields' => new external_multiple_structure( 520 new external_single_structure( 521 array( 522 'type' => new external_value(PARAM_ALPHANUMEXT, 'The type of the custom field - text field, checkbox...'), 523 'value' => new external_value(PARAM_RAW, 'The value of the custom field'), 524 'name' => new external_value(PARAM_RAW, 'The name of the custom field'), 525 'shortname' => new external_value(PARAM_RAW, 'The shortname of the custom field - to be able to build the field class in the code'), 526 ) 527 ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL), 528 'groups' => new external_multiple_structure( 529 new external_single_structure( 530 array( 531 'id' => new external_value(PARAM_INT, 'group id'), 532 'name' => new external_value(PARAM_RAW, 'group name'), 533 'description' => new external_value(PARAM_RAW, 'group description'), 534 'descriptionformat' => new external_format_value('description'), 535 ) 536 ), 'user groups', VALUE_OPTIONAL), 537 'roles' => new external_multiple_structure( 538 new external_single_structure( 539 array( 540 'roleid' => new external_value(PARAM_INT, 'role id'), 541 'name' => new external_value(PARAM_RAW, 'role name'), 542 'shortname' => new external_value(PARAM_ALPHANUMEXT, 'role shortname'), 543 'sortorder' => new external_value(PARAM_INT, 'role sortorder') 544 ) 545 ), 'user roles', VALUE_OPTIONAL), 546 'preferences' => new external_multiple_structure( 547 new external_single_structure( 548 array( 549 'name' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preferences'), 550 'value' => new external_value(PARAM_RAW, 'The value of the custom field'), 551 ) 552 ), 'User preferences', VALUE_OPTIONAL), 553 'enrolledcourses' => new external_multiple_structure( 554 new external_single_structure( 555 array( 556 'id' => new external_value(PARAM_INT, 'Id of the course'), 557 'fullname' => new external_value(PARAM_RAW, 'Fullname of the course'), 558 'shortname' => new external_value(PARAM_RAW, 'Shortname of the course') 559 ) 560 ), 'Courses where the user is enrolled - limited by which courses the user is able to see', VALUE_OPTIONAL) 561 ) 562 ) 563 ); 564 } 565 566 /** 567 * Returns description of get_course_enrolment_methods() parameters 568 * 569 * @return external_function_parameters 570 */ 571 public static function get_course_enrolment_methods_parameters() { 572 return new external_function_parameters( 573 array( 574 'courseid' => new external_value(PARAM_INT, 'Course id') 575 ) 576 ); 577 } 578 579 /** 580 * Get list of active course enrolment methods for current user. 581 * 582 * @param int $courseid 583 * @return array of course enrolment methods 584 */ 585 public static function get_course_enrolment_methods($courseid) { 586 587 $params = self::validate_parameters(self::get_course_enrolment_methods_parameters(), array('courseid' => $courseid)); 588 589 $coursecontext = context_course::instance($params['courseid']); 590 $categorycontext = $coursecontext->get_parent_context(); 591 self::validate_context($categorycontext); 592 593 $result = array(); 594 $enrolinstances = enrol_get_instances($params['courseid'], true); 595 foreach ($enrolinstances as $enrolinstance) { 596 if ($enrolplugin = enrol_get_plugin($enrolinstance->enrol)) { 597 if ($instanceinfo = $enrolplugin->get_enrol_info($enrolinstance)) { 598 $result[] = (array) $instanceinfo; 599 } 600 } 601 } 602 return $result; 603 } 604 605 /** 606 * Returns description of get_course_enrolment_methods() result value 607 * 608 * @return external_description 609 */ 610 public static function get_course_enrolment_methods_returns() { 611 return new external_multiple_structure( 612 new external_single_structure( 613 array( 614 'id' => new external_value(PARAM_INT, 'id of course enrolment instance'), 615 'courseid' => new external_value(PARAM_INT, 'id of course'), 616 'type' => new external_value(PARAM_PLUGIN, 'type of enrolment plugin'), 617 'name' => new external_value(PARAM_RAW, 'name of enrolment plugin'), 618 'status' => new external_value(PARAM_RAW, 'status of enrolment plugin'), 619 'wsfunction' => new external_value(PARAM_ALPHANUMEXT, 'webservice function to get more information', VALUE_OPTIONAL), 620 ) 621 ) 622 ); 623 } 624 } 625 626 /** 627 * Role external functions 628 * 629 * @package core_role 630 * @category external 631 * @copyright 2011 Jerome Mouneyrac 632 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 633 * @since Moodle 2.2 634 */ 635 class core_role_external extends external_api { 636 637 /** 638 * Returns description of method parameters 639 * 640 * @return external_function_parameters 641 */ 642 public static function assign_roles_parameters() { 643 return new external_function_parameters( 644 array( 645 'assignments' => new external_multiple_structure( 646 new external_single_structure( 647 array( 648 'roleid' => new external_value(PARAM_INT, 'Role to assign to the user'), 649 'userid' => new external_value(PARAM_INT, 'The user that is going to be assigned'), 650 'contextid' => new external_value(PARAM_INT, 'The context to assign the user role in', VALUE_OPTIONAL), 651 'contextlevel' => new external_value(PARAM_ALPHA, 'The context level to assign the user role in 652 (block, course, coursecat, system, user, module)', VALUE_OPTIONAL), 653 'instanceid' => new external_value(PARAM_INT, 'The Instance id of item where the role needs to be assigned', VALUE_OPTIONAL), 654 ) 655 ) 656 ) 657 ) 658 ); 659 } 660 661 /** 662 * Manual role assignments to users 663 * 664 * @param array $assignments An array of manual role assignment 665 */ 666 public static function assign_roles($assignments) { 667 global $DB; 668 669 // Do basic automatic PARAM checks on incoming data, using params description 670 // If any problems are found then exceptions are thrown with helpful error messages 671 $params = self::validate_parameters(self::assign_roles_parameters(), array('assignments'=>$assignments)); 672 673 $transaction = $DB->start_delegated_transaction(); 674 675 foreach ($params['assignments'] as $assignment) { 676 // Ensure correct context level with a instance id or contextid is passed. 677 $context = self::get_context_from_params($assignment); 678 679 // Ensure the current user is allowed to run this function in the enrolment context. 680 self::validate_context($context); 681 require_capability('moodle/role:assign', $context); 682 683 // throw an exception if user is not able to assign the role in this context 684 $roles = get_assignable_roles($context, ROLENAME_SHORT); 685 686 if (!array_key_exists($assignment['roleid'], $roles)) { 687 throw new invalid_parameter_exception('Can not assign roleid='.$assignment['roleid'].' in contextid='.$assignment['contextid']); 688 } 689 690 role_assign($assignment['roleid'], $assignment['userid'], $context->id); 691 } 692 693 $transaction->allow_commit(); 694 } 695 696 /** 697 * Returns description of method result value 698 * 699 * @return null 700 */ 701 public static function assign_roles_returns() { 702 return null; 703 } 704 705 706 /** 707 * Returns description of method parameters 708 * 709 * @return external_function_parameters 710 */ 711 public static function unassign_roles_parameters() { 712 return new external_function_parameters( 713 array( 714 'unassignments' => new external_multiple_structure( 715 new external_single_structure( 716 array( 717 'roleid' => new external_value(PARAM_INT, 'Role to assign to the user'), 718 'userid' => new external_value(PARAM_INT, 'The user that is going to be assigned'), 719 'contextid' => new external_value(PARAM_INT, 'The context to unassign the user role from', VALUE_OPTIONAL), 720 'contextlevel' => new external_value(PARAM_ALPHA, 'The context level to unassign the user role in 721 + (block, course, coursecat, system, user, module)', VALUE_OPTIONAL), 722 'instanceid' => new external_value(PARAM_INT, 'The Instance id of item where the role needs to be unassigned', VALUE_OPTIONAL), 723 ) 724 ) 725 ) 726 ) 727 ); 728 } 729 730 /** 731 * Unassign roles from users 732 * 733 * @param array $unassignments An array of unassignment 734 */ 735 public static function unassign_roles($unassignments) { 736 global $DB; 737 738 // Do basic automatic PARAM checks on incoming data, using params description 739 // If any problems are found then exceptions are thrown with helpful error messages 740 $params = self::validate_parameters(self::unassign_roles_parameters(), array('unassignments'=>$unassignments)); 741 742 $transaction = $DB->start_delegated_transaction(); 743 744 foreach ($params['unassignments'] as $unassignment) { 745 // Ensure the current user is allowed to run this function in the unassignment context 746 $context = self::get_context_from_params($unassignment); 747 self::validate_context($context); 748 require_capability('moodle/role:assign', $context); 749 750 // throw an exception if user is not able to unassign the role in this context 751 $roles = get_assignable_roles($context, ROLENAME_SHORT); 752 if (!array_key_exists($unassignment['roleid'], $roles)) { 753 throw new invalid_parameter_exception('Can not unassign roleid='.$unassignment['roleid'].' in contextid='.$unassignment['contextid']); 754 } 755 756 role_unassign($unassignment['roleid'], $unassignment['userid'], $context->id); 757 } 758 759 $transaction->allow_commit(); 760 } 761 762 /** 763 * Returns description of method result value 764 * 765 * @return null 766 */ 767 public static function unassign_roles_returns() { 768 return null; 769 } 770 } 771 772 773 /** 774 * Deprecated enrol and role external functions 775 * 776 * @package core_enrol 777 * @copyright 2010 Jerome Mouneyrac 778 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 779 * @since Moodle 2.0 780 * @deprecated Moodle 2.2 MDL-29106 - Please do not use this class any more. 781 * @see core_enrol_external 782 * @see core_role_external 783 */ 784 class moodle_enrol_external extends external_api { 785 786 787 /** 788 * Returns description of method parameters 789 * 790 * @return external_function_parameters 791 * @since Moodle 2.0 792 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 793 * @see core_enrol_external::get_enrolled_users_parameters() 794 */ 795 public static function get_enrolled_users_parameters() { 796 return new external_function_parameters( 797 array( 798 'courseid' => new external_value(PARAM_INT, 'Course id'), 799 'withcapability' => new external_value(PARAM_CAPABILITY, 'User should have this capability', VALUE_DEFAULT, null), 800 'groupid' => new external_value(PARAM_INT, 'Group id, null means all groups', VALUE_DEFAULT, null), 801 'onlyactive' => new external_value(PARAM_INT, 'True means only active, false means all participants', VALUE_DEFAULT, 0), 802 ) 803 ); 804 } 805 806 /** 807 * Get list of course participants. 808 * 809 * @param int $courseid 810 * @param text $withcapability 811 * @param int $groupid 812 * @param bool $onlyactive 813 * @return array of course participants 814 * @since Moodle 2.0 815 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 816 * @see core_enrol_external::get_enrolled_users() 817 */ 818 public static function get_enrolled_users($courseid, $withcapability = null, $groupid = null, $onlyactive = false) { 819 global $DB, $CFG, $USER; 820 821 // Do basic automatic PARAM checks on incoming data, using params description 822 // If any problems are found then exceptions are thrown with helpful error messages 823 $params = self::validate_parameters(self::get_enrolled_users_parameters(), array( 824 'courseid'=>$courseid, 825 'withcapability'=>$withcapability, 826 'groupid'=>$groupid, 827 'onlyactive'=>$onlyactive) 828 ); 829 830 $coursecontext = context_course::instance($params['courseid'], IGNORE_MISSING); 831 if ($courseid == SITEID) { 832 $context = context_system::instance(); 833 } else { 834 $context = $coursecontext; 835 } 836 837 try { 838 self::validate_context($context); 839 } catch (Exception $e) { 840 $exceptionparam = new stdClass(); 841 $exceptionparam->message = $e->getMessage(); 842 $exceptionparam->courseid = $params['courseid']; 843 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam); 844 } 845 846 if ($courseid == SITEID) { 847 require_capability('moodle/site:viewparticipants', $context); 848 } else { 849 require_capability('moodle/course:viewparticipants', $context); 850 } 851 852 if ($withcapability) { 853 require_capability('moodle/role:review', $coursecontext); 854 } 855 if ($groupid && groups_is_member($groupid)) { 856 require_capability('moodle/site:accessallgroups', $coursecontext); 857 } 858 if ($onlyactive) { 859 require_capability('moodle/course:enrolreview', $coursecontext); 860 } 861 862 list($sqlparams, $params) = get_enrolled_sql($coursecontext, $withcapability, $groupid, $onlyactive); 863 $sql = "SELECT ue.userid, e.courseid, u.firstname, u.lastname, u.username, c.id as usercontextid 864 FROM {user_enrolments} ue 865 JOIN {enrol} e ON (e.id = ue.enrolid) 866 JOIN {user} u ON (ue.userid = u.id) 867 JOIN {context} c ON (u.id = c.instanceid AND contextlevel = " . CONTEXT_USER . ") 868 WHERE e.courseid = :courseid AND ue.userid IN ($sqlparams) 869 GROUP BY ue.userid, e.courseid, u.firstname, u.lastname, u.username, c.id"; 870 $params['courseid'] = $courseid; 871 $enrolledusers = $DB->get_records_sql($sql, $params); 872 $result = array(); 873 $isadmin = is_siteadmin($USER); 874 $canviewfullnames = has_capability('moodle/site:viewfullnames', $context); 875 foreach ($enrolledusers as $enrolleduser) { 876 $profilimgurl = moodle_url::make_pluginfile_url($enrolleduser->usercontextid, 'user', 'icon', NULL, '/', 'f1'); 877 $profilimgurlsmall = moodle_url::make_pluginfile_url($enrolleduser->usercontextid, 'user', 'icon', NULL, '/', 'f2'); 878 $resultuser = array( 879 'courseid' => $enrolleduser->courseid, 880 'userid' => $enrolleduser->userid, 881 'fullname' => fullname($enrolleduser), 882 'profileimgurl' => $profilimgurl->out(false), 883 'profileimgurlsmall' => $profilimgurlsmall->out(false) 884 ); 885 // check if we can return username 886 if ($isadmin) { 887 $resultuser['username'] = $enrolleduser->username; 888 } 889 // check if we can return first and last name 890 if ($isadmin or $canviewfullnames) { 891 $resultuser['firstname'] = $enrolleduser->firstname; 892 $resultuser['lastname'] = $enrolleduser->lastname; 893 } 894 $result[] = $resultuser; 895 } 896 897 return $result; 898 } 899 900 /** 901 * Returns description of method result value 902 * 903 * @return external_description 904 * @since Moodle 2.0 905 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 906 * @see core_enrol_external::get_enrolled_users_returns() 907 */ 908 public static function get_enrolled_users_returns() { 909 return new external_multiple_structure( 910 new external_single_structure( 911 array( 912 'courseid' => new external_value(PARAM_INT, 'id of course'), 913 'userid' => new external_value(PARAM_INT, 'id of user'), 914 'firstname' => new external_value(PARAM_RAW, 'first name of user', VALUE_OPTIONAL), 915 'lastname' => new external_value(PARAM_RAW, 'last name of user', VALUE_OPTIONAL), 916 'fullname' => new external_value(PARAM_RAW, 'fullname of user'), 917 'username' => new external_value(PARAM_RAW, 'username of user', VALUE_OPTIONAL), 918 'profileimgurl' => new external_value(PARAM_URL, 'url of the profile image'), 919 'profileimgurlsmall' => new external_value(PARAM_URL, 'url of the profile image (small version)') 920 ) 921 ) 922 ); 923 } 924 925 /** 926 * Returns description of method parameters 927 * 928 * @return external_function_parameters 929 * @since Moodle 2.1 930 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 931 * @see core_enrol_external::get_users_courses_parameters() 932 */ 933 public static function get_users_courses_parameters() { 934 return core_enrol_external::get_users_courses_parameters(); 935 } 936 937 /** 938 * Get list of courses user is enrolled in (only active enrolments are returned). 939 * Please note the current user must be able to access the course, otherwise the course is not included. 940 * 941 * @param int $userid 942 * @return array of courses 943 * @since Moodle 2.1 944 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 945 * @see use core_enrol_external::get_users_courses() 946 */ 947 public static function get_users_courses($userid) { 948 return core_enrol_external::get_users_courses($userid); 949 } 950 951 /** 952 * Returns description of method result value 953 * 954 * @return external_description 955 * @since Moodle 2.1 956 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 957 * @see core_enrol_external::get_users_courses_returns() 958 */ 959 public static function get_users_courses_returns() { 960 return core_enrol_external::get_users_courses_returns(); 961 } 962 963 964 /** 965 * Returns description of method parameters 966 * 967 * @return external_function_parameters 968 * @since Moodle 2.0 969 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 970 * @see core_role_external::assign_roles_parameters() 971 */ 972 public static function role_assign_parameters() { 973 return core_role_external::assign_roles_parameters(); 974 } 975 976 /** 977 * Manual role assignments to users 978 * 979 * @param array $assignments An array of manual role assignment 980 * @since Moodle 2.0 981 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 982 * @see core_role_external::assign_roles() 983 */ 984 public static function role_assign($assignments) { 985 return core_role_external::assign_roles($assignments); 986 } 987 988 /** 989 * Returns description of method result value 990 * 991 * @return null 992 * @since Moodle 2.0 993 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 994 * @see core_role_external::assign_roles_returns() 995 */ 996 public static function role_assign_returns() { 997 return core_role_external::assign_roles_returns(); 998 } 999 1000 1001 /** 1002 * Returns description of method parameters 1003 * 1004 * @return external_function_parameters 1005 * @since Moodle 2.0 1006 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 1007 * @see core_role_external::unassign_roles_parameters() 1008 */ 1009 public static function role_unassign_parameters() { 1010 return core_role_external::unassign_roles_parameters(); 1011 } 1012 1013 /** 1014 * Unassign roles from users 1015 * 1016 * @param array $unassignments An array of unassignment 1017 * @since Moodle 2.0 1018 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 1019 * @see core_role_external::unassign_roles() 1020 */ 1021 public static function role_unassign($unassignments) { 1022 return core_role_external::unassign_roles($unassignments); 1023 } 1024 1025 /** 1026 * Returns description of method result value 1027 * 1028 * @return null 1029 * @since Moodle 2.0 1030 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. 1031 * @see core_role_external::unassign_roles_returns() 1032 */ 1033 public static function role_unassign_returns() { 1034 return core_role_external::unassign_roles_returns(); 1035 } 1036 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |