2

我有一个带有 SSO 的 Moodle。

用户登录一个网站并点击一个链接来访问我的 Moodle。

当他们到达时,我想让他们参加我 Moodle 上的每门课程。

但是,我无法让 sql 工作。

这是我所拥有的:

$ra = new object();
$ra->roleid = 5;
$ra->contextid = $contextid;
$ra->userid = $user->id;
$ra->hidden = 0;
$ra->enrol = 'manual';
//$ra->enrol = 'self';
/// Always round timestart downto 100 secs to help DBs to use their own caching algorithms
/// by repeating queries with the same exact parameters in a 100 secs time window
$ra->timestart = 0;
$ra->timeend = 0;
$ra->timemodified = time();
$ra->modifierid = 0;

// Enrol the User for the Course
$ra->id = $DB->insert_record('role_assignments',$ra);
4

3 回答 3

6

要注册用户,以下代码片段可能很有用它以自动方式模拟手动注册。

function enroll_user($userid, $course, $modifier) {                                                
    global $DB;                                                                                    
    $enrolData = $DB->get_record('enrol', array('enrol'=>'manual', 'courseid'=>$course));          
    $user_enrolment = new stdClass();                                                              
        $user_enrolment->enrolid = $enrolData->id;                                                 
        $user_enrolment->status = '0';                                                             
        $user_enrolment->userid = $userid;                                                         
        $user_enrolment->timestart = time();                                                       
        $user_enrolment->timeend =  '0';                                                           
        $user_enrolment->modifierid = $modifier;                                                   
        //Modifierid in this table is userid who enrolled this user manually
        $user_enrolment->timecreated = time();                                                     
        $user_enrolment->timemodified = time();                                                    
    $insertId = $DB->insert_record('user_enrolments', $user_enrolment);                            
    //addto log                                                                                    
    $context = $DB->get_record('context', array('contextlevel'=>50, 'instanceid'=>$course));          
    $role = new stdClass();                                                                        
        $role->roleid = 5;                                                                         
        $role->contextid = $context->id;                                                           
        $role->userid = $userid;                                                                   
        $role->component = '';                                                                     
        $role->itemid = 0;                                                                         
        $role->timemodified = time();                                                              
        $role->modifierid = $modifier;                                                             
    $insertId2 = $DB->insert_record('role_assignments', $role);                                    
    add_to_log($course, '', $modifierid, 'automated');                
    return array('user_enrolment'=>$insertId, 'role_assignment'=>$insertId2);                      
}           
于 2013-06-12T11:31:09.370 回答
3

这是我自动为学生注册课程的方式。您只需稍微操作一下即可将其用于所有课程 ID。我希望这可以帮助你。

// 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;
}
于 2013-10-31T16:06:48.977 回答
1

您可以使用自动注册插件进行注册。当用户点击课程时,他们会通过使用此插件自动注册课程。链接:https ://moodle.org/plugins/enrol_auto

于 2014-01-30T10:03:11.857 回答