]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-9432 - When restoring questions, links are not recoded. Merged from OU moodle.
authortjhunt <tjhunt>
Wed, 18 Apr 2007 15:56:21 +0000 (15:56 +0000)
committertjhunt <tjhunt>
Wed, 18 Apr 2007 15:56:21 +0000 (15:56 +0000)
backup/restorelib.php
question/restorelib.php
question/type/match/questiontype.php
question/type/multichoice/questiontype.php
question/type/questiontype.php

index 087bb336ec15a0680221d96297719532460bddb0..e1046f914d87f294bd4ebac15de1098e0744a2f1 100644 (file)
@@ -70,6 +70,8 @@
         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) {
                 }
             }
         }
+
+        // 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;
     }
     
index a0a18b00add1457ca898cdbd6d9ab4080298a871..cb380e8ece40bfc581a3d61ac8643bf9a11882d9 100644 (file)
         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;
+    }
 ?>
index 8c5e97e6078977ee7c7f74ab539717081ebbcb61..1b243b66ca1aa45e6b5ef65924cd36eb7d1d19d6 100644 (file)
@@ -370,7 +370,7 @@ class question_match_qtype extends default_questiontype {
 
     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;
             }
         }
@@ -609,6 +609,35 @@ class question_match_qtype extends default_questiontype {
         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 ////
 
index a10c7d12fa7084794002472e4e8464da5c90ea19..5697efedafdc63d831b772a1d9824fa67603e465 100644 (file)
@@ -14,6 +14,10 @@ class question_multichoice_qtype extends default_questiontype {
         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
@@ -549,6 +553,42 @@ class question_multichoice_qtype extends default_questiontype {
         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);
+                }
+            }
+        }
+    }
+
     /**
      * @return array of the numbering styles supported. For each one, there
      *      should be a lang string answernumberingxxx in teh qtype_multichoice
index 61dbcc13e9e6e8f13225eda0e711db994ab36aec..c93a61e2115c3a1a462add42e5f139d7d93fe55b 100644 (file)
@@ -74,6 +74,14 @@ class default_questiontype {
         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