]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-16360 - added support for mime-style subformat detection for portfolio supported...
authormjollnir_ <mjollnir_>
Fri, 5 Sep 2008 17:07:22 +0000 (17:07 +0000)
committermjollnir_ <mjollnir_>
Fri, 5 Sep 2008 17:07:22 +0000 (17:07 +0000)
works well with flickr plugin (tested by uploading an image as a forum attachment and being offered flickr plugin but not for normal attachments)

lang/en_utf8/portfolio.php
lib/portfoliolib.php
mod/assignment/lib.php
mod/assignment/type/online/assignment.class.php
mod/chat/lib.php
mod/data/lib.php
mod/forum/lib.php
mod/glossary/lib.php
mod/resource/lib.php
portfolio/type/flickr/lib.php

index a6b45a79162644d2e1344bc71fe3f0c199422f68..6c503770921adac8f6cd0ef416b2a5b0701323b5 100644 (file)
@@ -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)';
index 0ccb94dce9486b87c43d6606ba347e0f43aa3ef8..9b5ae175811c156bc8bb62084d0b72baabaf0354 100644 (file)
@@ -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 <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();
@@ -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
index 8f248aea622330817238c67dadf399fce55fa484..3b4597f21492ecce7511bb85486d3177ff129dbc 100644 (file)
@@ -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();
index be8b87f2b69a68ae408a0b87ebe4d4c282e6b9b6..381742239ea3e41d31c77200910b7c3661d4cff1 100644 (file)
@@ -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 {
index 4f7b6785816a5941db792328db882bf09d79fd71..edc6269c9290ec533857b2e160eddcafed7719c9 100644 (file)
@@ -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() {
index e452cf1d4ad462161821541aa93a3291a02d8342..daeddcae9abf313dabe20d25e04697dd6cbaca29 100755 (executable)
@@ -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();
index 1aef9a74473ecd88c9d5674c562f537e7979e88c..a007987d236e834544708ef277e583ed15004d53 100644 (file)
@@ -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 {
index ce197ae078b61e8a852b4a44d94cbab62a369f95..60ae1a86dece887cba91ec6c97145a2898240145 100644 (file)
@@ -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() {
index 16d0c57104f37a75a6ab1e8b9ab64d2fe39ef011..5bde803928f8e72f767ab15fd85c52cde586b548 100644 (file)
@@ -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;
     }
 
index dbf3a2519422dd170a2d3171c49aa819ddbfe578..ff5b8420fbc4ebd0da016fc84af31da2d8986824 100755 (executable)
@@ -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');
     }