]> git.mjollnir.org Git - moodle.git/commitdiff
questiontypes: MDL-17800 Add extra_question_fields() support to backup/restore functions.
authortjhunt <tjhunt>
Thu, 15 Jan 2009 07:29:07 +0000 (07:29 +0000)
committertjhunt <tjhunt>
Thu, 15 Jan 2009 07:29:07 +0000 (07:29 +0000)
* Implement this in the base class, and
* use this in the shortanswer type as a proof of concept.

This makes writing new question type subclasses a bit easier. Thanks to Oleg Sychev for this code.

question/type/questiontype.php
question/type/shortanswer/questiontype.php

index 9785f71ab0f294a2891214d1a7e6d54cf829ed19..3548835062c7a89584f80a4c52950d310747431d 100644 (file)
@@ -1661,8 +1661,35 @@ class default_questiontype {
      * This is used in question/backuplib.php
      */
     function backup($bf,$preferences,$question,$level=6) {
-        // The default type has nothing to back up
-        return true;
+
+        $status = true;
+        $extraquestionfields = $this->extra_question_fields();
+
+        if (is_array($extraquestionfields)) {
+            $questionextensiontable = array_shift($extraquestionfields);
+            $record = get_record($questionextensiontable, $this->questionid_column_name(), $question);
+            if ($record) {
+                $tagname = strtoupper($this->name());
+                $status = $status && fwrite($bf, start_tag($tagname, $level, true));
+                foreach ($extraquestionfields as $field) {
+                    if (!isset($record->$field)) {
+                        echo "No data for field $field when backuping " .
+                                $this->name() . ' question id ' . $question;
+                        return false;
+                    }
+                    fwrite($bf, full_tag(strtoupper($field), $level + 1, false, $record->$field));
+                }
+                $status = $status && fwrite($bf, end_tag($tagname, $level, true));
+            }
+        }
+
+        $extraasnwersfields = $this->extra_answer_fields();
+        if (is_array($extraasnwersfields)) {
+            //TODO backup the answers, with any extra data.
+        } else {
+            $status = $status && question_backup_answers($bf, $preferences, $question);
+        }
+        return $status;
     }
 
 /// RESTORE FUNCTIONS /////////////////
@@ -1673,8 +1700,29 @@ class default_questiontype {
      * This is used in question/restorelib.php
      */
     function restore($old_question_id,$new_question_id,$info,$restore) {
-        // The default question type has nothing to restore
-        return true;
+
+        $status = true;
+        $extraquestionfields = $this->extra_question_fields();
+
+        if (is_array($extraquestionfields)) {
+            $questionextensiontable = array_shift($extraquestionfields);
+            $tagname = strtoupper($this->name());
+            $recordinfo = $info['#'][$tagname][0];
+
+            $record = new stdClass;
+            $qidcolname = $this->questionid_column_name();
+            $record->$qidcolname = $new_question_id;
+            foreach ($extraquestionfields as $field) {
+                $record->$field = backup_todb($recordinfo['#'][strtoupper($field)]['0']['#']);
+            }
+            if (!insert_record($questionextensiontable, $record)) {
+                echo "Can't insert record in $questionextensiontable when restoring " .
+                                $this->name() . ' question id ' . $question;
+                $status = false;
+            }
+        }
+        //TODO restore extra data in answers
+        return $status;
     }
 
     function restore_map($old_question_id,$new_question_id,$info,$restore) {
index 74a4ea8b29edef43fd383fb3cecb65e2471ebf49..269e903ae6e2492e10c1d47723c5d4b9ff5926e6 100644 (file)
@@ -219,34 +219,6 @@ class question_shortanswer_qtype extends default_questiontype {
         }
         return 0;
     }
-    /// BACKUP FUNCTIONS ////////////////////////////
-
-    /*
-     * Backup the data in the question
-     *
-     * This is used in question/backuplib.php
-     */
-    function backup($bf,$preferences,$question,$level=6) {
-        global $DB;
-
-        $status = true;
-
-        $shortanswers = $DB->get_records('question_shortanswer', array('question' =>  $question), 'id ASC');
-        //If there are shortanswers
-        if ($shortanswers) {
-            //Iterate over each shortanswer
-            foreach ($shortanswers as $shortanswer) {
-                $status = fwrite ($bf,start_tag("SHORTANSWER",$level,true));
-                //Print shortanswer contents
-                fwrite ($bf,full_tag("ANSWERS",$level+1,false,$shortanswer->answers));
-                fwrite ($bf,full_tag("USECASE",$level+1,false,$shortanswer->usecase));
-                $status = fwrite ($bf,end_tag("SHORTANSWER",$level,true));
-            }
-            //Now print question_answers
-            $status = question_backup_answers($bf,$preferences,$question);
-        }
-        return $status;
-    }
 
 /// RESTORE FUNCTIONS /////////////////
 
@@ -258,67 +230,41 @@ class question_shortanswer_qtype extends default_questiontype {
     function restore($old_question_id,$new_question_id,$info,$restore) {
         global $DB;
 
-        $status = true;
-
-        //Get the shortanswers array
-        $shortanswers = $info['#']['SHORTANSWER'];
+        $status = parent::restore($old_question_id, $new_question_id, $info, $restore);
 
-        //Iterate over shortanswers
-        for($i = 0; $i < sizeof($shortanswers); $i++) {
-            $sho_info = $shortanswers[$i];
-
-            //Now, build the question_shortanswer record structure
-            $shortanswer = new stdClass;
-            $shortanswer->question = $new_question_id;
-            $shortanswer->answers = backup_todb($sho_info['#']['ANSWERS']['0']['#']);
-            $shortanswer->usecase = backup_todb($sho_info['#']['USECASE']['0']['#']);
+        if ($status) {
+            $extraquestionfields = $this->extra_question_fields();
+            $questionextensiontable = array_shift($extraquestionfields);
 
             //We have to recode the answers field (a list of answers id)
-            //Extracts answer id from sequence
-            $answers_field = "";
-            $in_first = true;
-            $tok = strtok($shortanswer->answers,",");
-            while ($tok) {
-                //Get the answer from backup_ids
-                $answer = backup_getid($restore->backup_unique_code,"question_answers",$tok);
-                if ($answer) {
-                    if ($in_first) {
-                        $answers_field .= $answer->new_id;
-                        $in_first = false;
-                    } else {
-                        $answers_field .= ",".$answer->new_id;
-                    }
-                }
-                //check for next
-                $tok = strtok(",");
-            }
-            //We have the answers field recoded to its new ids
-            $shortanswer->answers = $answers_field;
-
-            //The structure is equal to the db, so insert the question_shortanswer
-            $newid = $DB->insert_record ("question_shortanswer",$shortanswer);
-
-            //Do some output
-            if (($i+1) % 50 == 0) {
-                if (!defined('RESTORE_SILENTLY')) {
-                    echo ".";
-                    if (($i+1) % 1000 == 0) {
-                        echo "<br />";
+            $questionextradata = $DB->get_record($questionextensiontable, array($this->questionid_column_name() => $new_question_id));
+            if (isset($questionextradata->answers)) {
+                $answers_field = "";
+                $in_first = true;
+                $tok = strtok($questionextradata->answers, ",");
+                while ($tok) {
+                    // Get the answer from backup_ids
+                    $answer = backup_getid($restore->backup_unique_code,"question_answers",$tok);
+                    if ($answer) {
+                        if ($in_first) {
+                            $answers_field .= $answer->new_id;
+                            $in_first = false;
+                        } else {
+                            $answers_field .= ",".$answer->new_id;
+                        }
                     }
+                    // Check for next
+                    $tok = strtok(",");
                 }
-                backup_flush(300);
-            }
-
-            if (!$newid) {
-                $status = false;
+                // We have the answers field recoded to its new ids
+                $questionextradata->answers = $answers_field;
+                // Update the question
+                $status = $status && $DB->update_record($questionextensiontable, $questionextradata);
             }
         }
-
-        return $status;
     }
 
-
-        /**
+   /**
     * Prints the score obtained and maximum score available plus any penalty
     * information
     *