MDL-16587 bit of a large refactor of the portfolio formats. still more to come.

This commit is contained in:
mjollnir_ 2008-10-11 17:33:20 +00:00
parent a813ddc975
commit 6be1dcae99
16 changed files with 295 additions and 141 deletions

View file

@ -64,6 +64,15 @@ abstract class portfolio_caller_base {
*/
protected $supportedformats;
/**
* set this for single file exports
*/
protected $singlefile;
/**
* set this for multi file exports
*/
protected $multifiles;
public function __construct($callbackargs) {
$expected = call_user_func(array(get_class($this), 'expected_callbackargs'));
@ -122,6 +131,19 @@ abstract class portfolio_caller_base {
*/
public abstract function expected_time();
/**
* helper method to calculate expected time for multi or single file exports
*/
public function expected_time_file() {
if ($this->multifiles) {
return portfolio_expected_time_file($this->multifiles);
}
else if ($this->singlefile) {
return portfolio_expected_time_file($this->singlefile);
}
return PORTFOLIO_TIME_LOW;
}
/**
* used for displaying the navigation during the export screens.
*
@ -139,6 +161,24 @@ abstract class portfolio_caller_base {
*/
public abstract function get_sha1();
/**
* helper function to calculate the sha1 for multi or single file exports
*/
public function get_sha1_file() {
if (empty($this->singlefile) && empty($this->multifiles)) {
throw new portfolio_caller_exception('invalidsha1file', 'portfolio', $this->get_return_url());
}
if ($this->singlefile) {
return $this->singlefile->get_contenthash();
}
$sha1s = array();
foreach ($this->multifiles as $file) {
$sha1s[] = $file->get_contenthash();
}
asort($sha1s);
return sha1(implode('', $sha1s));
}
/*
* generic getter for properties belonging to this instance
* <b>outside</b> the subclasses
@ -248,6 +288,19 @@ abstract class portfolio_caller_base {
*/
public abstract function prepare_package();
/**
* helper function to copy files into the temp area
* for single or multi file exports.
*/
public function prepare_package_file() {
if ($this->singlefile) {
return $this->exporter->copy_existing_file($this->singlefile);
}
foreach ($this->multifiles as $file) {
$this->exporter->copy_existing_file($file);
}
}
/**
* array of formats this caller supports
* the intersection of what this function returns
@ -299,6 +352,44 @@ abstract class portfolio_caller_base {
public abstract function load_data();
public function set_file_and_format_data($ids=null /* ..pass arguments to area files here. */) {
$args = func_get_args();
array_shift($args); // shift off $ids
if (empty($ids) && count($args) == 0) {
return;
}
$files = array();
$fs = get_file_storage();
if (!empty($ids)) {
if (is_numeric($ids) || $ids instanceof stored_file) {
$ids = array($ids);
}
foreach ($ids as $id) {
if ($id instanceof stored_file) {
$files[] = $id;
} else {
$files[] = $fs->get_file_by_id($id);
}
}
} else if (count($args) != 0) {
if (count($args) != 3) {
throw new portfolio_caller_exception('invalidfileareaargs', 'portfolio');
}
$files = array_values(call_user_func_array(array($fs, 'get_area_files'), $args));
}
switch (count($files)) {
case 0: return;
case 1: {
$this->singlefile = $files[0];
$this->supportedformats = array(portfolio_format_from_file($this->singlefile));
return;
}
default: {
$this->multifiles = $files;
}
}
}
public static abstract function expected_callbackargs();
}

View file

@ -97,9 +97,15 @@ define('PORTFOLIO_FORMAT_FILE', 'file');
define('PORTFOLIO_FORMAT_MBKP', 'mbkp');
/**
* html - subtype of file
* richhtml - like html but with attachments.
*/
define('PORTFOLIO_FORMAT_HTML', 'html');
define('PORTFOLIO_FORMAT_RICHHTML', 'richhtml');
/**
* plainhtml - a single html representation - no attachments
*/
define('PORTFOLIO_FORMAT_PLAINHTML', 'plainhtml');
/**
* image - subtype of file

View file

@ -113,6 +113,12 @@ class portfolio_exporter {
*/
private $newfilehashes;
/**
* selected exportformat
* this is also set in export_config in the portfolio and caller classes
*/
private $format;
/**
* construct a new exporter for use
*
@ -142,6 +148,9 @@ class portfolio_exporter {
* like name, visible etc.
*/
public function get($field) {
if ($field == 'format') {
return portfolio_format_object($this->format);
}
if (property_exists($this, $field)) {
return $this->{$field};
}
@ -320,6 +329,7 @@ class portfolio_exporter {
$callerbits['hideformat'] = $pluginbits['hideformat'] = (count($formats) == 1);
$this->caller->set_export_config($callerbits);
$this->instance->set_export_config($pluginbits);
$this->set('format', $fromform->format);
return true;
} else {
$this->print_header('configexport');
@ -338,6 +348,7 @@ class portfolio_exporter {
'format' => $format,
'hideformat' => 1
);
$this->set('format', $format);
$this->instance->set_export_config($config);
$this->caller->set_export_config(array('format' => $format, 'hideformat' => 1));
if ($expectedtime == PORTFOLIO_TIME_FORCEQUEUE) {
@ -680,6 +691,9 @@ class portfolio_exporter {
}
$fs = get_file_storage();
$file_record = $this->new_file_record_base($oldfile->get_filename());
if ($dir = $this->get('format')->get_file_directory()) {
$file_record->filepath = '/'. $dir . '/';
}
try {
$newfile = $fs->create_file_from_storedfile($file_record, $oldfile->get_id());
$this->newfilehashes[$newfile->get_contenthash()] = $newfile;
@ -698,9 +712,12 @@ class portfolio_exporter {
* @param string $name filename to use
* @return new stored_file object
*/
public function write_new_file($content, $name) {
public function write_new_file($content, $name, $manifest=true) {
$fs = get_file_storage();
$file_record = $this->new_file_record_base($name);
if (empty($manifest) && ($dir = $this->get('format')->get_file_directory())) {
$file_record->filepath = '/' . $dir . '/';
}
return $fs->create_file_from_string($file_record, $content);
}

View file

@ -35,6 +35,14 @@ class portfolio_format_file {
public static function mimetypes() {
return array(null);
}
public static function get_file_directory() {
return null;
}
public static function file_output($file) {
return '';
}
}
/**
@ -51,7 +59,7 @@ class portfolio_format_image extends portfolio_format_file {
*
* in case we want to be really specific.
*/
class portfolio_format_html extends portfolio_format_file {
class portfolio_format_plainhtml extends portfolio_format_file {
public static function mimetypes() {
return array('text/html');
}
@ -82,11 +90,33 @@ class portfolio_format_text extends portfolio_format_file {
}
}
class portfolio_format_rich {
public static function mimetypes() {
return array(null);
}
}
class portfolio_format_richhtml extends portfolio_format_rich {
public static function get_file_directory() {
return 'site_files';
}
public static function file_output($file) {
$path = self::get_file_directory() . '/' . $file->get_filename();
if (in_array($file->get_mimetype(), portfolio_format_image::mimetypes())) {
return '<img src="' . $path . '" alt="' . $file->get_filename() . '" />';
}
return '<a href="' . $path . '">' . $file->get_filename() . '</a>';
}
}
class portfolio_format_leap extends portfolio_format_rich { }
/**
* later.... a moodle plugin might support this.
* it's commented out in portfolio_supported_formats so cannot currently be used.
*/
class portfolio_format_mbkp extends portfolio_format_file {}
class portfolio_format_mbkp extends portfolio_format_rich {}
?>

View file

@ -109,7 +109,17 @@ abstract class portfolio_plugin_base {
* @return array list of formats
*/
public static function supported_formats() {
return array(PORTFOLIO_FORMAT_FILE);
return array(PORTFOLIO_FORMAT_FILE, PORTFOLIO_FORMAT_RICHHTML);
}
/**
* override this if you are supporting the 'file' type (or a subformat)
* but have restrictions on mimetypes (see googledocs plugin for more info)
*
* @return boolean
*/
public static function file_mime_check($mimetype) {
return true;
}

View file

@ -221,7 +221,9 @@ class portfolio_add_button {
. $this->callbackclass . '&amp;course=' . (!empty($COURSE) ? $COURSE->id : 0);
$selectoutput = '';
if (count($this->instances) == 1) {
$instance = array_shift($this->instances);
$tmp = array_values($this->instances);
$instance = $tmp[0];
//$instance = array_shift($this->instances);
$formats = portfolio_supported_formats_intersect($this->formats, $instance->supported_formats());
if (count($formats) == 0) {
// bail. no common formats.
@ -457,13 +459,14 @@ function portfolio_instances($visibleonly=true, $useronly=true) {
*/
function portfolio_supported_formats() {
return array(
PORTFOLIO_FORMAT_FILE => 'portfolio_format_file',
PORTFOLIO_FORMAT_IMAGE => 'portfolio_format_image',
PORTFOLIO_FORMAT_HTML => 'portfolio_format_html',
PORTFOLIO_FORMAT_TEXT => 'portfolio_format_text',
PORTFOLIO_FORMAT_VIDEO => 'portfolio_format_video',
PORTFOLIO_FORMAT_FILE => 'portfolio_format_file',
PORTFOLIO_FORMAT_IMAGE => 'portfolio_format_image',
PORTFOLIO_FORMAT_RICHHTML => 'portfolio_format_richhtml',
PORTFOLIO_FORMAT_PLAINHTML => 'portfolio_format_plainhtml',
PORTFOLIO_FORMAT_TEXT => 'portfolio_format_text',
PORTFOLIO_FORMAT_VIDEO => 'portfolio_format_video',
/*PORTFOLIO_FORMAT_MBKP, */ // later
/*PORTFOLIO_FORMAT_PIOP, */ // also later
/*PORTFOLIO_FORMAT_LEAP, */ // also later
);
}
@ -570,6 +573,18 @@ function portfolio_most_specific_formats($specificformats, $generalformats) {
return array_merge(array_values($specificformats), array_values($generalformats));
}
/**
* helper function to return a format object from the constant
*
* @param string $name the constant PORTFOLIO_FORMAT_XXX
*
* @return portfolio_format object
*/
function portfolio_format_object($name) {
$formats = portfolio_supported_formats();
return new $formats[$name];
}
/**
* helper function to return an instance of a plugin (with config loaded)
*