From: mjollnir_ Date: Wed, 20 Aug 2008 13:30:37 +0000 (+0000) Subject: MDL-16127 - smarter handling of supported formats in callers X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=349242a31d404e8df85bfdfdd802425f13d2c6d7;p=moodle.git MDL-16127 - smarter handling of supported formats in callers --- diff --git a/admin/portfolio.php b/admin/portfolio.php index b08771dd7e..fd36268b3d 100644 --- a/admin/portfolio.php +++ b/admin/portfolio.php @@ -50,25 +50,21 @@ if (!empty($edit) || !empty($new)) { redirect($baseurl); exit; } else if ($fromform = $mform->get_data()){ - if (!confirm_sesskey()) { - print_error('confirmsesskeybad', '', $baseurl); + // unset whatever doesn't belong in fromform + foreach (array('edit', 'new', 'plugin', 'sesskey', 'submitbutton') as $key) { + unset($fromform->{$key}); } //this branch is where you process validated data. if ($edit) { - $success = $instance->set_config($fromform); - $success = $success && $instance->save(); - } - else { - $success = portfolio_static_function($plugin, 'create_instance', $plugin, $fromform->name, $fromform); - } - if ($success) { - $savedstr = get_string('instancesaved', 'portfolio'); - admin_externalpage_print_header(); - print_heading($savedstr); - redirect($baseurl, $savedstr, 3); + $instance->set_config($fromform); + $instance->save(); } else { - print_error('instancenotsaved', 'portfolio', $baseurl); + portfolio_static_function($plugin, 'create_instance', $plugin, $fromform->name, $fromform); } + $savedstr = get_string('instancesaved', 'portfolio'); + admin_externalpage_print_header(); + print_heading($savedstr); + redirect($baseurl, $savedstr, 3); exit; } else { admin_externalpage_print_header(); diff --git a/lib/portfoliolib.php b/lib/portfoliolib.php index deada132c5..e6e1213438 100644 --- a/lib/portfoliolib.php +++ b/lib/portfoliolib.php @@ -176,8 +176,10 @@ define('PORTFOLIO_ADD_TEXT_LINK', 4); * @param str $addstr string to use for the button or icon alt text or link text. * this is whole string, not key. optional, defaults to 'Add to portfolio'; * @param boolean $return whether to echo or return content (optional defaults to false (echo) +* @param array $callersupports if the calling code knows better than the static method on the calling class (supported_formats) +* eg, if there's a file that might be an image, you can pass it here instead */ -function portfolio_add_button($callbackclass, $callbackargs, $callbackfile=null, $format=PORTFOLIO_ADD_FULL_FORM, $addstr=null, $return=false) { +function portfolio_add_button($callbackclass, $callbackargs, $callbackfile=null, $format=PORTFOLIO_ADD_FULL_FORM, $addstr=null, $return=false, $callersupports=null) { global $SESSION, $CFG, $COURSE, $USER; @@ -219,7 +221,9 @@ function portfolio_add_button($callbackclass, $callbackargs, $callbackfile=null, require_once($CFG->dirroot . $callbackfile); - $callersupports = call_user_func(array($callbackclass, 'supported_formats')); + if (empty($callersupports)) { + $callersupports = call_user_func(array($callbackclass, 'supported_formats')); + } $formoutput = '
' . "\n"; $linkoutput = '' . "\n"; foreach ($instances as $instance) { - if (count(array_intersect($callerformats, $instance->supported_formats())) == 0) { + $formats = portfolio_supported_formats_intersect($callerformats, $instance->supported_formats()); + if (count($formats) == 0) { // bail. no common formats. continue; } @@ -386,7 +392,9 @@ function portfolio_instances($visibleonly=true, $useronly=true) { */ function portfolio_supported_formats() { return array( - PORTFOLIO_FORMAT_FILE => 'portfolio_format_file', + PORTFOLIO_FORMAT_FILE => 'portfolio_format_file', + PORTFOLIO_FORMAT_IMAGE => 'portfolio_format_image', + PORTFOLIO_FORMAT_HTML => 'portfolio_format_html', /*PORTFOLIO_FORMAT_MBKP, */ // later /*PORTFOLIO_FORMAT_PIOP, */ // also later ); @@ -648,9 +656,9 @@ abstract class portfolio_caller_base { protected $exporter; /** - * + * this can be overridden in subclasses constructors if they want */ - private $stage; + protected $supportedformats; /** * if this caller wants any additional config items @@ -829,10 +837,15 @@ abstract class portfolio_caller_base { * and what the selected portfolio plugin supports * will be used * use the constants PORTFOLIO_FORMAT_* + * if $caller is passed, that can be used for more specific guesses + * as this function must be called statically. * * @return array list of formats */ - public static function supported_formats() { + public static function supported_formats($caller=null) { + if ($caller && $caller->get('supportedformats')) { + return $caller->get('supportedformats'); + } return array(PORTFOLIO_FORMAT_FILE); } @@ -1294,13 +1307,6 @@ abstract class portfolio_plugin_base { require_once($CFG->dirroot . '/portfolio/type/' . $plugin . '/lib.php'); $classname = 'portfolio_plugin_' . $plugin; $obj = new $classname($newid); - // the form contains extra hidden fields used by the controller and stuff - // unset them here to avoid an exception - foreach ((array)$config as $key => $value) { - if (!in_array($key, $obj->get_allowed_config())) { - unset($config->{$key}); - } - } $obj->set_config($config); return $obj; } @@ -1925,7 +1931,7 @@ final class portfolio_exporter { if ($this->caller->has_export_config()) { $callerobj = $this->caller; } - $formats = portfolio_supported_formats_intersect($this->caller->supported_formats(), $this->instance->supported_formats()); + $formats = portfolio_supported_formats_intersect($this->caller->supported_formats($this->caller), $this->instance->supported_formats()); $expectedtime = $this->instance->expected_time($this->caller->expected_time()); if (count($formats) == 0) { // something went wrong, we should not have gotten this far. @@ -2090,7 +2096,7 @@ final class portfolio_exporter { public function process_stage_cleanup($pullok=false) { global $CFG, $DB, $SESSION; - if (!$pullok && !$this->get('instance')->is_push()) { + if (!$pullok && $this->get('instance') && !$this->get('instance')->is_push()) { unset($SESSION->portfolioexport); return true; } @@ -2313,7 +2319,7 @@ class portfolio_instance_select extends moodleform { $this->caller = $this->_customdata['caller']; $options = portfolio_instance_select( portfolio_instances(), - $this->caller->supported_formats(), + $this->caller->supported_formats($this->caller), get_class($this->caller), 'instance', true, diff --git a/mod/forum/lib.php b/mod/forum/lib.php index b5c57f10fc..f4c60adc2c 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -3880,7 +3880,12 @@ function forum_print_attachments($post, $cm, $type) { 'postid' => $post->id, 'attachment' => $file->get_id(), ); - $imagereturn .= portfolio_add_button('forum_portfolio_caller', $p, '/mod/forum/lib.php', PORTFOLIO_ADD_ICON_LINK, null, true); + $imagereturn .= portfolio_add_button( + 'forum_portfolio_caller', + $p, '/mod/forum/lib.php', + PORTFOLIO_ADD_ICON_LINK, + null, true, + array(PORTFOLIO_FORMAT_IMAGE)); } } else { $output .= "$iconimage "; @@ -7112,6 +7117,9 @@ class forum_portfolio_caller extends portfolio_module_caller_base { print_error('noattachments', 'forum'); } $this->files = array($f); + if (in_array($f->get_mimetype(), array('image/gif', 'image/jpeg', 'image/png'))) { + $this->supportedformats = array(PORTFOLIO_FORMAT_IMAGE); + } } $this->files = $fs->get_area_files(get_context_instance(CONTEXT_MODULE, $this->cm->id)->id, 'forum_attachment', $this->post->id, "timemodified", false); } diff --git a/portfolio/add.php b/portfolio/add.php index c731799cbf..76867fb78e 100644 --- a/portfolio/add.php +++ b/portfolio/add.php @@ -108,8 +108,7 @@ if (!$exporter->get('instance')) { exit; } else { - $exporter->print_header(); - print_heading(get_string('selectplugin', 'portfolio')); + $exporter->print_header('selectplugin'); print_simple_box_start(); $mform->display(); print_simple_box_end();