本文整理汇总了PHP中grade_user_unenrol函数的典型用法代码示例。如果您正苦于以下问题:PHP grade_user_unenrol函数的具体用法?PHP grade_user_unenrol怎么用?PHP grade_user_unenrol使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了grade_user_unenrol函数的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: unenrol_user
/**
* Unenrol user from course,
* the last unenrolment removes all remaining roles.
*
* @param stdClass $instance
* @param int $userid
* @return void
*/
public function unenrol_user(stdClass $instance, $userid)
{
global $CFG, $USER, $DB;
require_once "{$CFG->dirroot}/group/lib.php";
$name = $this->get_name();
$courseid = $instance->courseid;
if ($instance->enrol !== $name) {
throw new coding_exception('invalid enrol instance!');
}
$context = context_course::instance($instance->courseid, MUST_EXIST);
if (!($ue = $DB->get_record('user_enrolments', array('enrolid' => $instance->id, 'userid' => $userid)))) {
// weird, user not enrolled
return;
}
// Remove all users groups linked to this enrolment instance.
if ($gms = $DB->get_records('groups_members', array('userid' => $userid, 'component' => 'enrol_' . $name, 'itemid' => $instance->id))) {
foreach ($gms as $gm) {
groups_remove_member($gm->groupid, $gm->userid);
}
}
role_unassign_all(array('userid' => $userid, 'contextid' => $context->id, 'component' => 'enrol_' . $name, 'itemid' => $instance->id));
$DB->delete_records('user_enrolments', array('id' => $ue->id));
// add extra info and trigger event
$ue->courseid = $courseid;
$ue->enrol = $name;
$sql = "SELECT 'x'\n FROM {user_enrolments} ue\n JOIN {enrol} e ON (e.id = ue.enrolid)\n WHERE ue.userid = :userid AND e.courseid = :courseid";
if ($DB->record_exists_sql($sql, array('userid' => $userid, 'courseid' => $courseid))) {
$ue->lastenrol = false;
events_trigger('user_unenrolled', $ue);
// user still has some enrolments, no big cleanup yet
} else {
// the big cleanup IS necessary!
require_once "{$CFG->libdir}/gradelib.php";
// remove all remaining roles
role_unassign_all(array('userid' => $userid, 'contextid' => $context->id), true, false);
//clean up ALL invisible user data from course if this is the last enrolment - groups, grades, etc.
groups_delete_group_members($courseid, $userid);
grade_user_unenrol($courseid, $userid);
$DB->delete_records('user_lastaccess', array('userid' => $userid, 'courseid' => $courseid));
$ue->lastenrol = true;
// means user not enrolled any more
events_trigger('user_unenrolled', $ue);
}
// reset all enrol caches
$context->mark_dirty();
// reset current user enrolment caching
if ($userid == $USER->id) {
if (isset($USER->enrol['enrolled'][$courseid])) {
unset($USER->enrol['enrolled'][$courseid]);
}
if (isset($USER->enrol['tempguest'][$courseid])) {
unset($USER->enrol['tempguest'][$courseid]);
remove_temp_course_roles($context);
}
}
}
示例2: unenrol_user
/**
* Unenrol user from course,
* the last unenrolment removes all remaining roles.
*
* @param stdClass $instance
* @param int $userid
* @return void
*/
public function unenrol_user(stdClass $instance, $userid) {
global $CFG, $USER, $DB;
$name = $this->get_name();
$courseid = $instance->courseid;
if ($instance->enrol !== $name) {
throw new coding_exception('invalid enrol instance!');
}
$context = get_context_instance(CONTEXT_COURSE, $instance->courseid, MUST_EXIST);
if (!$ue = $DB->get_record('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$userid))) {
// weird, user not enrolled
return;
}
role_unassign_all(array('userid'=>$userid, 'contextid'=>$context->id, 'component'=>'enrol_'.$name, 'itemid'=>$instance->id));
$DB->delete_records('user_enrolments', array('id'=>$ue->id));
// add extra info and trigger event
$ue->courseid = $courseid;
$ue->enrol = $name;
$sql = "SELECT 'x'
FROM {user_enrolments} ue
JOIN {enrol} e ON (e.id = ue.enrolid)
WHERE ue.userid = :userid AND e.courseid = :courseid";
if ($DB->record_exists_sql($sql, array('userid'=>$userid, 'courseid'=>$courseid))) {
$ue->lastenrol = false;
events_trigger('user_unenrolled', $ue);
// user still has some enrolments, no big cleanup yet
} else {
// the big cleanup IS necessary!
require_once("$CFG->dirroot/group/lib.php");
require_once("$CFG->libdir/gradelib.php");
// remove all remaining roles
role_unassign_all(array('userid'=>$userid, 'contextid'=>$context->id), true, false);
//clean up ALL invisible user data from course if this is the last enrolment - groups, grades, etc.
groups_delete_group_members($courseid, $userid);
grade_user_unenrol($courseid, $userid);
$DB->delete_records('user_lastaccess', array('userid'=>$userid, 'courseid'=>$courseid));
$ue->lastenrol = true; // means user not enrolled any more
events_trigger('user_unenrolled', $ue);
}
// reset primitive require_login() caching
if ($userid == $USER->id) {
if (isset($USER->enrol['enrolled'][$courseid])) {
unset($USER->enrol['enrolled'][$courseid]);
}
if (isset($USER->enrol['tempguest'][$courseid])) {
unset($USER->enrol['tempguest'][$courseid]);
$USER->access = remove_temp_roles($context, $USER->access);
}
}
}