Moodle - 添加下拉菜单

Moodle - Add dropdown menu

美好的一天。

我正在尝试在我们编辑问题的页面上添加一个新的下拉菜单。此下拉菜单用于我的本地插件。我没有设法修改页面并添加 select 标签。

它应该是这样的。 (我用 'Inspect element' 来实现这个)

我创建了一个 table,它将包含问题的 question idobe category,因为修改 Moodle 定义的 table 不是一个好习惯。

我手动创建了 table,但我很快就会使用 XMLDB 编辑器。我还在阅读它,稍后会应用它。

包含内容的示例 table 结构。

mdl_question_obe

  id     question    obe_category
  1         1            FI
  2         2            FI
  3         3            S
  4         4            S  

question是问题的id,obe_category是问题的类别。

我认为一旦实现,还有其他修改要做,因为 mdl_question_obe table 将在问题保存后填充。

我怎样才能做到这一点?任何见解都可以!

如果我遗漏了什么,请发表评论。

像这样

/question/question.php

$mform->set_data($toform);之前添加这个以获取当前的obe类别

if ($id) {
    $toform->obe_category = $DB->get_field('question_obe', 'obe_category', array('question' => $id));
}

然后在/question/type/edit_question_form.php

之前 $mform->addElement('editor', 'questiontext', get_string('questiontext', 'question'),

添加这个

$obe_options('FI' => 'FI', 'S' => 'S', 'M' => 'M');

或者如果值存储在数据库中 table 然后使用它,假设 table 被称为 obe_category 并且文本字段被称为 obe_category

$obe_options = $DB->get_records_menu('obe_category', array(), 'obe_category', 'obe_category, obe_category');

然后

$mform->addElement('select', 'obe_category',
            get_string('selectobecategory', 'local_obe_category'), $obe_options);
$mform->setType('obe_category', PARAM_ALPHA);

然后回到 /question/question.php

就在$question = $qtypeobj->save_question($question, $fromform);

之后

添加更新/插入

if ($question_obe = $DB->get_record('question_obe', array('question' => $question->id)) {
    $question_obe->obe_category = $fromform->obe_category;
    $DB->update_record('question_obe', $question_obe);
} else {
    $question_obe = new stdClass();
    $question_obe->question = $question->id;
    $question_obe->obe_category = $fromform->obe_category;
    $DB->insert_record('question_obe', $question_obe);
}