MDL-24817 backup - added support for decode contents in plugins (and qtypes)

This commit is contained in:
Eloy Lafuente 2010-11-01 15:49:01 +00:00
parent b5e58c1831
commit 9f68f2d5a6
12 changed files with 119 additions and 193 deletions

View file

@ -1,109 +0,0 @@
<?php
/**
* Recode content links in question texts.
* @param object $restore the restore metadata object.
* @return boolean whether the operation succeeded.
*/
function question_decode_content_links_caller($restore) {
global $CFG, $QTYPES, $DB;
$status = true;
$i = 1; //Counter to send some output to the browser to avoid timeouts
// Get a list of which question types have custom field that will need decoding.
$qtypeswithextrafields = array();
$qtypeswithhtmlanswers = array();
foreach ($QTYPES as $qtype => $qtypeclass) {
$qtypeswithextrafields[$qtype] = method_exists($qtypeclass, 'decode_content_links_caller');
$qtypeswithhtmlanswers[$qtype] = $qtypeclass->has_html_answers();
}
$extraprocessing = array();
$coursemodulecontexts = array();
$context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
$coursemodulecontexts[] = $context->id;
$cms = $DB->get_records('course_modules', array('course'=>$restore->course_id), '', 'id');
if ($cms){
foreach ($cms as $cm){
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$coursemodulecontexts[] = $context->id;
}
}
$coursemodulecontextslist = join($coursemodulecontexts, ',');
// Decode links in questions.
list($usql, $params) = $DB->get_in_or_equal(explode(',', $coursemodulecontextslist));
if ($questions = $DB->get_records_sql("SELECT q.id, q.qtype, q.questiontext, q.generalfeedback
FROM {question} q, {question_categories} qc
WHERE q.category = qc.id
AND qc.contextid $usql", $params)) {
foreach ($questions as $question) {
$questiontext = restore_decode_content_links_worker($question->questiontext, $restore);
$generalfeedback = restore_decode_content_links_worker($question->generalfeedback, $restore);
if ($questiontext != $question->questiontext || $generalfeedback != $question->generalfeedback) {
$question->questiontext = $questiontext;
$question->generalfeedback = $generalfeedback;
$DB->update_record('question', $question);
}
// Do some output.
if (++$i % 5 == 0 && !defined('RESTORE_SILENTLY')) {
echo ".";
if ($i % 100 == 0) {
echo "<br />";
}
backup_flush(300);
}
// Decode any questiontype specific fields.
if ($qtypeswithextrafields[$question->qtype]) {
if (!array_key_exists($question->qtype, $extraprocessing)) {
$extraprocessing[$question->qtype] = array();
}
$extraprocessing[$question->qtype][] = $question->id;
}
}
}
// Decode links in answers.
if ($answers = $DB->get_records_sql("SELECT qa.id, qa.answer, qa.feedback, q.qtype
FROM {question_answers} qa, {question} q, {question_categories} qc
WHERE qa.question = q.id
AND q.category = qc.id
AND qc.contextid $usql", $params)) {
foreach ($answers as $answer) {
$feedback = restore_decode_content_links_worker($answer->feedback, $restore);
if ($qtypeswithhtmlanswers[$answer->qtype]) {
$answertext = restore_decode_content_links_worker($answer->answer, $restore);
} else {
$answertext = $answer->answer;
}
if ($feedback != $answer->feedback || $answertext != $answer->answer) {
unset($answer->qtype);
$answer->feedback = $feedback;
$answer->answer = $answertext;
$DB->update_record('question_answers', $answer);
}
// Do some output.
if (++$i % 5 == 0 && !defined('RESTORE_SILENTLY')) {
echo ".";
if ($i % 100 == 0) {
echo "<br />";
}
backup_flush(300);
}
}
}
// Do extra work for certain question types.
foreach ($extraprocessing as $qtype => $questionids) {
if (!$QTYPES[$qtype]->decode_content_links_caller($questionids, $restore, $i)) {
$status = false;
}
}
return $status;
}

View file

@ -168,4 +168,16 @@ class restore_qtype_match_plugin extends restore_qtype_plugin {
}
return implode(',', $resultarr);
}
/**
* Return the contents of this qtype to be processed by the links decoder
*/
static public function define_decode_contents() {
$contents = array();
$contents[] = new restore_decode_content('question_match_sub', array('questiontext'), 'question_match_sub');
return $contents;
}
}

