我想在moodle中以编程方式将addcohort添加到课程中。我已经阅读了文档,但确实在那里找到了它。有没有像chort_add_course() with parameters as course id and chort id
这样的函数?
答案 0 :(得分:3)
我猜您的意思是在课程中添加同组注册方法?然后该队列中的用户将自动注册到该课程中。
代码在/enrol/cohort/edit.php和/enrol/cohort/edit_form.php中 - 但是这样的内容会自动完成。
require_once($CFG->dirroot . '/enrol/cohort/locallib.php');
function cohort_add_course($courseid, $cohortid) {
global $DB;
if (!enrol_is_enabled('cohort')) {
// Not enabled.
return false;
}
if ($DB->record_exists('enrol', array('courseid' => $courseid, 'enrol' => 'cohort'))) {
// The course already has a cohort enrol method.
return false;
}
// Get the cohort enrol plugin
$enrol = enrol_get_plugin('cohort');
// Get the course record.
$course = $DB->get_record('course', array('id' => $courseid));
// Add a cohort instance to the course.
$instance = array();
$instance['name'] = 'custom instance name - can be blank';
$instance['status'] = ENROL_INSTANCE_ENABLED; // Enable it.
$instance['customint1'] = $cohortid; // Used to store the cohort id.
$instance['roleid'] = $enrol->get_config('roleid'); // Default role for cohort enrol which is usually student.
$instance['customint2'] = 0; // Optional group id.
$enrol->add_instance($course, $instance);
// Sync the existing cohort members.
$trace = new null_progress_trace();
enrol_cohort_sync($trace, $course->id);
$trace->finished();
}
答案 1 :(得分:2)
看看enroll / cohort / ajax.php并向下滚动到“case'enrolcohort':”。没有一个群组注册功能,但是那几行代码应该包含你想要的内容。
注意 - 你也可以使用下面的'enrolcohortusers'部分 - 不同之处在于,如果在群组中添加/删除新用户,则第一个将继续更新注册,第二个添加当前用户,但不会将来更新。