From 6fe7f2f198f57f0d4cdf2d4db3a0cef3c0fd829b Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Fri, 4 May 2012 18:43:13 +0100 Subject: [PATCH] MDL-31393 qtype_essay upgrade: save old question_answers.feebdack --- question/type/essay/db/upgrade.php | 80 ++++++++++++++++++++++++++++++ question/type/essay/version.php | 2 +- 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/question/type/essay/db/upgrade.php b/question/type/essay/db/upgrade.php index e74b8eaa2b3..376609df587 100644 --- a/question/type/essay/db/upgrade.php +++ b/question/type/essay/db/upgrade.php @@ -90,5 +90,85 @@ function xmldb_qtype_essay_upgrade($oldversion) { // Moodle v2.1.0 release upgrade line // Put any upgrade step following this + if ($oldversion < 2011060301) { + // In Moodle <= 2.0 essay had both question.generalfeedback and question_answers.feedback. + // This was silly, and in Moodel >= 2.1 only question.generalfeedback. To avoid + // dataloss, we concatenate question_answers.feedback onto the end of question.generalfeedback. + $toupdate = $DB->get_recordset_sql(" + SELECT q.id, + q.generalfeedback, + q.generalfeedbackformat, + qa.feedback, + qa.feedbackformat + + FROM {question} q + JOIN {question_answers} qa ON qa.question = q.id + + WHERE q.qtype = 'essay' + AND " . $DB->sql_isnotempty('question_answers', 'feedback', false, true)); + + foreach ($toupdate as $data) { + upgrade_set_timeout(60); + if ($data->generalfeedbackformat == $data->feedbackformat) { + $DB->set_field('question', 'generalfeedback', + $data->generalfeedback . $data->feedback, + array('id' => $data->id)); + + } else { + $newdata = new stdClass(); + $newdata->id = $data->id; + $newdata->generalfeedback = + qtype_essay_convert_to_html($data->generalfeedback, $data->generalfeedbackformat) . + qtype_essay_convert_to_html($data->feedback, $data->feedbackformat); + $newdata->generalfeedbackformat = FORMAT_HTML; + $DB->update_record('question', $newdata); + } + } + + $toupdate->close(); + + // Essay savepoint reached. + upgrade_plugin_savepoint(true, 2011060301, 'qtype', 'essay'); + } + + if ($oldversion < 2011060302) { + // Then we delete the old question_answers rows for essay questions. + $DB->delete_records_select('question_answers', + "question IN (SELECT id FROM {question} WHERE qtype = 'essay')"); + + // Essay savepoint reached. + upgrade_plugin_savepoint(true, 2011060302, 'qtype', 'essay'); + } + return true; } + +/** + * Convert some content to HTML. + * @param string $text the content to convert to HTML + * @param int $oldformat One of the FORMAT_... constants. + */ +function qtype_essay_convert_to_html($text, $oldformat) { + switch ($oldformat) { + // Similar to format_text. + + case FORMAT_PLAIN: + $text = s($text); + $text = str_replace(' ', '  ', $text); + $text = nl2br($text); + return $text; + + case FORMAT_MARKDOWN: + return markdown_to_html($text); + + case FORMAT_MOODLE: + return text_to_html($text); + + case FORMAT_HTML: + return $text; + + default: + throw new coding_exception( + 'Unexpected text format when upgrading essay questions.'); + } +} diff --git a/question/type/essay/version.php b/question/type/essay/version.php index cf4457b2a36..29a44b245d6 100644 --- a/question/type/essay/version.php +++ b/question/type/essay/version.php @@ -25,5 +25,5 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2011060300; +$plugin->version = 2011060302; $plugin->requires = 2011060313;