]> git.mjollnir.org Git - moodle.git/commitdiff
Moodle xml format can now export and import the actual image files, encoding
authorthepurpleblob <thepurpleblob>
Fri, 21 Apr 2006 12:35:57 +0000 (12:35 +0000)
committerthepurpleblob <thepurpleblob>
Fri, 21 Apr 2006 12:35:57 +0000 (12:35 +0000)
in 'base64' format.

question/format.php
question/format/xml/format.php

index 200aa7be143de8d25fdf6c5a9d636b9a3317746b..cc737d6881a77266b6f6231357960642c21709d6 100644 (file)
@@ -205,6 +205,39 @@ class qformat_default {
         return true;
     }
 
+    function importimagefile( $path, $base64 ) {
+    /// imports an image file encoded in base64 format
+    /// This should not be overridden.
+        global $CFG;
+
+        // all this to get the destination directory
+        // and filename!
+        $fullpath = "{$CFG->dataroot}/{$this->course->id}/$path";
+        $path_parts = pathinfo( $fullpath );
+        $destination = $path_parts['dirname'];
+        $file = clean_filename( $path_parts['basename'] );
+
+        // detect and fix any filename collision - get unique filename
+        $newfiles = resolve_filename_collisions( $destination, array($file) );        
+        $newfile = $newfiles[0];
+
+        // convert and save file contents
+        if (!$content = base64_decode( $base64 )) {
+            return false;
+        }
+        $newfullpath = "$destination/$newfile";
+        if (!$fh = fopen( $newfullpath, 'w' )) {
+            return false;
+        }
+        if (!fwrite( $fh, $content )) {
+            return false;
+        }
+        fclose( $fh );
+
+        // return the (possibly) new filename
+        return $newfile;
+    }
+
 // Export functions
 
 
index e3b94ebdf0169e50d490932776ebd4479b4a99ab..aec01a8760fcef6d0e2f460db24cda7c44b88452 100755 (executable)
@@ -79,6 +79,10 @@ class qformat_xml extends qformat_default {
         $qtext = $this->import_text( $question['#']['questiontext'][0]['#']['text'] );
         $qformat = $question['#']['questiontext'][0]['@']['format'];
         $image = $question['#']['image'][0]['#'];
+        if (!empty($question['#']['image_base64'][0]['#'])) {
+            $image_base64 = stripslashes( trim( $question['#']['image_base64'][0]['#'] ) );
+            $image = $this->importimagefile( $image, $image_base64 );
+        }
         $penalty = $question['#']['penalty'][0]['#'];
 
         $qo = $this->defaultquestion();
@@ -498,6 +502,24 @@ class qformat_xml extends qformat_default {
         return $content;
     }
 
+    function writeimage( $imagepath ) {
+    // includes image in base64
+        global $CFG;
+   
+        if (empty($imagepath)) {
+            return '';
+        }
+
+        $courseid = $this->course->id;
+        if (!$binary = file_get_contents( "{$CFG->dataroot}/$courseid/$imagepath" )) {
+            return '';
+        }
+
+        $content = "    <image_base64>\n".addslashes(base64_encode( $binary ))."\n".
+            "\n    </image_base64>\n";
+        return $content;
+    }
+
     function writequestion( $question ) {
     // turns question into string
     // question reflects database fields for general question and specific to type
@@ -518,10 +540,16 @@ class qformat_xml extends qformat_default {
         $expout .= "    <questiontext format=\"$qtformat\">\n";
         $expout .= $question_text;
         $expout .= "    </questiontext>\n";   
-        $expout .= "    <image>".$question->image."</image>\n";
+        $expout .= "    <image>{$question->image}</image>\n";
+        $expout .= $this->writeimage($question->image);
         $expout .= "    <penalty>{$question->penalty}</penalty>\n";
         $expout .= "    <hidden>{$question->hidden}</hidden>\n";
-        $expout .= "    <shuffleanswers>{$question->options->shuffleanswers}</shuffleanswers>\n";
+        if (!empty($question->options->shuffleanswers)) {
+            $expout .= "    <shuffleanswers>{$question->options->shuffleanswers}</shuffleanswers>\n";
+        }
+        else {
+            $expout .= "    <shuffleanswers>0</shuffleanswers>\n";
+        }
 
         // output depends on question type
         switch($question->qtype) {