From: thepurpleblob Date: Fri, 21 Apr 2006 12:35:57 +0000 (+0000) Subject: Moodle xml format can now export and import the actual image files, encoding X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=d08e16b2a52233b2a63e7cb81d567d47836248e9;p=moodle.git Moodle xml format can now export and import the actual image files, encoding in 'base64' format. --- diff --git a/question/format.php b/question/format.php index 200aa7be14..cc737d6881 100644 --- a/question/format.php +++ b/question/format.php @@ -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 diff --git a/question/format/xml/format.php b/question/format/xml/format.php index e3b94ebdf0..aec01a8760 100755 --- a/question/format/xml/format.php +++ b/question/format/xml/format.php @@ -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 = " \n".addslashes(base64_encode( $binary ))."\n". + "\n \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 .= " \n"; $expout .= $question_text; $expout .= " \n"; - $expout .= " ".$question->image."\n"; + $expout .= " {$question->image}\n"; + $expout .= $this->writeimage($question->image); $expout .= " {$question->penalty}\n"; $expout .= " {$question->hidden}\n"; - $expout .= " {$question->options->shuffleanswers}\n"; + if (!empty($question->options->shuffleanswers)) { + $expout .= " {$question->options->shuffleanswers}\n"; + } + else { + $expout .= " 0\n"; + } // output depends on question type switch($question->qtype) {