我有一个带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);
Run Code Online (Sandbox Code Playgroud)
要注册用户,以下代码段可能有用它以自动方式模拟手动注册.
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4973 次 |
| 最近记录: |