197
198
'users' => new external_multiple_structure(
198
199
new external_single_structure(
200
'id' => new external_value(PARAM_NUMBER, 'ID of the user'),
201
'id' => new external_value(PARAM_INT, 'ID of the user'),
201
202
'username' => new external_value(PARAM_RAW, 'Username', VALUE_OPTIONAL),
202
203
'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user', VALUE_OPTIONAL),
203
204
'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user', VALUE_OPTIONAL),
426
428
$course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
427
429
$coursecontext = context_course::instance($courseid, IGNORE_MISSING);
428
430
if ($courseid == SITEID) {
429
$context = get_system_context();
431
$context = context_system::instance();
431
433
$context = $coursecontext;
460
462
list($enrolledsql, $enrolledparams) = get_enrolled_sql($coursecontext, $withcapability, $groupid, $onlyactive);
461
list($ctxselect, $ctxjoin) = context_instance_preload_sql('u.id', CONTEXT_USER, 'ctx');
462
$sqlparams['courseid'] = $courseid;
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;
463
466
$sql = "SELECT u.* $ctxselect
464
467
FROM {user} u $ctxjoin
465
468
WHERE u.id IN ($enrolledsql)
467
470
$enrolledusers = $DB->get_recordset_sql($sql, $enrolledparams, $limitfrom, $limitnumber);
468
471
$users = array();
469
472
foreach ($enrolledusers as $user) {
470
context_instance_preload($user);
473
context_helper::preload_from_record($user);
471
474
if ($userdetails = user_get_user_details($user, $course, $userfields)) {
472
475
$users[] = $userdetails;
567
* Returns description of get_course_enrolment_methods() parameters
569
* @return external_function_parameters
571
public static function get_course_enrolment_methods_parameters() {
572
return new external_function_parameters(
574
'courseid' => new external_value(PARAM_INT, 'Course id')
580
* Get list of active course enrolment methods for current user.
582
* @param int $courseid
583
* @return array of course enrolment methods
585
public static function get_course_enrolment_methods($courseid) {
587
$params = self::validate_parameters(self::get_course_enrolment_methods_parameters(), array('courseid' => $courseid));
589
$coursecontext = context_course::instance($params['courseid']);
590
$categorycontext = $coursecontext->get_parent_context();
591
self::validate_context($categorycontext);
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;
606
* Returns description of get_course_enrolment_methods() result value
608
* @return external_description
610
public static function get_course_enrolment_methods_returns() {
611
return new external_multiple_structure(
612
new external_single_structure(
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),
587
648
'roleid' => new external_value(PARAM_INT, 'Role to assign to the user'),
588
649
'userid' => new external_value(PARAM_INT, 'The user that is going to be assigned'),
589
'contextid' => new external_value(PARAM_INT, 'The context to assign the user role in'),
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),
609
673
$transaction = $DB->start_delegated_transaction();
611
675
foreach ($params['assignments'] as $assignment) {
612
// Ensure the current user is allowed to run this function in the enrolment context
613
$context = context::instance_by_id($assignment['contextid'], IGNORE_MISSING);
676
// Ensure correct context level with a instance id or contextid is passed.
677
$context = self::get_context_from_params($assignment);
679
// Ensure the current user is allowed to run this function in the enrolment context.
614
680
self::validate_context($context);
615
681
require_capability('moodle/role:assign', $context);
621
687
throw new invalid_parameter_exception('Can not assign roleid='.$assignment['roleid'].' in contextid='.$assignment['contextid']);
624
role_assign($assignment['roleid'], $assignment['userid'], $assignment['contextid']);
690
role_assign($assignment['roleid'], $assignment['userid'], $context->id);
627
693
$transaction->allow_commit();
651
717
'roleid' => new external_value(PARAM_INT, 'Role to assign to the user'),
652
718
'userid' => new external_value(PARAM_INT, 'The user that is going to be assigned'),
653
'contextid' => new external_value(PARAM_INT, 'The context to unassign the user role from'),
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),
675
744
foreach ($params['unassignments'] as $unassignment) {
676
745
// Ensure the current user is allowed to run this function in the unassignment context
677
$context = context::instance_by_id($unassignment['contextid'], IGNORE_MISSING);
746
$context = self::get_context_from_params($unassignment);
678
747
self::validate_context($context);
679
748
require_capability('moodle/role:assign', $context);
684
753
throw new invalid_parameter_exception('Can not unassign roleid='.$unassignment['roleid'].' in contextid='.$unassignment['contextid']);
687
role_unassign($unassignment['roleid'], $unassignment['userid'], $unassignment['contextid']);
756
role_unassign($unassignment['roleid'], $unassignment['userid'], $context->id);
690
759
$transaction->allow_commit();