View file

@ -483,39 +483,6 @@ class question_match_qtype extends default_questiontype {
return 1 / count($question->options->subquestions);
}
/**
* Decode links in question type specific tables.
* @return bool success or failure.
*/
function decode_content_links_caller($questionids, $restore, &$i) {
global $DB;
$status = true;
// Decode links in the question_match_sub table.
if ($subquestions = $DB->get_records_list('question_match_sub', 'question', $questionids, '', 'id, questiontext')) {
foreach ($subquestions as $subquestion) {
$questiontext = restore_decode_content_links_worker($subquestion->questiontext, $restore);
if ($questiontext != $subquestion->questiontext) {
$subquestion->questiontext = $questiontext;
$DB->update_record('question_match_sub', $subquestion);
}
// Do some output.
if (++$i % 5 == 0 && !defined('RESTORE_SILENTLY')) {
echo ".";
if ($i % 100 == 0) {
echo "<br />";
}
backup_flush(300);
}
}
}
return $status;
}
function find_file_links($question, $courseid){
// find links in the question_match_sub table.
$urls = array();

View file

@ -75,8 +75,8 @@ class restore_qtype_multichoice_plugin extends restore_qtype_plugin {
$data->answers = implode(',', $answersarr);
// Insert record
$newitemid = $DB->insert_record('question_multichoice', $data);
// Create mapping (not needed, no files nor childs nor states here)
//$this->set_mapping('question_multichoice', $oldid, $newitemid);
// Create mapping (needed for decoding links)
$this->set_mapping('question_multichoice', $oldid, $newitemid);
} else {
// Nothing to remap if the question already existed
}
@ -121,4 +121,17 @@ class restore_qtype_multichoice_plugin extends restore_qtype_plugin {
}
return $result;
}
/**
* Return the contents of this qtype to be processed by the links decoder
*/
static public function define_decode_contents() {
$contents = array();
$fields = array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback');
$contents[] = new restore_decode_content('question_multichoice', $fields, 'question_multichoice');
return $contents;
}
}

View file

@ -420,46 +420,6 @@ class question_multichoice_qtype extends default_questiontype {
return $totalfraction / count($question->options->answers);
}
/**
* Decode links in question type specific tables.
* @return bool success or failure.
*/
function decode_content_links_caller($questionids, $restore, &$i) {
global $DB;
$status = true;
// Decode links in the question_multichoice table.
if ($multichoices = $DB->get_records_list('question_multichoice', 'question',
$questionids, '', 'id, correctfeedback, partiallycorrectfeedback, incorrectfeedback')) {
foreach ($multichoices as $multichoice) {
$correctfeedback = restore_decode_content_links_worker($multichoice->correctfeedback, $restore);
$partiallycorrectfeedback = restore_decode_content_links_worker($multichoice->partiallycorrectfeedback, $restore);
$incorrectfeedback = restore_decode_content_links_worker($multichoice->incorrectfeedback, $restore);
if ($correctfeedback != $multichoice->correctfeedback ||
$partiallycorrectfeedback != $multichoice->partiallycorrectfeedback ||
$incorrectfeedback != $multichoice->incorrectfeedback) {
$subquestion->correctfeedback = $correctfeedback;
$subquestion->partiallycorrectfeedback = $partiallycorrectfeedback;
$subquestion->incorrectfeedback = $incorrectfeedback;
$DB->update_record('question_multichoice', $multichoice);
}
// Do some output.
if (++$i % 5 == 0 && !defined('RESTORE_SILENTLY')) {
echo ".";
if ($i % 100 == 0) {
echo "<br />";
}
backup_flush(300);
}
}
}
return $status;
}
/**
* @return array of the numbering styles supported. For each one, there
* should be a lang string answernumberingxxx in teh qtype_multichoice