diff --git a/lang/en_utf8/portfolio.php b/lang/en_utf8/portfolio.php index a18e3d72968..42dca51ba5c 100644 --- a/lang/en_utf8/portfolio.php +++ b/lang/en_utf8/portfolio.php @@ -83,7 +83,6 @@ $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['invalidfileareaargs'] = 'Invalid file area arguments passed to set_file_and_format_data - must contain contextid, filearea and itemid'; $string['invalidsha1file'] = 'Invalid call to get_sha1_file - either single or multifiles must be set'; $string['invalidpreparepackagefile'] = 'Invalid call to prepare_package_file - either single or multifiles must be set'; diff --git a/lib/portfolio/caller.php b/lib/portfolio/caller.php index ae600aad4b2..431a8aa6f61 100644 --- a/lib/portfolio/caller.php +++ b/lib/portfolio/caller.php @@ -74,6 +74,11 @@ abstract class portfolio_caller_base { */ protected $multifiles; + /** + * set this for generated-file exports + */ + protected $intendedmimetype; + public function __construct($callbackargs) { $expected = call_user_func(array(get_class($this), 'expected_callbackargs')); foreach ($expected as $key => $required) { @@ -473,6 +478,14 @@ abstract class portfolio_caller_base { $this->supportedformats[] = $format; } + public function get_mimetype() { + if ($this->singlefile instanceof stored_file) { + return $this->singlefile->get_mimetype(); + } else if (!empty($this->intendedmimetype)) { + return $this->intendedmimetype; + } + } + /** * array of arguments the caller expects to be passed through to it * this must be keyed on the argument name, and the array value is a boolean, diff --git a/lib/portfolio/constants.php b/lib/portfolio/constants.php index 1492cea6516..c1c744af2a4 100644 --- a/lib/portfolio/constants.php +++ b/lib/portfolio/constants.php @@ -213,3 +213,8 @@ define('PORTFOLIO_ADD_ICON_LINK', 3); */ define('PORTFOLIO_ADD_TEXT_LINK', 4); +/** + * hacky way to turn the button class into a url to redirect to + * this replaces the old portfolio_fake_add_url function + */ +define('PORTFOLIO_ADD_FAKE_URL', 5); diff --git a/lib/portfoliolib.php b/lib/portfoliolib.php index 44f961678c6..0558866f777 100644 --- a/lib/portfoliolib.php +++ b/lib/portfoliolib.php @@ -84,6 +84,7 @@ class portfolio_add_button { private $formats; private $instances; private $file; // for single-file exports + private $intendedmimetype; // for writing specific types of files /** * constructor. either pass the options here or set them using the helper methods. @@ -163,7 +164,8 @@ class portfolio_add_button { * @param array $formats if the calling code knows better than the static method on the calling class (base_supported_formats) * eg, if it's going to be a single file, or if you know it's HTML, you can pass it here instead * this is almost always the case so you should always use this. - * {@see portfolio_format_from_file} for how to get the appropriate formats to pass here for uploaded files. + * {@see portfolio_format_from_mimetype} for how to get the appropriate formats to pass here for uploaded files. + * or just call set_format_by_file instead */ public function set_formats($formats=null) { if (is_string($formats)) { @@ -179,6 +181,10 @@ class portfolio_add_button { $this->formats = portfolio_most_specific_formats($formats, $callerformats); } + /** + * reset formats to the default + * which is usually what base_supported_formats returns + */ public function reset_formats() { $this->set_formats(); } @@ -195,13 +201,33 @@ class portfolio_add_button { */ public function set_format_by_file(stored_file $file, $extraformats=null) { $this->file = $file; + $fileformat = portfolio_format_from_mimetype($file->get_mimetype()); if (is_string($extraformats)) { - $this->set_formats(array(portfolio_format_from_file($file), $extraformats)); - } else if (is_array($extraformats)) { - $this->set_formats(array_merge(array(portfolio_format_from_file($file)), $extraformats)); - } else { - $this->set_formats(portfolio_format_from_file($file)); + $extraformats = array($extraformats); + } else if (!is_array($extraformats)) { + $extraformats = array(); } + $this->set_formats(array_merge(array($fileformat), $extraformats)); + } + + /** + * correllary to set_format_by_file, but this is used when we don't yet have a stored_file + * when we're writing out a new type of file (like csv or pdf) + * + * @param string $extn the file extension we intend to generate + * @param mixed $extraformats any additional formats other than by mimetype + * eg leap2a etc + */ + public function set_format_by_intended_file($extn, $extraformats=null) { + $mimetype = mimeinfo('type', 'something. ' . $extn); + $fileformat = portfolio_format_from_mimetype($mimetype); + $this->intendedmimetype = $fileformat; + if (is_string($extraformats)) { + $extraformats = array($extraformats); + } else if (!is_array($extraformats)) { + $extraformats = array(); + } + $this->set_formats(array_merge(array($fileformat), $extraformats)); } /* @@ -238,8 +264,7 @@ class portfolio_add_button { // use the caller defaults $this->set_formats(); } - $formoutput = '