简体   繁体   English

Moodle-添加下拉菜单

[英]Moodle - Add dropdown menu

Good day. 美好的一天。

I'm trying to add a new dropdown menu on the page where we edit a question. 我正在尝试在编辑问题的页面上添加新的下拉菜单。 This dropdown menu is used for my local plugin. 此下拉菜单用于我的本地插件。 I didn't manage to modify the page and add the select tag. 我没有设法修改页面并添加选择标签。

It should look like this. 它应该看起来像这样。 (I used 'Inspect element' in achieving this) (在实现这一目标时,我使用了“检查元素”) 在此处输入图片说明

I created a table that will contain the question id and obe category for the question since it's not a good practice to modify Moodle's defined tables. 我创建了一个表,其中将包含question idobe category ,因为修改Moodle的已定义表不是一个好习惯。

I manually created the table but I will use XMLDB Editor soon. 我手动创建了表,但是很快我将使用XMLDB编辑器。 I'm still reading on it, will apply it later on. 我仍在阅读它,稍后再应用。

Sample table structure with content. 带有内容的示例表结构。

mdl_question_obe

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

question is the id of the question, obe_category is the category of the question. questionquestion的ID, obe_category是问题的类别。

I think that there are other modifications to be done once this is achieved because the mdl_question_obe table will be populated after the question is saved. 我认为一旦实现,还需要进行其他修改,因为保存问题后将填充mdl_question_obe表。

How can I achieve this? 我该如何实现? Any insights will do! 任何见识都可以!

And please leave a comment if I missed something. 如果我错过了什么,请发表评论。

Something like this 像这样

In /question/question.php /question/question.php

before $mform->set_data($toform); $mform->set_data($toform); add this to get the current obe category 添加它以获得当前的obe类别

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

Then in /question/type/edit_question_form.php 然后在/question/type/edit_question_form.php

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

add this 加上这个

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

or if the values are stored in a database table then use this, assuming the table is called obe_category and the text field is called obe_category 或者,如果值存储在数据库表中,则使用该表,假设该表名为obe_category且文本字段称为obe_category

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

then 然后

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

Then back in /question/question.php 然后回到/question/question.php

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

add an update / insert 添加更新/插入

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM