MDL-61410 question: support course tags on import

This commit is contained in:
Simey Lameze 2018-03-02 12:29:26 +08:00
parent 2ee6e02e21
commit 13596866e7
2 changed files with 24 additions and 9 deletions

View file

@ -425,8 +425,14 @@ class qformat_default {
$result = question_bank::get_qtype($question->qtype)->save_question_options($question); $result = question_bank::get_qtype($question->qtype)->save_question_options($question);
if (isset($question->tags)) { if (!empty($question->tags)) {
core_tag_tag::set_item_tags('core_question', 'question', $question->id, $question->context, $question->tags); core_tag_tag::set_item_tags('core_question', 'question', $question->id,
$question->context, $question->tags);
}
if (!empty($question->coursetags)) {
core_tag_tag::set_item_tags('core_question', 'question', $question->id,
context_course::instance($this->course->id), $question->coursetags);
} }
if (!empty($result->error)) { if (!empty($result->error)) {

View file

@ -383,12 +383,20 @@ class qformat_xml extends qformat_default {
public function import_question_tags($qo, $questionxml) { public function import_question_tags($qo, $questionxml) {
global $CFG; global $CFG;
if (core_tag_tag::is_enabled('core_question', 'question') if (core_tag_tag::is_enabled('core_question', 'question')) {
&& array_key_exists('tags', $questionxml['#'])
&& !empty($questionxml['#']['tags'][0]['#']['tag'])) { $qo->tags = [];
$qo->tags = array(); if (!empty($questionxml['#']['tags'][0]['#']['tag'])) {
foreach ($questionxml['#']['tags'][0]['#']['tag'] as $tagdata) { foreach ($questionxml['#']['tags'][0]['#']['tag'] as $tagdata) {
$qo->tags[] = $this->getpath($tagdata, array('#', 'text', 0, '#'), '', true); $qo->tags[] = $this->getpath($tagdata, array('#', 'text', 0, '#'), '', true);
}
}
$qo->coursetags = [];
if (!empty($questionxml['#']['coursetags'][0]['#']['tag'])) {
foreach ($questionxml['#']['coursetags'][0]['#']['tag'] as $tagdata) {
$qo->coursetags[] = $this->getpath($tagdata, array('#', 'text', 0, '#'), '', true);
}
} }
} }
} }
@ -1472,7 +1480,8 @@ class qformat_xml extends qformat_default {
if (!is_null($tagobjects)) { if (!is_null($tagobjects)) {
$tagobjects = $tagobjects[$question->id]; $tagobjects = $tagobjects[$question->id];
$sortedtagobjects = question_sort_tags($tagobjects, $this->category); $categorycontext = context::instance_by_id($this->category->contextid);
$sortedtagobjects = question_sort_tags($tagobjects, $categorycontext);
if (!empty($sortedtagobjects->coursetags)) { if (!empty($sortedtagobjects->coursetags)) {
// Set them on the form to be rendered as existing tags. // Set them on the form to be rendered as existing tags.