Skip to content

Commit

Permalink
MDL-39169: Now the question's tags are added in backup/restore options
Browse files Browse the repository at this point in the history
  • Loading branch information
Maria Torres authored and danpoltawski committed May 6, 2013
1 parent 14cd647 commit f2dc4d2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
13 changes: 13 additions & 0 deletions backup/moodle2/backup_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1810,6 +1810,10 @@ protected function define_structure() {
$qhint = new backup_nested_element('question_hint', array('id'), array(
'hint', 'hintformat', 'shownumcorrect', 'clearwrong', 'options'));

$tags = new backup_nested_element('tags');

$tag = new backup_nested_element('tag', array('id'), array('name', 'rawname'));

// Build the tree

$qcategories->add_child($qcategory);
Expand All @@ -1818,6 +1822,9 @@ protected function define_structure() {
$question->add_child($qhints);
$qhints->add_child($qhint);

$question->add_child($tags);
$tags->add_child($tag);

// Define the sources

$qcategory->set_source_sql("
Expand All @@ -1837,6 +1844,12 @@ protected function define_structure() {
ORDER BY id',
array('questionid' => backup::VAR_PARENTID));

$tag->set_source_sql("SELECT t.id, t.name, t.rawname
FROM {tag} t
JOIN {tag_instance} ti ON ti.tagid = t.id
WHERE ti.itemid = ?
AND ti.itemtype = 'question'", array(backup::VAR_PARENTID));

// don't need to annotate ids nor files
// (already done by {@link backup_annotate_all_question_files}

Expand Down
27 changes: 26 additions & 1 deletion backup/moodle2/restore_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3063,13 +3063,15 @@ protected function define_structure() {
$hint = new restore_path_element('question_hint',
'/question_categories/question_category/questions/question/question_hints/question_hint');

$tag = new restore_path_element('tag','/question_categories/question_category/questions/question/tags/tag');

// Apply for 'qtype' plugins optional paths at question level
$this->add_plugin_structure('qtype', $question);

// Apply for 'local' plugins optional paths at question level
$this->add_plugin_structure('local', $question);

return array($category, $question, $hint);
return array($category, $question, $hint, $tag);
}

protected function process_question_category($data) {
Expand Down Expand Up @@ -3216,6 +3218,29 @@ protected function process_question_hint($data) {
$this->set_mapping('question_hint', $oldid, $newitemid);
}

protected function process_tag($data) {
global $CFG, $DB;

$data = (object)$data;
$newquestion = $this->get_new_parentid('question');

if (!empty($CFG->usetags)) { // if enabled in server
// TODO: This is highly inneficient. Each time we add one tag
// we fetch all the existing because tag_set() deletes them
// so everything must be reinserted on each call
$tags = array();
$existingtags = tag_get_tags('question', $newquestion);
// Re-add all the existitng tags
foreach ($existingtags as $existingtag) {
$tags[] = $existingtag->rawname;
}
// Add the one being restored
$tags[] = $data->rawname;
// Send all the tags back to the question
tag_set('question', $newquestion, $tags);
}
}

protected function after_execute() {
global $DB;

Expand Down

0 comments on commit f2dc4d2

Please sign in to comment.