1

使用Moodle 1.9,我已经成功地能够通过 php 注册用户

$user   = get_record("user", "id", $mqval['id']);
$course = get_record("course", "id", $cid);

if ( ! enrol_into_course($course, $user, 'manuel')) { 

} else {
  //echo 'success';
}

现在我想以同样的方式取消注册用户。我尝试使用unenrol_user,但没有用。我也尝试过role_unassign,但没有成功。

4

2 回答 2

1
//get instance that can unenrol
$enrols = enrol_get_plugins(true);
$enrolinstances = enrol_get_instances($courseid, true);
$unenrolled = false;
foreach ($enrolinstances as $instance) {
    if (!$unenrolled and $enrols[$instance->enrol]->allow_unenrol($instance)) {
        $unenrolinstance = $instance;
        $unenrolled = true;
    }
}

//unenrol the user in every course he's in
$enrolledusercourses = enrol_get_users_courses($userid);
foreach ($enrolledcourses as $course) {
    //unenrol the user
    $enrols[$unenrolinstance->enrol]->unenrol_user($unenrolinstance, $userid, $roleid);
}
于 2012-11-12T14:59:09.087 回答
0

我已经修改了上面的代码及其工作。

//unenrol the user in every course he's in
 $enrols = enrol_get_plugins(true);

$enrolledusercourses = enrol_get_users_courses($user->id);

foreach ($enrolledusercourses as $course) {
    //unenrol the user
    $courseid = $course->id;

    $enrolinstances = enrol_get_instances($courseid, true);
    $unenrolled = false;

    foreach ($enrolinstances as $instance) {
        if (!$unenrolled and $enrols[$instance->enrol]->allow_unenrol($instance)) {
            $unenrolinstance = $instance;
            $unenrolled = true;
        }
    }

    $enrols[$unenrolinstance->enrol]->unenrol_user($unenrolinstance, $user->id, $user->rollid);
}
于 2015-05-15T03:37:10.903 回答