Merge branch 'MDL-70248-310' of https://github.com/HuongNV13/moodle into MOODLE_310_S...
[moodle.git] / question / type / essay / backup / moodle2 / restore_qtype_essay_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 essay 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_essay_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() {
3b3d5e79
TH
41 return array(
42 new restore_path_element('essay', $this->get_pathfor('/essay'))
43 );
44 }
41941110 45
3b3d5e79
TH
46 /**
47 * Process the qtype/essay element
48 */
49 public function process_essay($data) {
50 global $DB;
41941110 51
3b3d5e79
TH
52 $data = (object)$data;
53 $oldid = $data->id;
41941110 54
60527d0c
JMV
55 if (!isset($data->responsetemplate)) {
56 $data->responsetemplate = '';
57 }
58 if (!isset($data->responsetemplateformat)) {
59 $data->responsetemplateformat = FORMAT_HTML;
60 }
a4f765eb
KT
61 if (!isset($data->responserequired)) {
62 $data->responserequired = 1;
63 }
64 if (!isset($data->attachmentsrequired)) {
65 $data->attachmentsrequired = 0;
66 }
60527d0c 67
3d9645ae 68 // Detect if the question is created or mapped.
3b3d5e79
TH
69 $questioncreated = $this->get_mappingid('question_created',
70 $this->get_old_parentid('question')) ? true : false;
41941110 71
3b3d5e79 72 // If the question has been created by restore, we need to create its
3d9645ae 73 // qtype_essay too.
3b3d5e79
TH
74 if ($questioncreated) {
75 $data->questionid = $this->get_new_parentid('question');
76 $newitemid = $DB->insert_record('qtype_essay_options', $data);
77 $this->set_mapping('qtype_essay', $oldid, $newitemid);
78 }
79 }
80
81 /**
82 * Return the contents of this qtype to be processed by the links decoder
83 */
84 public static function define_decode_contents() {
85 return array(
86 new restore_decode_content('qtype_essay_options', 'graderinfo', 'qtype_essay'),
87 );
41941110 88 }
cee74600
TH
89
90 /**
91 * When restoring old data, that does not have the essay options information
92 * in the XML, supply defaults.
93 */
94 protected function after_execute_question() {
95 global $DB;
96
97 $essayswithoutoptions = $DB->get_records_sql("
822b4abb 98 SELECT q.*
cee74600 99 FROM {question} q
822b4abb
TH
100 JOIN {backup_ids_temp} bi ON bi.newitemid = q.id
101 LEFT JOIN {qtype_essay_options} qeo ON qeo.questionid = q.id
cee74600 102 WHERE q.qtype = ?
822b4abb
TH
103 AND qeo.id IS NULL
104 AND bi.backupid = ?
105 AND bi.itemname = ?
106 ", array('essay', $this->get_restoreid(), 'question_created'));
cee74600
TH
107
108 foreach ($essayswithoutoptions as $q) {
109 $defaultoptions = new stdClass();
110 $defaultoptions->questionid = $q->id;
111 $defaultoptions->responseformat = 'editor';
a4f765eb 112 $defaultoptions->responserequired = 1;
cee74600
TH
113 $defaultoptions->responsefieldlines = 15;
114 $defaultoptions->attachments = 0;
a4f765eb 115 $defaultoptions->attachmentsrequired = 0;
cee74600
TH
116 $defaultoptions->graderinfo = '';
117 $defaultoptions->graderinfoformat = FORMAT_HTML;
60527d0c
JMV
118 $defaultoptions->responsetemplate = '';
119 $defaultoptions->responsetemplateformat = FORMAT_HTML;
cee74600
TH
120 $DB->insert_record('qtype_essay_options', $defaultoptions);
121 }
122 }
41941110 123}