Skip to content

Commit

Permalink
MDL-16360 - added support for mime-style subformat detection for port…
Browse files Browse the repository at this point in the history
…folio supported formats

works well with flickr plugin (tested by uploading an image as a forum attachment and being offered flickr plugin but not for normal attachments)
  • Loading branch information
mjollnir_ committed Sep 5, 2008
1 parent 0b46f19 commit ea0de12
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 16 deletions.
5 changes: 4 additions & 1 deletion lang/en_utf8/portfolio.php
Expand Up @@ -32,7 +32,9 @@
$string['format_file'] = 'File';
$string['format_html'] = 'HTML';
$string['format_image'] = 'Image';
$string['format_mbkp'] = 'Moodle Backup';
$string['format_mbkp'] = 'Moodle Backup Format';
$string['format_video'] = 'Video';
$string['format_text'] = 'Plain Text';
$string['hidden'] = 'Hidden';
$string['instancedeleted'] = 'Portfolio deleted successfully';
$string['instanceismisconfigured'] = 'Portfolio instance is misconfigured, skipping. Error was: $a';
Expand All @@ -41,6 +43,7 @@
$string['instancesaved'] = 'Portfolio saved successfully';
$string['invalidaddformat'] = 'Invalid add format passed to portfolio_add_button. ($a) Must be one of PORTFOLIO_ADD_XXX';
$string['invalidtempid'] = 'Invalid export id. maybe it has expired';
$string['invalidfileargument'] = 'Invalid file argument passed to portfolio_format_from_file - must be stored_file object';
$string['invalidformat'] = 'Something is exporting an invalid format, $a';
$string['invalidinstance'] = 'Could not find that portfolio instance';
$string['invalidproperty'] = 'Could not find that property ($a->property of $a->class)';
Expand Down
104 changes: 99 additions & 5 deletions lib/portfoliolib.php
Expand Up @@ -100,6 +100,15 @@
*/
define('PORTFOLIO_FORMAT_IMAGE', 'image');

/**
* video - subtype of file
*/
define('PORTFOLIO_FORMAT_VIDEO', 'video');

/**
* text - subtype of file
*/
define('PORTFOLIO_FORMAT_TEXT', 'text');


// **** EXPORT TIME LEVELS **** //
Expand Down Expand Up @@ -399,11 +408,59 @@ function portfolio_supported_formats() {
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_MBKP, */ // later
/*PORTFOLIO_FORMAT_PIOP, */ // also later
);
}

/**
* this function returns the revelant portfolio export format
* which is used to determine which portfolio plugins can be used
* for exporting this content
* according to the mime type of the given file
* this only works when exporting exactly <b>one</b> file
*
* @param stored_file $file file to check mime type for
* @return string the format constant (see PORTFOLIO_FORMAT_XXX constants)
*/
function portfolio_format_from_file($file) {
static $alreadymatched;
if (empty($alreadymatched)) {
$alreadymatched = array();
}
if (!($file instanceof stored_file)) {
throw new portfolio_exception('invalidfileargument', 'portfolio');
}
$mimetype = $file->get_mimetype();
if (array_key_exists($mimetype, $alreadymatched)) {
return $alreadymatched[$mimetype];
}
$allformats = portfolio_supported_formats();
foreach ($allformats as $format => $classname) {
$supportedmimetypes = call_user_func(array($classname, 'mimetypes'));
if (!is_array($supportedmimetypes)) {
debugging("one of the portfolio format classes, $classname, said it supported something funny for mimetypes, should have been array...");
debugging(print_r($supportedmimetypes, true));
continue;
}
if (in_array($mimetype, $supportedmimetypes)) {
$alreadymatched[$mimetype] = $format;
return $format;
}
}
return PORTFOLIO_FORMAT_FILE; // base case for files...
}

