Moodle: 登记学生,检测学生是否存在在课程 Check if user is enrolled or enroll a user in course

1. 检测学生是否存在课程中 Check if user is enrolled in specific Moodle course

require_once('../config.php');

global $USER;

// 获取当前课程信息
$course_id = $_GET['id'];
$course = $DB->get_record('course', array('id' => $course_id), '*', MUST_EXIST);

//echo '<pre>'; print_r($course); echo '</pre>';

// 获取该课程所有学员
$context  = get_context_instance(CONTEXT_COURSE, $course_id);
$students = get_role_users(5, $context); // 5 is student role

echo '<pre>'; 
	print_r($context); 
	echo count($students); echo '<br>'; 
	print_r($students); 
echo '</pre>';

// 获取当前用户是否在该课程中
$context = get_context_instance(CONTEXT_COURSE, $course_id, MUST_EXIST);
$enrolled = is_enrolled($context, $USER->id, '', true);
echo $enrolled ? 'yes' : 'no';

 

来源:http://stackoverflow.com/questions/8391529/check-if-user-is-enrolled-in-specific-moodle-course

 

2. 添加学员到课程中 how to enroll a user in all courses on Moodle

require_once('../config.php');

global $USER;

// enroll student to course (roleid = 5 is student role)
function enroll_to_course($courseid, $userid, $roleid=5, $extendbase=3, $extendperiod=0)  
{
    global $DB;

    $instance = $DB->get_record('enrol', array('courseid'=>$courseid, 'enrol'=>'manual'), '*', MUST_EXIST);
    $course = $DB->get_record('course', array('id'=>$instance->courseid), '*', MUST_EXIST);
    $today = time();
    $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);

    if(!$enrol_manual = enrol_get_plugin('manual')) { throw new coding_exception('Can not instantiate enrol_manual'); }
	
    switch($extendbase) 
    {
        case 2:
            $timestart = $course->startdate;
            break;
        case 3:
        default:
            $timestart = $today;
            break;
    }  
    if ($extendperiod <= 0) { $timeend = 0; }   // extendperiod are seconds
    else { $timeend = $timestart + $extendperiod; }
    $enrolled = $enrol_manual->enrol_user($instance, $userid, $roleid, $timestart, $timeend);
    add_to_log($course->id, 'course', 'enrol', '../enrol/users.php?id='.$course->id, $course->id);

    return $enrolled;
}

 

来源:http://stackoverflow.com/questions/16012157/how-to-enroll-a-user-in-all-courses-on-moodle

参考:http://stackoverflow.com/questions/11572189/moodle-automating-user-course-creation-and-enrolments

 

你可能感兴趣的:(check)