在moodle中设置课程老师

时间:2014-10-26 12:49:38

标签: php moodle

可以用什么功能来设置课程老师 我用它来学习课程的学生或老师

$context = get_context_instance(CONTEXT_COURSE, course_id);
$students = get_role_users(5, $context);

但是需要的是通过功能设置课程教师

1 个答案:

答案 0 :(得分:1)

为用户分配课程中的教师角色

$coursecontext = context_course::instance($courseid);
$teacherroleid = $DB->get_field('role', 'id', array('shortname' => 'teacher'));
role_assign($teacherroleid, $userid, $coursecontext);

更新:手动注册用户的课程 - 这也将分配一个角色,因此您可能不需要上述代码?

if (enrol_is_enabled('manual')) {
    // Ensure the manual enrolment plugin is enabled.
    $enrolplugin = enrol_get_plugin('manual');

    // Lookup the manual enrolment instance for this course.
    $instances = enrol_get_instances($this->course->id, true);
    foreach ($instances as $instance) {
        if ($instance->enrol === 'manual') {
            break;
        }
    }
    if ($instance->enrol !== 'manual') {
        throw new coding_exception('No manual enrol plugin in course');
    }

    // Enrol the user with the required role
    $enrolplugin->enrol_user($instance, $userid, $roleid);
}