From 887160c7cb855ae23a6eff9089efab7776f91526 Mon Sep 17 00:00:00 2001 From: Penny Leach Date: Wed, 18 Nov 2009 15:34:58 +0000 Subject: [PATCH] portfolio MDL-20896 added mimetype checking for portfolio plugins for single-file exports --- lib/portfolio/forms.php | 1 + lib/portfoliolib.php | 42 ++++++++++++++----- mod/assignment/lib.php | 2 +- .../type/upload/assignment.class.php | 2 +- mod/data/export_form.php | 2 +- mod/forum/lib.php | 6 +-- 6 files changed, 39 insertions(+), 16 deletions(-) diff --git a/lib/portfolio/forms.php b/lib/portfolio/forms.php index 61ce6dfbab..50928129e5 100644 --- a/lib/portfolio/forms.php +++ b/lib/portfolio/forms.php @@ -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 diff --git a/lib/portfoliolib.php b/lib/portfoliolib.php index 994edbf533..3ff296fa6f 100644 --- a/lib/portfoliolib.php +++ b/lib/portfoliolib.php @@ -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: * @@ -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" . ''; $linkoutput .= '&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 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" . '' . "\n"; $options[$instance->get('id')] = $instance->get('name'); diff --git a/mod/assignment/lib.php b/mod/assignment/lib.php index 120d8e13aa..dd6e750ccd 100644 --- a/mod/assignment/lib.php +++ b/mod/assignment/lib.php @@ -1808,7 +1808,7 @@ class assignment_base { $output .= ''.$mimetype.''.s($filename).''; 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 .= '
'; diff --git a/mod/assignment/type/upload/assignment.class.php b/mod/assignment/type/upload/assignment.class.php index df8f611f73..984826680d 100644 --- a/mod/assignment/type/upload/assignment.class.php +++ b/mod/assignment/type/upload/assignment.class.php @@ -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 .= '
'; diff --git a/mod/data/export_form.php b/mod/data/export_form.php index 5df3bfbe84..db1af478ff 100644 --- a/mod/data/export_form.php +++ b/mod/data/export_form.php @@ -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); diff --git a/mod/forum/lib.php b/mod/forum/lib.php index e9d49704d1..7b48fb7e0a 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -4358,7 +4358,7 @@ function forum_print_attachments($post, $cm, $type) { $output .= "".s($filename).""; 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 .= "
"; @@ -4372,7 +4372,7 @@ function forum_print_attachments($post, $cm, $type) { $imagereturn .= "
\"\""; 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("".s($filename).""); 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 .= '
'; -- 2.39.5