英文:
Bulk cohort enroll
问题
我想要批量将同学编组注册到课程中(通过编组同步,而不是从编组中注册用户)。阅读了REST API文档后,我没有找到实现这一点的方法。因此,我想用一个php脚本来实现。这是我尝试过的:
<?php
define('CLI_SCRIPT', true);
require_once('/var/www/moodle/config.php');
require_once($CFG->dirroot . '/lib/clilib.php');
require_once($CFG->dirroot . '/cohort/lib.php');
require_once($CFG->dirroot . '/enrol/cohort/lib.php');
$courseId = 205;
$cohortId = 167;
// 登录
$systemcontext = context_system::instance();
$userid = 2; // 用户管理员
$session = null;
// 注册
$enrollmentPlugin = enrol_get_plugin('cohort');
$enrollmentPlugin->sync_cohort($courseId, $cohortId);
但由于我不知道正确的函数和方法,所以它不起作用。有人能帮我完成这个脚本吗?
Moodle版本:3.11
PHP版本:7.4
英文:
I want to enroll cohorts to courses in bulk (via cohort sync, not enroll users from a cohort). After reading the REST API documentation, I haven't found a way to do this. Therefore, I want to achieve it using a php script. This is what I have tried:
<?php
define('CLI_SCRIPT', true);
require_once('/var/www/moodle/config.php');
require_once($CFG->dirroot . '/lib/clilib.php');
require_once($CFG->dirroot . '/cohort/lib.php');
require_once($CFG->dirroot . '/enrol/cohort/lib.php');
$courseId = 205;
$cohortId = 167;
// Login
$systemcontext = context_system::instance();
$userid = 2; // user admin
$session = null;
// Enroll
$enrollmentPlugin = enrol_get_plugin('cohort');
$enrollmentPlugin->sync_cohort($courseId, $cohortId);
But it doesn't work as I don't know proper functions and methods. Can someone help me complete the script?
Moodle Version: 3.11
PHP Version: 7.4
答案1
得分: 1
你可能想要使用类似以下的代码:
$studentroleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
$course = get_course($courseid);
$cohortplugin = enrol_get_plugin('cohort');
if (!$cohortplugin->can_add_instance($courseid)) {
// 显示一些错误消息?
return;
}
$cohortplugin->add_instance($course,
['customint1' => $cohortid,
'roleid' => $studentroleid]);
我认为你可以将多个 cohort 添加到单个 cohort enrol 插件中 - 我还没有尝试过,但可以使用数组作为 customint1
参数:
$cohortplugin->add_instance($course
['customint1' => [$cohortid1, $cohortid2],
'roleid' => $studentroleid]);
要使用 $fields['customint2'] == COHORT_CREATE_GROUP
创建一个 cohort 组,请检查用户是否具有所需的权限:
$context = context_course::instance($course->id);
if (!has_capability('moodle/course:managegroups', $context)) {
// 无法添加 cohort 组,显示一条消息?
return;
}
英文:
You probably want to use something like
$studentroleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
$course = get_course($courseid);
$cohortplugin = enrol_get_plugin('cohort');
if (!$cohortplugin->can_add_instance($courseid)) {
// Display some error message?
return;
}
$cohortplugin->add_instance($course,
['customint1' => $cohortid,
'roleid' => $studentroleid]);
And I think you can add multiple cohorts to a single cohort enrol plugin - I haven't tried it but use an array for the customint1
parameter
$cohortplugin->add_instance($course
['customint1' => [$cohortid1, $cohortid2],
'roleid' => $studentroleid]);
To create a cohort group using $fields['customint2'] == COHORT_CREATE_GROUP
, check if the user has the required capability
$context = context_course::instance($course->id);
if (!has_capability('moodle/course:managegroups', $context)) {
// Can't add a cohort group, display a message?
return;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论