/**
* walks both the caller formats and portfolio plugin formats
* and looks for matches (walking the hierarchy as well)
* and returns the intersection
*
* @param array $callerformats formats the caller supports
* @param array $pluginformats formats the portfolio plugin supports
*/
function portfolio_supported_formats_intersect($callerformats, $pluginformats) {
$allformats = portfolio_supported_formats();
$intersection = array();
Expand Down Expand Up @@ -847,12 +904,17 @@ public abstract function prepare_package();
* @return array list of formats
*/
public static function supported_formats($caller=null) {
if ($caller && $caller->get('supportedformats')) {
return $caller->get('supportedformats');
if ($caller && $formats = $caller->get('supportedformats')) {
if (is_array($formats)) {
return $formats;
}
debugging(get_class($caller) . ' has set a non array value of member variable supported formats - working around but should be fixed in code');
return array($formats);
}
return array(PORTFOLIO_FORMAT_FILE);
}


/**
* this is the "return to where you were" url
*
Expand Down Expand Up @@ -2505,23 +2567,55 @@ function portfolio_cron() {
*
* the most basic type - pretty much everything is a subtype
*/
class portfolio_format_file {}
class portfolio_format_file {
public static function mimetypes() {
return array(null);
}
}

/**
* this is just used to find an intersection of supported formats
* between the caller and portfolio plugins
*
* added for potential flickr plugin
*/
class portfolio_format_image extends portfolio_format_file {}
class portfolio_format_image extends portfolio_format_file {
public static function mimetypes() {
return mimeinfo_from_icon('type', 'image.gif', true);
}
}

/**
* this is just used to find an intersection of supported formats
* between the caller and portfolio plugins
*
* in case we want to be really specific.
*/
class portfolio_format_html extends portfolio_format_file {}
class portfolio_format_html extends portfolio_format_file {
public static function mimetypes() {
return array('text/html');
}
}

/**
* I guess there could be a youtube/google video plugin
* and anyway, the flickr plugin can support it already
*/
class portfolio_format_video extends portfolio_format_file {
public static function mimetypes() {
return mimeinfo_from_icon('type', 'video.gif', true);
}
}

/**
* class for plain text format.. not sure why we would need this yet
* but since resource module wants to export it... we can
*/
class portfolio_format_text extends portfolio_format_file {
public static function mimetypes() {
return array('text/plain');
}
}

/**
* this is just used to find an intersection of supported formats
Expand Down
15 changes: 9 additions & 6 deletions mod/assignment/lib.php
Expand Up @@ -3166,9 +3166,13 @@ public function __construct($callbackargs) {
if (!$this->assignment->portfolio_exportable()) {
throw new portfolio_caller_exception('notexportable', 'portfolio', $this->get_return_url());
}
global $USER;
$this->userid = $USER->id;
$this->file = (array_key_exists('file', $callbackargs)) ? $callbackargs['file'] : null;
if (array_key_exists('file', $callbackargs)) {
$fs = get_file_storage();
$this->file = $fs->get_file_by_id($callbackargs['file']);
$this->supportedformats = array(portfolio_format_from_file($this->file));
} else if (is_callable(array($this->assignment, 'portfolio_supported_formats'))) {
$this->supportedformats = $this->assignment->portfolio_supported_formats();
}
}

public function prepare_package() {
Expand All @@ -3193,11 +3197,10 @@ public function get_sha1() {
}

// default ...
$fs = get_file_storage();
$status = true;
if ($this->file) {
return $fs->get_file_by_id($this->file)->get_contenthash();
return $this->file->get_contenthash();
}
$fs = get_file_storage();
if ($files = $fs->get_area_files($this->assignment->context->id,
'assignment_submission', $this->user->id, '', false)) {
$sha1s = array();
Expand Down
4 changes: 4 additions & 0 deletions mod/assignment/type/online/assignment.class.php
Expand Up @@ -279,6 +279,10 @@ function portfolio_prepare_package($exporter, $userid=0) {
$submission = $this->get_submission($userid);
return $exporter->write_new_file(format_text($submission->data1, $submission->data2), 'assignment.html');
}

function portfolio_supported_formats() {
return array(PORTFOLIO_FORMAT_HTML);
}
}

class mod_assignment_online_edit_form extends moodleform {
Expand Down
1 change: 1 addition & 0 deletions mod/chat/lib.php
Expand Up @@ -855,6 +855,7 @@ public function __construct($callbackargs) {
$select,
$params
);
$this->supportedformats = array(PORTFOLIO_FORMAT_HTML);
}

public function expected_time() {
Expand Down
1 change: 1 addition & 0 deletions mod/data/lib.php
Expand Up @@ -2466,6 +2466,7 @@ public function __construct($callbackargs) {
$this->singlerecord = $DB->get_record('data_records', array('id' => $callbackargs['record']));
$this->singlerecord->content = $DB->get_records('data_content', array('recordid' => $this->singlerecord->id));
$this->exporttype = 'single';
$this->supportedformats = array(PORTFOLIO_FORMAT_HTML);
} else {
// all records as csv or whatever
$this->selectedfields = array();
Expand Down
4 changes: 1 addition & 3 deletions mod/forum/lib.php
Expand Up @@ -7209,9 +7209,7 @@ function __construct($callbackargs) {
throw new portfolio_caller_exception('noattachments', 'forum');
}
$this->postfiles = array($f);
if (in_array($f->get_mimetype(), array('image/gif', 'image/jpeg', 'image/png'))) {
$this->supportedformats = array(PORTFOLIO_FORMAT_IMAGE);
}
$this->supportedformats = array(portfolio_format_from_file($f));
} elseif ($this->post) {
$this->postfiles = $fs->get_area_files(get_context_instance(CONTEXT_MODULE, $this->cm->id)->id, 'forum_attachment', $this->post->id, "timemodified", false);
} else {
Expand Down
1 change: 1 addition & 0 deletions mod/glossary/lib.php
Expand Up @@ -2454,6 +2454,7 @@ public function __construct($callbackargs) {
|| !$this->entry = $DB->get_record('glossary_entries', array('id' => $callbackargs['entryid']))) {
throw new portfolio_caller_exception('noentry', 'glossary');
}
$this->supportedformats = array(PORTFOLIO_FORMAT_HTML);
}

public function expected_time() {
Expand Down
12 changes: 11 additions & 1 deletion mod/resource/lib.php
Expand Up @@ -722,6 +722,16 @@ public function __construct($callbackargs) {
$this->resourcefile = $CFG->dirroot.'/mod/resource/type/'.$this->cm->type.'/resource.class.php';
require_once($this->resourcefile);
$this->resource= new $resourceclass($this->cm->id);
// this is kind of yuk... but there's just not good enough OO here
$format = PORTFOLIO_FORMAT_FILE;
switch ($this->cm->type) {
case 'html':
$format = PORTFOLIO_FORMAT_HTML;
case 'text':
$format = PORTFOLIO_FORMAT_TEXT;
case 'file':
// $format = portfolio_format_from_mimetype($something); // change after we switch upload type resources over to new files api.
}
}

public function __wakeup() {
Expand All @@ -734,7 +744,7 @@ public function __wakeup() {
}

public function expected_time() {
// @todo penny check filesize if the type is uploadey
// @todo penny check filesize if the type is uploadey (not implemented yet)
return PORTFOLIO_TIME_LOW;
}

Expand Down
4 changes: 4 additions & 0 deletions portfolio/type/flickr/lib.php
Expand Up @@ -6,6 +6,10 @@ class portfolio_plugin_flickr extends portfolio_plugin_push_base {

private $flickr;

public static function supported_formats() {
return array(PORTFOLIO_FORMAT_IMAGE);
}

public static function get_name() {
return get_string('pluginname', 'portfolio_flickr');
}
Expand Down

0 comments on commit ea0de12

Please sign in to comment.