67a87e7d |
1 | <?php |
67a87e7d |
2 | /** |
87fcac8d |
3 | * Moodle - Modular Object-Oriented Dynamic Learning Environment |
4 | * http://moodle.org |
5 | * Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com |
6 | * |
7 | * This program is free software: you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by |
9 | * the Free Software Foundation, either version 2 of the License, or |
10 | * (at your option) any later version. |
11 | * |
12 | * This program is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * GNU General Public License for more details. |
16 | * |
17 | * You should have received a copy of the GNU General Public License |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | * |
20 | * @package moodle |
21 | * @subpackage portfolio |
22 | * @author Penny Leach <penny@catalyst.net.nz> |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL |
24 | * @copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com |
25 | * |
26 | * This file contains all global functions to do with manipulating portfolios |
27 | * everything else that is logically namespaced by class is in its own file |
28 | * in lib/portfolio/ directory. |
29 | */ |
30 | |
31 | // require all the sublibraries first. |
32 | require_once($CFG->libdir . '/portfolio/constants.php'); // all the constants for time, export format etc. |
33 | require_once($CFG->libdir . '/portfolio/exceptions.php'); // exception classes used by portfolio code |
34 | require_once($CFG->libdir . '/portfolio/formats.php'); // the export format hierarchy |
35 | require_once($CFG->libdir . '/portfolio/forms.php'); // the form classes that subclass moodleform |
36 | require_once($CFG->libdir . '/portfolio/exporter.php'); // the exporter class |
37 | require_once($CFG->libdir . '/portfolio/plugin.php'); // the base classes for plugins |
38 | require_once($CFG->libdir . '/portfolio/caller.php'); // the base classes for calling code |
39 | |
40 | /** |
ce09fecc |
41 | * use this to add a portfolio button or icon or form to a page |
67a87e7d |
42 | * |
ce09fecc |
43 | * These class methods do not check permissions. the caller must check permissions first. |
87fcac8d |
44 | * Later, during the export process, the caller class is instantiated and the check_permissions method is called |
67a87e7d |
45 | * |
ce09fecc |
46 | * This class can be used like this: |
47 | * $button = new portfolio_add_button(); |
48 | * $button->set_callback_options('name_of_caller_class', array('id' => 6), '/your/mod/lib.php'); |
49 | * $button->render(PORTFOLIO_ADD_FULL_FORM, get_string('addeverythingtoportfolio', 'yourmodule')); |
50 | * |
51 | * or like this: |
52 | * $button = new portfolio_add_button(array('callbackclass' => 'name_of_caller_class', 'callbackargs' => array('id' => 6), 'callbackfile' => '/your/mod/lib.php')); |
53 | * $somehtml .= $button->to_html(PORTFOLIO_ADD_TEXT_LINK); |
54 | * |
55 | * See http://docs.moodle.org/en/Development:Adding_a_Portfolio_Button_to_a_page for more information |
67a87e7d |
56 | */ |
ce09fecc |
57 | class portfolio_add_button { |
67a87e7d |
58 | |
ce09fecc |
59 | private $callbackclass; |
60 | private $callbackargs; |
61 | private $callbackfile; |
62 | private $formats; |
63 | private $instances; |
67a87e7d |
64 | |
ce09fecc |
65 | /** |
66 | * constructor. either pass the options here or set them using the helper methods. |
67 | * generally the code will be clearer if you use the helper methods. |
68 | * |
69 | * @param array $options keyed array of options: |
70 | * key 'callbackclass': name of the caller class (eg forum_portfolio_caller') |
71 | * key 'callbackargs': the array of callback arguments your caller class wants passed to it in the constructor |
72 | * key 'callbackfile': the file containing the class definition of your caller class. |
73 | * See set_callback_options for more information on these three. |
74 | * key 'formats': an array of PORTFOLIO_FORMATS this caller will support |
75 | * See set_formats for more information on this. |
76 | */ |
77 | public function __construct($options=null) { |
11ae365c |
78 | global $SESSION, $CFG; |
ce09fecc |
79 | if (isset($SESSION->portfolioexport)) { |
80 | $a = new StdClass; |
81 | $a->cancel = $CFG->wwwroot . '/portfolio/add.php?cancel=1'; |
82 | $a->finish = $CFG->wwwroot . '/portfolio/add.php?id=' . $SESSION->portfolioexport; |
83 | throw new portfolio_button_exception('alreadyexporting', 'portfolio', null, $a); |
84 | } |
380a251f |
85 | $this->instances = portfolio_instances(); |
ce09fecc |
86 | if (empty($options)) { |
87 | return true; |
88 | } |
89 | foreach ((array)$options as $key => $value) { |
90 | if (!in_array($key, $constructoroptions)) { |
91 | throw new portfolio_button_exception('invalidbuttonproperty', 'portfolio', $key); |
92 | } |
93 | $this->{$key} = $value; |
94 | } |
a239f01e |
95 | } |
96 | |
ce09fecc |
97 | /* |
98 | * @param string $class name of the class containing the callback functions |
99 | * activity modules should ALWAYS use their name_portfolio_caller |
100 | * other locations must use something unique |
101 | * @param mixed $argarray this can be an array or hash of arguments to pass |
102 | * back to the callback functions (passed by reference) |
103 | * these MUST be primatives to be added as hidden form fields. |
104 | * and the values get cleaned to PARAM_ALPHAEXT or PARAM_NUMBER or PARAM_PATH |
105 | * @param string $file this can be autodetected if it's in the same file as your caller, |
106 | * but often, the caller is a script.php and the class in a lib.php |
107 | * so you can pass it here if necessary. |
108 | * this path should be relative (ie, not include) dirroot, eg '/mod/forum/lib.php' |
109 | */ |
110 | public function set_callback_options($class, array $argarray, $file=null) { |
111 | global $CFG; |
112 | if (empty($file)) { |
113 | $backtrace = debug_backtrace(); |
114 | if (!array_key_exists(0, $backtrace) || !array_key_exists('file', $backtrace[0]) || !is_readable($backtrace[0]['file'])) { |
115 | throw new portfolio_button_exception('nocallbackfile', 'portfolio'); |
116 | } |
117 | |
118 | $file = substr($backtrace[0]['file'], strlen($CFG->dirroot)); |
119 | } else if (!is_readable($CFG->dirroot . $file)) { |
120 | throw new portfolio_button_exception('nocallbackfile', 'portfolio', $file); |
121 | } |
122 | $this->callbackfile = $file; |
123 | require_once($CFG->dirroot . $file); |
124 | if (!class_exists($class)) { |
125 | throw new portfolio_button_exception('nocallbackclass', 'portfolio', $class); |
126 | } |
0d06b6fd |
127 | |
128 | // this will throw exceptions |
129 | // but should not actually do anything other than verify callbackargs |
130 | $test = new $class($argarray); |
131 | unset($test); |
132 | |
ce09fecc |
133 | $this->callbackclass = $class; |
134 | $this->callbackargs = $argarray; |
67a87e7d |
135 | } |
136 | |
ce09fecc |
137 | /* |
9d7432f6 |
138 | * sets the available export formats for this content |
139 | * this function will also poll the static function in the caller class |
140 | * and make sure we're not overriding a format that has nothing to do with mimetypes |
141 | * eg if you pass IMAGE here but the caller can export LEAP it will keep LEAP as well. |
142 | * see portfolio_most_specific_formats for more information |
143 | * |
ce09fecc |
144 | * @param array $formats if the calling code knows better than the static method on the calling class (supported_formats) |
145 | * eg, if it's going to be a single file, or if you know it's HTML, you can pass it here instead |
146 | * this is almost always the case so you should always use this. |
147 | * {@see portfolio_format_from_file} for how to get the appropriate formats to pass here for uploaded files. |
148 | */ |
149 | public function set_formats($formats=null) { |
150 | if (is_string($formats)) { |
151 | $formats = array($formats); |
152 | } |
153 | if (empty($formats)) { |
9d7432f6 |
154 | $formats = array(); |
ce09fecc |
155 | } |
9d7432f6 |
156 | if (empty($this->callbackclass)) { |
157 | throw new portfolio_button_exception('noclassbeforeformats', 'portfolio'); |
d1581fc5 |
158 | } |
9d7432f6 |
159 | $callerformats = call_user_func(array($this->callbackclass, 'supported_formats')); |
160 | $this->formats = portfolio_most_specific_formats($formats, $callerformats); |
6fdd8fa7 |
161 | } |
162 | |
ce09fecc |
163 | /* |
164 | * echo the form/button/icon/text link to the page |
165 | * |
166 | * @param int $format format to display the button or form or icon or link. |
167 | * See constants PORTFOLIO_ADD_XXX for more info. |
168 | * optional, defaults to PORTFOLI_ADD_FULL_FORM |
169 | * @param str $addstr string to use for the button or icon alt text or link text. |
170 | * this is whole string, not key. optional, defaults to 'Add to portfolio'; |
171 | */ |
172 | public function render($format=null, $addstr=null) { |
380a251f |
173 | echo $this->to_html($format, $addstr); |
84a44985 |
174 | } |
175 | |
ce09fecc |
176 | /* |
177 | * returns the form/button/icon/text link as html |
178 | * |
179 | * @param int $format format to display the button or form or icon or link. |
180 | * See constants PORTFOLIO_ADD_XXX for more info. |
181 | * optional, defaults to PORTFOLI_ADD_FULL_FORM |
182 | * @param str $addstr string to use for the button or icon alt text or link text. |
183 | * this is whole string, not key. optional, defaults to 'Add to portfolio'; |
184 | */ |
185 | public function to_html($format=null, $addstr=null) { |
186 | global $CFG, $COURSE; |
187 | if (!$this->is_renderable()) { |
ed1fcf79 |
188 | return; |
189 | } |
380a251f |
190 | if (empty($this->callbackclass) || empty($this->callbackfile)) { |
191 | throw new portfolio_button_exception('mustsetcallbackoptions', 'portfolio'); |
ce09fecc |
192 | } |
193 | if (empty($this->formats)) { |
194 | // use the caller defaults |
195 | $this->set_formats(); |
196 | } |
197 | $formoutput = '<form method="post" action="' . $CFG->wwwroot . '/portfolio/add.php" id="portfolio-add-button">' . "\n"; |
198 | $linkoutput = '<a href="' . $CFG->wwwroot . '/portfolio/add.php?'; |
199 | foreach ($this->callbackargs as $key => $value) { |
200 | if (!empty($value) && !is_string($value) && !is_numeric($value)) { |
201 | $a->key = $key; |
202 | $a->value = print_r($value, true); |
203 | debugging(get_string('nonprimative', 'portfolio', $a)); |
204 | return; |
205 | } |
206 | $linkoutput .= 'ca_' . $key . '=' . $value . '&'; |
207 | $formoutput .= "\n" . '<input type="hidden" name="ca_' . $key . '" value="' . $value . '" />'; |
208 | } |
209 | $formoutput .= "\n" . '<input type="hidden" name="callbackfile" value="' . $this->callbackfile . '" />'; |
210 | $formoutput .= "\n" . '<input type="hidden" name="callbackclass" value="' . $this->callbackclass . '" />'; |
211 | $formoutput .= "\n" . '<input type="hidden" name="course" value="' . (!empty($COURSE) ? $COURSE->id : 0) . '" />'; |
212 | $linkoutput .= 'callbackfile=' . $this->callbackfile . '&callbackclass=' |
213 | . $this->callbackclass . '&course=' . (!empty($COURSE) ? $COURSE->id : 0); |
214 | $selectoutput = ''; |
215 | if (count($this->instances) == 1) { |
216 | $instance = array_shift($this->instances); |
217 | $formats = portfolio_supported_formats_intersect($this->formats, $instance->supported_formats()); |
218 | if (count($formats) == 0) { |
219 | // bail. no common formats. |
220 | debugging(get_string('nocommonformats', 'portfolio', $this->callbackclass)); |
221 | return; |
222 | } |
223 | if ($error = portfolio_instance_sanity_check($instance)) { |
224 | // bail, plugin is misconfigured |
225 | debugging(get_string('instancemisconfigured', 'portfolio', get_string($error[$instance->get('id')], 'portfolio_' . $instance->get('plugin')))); |
226 | return; |
227 | } |
228 | $formoutput .= "\n" . '<input type="hidden" name="instance" value="' . $instance->get('id') . '" />'; |
229 | $linkoutput .= '&instance=' . $instance->get('id'); |
230 | } |
231 | else { |
232 | $selectoutput = portfolio_instance_select($this->instances, $this->formats, $this->callbackclass, 'instance', true); |
ed1fcf79 |
233 | } |
67a87e7d |
234 | |
ce09fecc |
235 | if (empty($addstr)) { |
236 | $addstr = get_string('addtoportfolio', 'portfolio'); |
237 | } |
238 | if (empty($format)) { |
239 | $format = PORTFOLIO_ADD_FULL_FORM; |
240 | } |
241 | switch ($format) { |
242 | case PORTFOLIO_ADD_FULL_FORM: |
243 | $formoutput .= $selectoutput; |
244 | $formoutput .= "\n" . '<input type="submit" value="' . $addstr .'" />'; |
245 | $formoutput .= "\n" . '</form>'; |
246 | break; |
247 | case PORTFOLIO_ADD_ICON_FORM: |
248 | $formoutput .= $selectoutput; |
249 | $formoutput .= "\n" . '<input type="image" src="' . $CFG->pixpath . '/t/portfolio.gif" alt=' . $addstr .'" />'; |
250 | $formoutput .= "\n" . '</form>'; |
251 | break; |
252 | case PORTFOLIO_ADD_ICON_LINK: |
253 | $linkoutput .= '"><img src="' . $CFG->pixpath . '/t/portfolio.gif" alt=' . $addstr .'" /></a>'; |
254 | break; |
255 | case PORTFOLIO_ADD_TEXT_LINK: |
256 | $linkoutput .= '">' . $addstr .'</a>'; |
257 | break; |
258 | default: |
259 | debugging(get_string('invalidaddformat', 'portfolio', $format)); |
260 | } |
261 | $output = (in_array($format, array(PORTFOLIO_ADD_FULL_FORM, PORTFOLIO_ADD_ICON_FORM)) ? $formoutput : $linkoutput); |
262 | return $output; |
349242a3 |
263 | } |
67a87e7d |
264 | |
ce09fecc |
265 | /** |
266 | * does some internal checks |
267 | * these are not errors, just situations |
268 | * where it's not appropriate to add the button |
269 | */ |
270 | private function is_renderable() { |
271 | global $CFG; |
272 | if (empty($CFG->enableportfolios)) { |
273 | return false; |
67a87e7d |
274 | } |
ce09fecc |
275 | if (defined('PORTFOLIO_INTERNAL')) { |
276 | // something somewhere has detected a risk of this being called during inside the preparation |
277 | // eg forum_print_attachments |
278 | return false; |
67a87e7d |
279 | } |
380a251f |
280 | if (empty($this->instances) || count($this->instances) == 0) { |
ce09fecc |
281 | return false; |
67a87e7d |
282 | } |
ce09fecc |
283 | return true; |
67a87e7d |
284 | } |
ef6f0f60 |
285 | |
286 | /** |
287 | * Getter for $format property |
288 | * @return array |
289 | */ |
290 | public function get_formats() { |
291 | return $this->formats; |
292 | } |
293 | |
294 | /** |
295 | * Getter for $callbackargs property |
296 | * @return array |
297 | */ |
298 | public function get_callbackargs() { |
299 | return $this->callbackargs; |
300 | } |
301 | |
302 | /** |
303 | * Getter for $callbackfile property |
304 | * @return array |
305 | */ |
306 | public function get_callbackfile() { |
307 | return $this->callbackfile; |
308 | } |
309 | |
310 | /** |
311 | * Getter for $callbackclass property |
312 | * @return array |
313 | */ |
314 | public function get_callbackclass() { |
315 | return $this->callbackclass; |
316 | } |
ce09fecc |
317 | } |
67a87e7d |
318 | |
67a87e7d |
319 | /** |
320 | * returns a drop menu with a list of available instances. |
321 | * |
87fcac8d |
322 | * @param array $instances array of portfolio plugin instance objects - the instances to put in the menu |
323 | * @param array $callerformats array of PORTFOLIO_FORMAT_XXX constants - the formats the caller supports (this is used to filter plugins) |
324 | * @param array $callbackclass the callback class name - used for debugging only for when there are no common formats |
325 | * @param string $selectname the name of the select element. Optional, defaults to instance. |
326 | * @param boolean $return whether to print or return the output. Optional, defaults to print. |
327 | * @param booealn $returnarray if returning, whether to return the HTML or the array of options. Optional, defaults to HTML. |
67a87e7d |
328 | * |
329 | * @return string the html, from <select> to </select> inclusive. |
330 | */ |
9eb0a772 |
331 | function portfolio_instance_select($instances, $callerformats, $callbackclass, $selectname='instance', $return=false, $returnarray=false) { |
332 | global $CFG; |
333 | |
90658eef |
334 | if (empty($CFG->enableportfolios)) { |
9eb0a772 |
335 | return; |
336 | } |
337 | |
67a87e7d |
338 | $insane = portfolio_instance_sanity_check(); |
339 | $count = 0; |
9eb0a772 |
340 | $selectoutput = "\n" . '<select name="' . $selectname . '">' . "\n"; |
67a87e7d |
341 | foreach ($instances as $instance) { |
349242a3 |
342 | $formats = portfolio_supported_formats_intersect($callerformats, $instance->supported_formats()); |
343 | if (count($formats) == 0) { |
67a87e7d |
344 | // bail. no common formats. |
345 | continue; |
346 | } |
347 | if (array_key_exists($instance->get('id'), $insane)) { |
348 | // bail, plugin is misconfigured |
349 | debugging(get_string('instancemisconfigured', 'portfolio', get_string($insane[$instance->get('id')], 'portfolio_' . $instance->get('plugin')))); |
350 | continue; |
351 | } |
352 | $count++; |
9eb0a772 |
353 | $selectoutput .= "\n" . '<option value="' . $instance->get('id') . '">' . $instance->get('name') . '</option>' . "\n"; |
354 | $options[$instance->get('id')] = $instance->get('name'); |
67a87e7d |
355 | } |
356 | if (empty($count)) { |
357 | // bail. no common formats. |
358 | debugging(get_string('nocommonformats', 'portfolio', $callbackclass)); |
359 | return; |
360 | } |
361 | $selectoutput .= "\n" . "</select>\n"; |
9eb0a772 |
362 | if (!empty($returnarray)) { |
363 | return $options; |
364 | } |
365 | if (!empty($return)) { |
366 | return $selectoutput; |
367 | } |
368 | echo $selectoutput; |
67a87e7d |
369 | } |
370 | |
371 | /** |
372 | * return all portfolio instances |
373 | * |
87fcac8d |
374 | * @todo check capabilities here - see MDL-15768 |
375 | * |
376 | * @param boolean visibleonly Don't include hidden instances. Defaults to true and will be overridden to true if the next parameter is true |
377 | * @param boolean useronly Check the visibility preferences and permissions of the logged in user. Defaults to true. |
378 | * |
67a87e7d |
379 | * @return array of portfolio instances (full objects, not just database records) |
380 | */ |
381 | function portfolio_instances($visibleonly=true, $useronly=true) { |
382 | |
383 | global $DB, $USER; |
384 | |
385 | $values = array(); |
386 | $sql = 'SELECT * FROM {portfolio_instance}'; |
387 | |
388 | if ($visibleonly || $useronly) { |
389 | $values[] = 1; |
390 | $sql .= ' WHERE visible = ?'; |
391 | } |
392 | if ($useronly) { |
393 | $sql .= ' AND id NOT IN ( |
394 | SELECT instance FROM {portfolio_instance_user} |
395 | WHERE userid = ? AND name = ? AND value = ? |
396 | )'; |
397 | $values = array_merge($values, array($USER->id, 'visible', 0)); |
398 | } |
399 | $sql .= ' ORDER BY name'; |
400 | |
401 | $instances = array(); |
402 | foreach ($DB->get_records_sql($sql, $values) as $instance) { |
a50ef3d3 |
403 | $instances[$instance->id] = portfolio_instance($instance->id, $instance); |
67a87e7d |
404 | } |
67a87e7d |
405 | return $instances; |
406 | } |
407 | |
408 | /** |
87fcac8d |
409 | * Supported formats currently in use. |
410 | * |
411 | * Canonical place for a list of all formats |
412 | * that portfolio plugins and callers |
67a87e7d |
413 | * can use for exporting content |
414 | * |
87fcac8d |
415 | * @return keyed array of all the available export formats (constant => classname) |
67a87e7d |
416 | */ |
417 | function portfolio_supported_formats() { |
418 | return array( |
349242a3 |
419 | PORTFOLIO_FORMAT_FILE => 'portfolio_format_file', |
420 | PORTFOLIO_FORMAT_IMAGE => 'portfolio_format_image', |
421 | PORTFOLIO_FORMAT_HTML => 'portfolio_format_html', |
ea0de12f |
422 | PORTFOLIO_FORMAT_TEXT => 'portfolio_format_text', |
423 | PORTFOLIO_FORMAT_VIDEO => 'portfolio_format_video', |
5071079c |
424 | /*PORTFOLIO_FORMAT_MBKP, */ // later |
425 | /*PORTFOLIO_FORMAT_PIOP, */ // also later |
67a87e7d |
426 | ); |
427 | } |
428 | |
ea0de12f |
429 | /** |
87fcac8d |
430 | * Deduce export format from file mimetype |
431 | * |
432 | * This function returns the revelant portfolio export format |
ea0de12f |
433 | * which is used to determine which portfolio plugins can be used |
434 | * for exporting this content |
435 | * according to the mime type of the given file |
436 | * this only works when exporting exactly <b>one</b> file |
437 | * |
438 | * @param stored_file $file file to check mime type for |
87fcac8d |
439 | * |
ea0de12f |
440 | * @return string the format constant (see PORTFOLIO_FORMAT_XXX constants) |
441 | */ |
87fcac8d |
442 | function portfolio_format_from_file(stored_file $file) { |
ea0de12f |
443 | static $alreadymatched; |
444 | if (empty($alreadymatched)) { |
445 | $alreadymatched = array(); |
446 | } |
447 | if (!($file instanceof stored_file)) { |
448 | throw new portfolio_exception('invalidfileargument', 'portfolio'); |
449 | } |
450 | $mimetype = $file->get_mimetype(); |
451 | if (array_key_exists($mimetype, $alreadymatched)) { |
452 | return $alreadymatched[$mimetype]; |
453 | } |
454 | $allformats = portfolio_supported_formats(); |
455 | foreach ($allformats as $format => $classname) { |
456 | $supportedmimetypes = call_user_func(array($classname, 'mimetypes')); |
457 | if (!is_array($supportedmimetypes)) { |
458 | debugging("one of the portfolio format classes, $classname, said it supported something funny for mimetypes, should have been array..."); |
459 | debugging(print_r($supportedmimetypes, true)); |
460 | continue; |
461 | } |
462 | if (in_array($mimetype, $supportedmimetypes)) { |
463 | $alreadymatched[$mimetype] = $format; |
464 | return $format; |
465 | } |
466 | } |
467 | return PORTFOLIO_FORMAT_FILE; // base case for files... |
468 | } |
469 | |
470 | /** |
87fcac8d |
471 | * Intersection of plugin formats and caller formats |
472 | * |
473 | * Walks both the caller formats and portfolio plugin formats |
ea0de12f |
474 | * and looks for matches (walking the hierarchy as well) |
475 | * and returns the intersection |
476 | * |
477 | * @param array $callerformats formats the caller supports |
478 | * @param array $pluginformats formats the portfolio plugin supports |
479 | */ |
7812e882 |
480 | function portfolio_supported_formats_intersect($callerformats, $pluginformats) { |
481 | $allformats = portfolio_supported_formats(); |
482 | $intersection = array(); |
483 | foreach ($callerformats as $cf) { |
484 | if (!array_key_exists($cf, $allformats)) { |
485 | debugging(get_string('invalidformat', 'portfolio', $cf)); |
486 | continue; |
487 | } |
34035201 |
488 | $cfobj = new $allformats[$cf](); |
7812e882 |
489 | foreach ($pluginformats as $p => $pf) { |
490 | if (!array_key_exists($pf, $allformats)) { |
491 | debugging(get_string('invalidformat', 'portfolio', $pf)); |
492 | unset($pluginformats[$p]); // to avoid the same warning over and over |
493 | continue; |
494 | } |
34035201 |
495 | if ($cfobj instanceof $allformats[$pf]) { |
7812e882 |
496 | $intersection[] = $cf; |
497 | } |
498 | } |
499 | } |
500 | return $intersection; |
501 | } |
502 | |
9d7432f6 |
503 | /** |
504 | * return the combination of the two arrays of formats with duplicates in terms of specificity removed |
505 | * use case: a module is exporting a single file, so the general formats would be FILE and MBKP |
506 | * while the specific formats would be the specific subclass of FILE based on mime (say IMAGE) |
507 | * and this function would return IMAGE and MBKP |
508 | * |
509 | * @param array $specificformats array of more specific formats (eg based on mime detection) |
510 | * @param array $generalformats array of more general formats (usually more supported) |
511 | * |
512 | * @return array merged formats with dups removed |
513 | */ |
514 | function portfolio_most_specific_formats($specificformats, $generalformats) { |
515 | $allformats = portfolio_supported_formats(); |
516 | foreach ($specificformats as $f) { |
517 | // look for something less specific and remove it, ie outside of the inheritance tree of the current formats. |
518 | if (!array_key_exists($f, $allformats)) { |
519 | throw new portfolio_button_exception('invalidformat', 'portfolio', $f); |
520 | } |
521 | $fobj = new $allformats[$f]; |
522 | foreach ($generalformats as $key => $cf) { |
523 | $cfclass = $allformats[$cf]; |
524 | if ($fobj instanceof $cfclass) { |
525 | unset($generalformats[$cf]); |
526 | } |
527 | } |
528 | } |
529 | return array_merge(array_values($specificformats), array_values($generalformats)); |
530 | } |
531 | |
67a87e7d |
532 | /** |
533 | * helper function to return an instance of a plugin (with config loaded) |
534 | * |
87fcac8d |
535 | * @param int $instance id of instance |
536 | * @param array $record database row that corresponds to this instance |
537 | * this is passed to avoid unnecessary lookups |
538 | * Optional, and the record will be retrieved if null. |
67a87e7d |
539 | * |
540 | * @return subclass of portfolio_plugin_base |
541 | */ |
542 | function portfolio_instance($instanceid, $record=null) { |
543 | global $DB, $CFG; |
544 | |
545 | if ($record) { |
546 | $instance = $record; |
547 | } else { |
548 | if (!$instance = $DB->get_record('portfolio_instance', array('id' => $instanceid))) { |
34035201 |
549 | throw new portfolio_exception('invalidinstance', 'portfolio'); |
67a87e7d |
550 | } |
551 | } |
552 | require_once($CFG->dirroot . '/portfolio/type/'. $instance->plugin . '/lib.php'); |
553 | $classname = 'portfolio_plugin_' . $instance->plugin; |
554 | return new $classname($instanceid, $instance); |
555 | } |
556 | |
557 | /** |
87fcac8d |
558 | * Helper function to call a static function on a portfolio plugin class |
559 | * |
560 | * This will figure out the classname and require the right file and call the function. |
67a87e7d |
561 | * you can send a variable number of arguments to this function after the first two |
562 | * and they will be passed on to the function you wish to call. |
563 | * |
87fcac8d |
564 | * @param string $plugin name of plugin |
67a87e7d |
565 | * @param string $function function to call |
566 | */ |
567 | function portfolio_static_function($plugin, $function) { |
568 | global $CFG; |
569 | |
570 | $pname = null; |
571 | if (is_object($plugin) || is_array($plugin)) { |
572 | $plugin = (object)$plugin; |
573 | $pname = $plugin->name; |
574 | } else { |
575 | $pname = $plugin; |
576 | } |
577 | |
578 | $args = func_get_args(); |
579 | if (count($args) <= 2) { |
580 | $args = array(); |
581 | } |
582 | else { |
583 | array_shift($args); |
584 | array_shift($args); |
585 | } |
586 | |
587 | require_once($CFG->dirroot . '/portfolio/type/' . $plugin . '/lib.php'); |
588 | return call_user_func_array(array('portfolio_plugin_' . $plugin, $function), $args); |
589 | } |
590 | |
591 | /** |
592 | * helper function to check all the plugins for sanity and set any insane ones to invisible. |
593 | * |
594 | * @param array $plugins to check (if null, defaults to all) |
595 | * one string will work too for a single plugin. |
596 | * |
597 | * @return array array of insane instances (keys= id, values = reasons (keys for plugin lang) |
598 | */ |
599 | function portfolio_plugin_sanity_check($plugins=null) { |
600 | global $DB; |
601 | if (is_string($plugins)) { |
602 | $plugins = array($plugins); |
603 | } else if (empty($plugins)) { |
604 | $plugins = get_list_of_plugins('portfolio/type'); |
605 | } |
606 | |
607 | $insane = array(); |
608 | foreach ($plugins as $plugin) { |
609 | if ($result = portfolio_static_function($plugin, 'plugin_sanity_check')) { |
610 | $insane[$plugin] = $result; |
611 | } |
612 | } |
613 | if (empty($insane)) { |
614 | return array(); |
615 | } |
616 | list($where, $params) = $DB->get_in_or_equal(array_keys($insane)); |
617 | $where = ' plugin ' . $where; |
618 | $DB->set_field_select('portfolio_instance', 'visible', 0, $where, $params); |
619 | return $insane; |
620 | } |
621 | |
622 | /** |
623 | * helper function to check all the instances for sanity and set any insane ones to invisible. |
624 | * |
625 | * @param array $instances to check (if null, defaults to all) |
626 | * one instance or id will work too |
627 | * |
628 | * @return array array of insane instances (keys= id, values = reasons (keys for plugin lang) |
629 | */ |
630 | function portfolio_instance_sanity_check($instances=null) { |
631 | global $DB; |
632 | if (empty($instances)) { |
633 | $instances = portfolio_instances(false); |
634 | } else if (!is_array($instances)) { |
635 | $instances = array($instances); |
636 | } |
637 | |
638 | $insane = array(); |
639 | foreach ($instances as $instance) { |
640 | if (is_object($instance) && !($instance instanceof portfolio_plugin_base)) { |
641 | $instance = portfolio_instance($instance->id, $instance); |
642 | } else if (is_numeric($instance)) { |
643 | $instance = portfolio_instance($instance); |
644 | } |
645 | if (!($instance instanceof portfolio_plugin_base)) { |
646 | debugging('something weird passed to portfolio_instance_sanity_check, not subclass or id'); |
647 | continue; |
648 | } |
649 | if ($result = $instance->instance_sanity_check()) { |
650 | $insane[$instance->get('id')] = $result; |
651 | } |
652 | } |
653 | if (empty($insane)) { |
654 | return array(); |
655 | } |
656 | list ($where, $params) = $DB->get_in_or_equal(array_keys($insane)); |
657 | $where = ' id ' . $where; |
658 | $DB->set_field_select('portfolio_instance', 'visible', 0, $where, $params); |
659 | return $insane; |
660 | } |
661 | |
662 | /** |
663 | * helper function to display a table of plugins (or instances) and reasons for disabling |
664 | * |
665 | * @param array $insane array of insane plugins (key = plugin (or instance id), value = reason) |
666 | * @param array $instances if reporting instances rather than whole plugins, pass the array (key = id, value = object) here |
667 | * |
668 | */ |
a50ef3d3 |
669 | function portfolio_report_insane($insane, $instances=false, $return=false) { |
67a87e7d |
670 | if (empty($insane)) { |
671 | return; |
672 | } |
673 | |
674 | static $pluginstr; |
675 | if (empty($pluginstr)) { |
676 | $pluginstr = get_string('plugin', 'portfolio'); |
677 | } |
678 | if ($instances) { |
679 | $headerstr = get_string('someinstancesdisabled', 'portfolio'); |
680 | } else { |
681 | $headerstr = get_string('somepluginsdisabled', 'portfolio'); |
682 | } |
683 | |
a50ef3d3 |
684 | $output = notify($headerstr, 'notifyproblem', 'center', true); |
67a87e7d |
685 | $table = new StdClass; |
686 | $table->head = array($pluginstr, ''); |
687 | $table->data = array(); |
688 | foreach ($insane as $plugin => $reason) { |
689 | if ($instances) { |
67a87e7d |
690 | $instance = $instances[$plugin]; |
691 | $plugin = $instance->get('plugin'); |
692 | $name = $instance->get('name'); |
693 | } else { |
694 | $name = $plugin; |
695 | } |
696 | $table->data[] = array($name, get_string($reason, 'portfolio_' . $plugin)); |
697 | } |
a50ef3d3 |
698 | $output .= print_table($table, true); |
699 | $output .= '<br /><br /><br />'; |
700 | |
701 | if ($return) { |
702 | return $output; |
703 | } |
704 | echo $output; |
67a87e7d |
705 | } |
706 | |
9eb0a772 |
707 | /** |
708 | * fake the url to portfolio/add.php from data from somewhere else |
709 | * you should use portfolio_add_button instead 99% of the time |
710 | * |
87fcac8d |
711 | * @param int $instanceid instanceid (optional, will force a new screen if not specified) |
712 | * @param string $classname callback classname |
713 | * @param string $classfile file containing the callback class definition |
714 | * @param array $callbackargs arguments to pass to the callback class |
9eb0a772 |
715 | */ |
716 | function portfolio_fake_add_url($instanceid, $classname, $classfile, $callbackargs) { |
717 | global $CFG; |
718 | $url = $CFG->wwwroot . '/portfolio/add.php?instance=' . $instanceid . '&callbackclass=' . $classname . '&callbackfile=' . $classfile; |
719 | |
720 | if (is_object($callbackargs)) { |
721 | $callbackargs = (array)$callbackargs; |
722 | } |
723 | if (!is_array($callbackargs) || empty($callbackargs)) { |
724 | return $url; |
725 | } |
726 | foreach ($callbackargs as $key => $value) { |
727 | $url .= '&ca_' . $key . '=' . urlencode($value); |
728 | } |
729 | return $url; |
730 | } |
67a87e7d |
731 | |
67a87e7d |
732 | |
67a87e7d |
733 | |
87fcac8d |
734 | /** |
735 | * event handler for the portfolio_send event |
736 | */ |
737 | function portfolio_handle_event($eventdata) { |
738 | global $CFG; |
739 | $exporter = portfolio_exporter::rewaken_object($eventdata); |
740 | $exporter->process_stage_package(); |
741 | $exporter->process_stage_send(); |
742 | $exporter->save(); |
743 | $exporter->process_stage_cleanup(); |
744 | return true; |
67a87e7d |
745 | } |
746 | |
87fcac8d |
747 | /** |
748 | * main portfolio cronjob |
749 | * currently just cleans up expired transfer records. |
750 | * |
751 | * @todo add hooks in the plugins - either per instance or per plugin |
752 | */ |
753 | function portfolio_cron() { |
754 | global $DB; |
9eb0a772 |
755 | |
87fcac8d |
756 | if ($expired = $DB->get_records_select('portfolio_tempdata', 'expirytime < ?', array(time()), '', 'id')) { |
757 | foreach ($expired as $d) { |
758 | $e = portfolio_exporter::rewaken_object($d); |
759 | $e->process_stage_cleanup(true); |
9eb0a772 |
760 | } |
192ce92b |
761 | } |
5071079c |
762 | } |
763 | |
67a87e7d |
764 | /** |
87fcac8d |
765 | * helper function to rethrow a caught portfolio_exception as an export exception |
766 | * |
767 | * used because when a portfolio_export exception is thrown the export is cancelled |
768 | * |
769 | * @param portfolio_exporter $exporter current exporter object |
770 | * @param exception $exception exception to rethrow |
771 | * |
772 | * @return void |
773 | * @throws portfolio_export_exceptiog |
67a87e7d |
774 | */ |
87fcac8d |
775 | function portfolio_export_rethrow_exception($exporter, $exception) { |
776 | throw new portfolio_export_exception($exporter, $exception->errorcode, $exception->module, $exception->link, $exception->a); |
777 | } |
67a87e7d |
778 | |
bee4bce2 |
779 | /** |
780 | * try and determine expected_time for purely file based exports |
781 | * or exports that might include large file attachments. |
782 | * |
783 | * @param mixed $totest - either an array of stored_file objects or a single stored_file object |
784 | * |
785 | * @return constant PORTFOLIO_TIME_XXX |
786 | */ |
787 | function portfolio_expected_time_file($totest) { |
788 | global $CFG; |
789 | if ($totest instanceof stored_file) { |
790 | $totest = array($totest); |
791 | } |
792 | $size = 0; |
793 | foreach ($totest as $file) { |
794 | if (!($file instanceof stored_file)) { |
795 | debugging('something weird passed to portfolio_expected_time_file - not stored_file object'); |
796 | debugging(print_r($file, true)); |
797 | continue; |
798 | } |
799 | $size += $file->get_filesize(); |
800 | } |
801 | |
802 | $fileinfo = portfolio_filesize_info(); |
803 | |
804 | $moderate = $high = 0; // avoid warnings |
805 | |
806 | foreach (array('moderate', 'high') as $setting) { |
807 | $settingname = 'portfolio_' . $setting . '_filesize_threshold'; |
808 | if (empty($CFG->{$settingname}) || !array_key_exists($CFG->{$settingname}, $fileinfo['options'])) { |
809 | debugging("weird or unset admin value for $settingname, using default instead"); |
810 | $$setting = $fileinfo[$setting]; |
811 | } else { |
812 | $$setting = $CFG->{$settingname}; |
813 | } |
814 | } |
815 | |
816 | if ($size < $moderate) { |
817 | return PORTFOLIO_TIME_LOW; |
818 | } else if ($size < $high) { |
819 | return PORTFOLIO_TIME_MODERATE; |
820 | } |
821 | return PORTFOLIO_TIME_HIGH; |
822 | } |
823 | |
824 | |
825 | /** |
826 | * the default filesizes and threshold information for file based transfers |
827 | * this shouldn't need to be used outside the admin pages and the portfolio code |
828 | */ |
829 | function portfolio_filesize_info() { |
830 | $filesizes = array(); |
831 | $sizelist = array(10240, 51200, 102400, 512000, 1048576, 2097152, 5242880, 10485760, 20971520, 52428800); |
832 | foreach ($sizelist as $size) { |
833 | $filesizes[$size] = display_size($size); |
834 | } |
835 | return array( |
836 | 'options' => $filesizes, |
837 | 'moderate' => 1048576, |
838 | 'high' => 5242880, |
839 | ); |
840 | } |
841 | |
842 | /** |
843 | * try and determine expected_time for purely database based exports |
844 | * or exports that might include large parts of a database |
845 | * |
846 | * @param integer $recordcount - number of records trying to export |
847 | * |
848 | * @return constant PORTFOLIO_TIME_XXX |
849 | */ |
850 | function portfolio_expected_time_db($recordcount) { |
851 | global $CFG; |
852 | |
853 | if (empty($CFG->portfolio_moderate_dbsize_threshold)) { |
854 | set_config('portfolio_moderate_dbsize_threshold', 10); |
855 | } |
856 | if (empty($CFG->portfolio_high_dbsize_threshold)) { |
857 | set_config('portfolio_high_dbsize_threshold', 50); |
858 | } |
859 | if ($recordcount < $CFG->portfolio_moderate_dbsize_threshold) { |
860 | return PORTFOLIO_TIME_LOW; |
861 | } else if ($recordcount < $CFG->portfolio_high_dbsize_threshold) { |
862 | return PORTFOLIO_TIME_MODERATE; |
863 | } |
864 | return PORTFOLIO_TIME_HIGH; |
865 | } |
67a87e7d |
866 | |
67a87e7d |
867 | ?> |