Moodle xml format can now export and import the actual image files, encoding
in 'base64' format.
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user