Moodle 用户注册 API [英] Moodle user enrollment API
本文介绍了Moodle 用户注册 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我被要求创建一个引擎,将我们的 SQL 数据转换为 Moodle 数据(用户、课程、注册).
I have been asked to create an engine that transform our data on SQL to Moodle data (user, courses, enrollments).
我遇到了需要使用 Moodle API 以编程方式注册学生而无需操作数据库的问题.
i got stuck at a point where i need to enroll a student programmatically using Moodle API without the need to manipulate the database.
喜欢:enroll_user($courseid, $userid)
like: enroll_user($courseid, $userid)
有人对此有任何意见吗?
does anyone have any input regarding this?
推荐答案
类似这样的事情
$context = context_course::instance($course->id);
// What role to enrol as?
$studentroleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
// Loop through the students.
foreach ($users as $user) {
if (!is_enrolled($context, $user->id)) {
// Not already enrolled so try enrolling them.
if (!enrol_try_internal_enrol($course->id, $user->id, $studentroleid, time())) {
// There's a problem.
throw new moodle_exception('unabletoenrolerrormessage', 'langsourcefile');
}
}
}
这篇关于Moodle 用户注册 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文