以编程方式创建 Moodle 活动

Create Moodle activities programmatically

有谁知道是否可以在 Moodle 中以编程方式将 activity 添加到课程中?

我想也许可以使用自定义插件 lib.php 中的 class module_add_instance()...

例如

function feedback_add_instance(stdClass $mod) {
   global $DB;

   $newmodule->timecreated = time();

   // You may have to add extra stuff in here.

   $newmodule->id = $DB->insert_record('exams', $newmodule);

   unicexams_grade_item_update($newmodule);

   return $newmodule->id;
}

但话说回来:$mod 变量是什么?它包含什么以及如何构造它?

有人了解这方面的知识吗?或者有什么建议?

add_moduleinfo() 会更好。以下是我用于 facetoface 的内容。

首先,编辑 /course/modedit.php,然后临时添加以下内容,然后通过前端添加所需的 activity - 这将为您提供大部分所需属性的列表:

var_dump($fromform);
die();
$fromform = add_moduleinfo($fromform, $course, $mform);

创建facetoface实例的代码

$newfacetoface = new stdClass();
$newfacetoface->name = $facetoface->facetofacename;
$newfacetoface->intro = '';
$newfacetoface->thirdparty = '';
$newfacetoface->display = 6;
$newfacetoface->approvalreqd = 0;
$newfacetoface->selfapprovaltandc = $strmgr->get_string('selfapprovaltandccontents', 'facetoface', $facetoface->langcode);
$newfacetoface->allowcancellationsdefault = 1;
$newfacetoface->cancellationscutoffdefault = 0;
$newfacetoface->multiplesessions = 1; // Allow multiple sessions.
$newfacetoface->managerreserve = '0';
$newfacetoface->maxmanagerreserves = '1';
$newfacetoface->reservecancel = '1';
$newfacetoface->reservecanceldays = '1';
$newfacetoface->reservedays = '2';
$newfacetoface->showoncalendar = '1';
$newfacetoface->usercalentry = '1';
$newfacetoface->shortname = '';
$newfacetoface->published = $facetoface->visible;
$newfacetoface->branches = $facetoface->branches;
$newfacetoface->visible = $facetoface->visible;
$newfacetoface->cmidnumber = $facetoface->facetofaceid;
$newfacetoface->idnumber = $facetoface->facetofaceid;
$newfacetoface->groupmode = '0';
$newfacetoface->availabilityconditionsjson = '{"op":"&","c":[],"showc":[]}';
$newfacetoface->completionunlocked = 1;
$newfacetoface->completionunlockednoreset = 0;
$newfacetoface->completion = COMPLETION_TRACKING_AUTOMATIC;
$newfacetoface->completionstatusrequired = '{"100":1}';
$newfacetoface->completionexpected = 0;
$newfacetoface->course = $course->id;
$newfacetoface->coursemodule = 0;
$newfacetoface->section = 1;
$newfacetoface->module = $moduleid;
$newfacetoface->modulename = 'facetoface';
$newfacetoface->instance = 0;
$newfacetoface->add = 'facetoface';
$newfacetoface->update = 0;
$newfacetoface->return = 0;
$newfacetoface->sr = 0;

$moduleinfo = add_moduleinfo($newfacetoface, $course);