Permalink
Browse files

MDL-9432 - When restoring questions, links are not recoded. Merged fr…

…om OU moodle.
  • Loading branch information...
tjhunt
tjhunt committed Apr 18, 2007
1 parent 0145441 commit 0bb51a9c00615bc497ef24b0fae340f5e3288700
View
@@ -70,6 +70,8 @@ function restore_decode_content_links($restore) {
if (!defined('RESTORE_SILENTLY')) {
echo "<ul>";
}
+
+ // Restore links in modules.
foreach ($restore->mods as $name => $info) {
//If the module is being restored
if (isset($info->restore) && $info->restore == 1) {
@@ -87,12 +89,22 @@ function restore_decode_content_links($restore) {
}
}
}
+
+ // TODO: process all html text also in blocks too
+
+ // Restore links in questions.
+ if (!defined('RESTORE_SILENTLY')) {
+ echo '<li>' . get_string('from') . ' ' . get_string('questions', 'quiz');
+ }
+ $status = question_decode_content_links_caller($restore);
+ if (!defined('RESTORE_SILENTLY')) {
+ echo '</li>';
+ }
+
if (!defined('RESTORE_SILENTLY')) {
echo "</ul>";
}
-
- // TODO: process all html text also in blocks too
-
+
return $status;
}
View
@@ -739,4 +739,105 @@ function question_states_restore_mods($attempt_id,$info,$restore) {
return $status;
}
+ /**
+ * 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;
+ $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();
+
+ // Decode links in questions.
+ if ($questions = get_records_sql('SELECT q.id, q.qtype, q.questiontext, q.generalfeedback
+ FROM ' . $CFG->prefix . 'question q,
+ ' . $CFG->prefix . 'question_categories qc
+ WHERE q.category = qc.id
+ AND qc.course = ' . $restore->course_id)) {
+
+ 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 = addslashes($questiontext);
+ $question->generalfeedback = addslashes($generalfeedback);
+ if (!update_record('question', $question)) {
+ $status = false;
+ }
+ }
+
+ // 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 = get_records_sql('SELECT qa.id, qa.answer, qa.feedback, q.qtype
+ FROM ' . $CFG->prefix . 'question_answers qa,
+ ' . $CFG->prefix . 'question q,
+ ' . $CFG->prefix . 'question_categories qc
+ WHERE qa.question = q.id
+ AND q.category = qc.id
+ AND qc.course = ' . $restore->course_id)) {
+
+ 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 = addslashes($feedback);
+ $answer->answer = addslashes($answertext);
+ if (!update_record('question_answers', $answer)) {
+ $status = false;
+ }
+ }
+
+ // 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;
+ }
?>
@@ -366,7 +366,7 @@ function grade_responses(&$question, &$state, $cmoptions) {
function compare_responses($question, $state, $teststate) {
foreach ($state->responses as $i=>$sr){
- if($state->responses[$i] != $teststate->responses[$i]){
+ if(empty($teststate->responses[$i]) || $state->responses[$i] != $teststate->responses[$i]){
return false;
}
}
@@ -605,6 +605,35 @@ function restore_recode_answer($state, $restore) {
return $answer_field;
}
+ /**
+ * Decode links in question type specific tables.
+ * @return bool success or failure.
+ */
+ function decode_content_links_caller($questionids, $restore, &$i) {
+ // Decode links in the question_match_sub table.
+ if ($subquestions = get_records_list('question_match_sub', 'question',
+ implode(',', $questionids), '', 'id, questiontext')) {
+
+ foreach ($subquestions as $subquestion) {
+ $questiontext = restore_decode_content_links_worker($subquestion->questiontext, $restore);
+ if ($questiontext != $subquestion->questiontext) {
+ $subquestion->questiontext = addslashes($questiontext);
+ if (!update_record('question_match_sub', $subquestion)) {
+ $status = false;
+ }
+ }
+
+ // Do some output.
+ if (++$i % 5 == 0 && !defined('RESTORE_SILENTLY')) {
+ echo ".";
+ if ($i % 100 == 0) {
+ echo "<br />";
+ }
+ backup_flush(300);
+ }
+ }
+ }
+ }
}
//// END OF CLASS ////
@@ -17,6 +17,10 @@ function name() {
return 'multichoice';
}
+ function has_html_answers() {
+ return true;
+ }
+
function get_question_options(&$question) {
// Get additional information from database
// and attach it to the question object
@@ -551,6 +555,41 @@ function restore_recode_answer($state, $restore) {
return implode(',', $order).':'.implode(',', $responses);
}
+ /**
+ * Decode links in question type specific tables.
+ * @return bool success or failure.
+ */
+ function decode_content_links_caller($questionids, $restore, &$i) {
+ // Decode links in the question_multichoice table.
+ if ($multichoices = get_records_list('question_multichoice', 'question',
+ implode(',', $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 = addslashes($correctfeedback);
+ $subquestion->partiallycorrectfeedback = addslashes($partiallycorrectfeedback);
+ $subquestion->incorrectfeedback = addslashes($incorrectfeedback);
+ if (!update_record('question_multichoice', $multichoice)) {
+ $status = false;
+ }
+ }
+
+ // Do some output.
+ if (++$i % 5 == 0 && !defined('RESTORE_SILENTLY')) {
+ echo ".";
+ if ($i % 100 == 0) {
+ echo "<br />";
+ }
+ backup_flush(300);
+ }
+ }
+ }
+ }
}
//// END OF CLASS ////
@@ -71,6 +71,14 @@ function is_usable_by_random() {
return true;
}
+ /**
+ * @return whether the question_answers.answer field needs to have
+ * restore_decode_content_links_worker called on it.
+ */
+ function has_html_answers() {
+ return false;
+ }
+
/**
* Return an instance of the question editing form definition. This looks for a
* class called edit_{$this->name()}_question_form in the file

0 comments on commit 0bb51a9

Please sign in to comment.