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