MDL-26587 Question : fixed restoring shortanswer where answers are missing due to...
[moodle.git] / question / type / shortanswer / backup / moodle2 / restore_qtype_shortanswer_plugin.class.php
CommitLineData
41941110 1<?php
41941110
EL
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * @package moodlecore
19 * @subpackage backup-moodle2
20 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
a17b297d 24
41941110
EL
25defined('MOODLE_INTERNAL') || die();
26
a17b297d 27
41941110
EL
28/**
29 * restore plugin class that provides the necessary information
30 * needed to restore one shortanswer qtype plugin
f7970e3c
TH
31 *
32 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41941110
EL
34 */
35class restore_qtype_shortanswer_plugin extends restore_qtype_plugin {
36
37 /**
38 * Returns the paths to be handled by the plugin at question level
39 */
40 protected function define_question_plugin_structure() {
41
42 $paths = array();
43
44 // This qtype uses question_answers, add them
45 $this->add_question_question_answers($paths);
46
47 // Add own qtype stuff
48 $elename = 'shortanswer';
8cfc4fbd
TH
49 // we used get_recommended_name() so this works
50 $elepath = $this->get_pathfor('/shortanswer');
41941110
EL
51 $paths[] = new restore_path_element($elename, $elepath);
52
41941110
EL
53 return $paths; // And we return the interesting paths
54 }
55
56 /**
57 * Process the qtype/shortanswer element
58 */
59 public function process_shortanswer($data) {
60 global $DB;
61
62 $data = (object)$data;
63 $oldid = $data->id;
64
65 // Detect if the question is created or mapped
66 $oldquestionid = $this->get_old_parentid('question');
67 $newquestionid = $this->get_new_parentid('question');
68 $questioncreated = $this->get_mappingid('question_created', $oldquestionid) ? true : false;
69
8cfc4fbd 70 // If the question has been created by restore, we need to create its
5f3d7b19
AB
71 // question_shortanswer too, if they are defined (the gui should ensure this).
72 if ($questioncreated && !empty($data->answers)) {
41941110
EL
73 // Adjust some columns
74 $data->question = $newquestionid;
75 // Map sequence of question_answer ids
76 $answersarr = explode(',', $data->answers);
77 foreach ($answersarr as $key => $answer) {
78 $answersarr[$key] = $this->get_mappingid('question_answer', $answer);
79 }
80 $data->answers = implode(',', $answersarr);
81 // Insert record
82 $newitemid = $DB->insert_record('question_shortanswer', $data);
83 // Create mapping
84 $this->set_mapping('question_shortanswer', $oldid, $newitemid);
41941110
EL
85 }
86 }
87}