Merge branch 'MDL-81456-main' of https://github.com/andrewnicols/moodle
[moodle.git] / backup / moodle2 / restore_qtype_extrafields_plugin.class.php
blobe976f38520c884dde40074214976f574b28d2038
1 <?php
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.
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/>.
17 /**
18 * Defines restore_qtype_extrafields_plugin class
20 * @package core_backup
21 * @copyright 2012 Oleg Sychev, Volgograd State Technical University
22 * @author Valeriy Streltsov <vostreltsov@gmail.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->dirroot . '/question/engine/bank.php');
31 /**
32 * Class extending restore_qtype_plugin in order to use extra fields method
34 * See qtype_shortanswer for an example
36 * @copyright 2012 Oleg Sychev, Volgograd State Technical University
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class restore_qtype_extrafields_plugin extends restore_qtype_plugin {
41 /**
42 * Question type class for a particular question type
43 * @var question_type
45 protected $qtypeobj;
47 /**
48 * Constructor
50 * @param string $plugintype plugin type
51 * @param string $pluginname plugin name
52 * @param restore_step $step step
54 public function __construct($plugintype, $pluginname, $step) {
55 parent::__construct($plugintype, $pluginname, $step);
56 $this->qtypeobj = question_bank::get_qtype($this->pluginname);
59 /**
60 * Returns the paths to be handled by the plugin at question level.
62 protected function define_question_plugin_structure() {
63 $paths = array();
65 // This qtype uses question_answers, add them.
66 $this->add_question_question_answers($paths);
68 // Add own qtype stuff.
69 $elepath = $this->get_pathfor('/' . $this->qtypeobj->name());
70 $paths[] = new restore_path_element($this->qtypeobj->name(), $elepath);
72 $elepath = $this->get_pathfor('/answers/answer/extraanswerdata');
73 $paths[] = new restore_path_element('extraanswerdata', $elepath);
75 return $paths;
78 /**
79 * Processes the extra answer data
81 * @param array $data extra answer data
83 public function process_extraanswerdata($data) {
84 global $DB;
86 $extra = $this->qtypeobj->extra_answer_fields();
87 $tablename = array_shift($extra);
89 $oldquestionid = $this->get_old_parentid('question');
90 $questioncreated = $this->get_mappingid('question_created', $oldquestionid) ? true : false;
92 if ($questioncreated) {
93 $data['answerid'] = $this->get_mappingid('question_answer', $data['id']);
94 $DB->insert_record($tablename, $data);
95 } else {
96 $DB->update_record($tablename, $data);
101 * Process the qtype/... element.
103 * @param array $data question data
105 public function really_process_extra_question_fields($data) {
106 global $DB;
108 $oldid = $data['id'];
110 // Detect if the question is created or mapped.
111 $oldquestionid = $this->get_old_parentid('question');
112 $newquestionid = $this->get_new_parentid('question');
113 $questioncreated = $this->get_mappingid('question_created', $oldquestionid) ? true : false;
115 // If the question has been created by restore, we need to create its qtype_... too.
116 if ($questioncreated) {
117 $extraquestionfields = $this->qtypeobj->extra_question_fields();
118 $tablename = array_shift($extraquestionfields);
120 // Adjust some columns.
121 $qtfield = $this->qtypeobj->questionid_column_name();
122 $data[$qtfield] = $newquestionid;
124 // Insert record.
125 $newitemid = $DB->insert_record($tablename, $data);
127 // Create mapping.
128 $this->set_mapping($tablename, $oldid, $newitemid);