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) { |
78 | global $SESSION; |
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 | } |
127 | $this->callbackclass = $class; |
128 | $this->callbackargs = $argarray; |
67a87e7d |
129 | } |
130 | |
ce09fecc |
131 | /* |
132 | * @param array $formats if the calling code knows better than the static method on the calling class (supported_formats) |
133 | * eg, if it's going to be a single file, or if you know it's HTML, you can pass it here instead |
134 | * this is almost always the case so you should always use this. |
135 | * {@see portfolio_format_from_file} for how to get the appropriate formats to pass here for uploaded files. |
136 | */ |
137 | public function set_formats($formats=null) { |
138 | if (is_string($formats)) { |
139 | $formats = array($formats); |
140 | } |
141 | if (empty($formats)) { |
142 | if (empty($this->callbackclass)) { |
143 | throw new portfolio_button_exception('noformatsorclass', 'portfolio'); |
144 | } |
145 | $formats = call_user_func(array($this->callbackclass, 'supported_formats')); |
146 | } |
d1581fc5 |
147 | $allformats = portfolio_supported_formats(); |
148 | foreach ($formats as $f) { |
149 | if (!array_key_exists($f, $allformats)) { |
150 | throw new portfolio_button_exception('invalidformat', 'portfolio', $f); |
151 | } |
152 | } |
ce09fecc |
153 | $this->formats = $formats; |
6fdd8fa7 |
154 | } |
155 | |
ce09fecc |
156 | /* |
157 | * echo the form/button/icon/text link to the page |
158 | * |
159 | * @param int $format format to display the button or form or icon or link. |
160 | * See constants PORTFOLIO_ADD_XXX for more info. |
161 | * optional, defaults to PORTFOLI_ADD_FULL_FORM |
162 | * @param str $addstr string to use for the button or icon alt text or link text. |
163 | * this is whole string, not key. optional, defaults to 'Add to portfolio'; |
164 | */ |
165 | public function render($format=null, $addstr=null) { |
380a251f |
166 | echo $this->to_html($format, $addstr); |
84a44985 |
167 | } |
168 | |
ce09fecc |
169 | /* |
170 | * returns the form/button/icon/text link as html |
171 | * |
172 | * @param int $format format to display the button or form or icon or link. |
173 | * See constants PORTFOLIO_ADD_XXX for more info. |
174 | * optional, defaults to PORTFOLI_ADD_FULL_FORM |
175 | * @param str $addstr string to use for the button or icon alt text or link text. |
176 | * this is whole string, not key. optional, defaults to 'Add to portfolio'; |
177 | */ |
178 | public function to_html($format=null, $addstr=null) { |
179 | global $CFG, $COURSE; |
180 | if (!$this->is_renderable()) { |
ed1fcf79 |
181 | return; |
182 | } |
380a251f |
183 | if (empty($this->callbackclass) || empty($this->callbackfile)) { |
184 | throw new portfolio_button_exception('mustsetcallbackoptions', 'portfolio'); |
ce09fecc |
185 | } |
186 | if (empty($this->formats)) { |
187 | // use the caller defaults |
188 | $this->set_formats(); |
189 | } |
190 | $formoutput = '<form method="post" action="' . $CFG->wwwroot . '/portfolio/add.php" id="portfolio-add-button">' . "\n"; |
191 | $linkoutput = '<a href="' . $CFG->wwwroot . '/portfolio/add.php?'; |
192 | foreach ($this->callbackargs as $key => $value) { |
193 | if (!empty($value) && !is_string($value) && !is_numeric($value)) { |
194 | $a->key = $key; |
195 | $a->value = print_r($value, true); |
196 | debugging(get_string('nonprimative', 'portfolio', $a)); |
197 | return; |
198 | } |
199 | $linkoutput .= 'ca_' . $key . '=' . $value . '&'; |
200 | $formoutput .= "\n" . '<input type="hidden" name="ca_' . $key . '" value="' . $value . '" />'; |
201 | } |
202 | $formoutput .= "\n" . '<input type="hidden" name="callbackfile" value="' . $this->callbackfile . '" />'; |
203 | $formoutput .= "\n" . '<input type="hidden" name="callbackclass" value="' . $this->callbackclass . '" />'; |
204 | $formoutput .= "\n" . '<input type="hidden" name="course" value="' . (!empty($COURSE) ? $COURSE->id : 0) . '" />'; |
205 | $linkoutput .= 'callbackfile=' . $this->callbackfile . '&callbackclass=' |
206 | . $this->callbackclass . '&course=' . (!empty($COURSE) ? $COURSE->id : 0); |
207 | $selectoutput = ''; |
208 | if (count($this->instances) == 1) { |
209 | $instance = array_shift($this->instances); |
210 | $formats = portfolio_supported_formats_intersect($this->formats, $instance->supported_formats()); |
211 | if (count($formats) == 0) { |
212 | // bail. no common formats. |
213 | debugging(get_string('nocommonformats', 'portfolio', $this->callbackclass)); |
214 | return; |
215 | } |
216 | if ($error = portfolio_instance_sanity_check($instance)) { |
217 | // bail, plugin is misconfigured |
218 | debugging(get_string('instancemisconfigured', 'portfolio', get_string($error[$instance->get('id')], 'portfolio_' . $instance->get('plugin')))); |
219 | return; |
220 | } |
221 | $formoutput .= "\n" . '<input type="hidden" name="instance" value="' . $instance->get('id') . '" />'; |
222 | $linkoutput .= '&instance=' . $instance->get('id'); |
223 | } |
224 | else { |
225 | $selectoutput = portfolio_instance_select($this->instances, $this->formats, $this->callbackclass, 'instance', true); |
ed1fcf79 |
226 | } |
67a87e7d |
227 | |
ce09fecc |
228 | if (empty($addstr)) { |
229 | $addstr = get_string('addtoportfolio', 'portfolio'); |
230 | } |
231 | if (empty($format)) { |
232 | $format = PORTFOLIO_ADD_FULL_FORM; |
233 | } |
234 | switch ($format) { |
235 | case PORTFOLIO_ADD_FULL_FORM: |
236 | $formoutput .= $selectoutput; |
237 | $formoutput .= "\n" . '<input type="submit" value="' . $addstr .'" />'; |
238 | $formoutput .= "\n" . '</form>'; |
239 | break; |
240 | case PORTFOLIO_ADD_ICON_FORM: |
241 | $formoutput .= $selectoutput; |
242 | $formoutput .= "\n" . '<input type="image" src="' . $CFG->pixpath . '/t/portfolio.gif" alt=' . $addstr .'" />'; |
243 | $formoutput .= "\n" . '</form>'; |
244 | break; |
245 | case PORTFOLIO_ADD_ICON_LINK: |
246 | $linkoutput .= '"><img src="' . $CFG->pixpath . '/t/portfolio.gif" alt=' . $addstr .'" /></a>'; |
247 | break; |
248 | case PORTFOLIO_ADD_TEXT_LINK: |
249 | $linkoutput .= '">' . $addstr .'</a>'; |
250 | break; |
251 | default: |
252 | debugging(get_string('invalidaddformat', 'portfolio', $format)); |
253 | } |
254 | $output = (in_array($format, array(PORTFOLIO_ADD_FULL_FORM, PORTFOLIO_ADD_ICON_FORM)) ? $formoutput : $linkoutput); |
255 | return $output; |
349242a3 |
256 | } |
67a87e7d |
257 | |
ce09fecc |
258 | /** |
259 | * does some internal checks |
260 | * these are not errors, just situations |
261 | * where it's not appropriate to add the button |
262 | */ |
263 | private function is_renderable() { |
264 | global $CFG; |
265 | if (empty($CFG->enableportfolios)) { |
266 | return false; |
67a87e7d |
267 | } |
ce09fecc |
268 | if (defined('PORTFOLIO_INTERNAL')) { |
269 | // something somewhere has detected a risk of this being called during inside the preparation |
270 | // eg forum_print_attachments |
271 | return false; |
67a87e7d |
272 | } |
380a251f |
273 | if (empty($this->instances) || count($this->instances) == 0) { |
ce09fecc |
274 | return false; |
67a87e7d |
275 | } |
ce09fecc |
276 | return true; |
67a87e7d |
277 | } |
ce09fecc |
278 | } |
67a87e7d |
279 | |
ce09fecc |
280 | |
281 | function portfolio_add_button($callbackclass, $callbackargs, $callbackfile=null, $format=PORTFOLIO_ADD_FULL_FORM, $addstr=null, $return=false, $callersupports=null) { |
282 | $button = new portfolio_add_button(); |
283 | $button->set_callback_options($callbackclass, $callbackargs, $callbackfile); |
284 | $button->set_formats($callersupports); |
67a87e7d |
285 | if ($return) { |
ce09fecc |
286 | return $button->to_html($format, $addstr); |
67a87e7d |
287 | } |
ce09fecc |
288 | $button->render(); |
67a87e7d |
289 | } |
290 | |
291 | /** |
292 | * returns a drop menu with a list of available instances. |
293 | * |
87fcac8d |
294 | * @param array $instances array of portfolio plugin instance objects - the instances to put in the menu |
295 | * @param array $callerformats array of PORTFOLIO_FORMAT_XXX constants - the formats the caller supports (this is used to filter plugins) |
296 | * @param array $callbackclass the callback class name - used for debugging only for when there are no common formats |
297 | * @param string $selectname the name of the select element. Optional, defaults to instance. |
298 | * @param boolean $return whether to print or return the output. Optional, defaults to print. |
299 | * @param booealn $returnarray if returning, whether to return the HTML or the array of options. Optional, defaults to HTML. |
67a87e7d |
300 | * |
301 | * @return string the html, from <select> to </select> inclusive. |
302 | */ |
9eb0a772 |
303 | function portfolio_instance_select($instances, $callerformats, $callbackclass, $selectname='instance', $return=false, $returnarray=false) { |
304 | global $CFG; |
305 | |
90658eef |
306 | if (empty($CFG->enableportfolios)) { |
9eb0a772 |
307 | return; |
308 | } |
309 | |
67a87e7d |
310 | $insane = portfolio_instance_sanity_check(); |
311 | $count = 0; |
9eb0a772 |
312 | $selectoutput = "\n" . '<select name="' . $selectname . '">' . "\n"; |
67a87e7d |
313 | foreach ($instances as $instance) { |
349242a3 |
314 | $formats = portfolio_supported_formats_intersect($callerformats, $instance->supported_formats()); |
315 | if (count($formats) == 0) { |
67a87e7d |
316 | // bail. no common formats. |
317 | continue; |
318 | } |
319 | if (array_key_exists($instance->get('id'), $insane)) { |
320 | // bail, plugin is misconfigured |
321 | debugging(get_string('instancemisconfigured', 'portfolio', get_string($insane[$instance->get('id')], 'portfolio_' . $instance->get('plugin')))); |
322 | continue; |
323 | } |
324 | $count++; |
9eb0a772 |
325 | $selectoutput .= "\n" . '<option value="' . $instance->get('id') . '">' . $instance->get('name') . '</option>' . "\n"; |
326 | $options[$instance->get('id')] = $instance->get('name'); |
67a87e7d |
327 | } |
328 | if (empty($count)) { |
329 | // bail. no common formats. |
330 | debugging(get_string('nocommonformats', 'portfolio', $callbackclass)); |
331 | return; |
332 | } |
333 | $selectoutput .= "\n" . "</select>\n"; |
9eb0a772 |
334 | if (!empty($returnarray)) { |
335 | return $options; |
336 | } |
337 | if (!empty($return)) { |
338 | return $selectoutput; |
339 | } |
340 | echo $selectoutput; |
67a87e7d |
341 | } |
342 | |
343 | /** |
344 | * return all portfolio instances |
345 | * |
87fcac8d |
346 | * @todo check capabilities here - see MDL-15768 |
347 | * |
348 | * @param boolean visibleonly Don't include hidden instances. Defaults to true and will be overridden to true if the next parameter is true |
349 | * @param boolean useronly Check the visibility preferences and permissions of the logged in user. Defaults to true. |
350 | * |
67a87e7d |
351 | * @return array of portfolio instances (full objects, not just database records) |
352 | */ |
353 | function portfolio_instances($visibleonly=true, $useronly=true) { |
354 | |
355 | global $DB, $USER; |
356 | |
357 | $values = array(); |
358 | $sql = 'SELECT * FROM {portfolio_instance}'; |
359 | |
360 | if ($visibleonly || $useronly) { |
361 | $values[] = 1; |
362 | $sql .= ' WHERE visible = ?'; |
363 | } |
364 | if ($useronly) { |
365 | $sql .= ' AND id NOT IN ( |
366 | SELECT instance FROM {portfolio_instance_user} |
367 | WHERE userid = ? AND name = ? AND value = ? |
368 | )'; |
369 | $values = array_merge($values, array($USER->id, 'visible', 0)); |
370 | } |
371 | $sql .= ' ORDER BY name'; |
372 | |
373 | $instances = array(); |
374 | foreach ($DB->get_records_sql($sql, $values) as $instance) { |
a50ef3d3 |
375 | $instances[$instance->id] = portfolio_instance($instance->id, $instance); |
67a87e7d |
376 | } |
67a87e7d |
377 | return $instances; |
378 | } |
379 | |
380 | /** |
87fcac8d |
381 | * Supported formats currently in use. |
382 | * |
383 | * Canonical place for a list of all formats |
384 | * that portfolio plugins and callers |
67a87e7d |
385 | * can use for exporting content |
386 | * |
87fcac8d |
387 | * @return keyed array of all the available export formats (constant => classname) |
67a87e7d |
388 | */ |
389 | function portfolio_supported_formats() { |
390 | return array( |
349242a3 |
391 | PORTFOLIO_FORMAT_FILE => 'portfolio_format_file', |
392 | PORTFOLIO_FORMAT_IMAGE => 'portfolio_format_image', |
393 | PORTFOLIO_FORMAT_HTML => 'portfolio_format_html', |
ea0de12f |
394 | PORTFOLIO_FORMAT_TEXT => 'portfolio_format_text', |
395 | PORTFOLIO_FORMAT_VIDEO => 'portfolio_format_video', |
5071079c |
396 | /*PORTFOLIO_FORMAT_MBKP, */ // later |
397 | /*PORTFOLIO_FORMAT_PIOP, */ // also later |
67a87e7d |
398 | ); |
399 | } |
400 | |
ea0de12f |
401 | /** |
87fcac8d |
402 | * Deduce export format from file mimetype |
403 | * |
404 | * This function returns the revelant portfolio export format |
ea0de12f |
405 | * which is used to determine which portfolio plugins can be used |
406 | * for exporting this content |
407 | * according to the mime type of the given file |
408 | * this only works when exporting exactly <b>one</b> file |
409 | * |
410 | * @param stored_file $file file to check mime type for |
87fcac8d |
411 | * |
ea0de12f |
412 | * @return string the format constant (see PORTFOLIO_FORMAT_XXX constants) |
413 | */ |
87fcac8d |
414 | function portfolio_format_from_file(stored_file $file) { |
ea0de12f |
415 | static $alreadymatched; |
416 | if (empty($alreadymatched)) { |
417 | $alreadymatched = array(); |
418 | } |
419 | if (!($file instanceof stored_file)) { |
420 | throw new portfolio_exception('invalidfileargument', 'portfolio'); |
421 | } |
422 | $mimetype = $file->get_mimetype(); |
423 | if (array_key_exists($mimetype, $alreadymatched)) { |
424 | return $alreadymatched[$mimetype]; |
425 | } |
426 | $allformats = portfolio_supported_formats(); |
427 | foreach ($allformats as $format => $classname) { |
428 | $supportedmimetypes = call_user_func(array($classname, 'mimetypes')); |
429 | if (!is_array($supportedmimetypes)) { |
430 | debugging("one of the portfolio format classes, $classname, said it supported something funny for mimetypes, should have been array..."); |
431 | debugging(print_r($supportedmimetypes, true)); |
432 | continue; |
433 | } |
434 | if (in_array($mimetype, $supportedmimetypes)) { |
435 | $alreadymatched[$mimetype] = $format; |
436 | return $format; |
437 | } |
438 | } |
439 | return PORTFOLIO_FORMAT_FILE; // base case for files... |
440 | } |
441 | |
442 | /** |
87fcac8d |
443 | * Intersection of plugin formats and caller formats |
444 | * |
445 | * Walks both the caller formats and portfolio plugin formats |
ea0de12f |
446 | * and looks for matches (walking the hierarchy as well) |
447 | * and returns the intersection |
448 | * |
449 | * @param array $callerformats formats the caller supports |
450 | * @param array $pluginformats formats the portfolio plugin supports |
451 | */ |
7812e882 |
452 | function portfolio_supported_formats_intersect($callerformats, $pluginformats) { |
453 | $allformats = portfolio_supported_formats(); |
454 | $intersection = array(); |
455 | foreach ($callerformats as $cf) { |
456 | if (!array_key_exists($cf, $allformats)) { |
457 | debugging(get_string('invalidformat', 'portfolio', $cf)); |
458 | continue; |
459 | } |
34035201 |
460 | $cfobj = new $allformats[$cf](); |
7812e882 |
461 | foreach ($pluginformats as $p => $pf) { |
462 | if (!array_key_exists($pf, $allformats)) { |
463 | debugging(get_string('invalidformat', 'portfolio', $pf)); |
464 | unset($pluginformats[$p]); // to avoid the same warning over and over |
465 | continue; |
466 | } |
34035201 |
467 | if ($cfobj instanceof $allformats[$pf]) { |
7812e882 |
468 | $intersection[] = $cf; |
469 | } |
470 | } |
471 | } |
472 | return $intersection; |
473 | } |
474 | |
67a87e7d |
475 | /** |
476 | * helper function to return an instance of a plugin (with config loaded) |
477 | * |
87fcac8d |
478 | * @param int $instance id of instance |
479 | * @param array $record database row that corresponds to this instance |
480 | * this is passed to avoid unnecessary lookups |
481 | * Optional, and the record will be retrieved if null. |
67a87e7d |
482 | * |
483 | * @return subclass of portfolio_plugin_base |
484 | */ |
485 | function portfolio_instance($instanceid, $record=null) { |
486 | global $DB, $CFG; |
487 | |
488 | if ($record) { |
489 | $instance = $record; |
490 | } else { |
491 | if (!$instance = $DB->get_record('portfolio_instance', array('id' => $instanceid))) { |
34035201 |
492 | throw new portfolio_exception('invalidinstance', 'portfolio'); |
67a87e7d |
493 | } |
494 | } |
495 | require_once($CFG->dirroot . '/portfolio/type/'. $instance->plugin . '/lib.php'); |
496 | $classname = 'portfolio_plugin_' . $instance->plugin; |
497 | return new $classname($instanceid, $instance); |
498 | } |
499 | |
500 | /** |
87fcac8d |
501 | * Helper function to call a static function on a portfolio plugin class |
502 | * |
503 | * This will figure out the classname and require the right file and call the function. |
67a87e7d |
504 | * you can send a variable number of arguments to this function after the first two |
505 | * and they will be passed on to the function you wish to call. |
506 | * |
87fcac8d |
507 | * @param string $plugin name of plugin |
67a87e7d |
508 | * @param string $function function to call |
509 | */ |
510 | function portfolio_static_function($plugin, $function) { |
511 | global $CFG; |
512 | |
513 | $pname = null; |
514 | if (is_object($plugin) || is_array($plugin)) { |
515 | $plugin = (object)$plugin; |
516 | $pname = $plugin->name; |
517 | } else { |
518 | $pname = $plugin; |
519 | } |
520 | |
521 | $args = func_get_args(); |
522 | if (count($args) <= 2) { |
523 | $args = array(); |
524 | } |
525 | else { |
526 | array_shift($args); |
527 | array_shift($args); |
528 | } |
529 | |
530 | require_once($CFG->dirroot . '/portfolio/type/' . $plugin . '/lib.php'); |
531 | return call_user_func_array(array('portfolio_plugin_' . $plugin, $function), $args); |
532 | } |
533 | |
534 | /** |
535 | * helper function to check all the plugins for sanity and set any insane ones to invisible. |
536 | * |
537 | * @param array $plugins to check (if null, defaults to all) |
538 | * one string will work too for a single plugin. |
539 | * |
540 | * @return array array of insane instances (keys= id, values = reasons (keys for plugin lang) |
541 | */ |
542 | function portfolio_plugin_sanity_check($plugins=null) { |
543 | global $DB; |
544 | if (is_string($plugins)) { |
545 | $plugins = array($plugins); |
546 | } else if (empty($plugins)) { |
547 | $plugins = get_list_of_plugins('portfolio/type'); |
548 | } |
549 | |
550 | $insane = array(); |
551 | foreach ($plugins as $plugin) { |
552 | if ($result = portfolio_static_function($plugin, 'plugin_sanity_check')) { |
553 | $insane[$plugin] = $result; |
554 | } |
555 | } |
556 | if (empty($insane)) { |
557 | return array(); |
558 | } |
559 | list($where, $params) = $DB->get_in_or_equal(array_keys($insane)); |
560 | $where = ' plugin ' . $where; |
561 | $DB->set_field_select('portfolio_instance', 'visible', 0, $where, $params); |
562 | return $insane; |
563 | } |
564 | |
565 | /** |
566 | * helper function to check all the instances for sanity and set any insane ones to invisible. |
567 | * |
568 | * @param array $instances to check (if null, defaults to all) |
569 | * one instance or id will work too |
570 | * |
571 | * @return array array of insane instances (keys= id, values = reasons (keys for plugin lang) |
572 | */ |
573 | function portfolio_instance_sanity_check($instances=null) { |
574 | global $DB; |
575 | if (empty($instances)) { |
576 | $instances = portfolio_instances(false); |
577 | } else if (!is_array($instances)) { |
578 | $instances = array($instances); |
579 | } |
580 | |
581 | $insane = array(); |
582 | foreach ($instances as $instance) { |
583 | if (is_object($instance) && !($instance instanceof portfolio_plugin_base)) { |
584 | $instance = portfolio_instance($instance->id, $instance); |
585 | } else if (is_numeric($instance)) { |
586 | $instance = portfolio_instance($instance); |
587 | } |
588 | if (!($instance instanceof portfolio_plugin_base)) { |
589 | debugging('something weird passed to portfolio_instance_sanity_check, not subclass or id'); |
590 | continue; |
591 | } |
592 | if ($result = $instance->instance_sanity_check()) { |
593 | $insane[$instance->get('id')] = $result; |
594 | } |
595 | } |
596 | if (empty($insane)) { |
597 | return array(); |
598 | } |
599 | list ($where, $params) = $DB->get_in_or_equal(array_keys($insane)); |
600 | $where = ' id ' . $where; |
601 | $DB->set_field_select('portfolio_instance', 'visible', 0, $where, $params); |
602 | return $insane; |
603 | } |
604 | |
605 | /** |
606 | * helper function to display a table of plugins (or instances) and reasons for disabling |
607 | * |
608 | * @param array $insane array of insane plugins (key = plugin (or instance id), value = reason) |
609 | * @param array $instances if reporting instances rather than whole plugins, pass the array (key = id, value = object) here |
610 | * |
611 | */ |
a50ef3d3 |
612 | function portfolio_report_insane($insane, $instances=false, $return=false) { |
67a87e7d |
613 | if (empty($insane)) { |
614 | return; |
615 | } |
616 | |
617 | static $pluginstr; |
618 | if (empty($pluginstr)) { |
619 | $pluginstr = get_string('plugin', 'portfolio'); |
620 | } |
621 | if ($instances) { |
622 | $headerstr = get_string('someinstancesdisabled', 'portfolio'); |
623 | } else { |
624 | $headerstr = get_string('somepluginsdisabled', 'portfolio'); |
625 | } |
626 | |
a50ef3d3 |
627 | $output = notify($headerstr, 'notifyproblem', 'center', true); |
67a87e7d |
628 | $table = new StdClass; |
629 | $table->head = array($pluginstr, ''); |
630 | $table->data = array(); |
631 | foreach ($insane as $plugin => $reason) { |
632 | if ($instances) { |
67a87e7d |
633 | $instance = $instances[$plugin]; |
634 | $plugin = $instance->get('plugin'); |
635 | $name = $instance->get('name'); |
636 | } else { |
637 | $name = $plugin; |
638 | } |
639 | $table->data[] = array($name, get_string($reason, 'portfolio_' . $plugin)); |
640 | } |
a50ef3d3 |
641 | $output .= print_table($table, true); |
642 | $output .= '<br /><br /><br />'; |
643 | |
644 | if ($return) { |
645 | return $output; |
646 | } |
647 | echo $output; |
67a87e7d |
648 | } |
649 | |
9eb0a772 |
650 | /** |
651 | * fake the url to portfolio/add.php from data from somewhere else |
652 | * you should use portfolio_add_button instead 99% of the time |
653 | * |
87fcac8d |
654 | * @param int $instanceid instanceid (optional, will force a new screen if not specified) |
655 | * @param string $classname callback classname |
656 | * @param string $classfile file containing the callback class definition |
657 | * @param array $callbackargs arguments to pass to the callback class |
9eb0a772 |
658 | */ |
659 | function portfolio_fake_add_url($instanceid, $classname, $classfile, $callbackargs) { |
660 | global $CFG; |
661 | $url = $CFG->wwwroot . '/portfolio/add.php?instance=' . $instanceid . '&callbackclass=' . $classname . '&callbackfile=' . $classfile; |
662 | |
663 | if (is_object($callbackargs)) { |
664 | $callbackargs = (array)$callbackargs; |
665 | } |
666 | if (!is_array($callbackargs) || empty($callbackargs)) { |
667 | return $url; |
668 | } |
669 | foreach ($callbackargs as $key => $value) { |
670 | $url .= '&ca_' . $key . '=' . urlencode($value); |
671 | } |
672 | return $url; |
673 | } |
67a87e7d |
674 | |
67a87e7d |
675 | |
67a87e7d |
676 | |
87fcac8d |
677 | /** |
678 | * event handler for the portfolio_send event |
679 | */ |
680 | function portfolio_handle_event($eventdata) { |
681 | global $CFG; |
682 | $exporter = portfolio_exporter::rewaken_object($eventdata); |
683 | $exporter->process_stage_package(); |
684 | $exporter->process_stage_send(); |
685 | $exporter->save(); |
686 | $exporter->process_stage_cleanup(); |
687 | return true; |
67a87e7d |
688 | } |
689 | |
87fcac8d |
690 | /** |
691 | * main portfolio cronjob |
692 | * currently just cleans up expired transfer records. |
693 | * |
694 | * @todo add hooks in the plugins - either per instance or per plugin |
695 | */ |
696 | function portfolio_cron() { |
697 | global $DB; |
9eb0a772 |
698 | |
87fcac8d |
699 | if ($expired = $DB->get_records_select('portfolio_tempdata', 'expirytime < ?', array(time()), '', 'id')) { |
700 | foreach ($expired as $d) { |
701 | $e = portfolio_exporter::rewaken_object($d); |
702 | $e->process_stage_cleanup(true); |
9eb0a772 |
703 | } |
192ce92b |
704 | } |
5071079c |
705 | } |
706 | |
67a87e7d |
707 | /** |
87fcac8d |
708 | * helper function to rethrow a caught portfolio_exception as an export exception |
709 | * |
710 | * used because when a portfolio_export exception is thrown the export is cancelled |
711 | * |
712 | * @param portfolio_exporter $exporter current exporter object |
713 | * @param exception $exception exception to rethrow |
714 | * |
715 | * @return void |
716 | * @throws portfolio_export_exceptiog |
67a87e7d |
717 | */ |
87fcac8d |
718 | function portfolio_export_rethrow_exception($exporter, $exception) { |
719 | throw new portfolio_export_exception($exporter, $exception->errorcode, $exception->module, $exception->link, $exception->a); |
720 | } |
67a87e7d |
721 | |
bee4bce2 |
722 | /** |
723 | * try and determine expected_time for purely file based exports |
724 | * or exports that might include large file attachments. |
725 | * |
726 | * @param mixed $totest - either an array of stored_file objects or a single stored_file object |
727 | * |
728 | * @return constant PORTFOLIO_TIME_XXX |
729 | */ |
730 | function portfolio_expected_time_file($totest) { |
731 | global $CFG; |
732 | if ($totest instanceof stored_file) { |
733 | $totest = array($totest); |
734 | } |
735 | $size = 0; |
736 | foreach ($totest as $file) { |
737 | if (!($file instanceof stored_file)) { |
738 | debugging('something weird passed to portfolio_expected_time_file - not stored_file object'); |
739 | debugging(print_r($file, true)); |
740 | continue; |
741 | } |
742 | $size += $file->get_filesize(); |
743 | } |
744 | |
745 | $fileinfo = portfolio_filesize_info(); |
746 | |
747 | $moderate = $high = 0; // avoid warnings |
748 | |
749 | foreach (array('moderate', 'high') as $setting) { |
750 | $settingname = 'portfolio_' . $setting . '_filesize_threshold'; |
751 | if (empty($CFG->{$settingname}) || !array_key_exists($CFG->{$settingname}, $fileinfo['options'])) { |
752 | debugging("weird or unset admin value for $settingname, using default instead"); |
753 | $$setting = $fileinfo[$setting]; |
754 | } else { |
755 | $$setting = $CFG->{$settingname}; |
756 | } |
757 | } |
758 | |
759 | if ($size < $moderate) { |
760 | return PORTFOLIO_TIME_LOW; |
761 | } else if ($size < $high) { |
762 | return PORTFOLIO_TIME_MODERATE; |
763 | } |
764 | return PORTFOLIO_TIME_HIGH; |
765 | } |
766 | |
767 | |
768 | /** |
769 | * the default filesizes and threshold information for file based transfers |
770 | * this shouldn't need to be used outside the admin pages and the portfolio code |
771 | */ |
772 | function portfolio_filesize_info() { |
773 | $filesizes = array(); |
774 | $sizelist = array(10240, 51200, 102400, 512000, 1048576, 2097152, 5242880, 10485760, 20971520, 52428800); |
775 | foreach ($sizelist as $size) { |
776 | $filesizes[$size] = display_size($size); |
777 | } |
778 | return array( |
779 | 'options' => $filesizes, |
780 | 'moderate' => 1048576, |
781 | 'high' => 5242880, |
782 | ); |
783 | } |
784 | |
785 | /** |
786 | * try and determine expected_time for purely database based exports |
787 | * or exports that might include large parts of a database |
788 | * |
789 | * @param integer $recordcount - number of records trying to export |
790 | * |
791 | * @return constant PORTFOLIO_TIME_XXX |
792 | */ |
793 | function portfolio_expected_time_db($recordcount) { |
794 | global $CFG; |
795 | |
796 | if (empty($CFG->portfolio_moderate_dbsize_threshold)) { |
797 | set_config('portfolio_moderate_dbsize_threshold', 10); |
798 | } |
799 | if (empty($CFG->portfolio_high_dbsize_threshold)) { |
800 | set_config('portfolio_high_dbsize_threshold', 50); |
801 | } |
802 | if ($recordcount < $CFG->portfolio_moderate_dbsize_threshold) { |
803 | return PORTFOLIO_TIME_LOW; |
804 | } else if ($recordcount < $CFG->portfolio_high_dbsize_threshold) { |
805 | return PORTFOLIO_TIME_MODERATE; |
806 | } |
807 | return PORTFOLIO_TIME_HIGH; |
808 | } |
67a87e7d |
809 | |
67a87e7d |
810 | ?> |