]> git.mjollnir.org Git - moodle.git/commitdiff
portfolio MDL-20896 added mimetype checking for portfolio plugins for single-file...
authorPenny Leach <penny@liip.ch>
Wed, 18 Nov 2009 15:34:58 +0000 (15:34 +0000)
committerPenny Leach <penny@liip.ch>
Wed, 18 Nov 2009 15:34:58 +0000 (15:34 +0000)
lib/portfolio/forms.php
lib/portfoliolib.php
mod/assignment/lib.php
mod/assignment/type/upload/assignment.class.php
mod/data/export_form.php
mod/forum/lib.php

index 61ce6dfbab1a470c5c9175e112533f850ad5bf62..50928129e53f92983202398931d468446d5f336f 100644 (file)
@@ -258,6 +258,7 @@ class portfolio_instance_select extends moodleform {
             portfolio_instances(),
             $this->caller->supported_formats($this->caller),
             get_class($this->caller),
+            $this->caller->get('singlefile'),
             'instance',
             true,
             true
index 994edbf533a86bd49786d16de05076dd865e6964..3ff296fa6f4d09b436909c47e5a26cd264473443 100644 (file)
@@ -43,6 +43,7 @@ require_once($CFG->libdir . '/portfolio/caller.php');      // the base classes f
  *
  * These class methods do not check permissions. the caller must check permissions first.
  * Later, during the export process, the caller class is instantiated and the check_permissions method is called
+ * If you are exporting a single file, you should always call set_format_by_file($file)
  *
  * This class can be used like this:
  * <code>
@@ -71,6 +72,7 @@ class portfolio_add_button {
     private $callbackfile;
     private $formats;
     private $instances;
+    private $file; // for single-file exports
 
     /**
     * constructor. either pass the options here or set them using the helper methods.
@@ -82,7 +84,7 @@ class portfolio_add_button {
     *                       key 'callbackfile': the file containing the class definition of your caller class.
     *                       See set_callback_options for more information on these three.
     *                       key 'formats': an array of PORTFOLIO_FORMATS this caller will support
-    *                       See set_formats for more information on this.
+    *                       See set_formats or set_format_by_file for more information on this.
     */
     public function __construct($options=null) {
         global $SESSION, $CFG;
@@ -165,6 +167,18 @@ class portfolio_add_button {
         $this->formats = portfolio_most_specific_formats($formats, $callerformats);
     }
 
+    /**
+     * if we already know we have exactly one file,
+     * bypass set_formats and just pass the file
+     * so we can detect the formats by mimetype.
+     *
+     * @param stored_file $file
+     */
+    public function set_format_by_file(stored_file $file) {
+        $this->file = $file;
+        $this->formats = array(portfolio_format_from_file($file));
+    }
+
     /*
     * echo the form/button/icon/text link to the page
     *
@@ -238,11 +252,15 @@ class portfolio_add_button {
                 debugging(get_string('singleinstancenomultiallowed', 'portfolio'));
                 return;
             }
+            if ($this->file && $this->file instanceof stored_file && !$instance->file_mime_check($this->file->get_mimetype())) {
+                // bail, we have a specific file and this plugin doesn't support it
+                return;
+            }
             $formoutput .= "\n" . '<input type="hidden" name="instance" value="' . $instance->get('id') . '" />';
             $linkoutput .= '&amp;instance=' . $instance->get('id');
         }
         else {
-            if (!$selectoutput = portfolio_instance_select($this->instances, $this->formats, $this->callbackclass, 'instance', true)) {
+            if (!$selectoutput = portfolio_instance_select($this->instances, $this->formats, $this->file, $this->callbackclass, 'instance', true)) {
                 return;
             }
         }
@@ -334,16 +352,17 @@ class portfolio_add_button {
 /**
 * returns a drop menu with a list of available instances.
 *
-* @param array    $instances     array of portfolio plugin instance objects - the instances to put in the menu
-* @param array    $callerformats array of PORTFOLIO_FORMAT_XXX constants - the formats the caller supports (this is used to filter plugins)
-* @param array    $callbackclass the callback class name - used for debugging only for when there are no common formats
-* @param string   $selectname    the name of the select element. Optional, defaults to instance.
-* @param boolean  $return        whether to print or return the output. Optional, defaults to print.
-* @param booealn  $returnarray   if returning, whether to return the HTML or the array of options. Optional, defaults to HTML.
+* @param array          $instances      array of portfolio plugin instance objects - the instances to put in the menu
+* @param array          $callerformats  array of PORTFOLIO_FORMAT_XXX constants - the formats the caller supports (this is used to filter plugins)
+* @param array          $callbackclass  the callback class name - used for debugging only for when there are no common formats
+* @param stored_file    $file           if we already know we have exactly one file, pass it here to do mime filtering.
+* @param string         $selectname     the name of the select element. Optional, defaults to instance.
+* @param boolean        $return         whether to print or return the output. Optional, defaults to print.
+* @param booealn        $returnarray    if returning, whether to return the HTML or the array of options. Optional, defaults to HTML.
 *
 * @return string the html, from <select> to </select> inclusive.
 */
-function portfolio_instance_select($instances, $callerformats, $callbackclass, $selectname='instance', $return=false, $returnarray=false) {
+function portfolio_instance_select($instances, $callerformats, $callbackclass, $file=null, $selectname='instance', $return=false, $returnarray=false) {
     global $CFG, $USER;
 
     if (empty($CFG->enableportfolios)) {
@@ -375,7 +394,10 @@ function portfolio_instance_select($instances, $callerformats, $callbackclass, $
             // bail, already exporting something with this plugin and it doesn't support multiple exports
             continue;
         }
-
+        if ($file && $file instanceof stored_file && !$instance->file_mime_check($file->get_mimetype())) {
+            // bail, we have a specific file and this plugin doesn't support it
+            continue;
+        }
         $count++;
         $selectoutput .= "\n" . '<option value="' . $instance->get('id') . '">' . $instance->get('name') . '</option>' . "\n";
         $options[$instance->get('id')] = $instance->get('name');
index 120d8e13aad0fd9ea45f6f3bc7dd38e248953642..dd6e750ccd5021803ce47b68e5e93815827e3ff7 100644 (file)
@@ -1808,7 +1808,7 @@ class assignment_base {
                 $output .= '<a href="'.$path.'" ><img src="'.$OUTPUT->old_icon_url(file_mimetype_icon($mimetype)).'" class="icon" alt="'.$mimetype.'" />'.s($filename).'</a>';
                 if ($this->portfolio_exportable() && has_capability('mod/assignment:exportownsubmission', $this->context)) {
                     $button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id, 'fileid' => $file->get_id()));
-                    $button->set_formats(portfolio_format_from_file($file));
+                    $button->set_format_by_file($file);
                     $output .= $button->to_html(PORTFOLIO_ADD_ICON_LINK);
                 }
                 $output .= '<br />';
index df8f611f7336cf7c48126202a405ab0ce0262a8c..984826680d93101fd6abc5973c34292653bc0b2f 100644 (file)
@@ -360,7 +360,7 @@ class assignment_upload extends assignment_base {
 
                 if (has_capability('mod/assignment:exportownsubmission', $this->context)) {
                     $button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id, 'fileid' => $file->get_id()), '/mod/assignment/lib.php');
-                    $button->set_formats(portfolio_format_from_file($file));
+                    $button->set_format_by_file($file);
                     $output .= $button->to_html(PORTFOLIO_ADD_ICON_LINK);
                 }
                 $output .= '<br />';
index 5df3bfbe845c13cf4489133d8cbf6954e00c5d96..db1af478fff6cb5e10f299c6836ad26a828e7ef1 100644 (file)
@@ -60,7 +60,7 @@ class mod_data_export_form extends moodleform {
             if ($portfoliooptions = portfolio_instance_select(
                 portfolio_instances(),
                 call_user_func(array('data_portfolio_caller', 'supported_formats')),
-                'data_portfolio_caller', '', true, true)) {
+                'data_portfolio_caller', null, '', true, true)) {
                 $mform->addElement('header', 'notice', get_string('portfolionotfile', 'data') . ':');
                 $portfoliooptions[0] = get_string('none');
                 ksort($portfoliooptions);
index e9d49704d13945ab67e704d819e5fbfee159e2ca..7b48fb7e0a3dae38ba9a60c55f92164db956a816 100644 (file)
@@ -4358,7 +4358,7 @@ function forum_print_attachments($post, $cm, $type) {
                 $output .= "<a href=\"$path\">".s($filename)."</a>";
                 if ($canexport) {
                     $button->set_callback_options('forum_portfolio_caller', array('postid' => $post->id, 'attachment' => $file->get_id()));
-                    $button->set_formats(portfolio_format_from_file($file));
+                    $button->set_format_by_file($file);
                     $output .= $button->to_html(PORTFOLIO_ADD_ICON_LINK);
                 }
                 $output .= "<br />";
@@ -4372,7 +4372,7 @@ function forum_print_attachments($post, $cm, $type) {
                     $imagereturn .= "<br /><img src=\"$path\" alt=\"\" />";
                     if ($canexport) {
                         $button->set_callback_options('forum_portfolio_caller', array('postid' => $post->id, 'attachment' => $file->get_id()));
-                        $button->set_formats(portfolio_format_from_file($file));
+                        $button->set_format_by_file($file);
                         $imagereturn .= $button->to_html(PORTFOLIO_ADD_ICON_LINK);
                     }
                 } else {
@@ -4380,7 +4380,7 @@ function forum_print_attachments($post, $cm, $type) {
                     $output .= filter_text("<a href=\"$path\">".s($filename)."</a>");
                     if ($canexport) {
                         $button->set_callback_options('forum_portfolio_caller', array('postid' => $post->id, 'attachment' => $file->get_id()));
-                        $button->set_formats(portfolio_format_from_file($file));
+                        $button->set_format_by_file($file);
                         $output .= $button->to_html(PORTFOLIO_ADD_ICON_LINK);
                     }
                     $output .= '<br />';