在启用Moodle的网站上,我想为用户提供一些优惠券。
我检查了用户是否已登录,但我还想检查当前登录用户是否在特定课程(3个课程ID的数组)中进行了编辑。
到目前为止,我尝试使用$USER->currentcourseaccess
或$USER-> lastcourseaccess
,但他们没有做到这一点。
那么,如何检查当前登录的用户是否在特定的Moodle课程中注册?
修改:该网站使用 Moodle 2 。
答案 0 :(得分:5)
我只是使用这个简单的2行解决方案(使用课程和用户ID):
global $USER;
$context = get_context_instance(CONTEXT_COURSE, $courseid, MUST_EXIST);
$enrolled = is_enrolled($context, $USER->id, '', true);
答案 1 :(得分:4)
您需要先获取课程上下文,然后根据该上下文中具有特定角色ID的已注册用户列表检查您的用户(Student的默认角色ID为5)。如果是Moodle 2.0+ API,您可以在不直接查询数据库的情况下执行此操作:
$context = get_context_instance(CONTEXT_COURSE, $course_id);
$students = get_role_users(5, $context);
在Moodle 1.9中,您需要手动从数据库中获取内容:
mdl_contexts
表格contextlevel = CONTEXT_COURSE (CONTEXT_COURSE = 50)
和instanceid = <id of course>
mdl_role_assignments
和contextid = <first result>
roleid = 5
答案 2 :(得分:3)
you can also run this sql command:
SELECT c.id AS id, c.fullname,c.shortname, u.username, u.firstname,
u.lastname, u.email
FROM mdl_role_assignments ra, mdl_user u, mdl_course c, mdl_context cxt
WHERE ra.userid = u.id
AND ra.contextid = cxt.id
AND cxt.contextlevel = 50
AND cxt.instanceid = c.id
AND u.username = 'User_Username'
AND c.shortname = 'Course_Shortname'
AND (roleid =5 OR roleid=3);
this should check if the user with user name 'User_Username' is enrolled in the course with shortname 'Course_Shortname' as student or teacher
hope it helps.
答案 3 :(得分:1)
注册检查代码段:
function check_enrollment($username, $course){
global $DB;
$sql = "SELECT count(*)
FROM mdl_user_enrolments a,
mdl_enrol b,
mdl_user c
WHERE c.username='$username'
AND a.userid=c.id
AND b.courseid=$course
AND a.enrolid=b.id";
$n = $DB->count_records_sql($sql);
if($n==0) {
//user not enrolled
return False;
} elseif($n==1) {
//user already enrolled
return True;
} else {
//, bad data ie<Data sanity not maintained>
add_to_log($course, 'ERROR: check-enrollment', 'Entered into mordor code block');
return False;
}
}