You are here

function qformat_default::importimagefile in Quiz 6.6

Same name and namespace in other branches
  1. 6.5 includes/moodle/question/format.php \qformat_default::importimagefile()

Import an image file encoded in base64 format

Parameters

string path path (in course data) to store picture:

string base64 encoded picture:

Return value

string filename (nb. collisions are handled)

1 call to qformat_default::importimagefile()
qformat_xml::import_headers in includes/moodle/question/format/xml/format.php
import parts of question common to all types

File

includes/moodle/question/format.php, line 544

Class

qformat_default
Base class for question import and export formats.

Code

function importimagefile($path, $base64) {
  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']);

  // check if path exists
  check_dir_exists($destination, true, true);

  // 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 '';
  }
  $newfullpath = "{$destination}/{$newfile}";
  if (!($fh = fopen($newfullpath, 'w'))) {
    return '';
  }
  if (!fwrite($fh, $content)) {
    return '';
  }
  fclose($fh);

  // return the (possibly) new filename
  $newfile = ereg_replace("{$CFG->dataroot}/{$this->course->id}/", '', $newfullpath);
  return $newfile;
}