MDL-31393 qtype_essay upgrade: save old question_answers.feebdack

This commit is contained in:
Tim Hunt
2012-05-09 15:13:29 +01:00
parent 6791b08315
commit 6fe7f2f198
2 changed files with 81 additions and 1 deletions
+80
View File
@@ -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(' ', '&nbsp; ', $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.');
}
}
+1 -1
View File
@@ -25,5 +25,5 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2011060300;
$plugin->version = 2011060302;
$plugin->requires = 2011060313;