~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to enrol/externallib.php

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
113
113
                    foreach ($thefields as $f) {
114
114
                        $userfields[] = clean_param($f, PARAM_ALPHANUMEXT);
115
115
                    }
 
116
                    break;
116
117
                case 'limitfrom' :
117
118
                    $limitfrom = clean_param($option['value'], PARAM_INT);
118
119
                    break;
197
198
                    'users' => new external_multiple_structure(
198
199
                        new external_single_structure(
199
200
                array(
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),
414
415
                foreach ($thefields as $f) {
415
416
                    $userfields[] = clean_param($f, PARAM_ALPHANUMEXT);
416
417
                }
 
418
                break;
417
419
            case 'limitfrom' :
418
420
                $limitfrom = clean_param($option['value'], PARAM_INT);
419
421
                break;
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();
430
432
        } else {
431
433
            $context = $coursecontext;
432
434
        }
458
460
        }
459
461
 
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;
473
476
            }
560
563
        );
561
564
    }
562
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
    }
563
624
}
564
625
 
565
626
/**
586
647
                        array(
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),
590
654
                        )
591
655
                    )
592
656
                )
609
673
        $transaction = $DB->start_delegated_transaction();
610
674
 
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);
 
678
 
 
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);
616
682
 
621
687
                throw new invalid_parameter_exception('Can not assign roleid='.$assignment['roleid'].' in contextid='.$assignment['contextid']);
622
688
            }
623
689
 
624
 
            role_assign($assignment['roleid'], $assignment['userid'], $assignment['contextid']);
 
690
            role_assign($assignment['roleid'], $assignment['userid'], $context->id);
625
691
        }
626
692
 
627
693
        $transaction->allow_commit();
650
716
                        array(
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),
654
723
                        )
655
724
                    )
656
725
                )
674
743
 
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);
680
749
 
684
753
                throw new invalid_parameter_exception('Can not unassign roleid='.$unassignment['roleid'].' in contextid='.$unassignment['contextid']);
685
754
            }
686
755
 
687
 
            role_unassign($unassignment['roleid'], $unassignment['userid'], $unassignment['contextid']);
 
756
            role_unassign($unassignment['roleid'], $unassignment['userid'], $context->id);
688
757
        }
689
758
 
690
759
        $transaction->allow_commit();