From: mjollnir_ Date: Fri, 5 Sep 2008 17:07:22 +0000 (+0000) Subject: MDL-16360 - added support for mime-style subformat detection for portfolio supported... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=ea0de12f101bebd188adf50b6b008fd17ccee358;p=moodle.git MDL-16360 - added support for mime-style subformat detection for portfolio 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) --- diff --git a/lang/en_utf8/portfolio.php b/lang/en_utf8/portfolio.php index a6b45a7916..6c50377092 100644 --- a/lang/en_utf8/portfolio.php +++ b/lang/en_utf8/portfolio.php @@ -32,7 +32,9 @@ $string['filenotfound'] = 'File not found'; $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'; @@ -41,6 +43,7 @@ $string['instancenotdelete'] = 'Failed to delete portfolio'; $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)'; diff --git a/lib/portfoliolib.php b/lib/portfoliolib.php index 0ccb94dce9..9b5ae17581 100644 --- a/lib/portfoliolib.php +++ b/lib/portfoliolib.php @@ -100,6 +100,15 @@ define('PORTFOLIO_FORMAT_HTML', 'html'); */ 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 **** // @@ -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 one 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(); @@ -847,12 +904,17 @@ abstract class portfolio_caller_base { * @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 * @@ -2505,7 +2567,11 @@ 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 @@ -2513,7 +2579,11 @@ class portfolio_format_file {} * * 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 @@ -2521,7 +2591,31 @@ 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_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 diff --git a/mod/assignment/lib.php b/mod/assignment/lib.php index 8f248aea62..3b4597f214 100644 --- a/mod/assignment/lib.php +++ b/mod/assignment/lib.php @@ -3166,9 +3166,13 @@ class assignment_portfolio_caller extends portfolio_module_caller_base { 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() { @@ -3193,11 +3197,10 @@ class assignment_portfolio_caller extends portfolio_module_caller_base { } // 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(); diff --git a/mod/assignment/type/online/assignment.class.php b/mod/assignment/type/online/assignment.class.php index be8b87f2b6..381742239e 100644 --- a/mod/assignment/type/online/assignment.class.php +++ b/mod/assignment/type/online/assignment.class.php @@ -279,6 +279,10 @@ class assignment_online extends assignment_base { $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 { diff --git a/mod/chat/lib.php b/mod/chat/lib.php index 4f7b678581..edc6269c92 100644 --- a/mod/chat/lib.php +++ b/mod/chat/lib.php @@ -855,6 +855,7 @@ class chat_portfolio_caller extends portfolio_module_caller_base { $select, $params ); + $this->supportedformats = array(PORTFOLIO_FORMAT_HTML); } public function expected_time() { diff --git a/mod/data/lib.php b/mod/data/lib.php index e452cf1d4a..daeddcae9a 100755 --- a/mod/data/lib.php +++ b/mod/data/lib.php @@ -2466,6 +2466,7 @@ class data_portfolio_caller extends portfolio_module_caller_base { $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(); diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 1aef9a7447..a007987d23 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -7209,9 +7209,7 @@ class forum_portfolio_caller extends portfolio_module_caller_base { 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 { diff --git a/mod/glossary/lib.php b/mod/glossary/lib.php index ce197ae078..60ae1a86de 100644 --- a/mod/glossary/lib.php +++ b/mod/glossary/lib.php @@ -2454,6 +2454,7 @@ class glossary_entry_portfolio_caller extends portfolio_module_caller_base { || !$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() { diff --git a/mod/resource/lib.php b/mod/resource/lib.php index 16d0c57104..5bde803928 100644 --- a/mod/resource/lib.php +++ b/mod/resource/lib.php @@ -722,6 +722,16 @@ class resource_portfolio_caller extends portfolio_module_caller_base { $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() { @@ -734,7 +744,7 @@ class resource_portfolio_caller extends portfolio_module_caller_base { } 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; } diff --git a/portfolio/type/flickr/lib.php b/portfolio/type/flickr/lib.php index dbf3a25194..ff5b8420fb 100755 --- a/portfolio/type/flickr/lib.php +++ b/portfolio/type/flickr/lib.php @@ -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'); }