2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Essay question type upgrade code.
22 * @copyright 2011 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
31 * Upgrade code for the essay question type.
32 * @param int $oldversion the version we are upgrading from.
34 function xmldb_qtype_essay_upgrade($oldversion) {
37 $dbman = $DB->get_manager();
40 // Moodle v2.2.0 release upgrade line
41 // Put any upgrade step following this
43 if ($oldversion < 2011102701) {
46 JOIN {question_answers} qa ON qa.question = q.id
48 WHERE q.qtype = 'essay'
49 AND " . $DB->sql_isnotempty('question_answers', 'feedback', false, true);
50 // In Moodle <= 2.0 essay had both question.generalfeedback and question_answers.feedback.
51 // This was silly, and in Moodel >= 2.1 only question.generalfeedback. To avoid
52 // dataloss, we concatenate question_answers.feedback onto the end of question.generalfeedback.
53 $count = $DB->count_records_sql("
54 SELECT COUNT(1) $sql");
56 $progressbar = new progress_bar('essay23', 500, true);
59 $toupdate = $DB->get_recordset_sql("
62 q.generalfeedbackformat,
67 foreach ($toupdate as $data) {
68 $progressbar->update($done, $count, "Updating essay feedback ($done/$count).");
69 upgrade_set_timeout(60);
70 if ($data->generalfeedbackformat == $data->feedbackformat) {
71 $DB->set_field('question', 'generalfeedback',
72 $data->generalfeedback . $data->feedback,
73 array('id' => $data->id));
76 $newdata = new stdClass();
77 $newdata->id = $data->id;
78 $newdata->generalfeedback =
79 qtype_essay_convert_to_html($data->generalfeedback, $data->generalfeedbackformat) .
80 qtype_essay_convert_to_html($data->feedback, $data->feedbackformat);
81 $newdata->generalfeedbackformat = FORMAT_HTML;
82 $DB->update_record('question', $newdata);
86 $progressbar->update($count, $count, "Updating essay feedback complete!");
90 // Essay savepoint reached.
91 upgrade_plugin_savepoint(true, 2011102701, 'qtype', 'essay');
94 if ($oldversion < 2011102702) {
95 // Then we delete the old question_answers rows for essay questions.
96 $DB->delete_records_select('question_answers',
97 "question IN (SELECT id FROM {question} WHERE qtype = 'essay')");
99 // Essay savepoint reached.
100 upgrade_plugin_savepoint(true, 2011102702, 'qtype', 'essay');
103 // Moodle v2.3.0 release upgrade line
104 // Put any upgrade step following this
107 // Moodle v2.4.0 release upgrade line
108 // Put any upgrade step following this
110 if ($oldversion < 2013011800) {
111 // Then we delete the old question_answers rows for essay questions.
112 $DB->delete_records_select('qtype_essay_options', "NOT EXISTS (
113 SELECT 1 FROM {question} WHERE qtype = 'essay' AND
114 {question}.id = {qtype_essay_options}.questionid)");
116 // Essay savepoint reached.
117 upgrade_plugin_savepoint(true, 2013011800, 'qtype', 'essay');
124 * Convert some content to HTML.
125 * @param string $text the content to convert to HTML
126 * @param int $oldformat One of the FORMAT_... constants.
128 function qtype_essay_convert_to_html($text, $oldformat) {
129 switch ($oldformat) {
130 // Similar to format_text.
134 $text = str_replace(' ', ' ', $text);
135 $text = nl2br($text);
138 case FORMAT_MARKDOWN:
139 return markdown_to_html($text);
142 return text_to_html($text);
148 throw new coding_exception(
149 'Unexpected text format when upgrading essay questions.');