67a87e7d |
1 | <?php |
67a87e7d |
2 | /** |
3 | * this file contains: |
4 | * {@link portfolio_add_button} -entry point for callers |
5 | * {@link class portfolio_plugin_base} - class plugins extend |
6 | * {@link class portfolio_caller_base} - class callers extend |
7 | * {@link class portfolio_admin_form} - base moodleform class for plugin administration |
8 | * {@link class portfolio_user_form} - base moodleform class for plugin instance user config |
9 | * {@link class portfolio_export_form} - base moodleform class for during-export configuration (eg metadata) |
10 | * {@link class portfolio_exporter} - class used during export process |
11 | * |
12 | * and some helper functions: |
13 | * {@link portfolio_instances - returns an array of all configured instances |
14 | * {@link portfolio_instance - returns an instance of the right class given an id |
15 | * {@link portfolio_instance_select} - returns a drop menu of available instances |
16 | * {@link portfolio_static_function - requires the file, and calls a static function on the given class |
17 | " {@link portfolio_plugin_sanity_check - polls given (or all) portfolio_plugins for sanity and disables insane ones |
18 | " {@link portfolio_instance_sanity_check - polls given (or all) portfolio instances for sanity and disables insane ones |
19 | * {@link portfolio_report_instane} - returns a table of insane plugins and the reasons (used for plugins or instances thereof) |
20 | * {@link portfolio_supported_formats - returns array of all available formats for plugins and callers to use |
21 | * {@link portfolio_handle_event} - event handler for queued transfers that get triggered on cron |
22 | * |
23 | */ |
24 | require_once ($CFG->libdir.'/formslib.php'); |
25 | |
26 | // **** EXPORT STAGE CONSTANTS **** // |
27 | |
28 | /** |
29 | * display a form to the user |
30 | * this one might not be used if neither |
31 | * the plugin, or the caller has any config. |
32 | */ |
33 | define('PORTFOLIO_STAGE_CONFIG', 1); |
34 | |
35 | /** |
36 | * summarise the form and ask for confirmation |
37 | * if we skipped PORTFOLIO_STAGE_CONFIG, |
38 | * just confirm the send. |
39 | */ |
40 | define('PORTFOLIO_STAGE_CONFIRM', 2); |
41 | |
42 | /** |
43 | * either queue the event and skip to PORTFOLIO_STAGE_FINISHED |
44 | * or continue to PORTFOLIO_STAGE_PACKAGE |
45 | */ |
46 | |
47 | define('PORTFOLIO_STAGE_QUEUEORWAIT', 3); |
48 | |
49 | /** |
50 | * package up the various bits |
51 | * during this stage both the caller |
52 | * and the plugin get their package methods called |
53 | */ |
54 | define('PORTFOLIO_STAGE_PACKAGE', 4); |
55 | |
56 | /* |
57 | * the portfolio plugin must send the file |
58 | */ |
59 | define('PORTFOLIO_STAGE_SEND', 5); |
60 | |
61 | /** |
62 | * cleanup the temporary area |
63 | */ |
64 | define('PORTFOLIO_STAGE_CLEANUP', 6); |
65 | |
66 | /** |
67 | * display the "finished notification" |
68 | */ |
69 | define('PORTFOLIO_STAGE_FINISHED', 7); |
70 | |
71 | |
72 | |
73 | // **** EXPORT FORMAT CONSTANTS **** // |
74 | // these should always correspond to a string |
75 | // in the portfolio module, called format_{$value} |
76 | // **** **** // |
77 | |
67a87e7d |
78 | |
79 | /** |
80 | * file - the most basic fallback format. |
81 | * this should always be supported |
82 | * in remote system.s |
83 | */ |
84 | define('PORTFOLIO_FORMAT_FILE', 'file'); |
85 | |
86 | /** |
87 | * moodle backup - the plugin needs to be able to write a complete backup |
88 | * the caller need to be able to export the particular XML bits to insert |
89 | * into moodle.xml (?and the file bits if necessary) |
90 | */ |
91 | define('PORTFOLIO_FORMAT_MBKP', 'mbkp'); |
92 | |
7812e882 |
93 | /** |
94 | * html - subtype of file |
95 | */ |
96 | define('PORTFOLIO_FORMAT_HTML', 'html'); |
97 | |
98 | /** |
99 | * image - subtype of file |
100 | */ |
101 | define('PORTFOLIO_FORMAT_IMAGE', 'image'); |
102 | |
103 | |
67a87e7d |
104 | |
105 | // **** EXPORT TIME LEVELS **** // |
106 | // these should correspond to a string |
107 | // in the portfolio module, called time_{$value} |
108 | |
109 | /** |
110 | * no delay. don't even offer the user the option |
111 | * of not waiting for the transfer |
112 | */ |
113 | define('PORTFOLIO_TIME_LOW', 'low'); |
114 | |
115 | /** |
116 | * a small delay. user can still easily opt to |
117 | * watch this transfer and wait. |
118 | */ |
119 | define('PORTFOLIO_TIME_MODERATE', 'moderate'); |
120 | |
121 | /** |
122 | * slow. the user really should not be given the option |
123 | * to choose this. |
124 | */ |
125 | define('PORTFOLIO_TIME_HIGH', 'high'); |
126 | |
83f60058 |
127 | /** |
128 | * very slow, or immediate transfers not supported |
129 | */ |
130 | define('PORTFOLIO_TIME_FORCEQUEUE', 'queue'); |
131 | |
866d543f |
132 | // ************************************************** // |
133 | // available ways to add the portfolio export to a page |
134 | // ************************************************** // |
135 | |
136 | /** |
137 | * a whole form, containing a drop down menu (where necessary) |
138 | * and a submit button |
139 | */ |
140 | define('PORTFOLIO_ADD_FULL_FORM', 1); |
141 | |
142 | |
143 | /** |
144 | * a whole form, containing a drop down menu (where necessary) |
145 | * but has an icon instead of a button to submit |
146 | */ |
147 | define('PORTFOLIO_ADD_ICON_FORM', 2); |
148 | |
149 | /** |
150 | * just an icon with a link around it (yuk, as will result in a long url |
151 | * only use where necessary) |
152 | */ |
153 | define('PORTFOLIO_ADD_ICON_LINK', 3); |
154 | |
155 | /** |
156 | * just some text with a link around it (yuk, as will result in a long url |
157 | * only use where necessary) |
158 | */ |
159 | define('PORTFOLIO_ADD_TEXT_LINK', 4); |
67a87e7d |
160 | |
161 | /** |
162 | * entry point to add an 'add to portfolio' button somewhere in moodle |
163 | * this function does not check permissions. the caller must check permissions first. |
164 | * later, during the export process, the caller class is instantiated and the check_permissions method is called |
165 | * but not in this function. |
166 | * |
167 | * @param string $callbackclass name of the class containing the callback functions |
168 | * activity modules should ALWAYS use their name_portfolio_caller |
169 | * other locations must use something unique |
170 | * @param mixed $callbackargs this can be an array or hash of arguments to pass |
171 | * back to the callback functions (passed by reference) |
172 | * these MUST be primatives to be added as hidden form fields. |
173 | * and the values get cleaned to PARAM_ALPHAEXT or PARAM_NUMBER or PARAM_PATH |
ed1fcf79 |
174 | * @param string $callbackfile this can be autodetected if it's in the same file as your caller, |
175 | * but more often, the caller is a script.php and the class in a lib.php |
176 | * so you can pass it here if necessary. |
177 | * this path should be relative (ie, not include) dirroot |
866d543f |
178 | * @param int $format format to display the button or form or icon or link. |
179 | * See constants PORTFOLIO_ADD_XXX for more info. |
180 | * optional, defaults to PORTFOLI_ADD_FULL_FORM |
181 | * @param str $addstr string to use for the button or icon alt text or link text. |
182 | * this is whole string, not key. optional, defaults to 'Add to portfolio'; |
67a87e7d |
183 | * @param boolean $return whether to echo or return content (optional defaults to false (echo) |
349242a3 |
184 | * @param array $callersupports if the calling code knows better than the static method on the calling class (supported_formats) |
185 | * eg, if there's a file that might be an image, you can pass it here instead |
67a87e7d |
186 | */ |
349242a3 |
187 | function portfolio_add_button($callbackclass, $callbackargs, $callbackfile=null, $format=PORTFOLIO_ADD_FULL_FORM, $addstr=null, $return=false, $callersupports=null) { |
67a87e7d |
188 | |
189 | global $SESSION, $CFG, $COURSE, $USER; |
190 | |
90658eef |
191 | if (empty($CFG->enableportfolios)) { |
a239f01e |
192 | return; |
193 | } |
194 | |
67a87e7d |
195 | if (!$instances = portfolio_instances()) { |
196 | return; |
197 | } |
198 | |
84a44985 |
199 | if (defined('PORTFOLIO_INTERNAL')) { |
6fdd8fa7 |
200 | // something somewhere has detected a risk of this being called during inside the preparation |
201 | // eg forum_print_attachments |
202 | return; |
203 | } |
204 | |
84a44985 |
205 | if (isset($SESSION->portfolioexport)) { |
ac6a5492 |
206 | $a = new StdClass; |
207 | $a->cancel = $CFG->wwwroot . '/portfolio/add.php?cancel=1'; |
208 | $a->finish = $CFG->wwwroot . '/portfolio/add.php?id=' . $SESSION->portfolioexport; |
3bb8a2c7 |
209 | throw new portfolio_exception('alreadyexporting', 'portfolio', null, $a); |
84a44985 |
210 | } |
211 | |
ed1fcf79 |
212 | if (empty($callbackfile)) { |
213 | $backtrace = debug_backtrace(); |
214 | if (!array_key_exists(0, $backtrace) || !array_key_exists('file', $backtrace[0]) || !is_readable($backtrace[0]['file'])) { |
215 | debugging(get_string('nocallbackfile', 'portfolio')); |
216 | return; |
217 | } |
218 | |
219 | $callbackfile = substr($backtrace[0]['file'], strlen($CFG->dirroot)); |
220 | } else { |
221 | if (!is_readable($CFG->dirroot . $callbackfile)) { |
222 | debugging(get_string('nocallbackfile', 'portfolio')); |
223 | return; |
224 | } |
67a87e7d |
225 | } |
226 | |
67a87e7d |
227 | require_once($CFG->dirroot . $callbackfile); |
228 | |
349242a3 |
229 | if (empty($callersupports)) { |
230 | $callersupports = call_user_func(array($callbackclass, 'supported_formats')); |
231 | } |
67a87e7d |
232 | |
866d543f |
233 | $formoutput = '<form method="post" action="' . $CFG->wwwroot . '/portfolio/add.php" id="portfolio-add-button">' . "\n"; |
234 | $linkoutput = '<a href="' . $CFG->wwwroot . '/portfolio/add.php?'; |
67a87e7d |
235 | foreach ($callbackargs as $key => $value) { |
236 | if (!empty($value) && !is_string($value) && !is_numeric($value)) { |
237 | $a->key = $key; |
238 | $a->value = print_r($value, true); |
239 | debugging(get_string('nonprimative', 'portfolio', $a)); |
240 | return; |
241 | } |
866d543f |
242 | $linkoutput .= 'ca_' . $key . '=' . $value . '&'; |
243 | $formoutput .= "\n" . '<input type="hidden" name="ca_' . $key . '" value="' . $value . '" />'; |
67a87e7d |
244 | } |
866d543f |
245 | $formoutput .= "\n" . '<input type="hidden" name="callbackfile" value="' . $callbackfile . '" />'; |
246 | $formoutput .= "\n" . '<input type="hidden" name="callbackclass" value="' . $callbackclass . '" />'; |
247 | $formoutput .= "\n" . '<input type="hidden" name="course" value="' . (!empty($COURSE) ? $COURSE->id : 0) . '" />'; |
248 | $linkoutput .= 'callbackfile=' . $callbackfile . '&callbackclass=' |
249 | . $callbackclass . '&course=' . (!empty($COURSE) ? $COURSE->id : 0); |
67a87e7d |
250 | $selectoutput = ''; |
251 | if (count($instances) == 1) { |
252 | $instance = array_shift($instances); |
349242a3 |
253 | $formats = portfolio_supported_formats_intersect($callersupports, $instance->supported_formats()); |
254 | if (count($formats) == 0) { |
67a87e7d |
255 | // bail. no common formats. |
256 | debugging(get_string('nocommonformats', 'portfolio', $callbackclass)); |
257 | return; |
258 | } |
259 | if ($error = portfolio_instance_sanity_check($instance)) { |
260 | // bail, plugin is misconfigured |
261 | debugging(get_string('instancemisconfigured', 'portfolio', get_string($error[$instance->get('id')], 'portfolio_' . $instance->get('plugin')))); |
262 | return; |
263 | } |
866d543f |
264 | $formoutput .= "\n" . '<input type="hidden" name="instance" value="' . $instance->get('id') . '" />'; |
265 | $linkoutput .= '&instance=' . $instance->get('id'); |
67a87e7d |
266 | } |
267 | else { |
7c61a8f0 |
268 | $selectoutput = portfolio_instance_select($instances, $callersupports, $callbackclass, 'instance', true); |
67a87e7d |
269 | } |
270 | |
866d543f |
271 | if (empty($addstr)) { |
272 | $addstr = get_string('addtoportfolio', 'portfolio'); |
67a87e7d |
273 | } |
866d543f |
274 | if (empty($format)) { |
275 | $format = PORTFOLIO_ADD_FULL_FORM; |
276 | } |
277 | switch ($format) { |
278 | case PORTFOLIO_ADD_FULL_FORM: |
279 | $formoutput .= $selectoutput; |
280 | $formoutput .= "\n" . '<input type="submit" value="' . $addstr .'" />'; |
281 | $formoutput .= "\n" . '</form>'; |
282 | break; |
283 | case PORTFOLIO_ADD_ICON_FORM: |
284 | $formoutput .= $selectoutput; |
285 | $formoutput .= "\n" . '<input type="image" src="' . $CFG->pixpath . '/t/portfolio.gif" alt=' . $addstr .'" />'; |
286 | $formoutput .= "\n" . '</form>'; |
287 | break; |
288 | case PORTFOLIO_ADD_ICON_LINK: |
289 | $linkoutput .= '"><img src="' . $CFG->pixpath . '/t/portfolio.gif" alt=' . $addstr .'" /></a>'; |
290 | break; |
291 | case PORTFOLIO_ADD_TEXT_LINK: |
292 | $linkoutput .= '">' . $addstr .'</a>'; |
293 | break; |
294 | default: |
3bb8a2c7 |
295 | debugging(get_string('invalidaddformat', 'portfolio', $format)); |
866d543f |
296 | } |
297 | $output = (in_array($format, array(PORTFOLIO_ADD_FULL_FORM, PORTFOLIO_ADD_ICON_FORM)) ? $formoutput : $linkoutput); |
67a87e7d |
298 | if ($return) { |
299 | return $output; |
300 | } else { |
301 | echo $output; |
302 | } |
303 | return true; |
304 | } |
305 | |
306 | /** |
307 | * returns a drop menu with a list of available instances. |
308 | * |
309 | * @param array $instances the instances to put in the menu |
310 | * @param array $callerformats the formats the caller supports |
311 | (this is used to filter plugins) |
312 | * @param array $callbackclass the callback class name |
313 | * |
314 | * @return string the html, from <select> to </select> inclusive. |
315 | */ |
9eb0a772 |
316 | function portfolio_instance_select($instances, $callerformats, $callbackclass, $selectname='instance', $return=false, $returnarray=false) { |
317 | global $CFG; |
318 | |
90658eef |
319 | if (empty($CFG->enableportfolios)) { |
9eb0a772 |
320 | return; |
321 | } |
322 | |
67a87e7d |
323 | $insane = portfolio_instance_sanity_check(); |
324 | $count = 0; |
9eb0a772 |
325 | $selectoutput = "\n" . '<select name="' . $selectname . '">' . "\n"; |
67a87e7d |
326 | foreach ($instances as $instance) { |
349242a3 |
327 | $formats = portfolio_supported_formats_intersect($callerformats, $instance->supported_formats()); |
328 | if (count($formats) == 0) { |
67a87e7d |
329 | // bail. no common formats. |
330 | continue; |
331 | } |
332 | if (array_key_exists($instance->get('id'), $insane)) { |
333 | // bail, plugin is misconfigured |
334 | debugging(get_string('instancemisconfigured', 'portfolio', get_string($insane[$instance->get('id')], 'portfolio_' . $instance->get('plugin')))); |
335 | continue; |
336 | } |
337 | $count++; |
9eb0a772 |
338 | $selectoutput .= "\n" . '<option value="' . $instance->get('id') . '">' . $instance->get('name') . '</option>' . "\n"; |
339 | $options[$instance->get('id')] = $instance->get('name'); |
67a87e7d |
340 | } |
341 | if (empty($count)) { |
342 | // bail. no common formats. |
343 | debugging(get_string('nocommonformats', 'portfolio', $callbackclass)); |
344 | return; |
345 | } |
346 | $selectoutput .= "\n" . "</select>\n"; |
9eb0a772 |
347 | if (!empty($returnarray)) { |
348 | return $options; |
349 | } |
350 | if (!empty($return)) { |
351 | return $selectoutput; |
352 | } |
353 | echo $selectoutput; |
67a87e7d |
354 | } |
355 | |
356 | /** |
357 | * return all portfolio instances |
358 | * |
359 | * @param boolean visibleonly don't include hidden instances (defaults to true and will be overridden to true if the next parameter is true) |
360 | * @param boolean useronly check the visibility preferences and permissions of the logged in user |
361 | * @return array of portfolio instances (full objects, not just database records) |
362 | */ |
363 | function portfolio_instances($visibleonly=true, $useronly=true) { |
364 | |
365 | global $DB, $USER; |
366 | |
367 | $values = array(); |
368 | $sql = 'SELECT * FROM {portfolio_instance}'; |
369 | |
370 | if ($visibleonly || $useronly) { |
371 | $values[] = 1; |
372 | $sql .= ' WHERE visible = ?'; |
373 | } |
374 | if ($useronly) { |
375 | $sql .= ' AND id NOT IN ( |
376 | SELECT instance FROM {portfolio_instance_user} |
377 | WHERE userid = ? AND name = ? AND value = ? |
378 | )'; |
379 | $values = array_merge($values, array($USER->id, 'visible', 0)); |
380 | } |
381 | $sql .= ' ORDER BY name'; |
382 | |
383 | $instances = array(); |
384 | foreach ($DB->get_records_sql($sql, $values) as $instance) { |
a50ef3d3 |
385 | $instances[$instance->id] = portfolio_instance($instance->id, $instance); |
67a87e7d |
386 | } |
387 | // @todo check capabilities here - see MDL-15768 |
388 | return $instances; |
389 | } |
390 | |
391 | /** |
392 | * supported formats that portfolio plugins and callers |
393 | * can use for exporting content |
394 | * |
395 | * @return array of all the available export formats |
396 | */ |
397 | function portfolio_supported_formats() { |
398 | return array( |
349242a3 |
399 | PORTFOLIO_FORMAT_FILE => 'portfolio_format_file', |
400 | PORTFOLIO_FORMAT_IMAGE => 'portfolio_format_image', |
401 | PORTFOLIO_FORMAT_HTML => 'portfolio_format_html', |
5071079c |
402 | /*PORTFOLIO_FORMAT_MBKP, */ // later |
403 | /*PORTFOLIO_FORMAT_PIOP, */ // also later |
67a87e7d |
404 | ); |
405 | } |
406 | |
7812e882 |
407 | function portfolio_supported_formats_intersect($callerformats, $pluginformats) { |
408 | $allformats = portfolio_supported_formats(); |
409 | $intersection = array(); |
410 | foreach ($callerformats as $cf) { |
411 | if (!array_key_exists($cf, $allformats)) { |
412 | debugging(get_string('invalidformat', 'portfolio', $cf)); |
413 | continue; |
414 | } |
34035201 |
415 | $cfobj = new $allformats[$cf](); |
7812e882 |
416 | foreach ($pluginformats as $p => $pf) { |
417 | if (!array_key_exists($pf, $allformats)) { |
418 | debugging(get_string('invalidformat', 'portfolio', $pf)); |
419 | unset($pluginformats[$p]); // to avoid the same warning over and over |
420 | continue; |
421 | } |
34035201 |
422 | if ($cfobj instanceof $allformats[$pf]) { |
7812e882 |
423 | $intersection[] = $cf; |
424 | } |
425 | } |
426 | } |
427 | return $intersection; |
428 | } |
429 | |
67a87e7d |
430 | /** |
431 | * helper function to return an instance of a plugin (with config loaded) |
432 | * |
433 | * @param int $instance id of instance |
434 | * @param array $record database row that corresponds to this instance |
435 | * this is passed to avoid unnecessary lookups |
436 | * |
437 | * @return subclass of portfolio_plugin_base |
438 | */ |
439 | function portfolio_instance($instanceid, $record=null) { |
440 | global $DB, $CFG; |
441 | |
442 | if ($record) { |
443 | $instance = $record; |
444 | } else { |
445 | if (!$instance = $DB->get_record('portfolio_instance', array('id' => $instanceid))) { |
34035201 |
446 | throw new portfolio_exception('invalidinstance', 'portfolio'); |
67a87e7d |
447 | } |
448 | } |
449 | require_once($CFG->dirroot . '/portfolio/type/'. $instance->plugin . '/lib.php'); |
450 | $classname = 'portfolio_plugin_' . $instance->plugin; |
451 | return new $classname($instanceid, $instance); |
452 | } |
453 | |
454 | /** |
455 | * helper function to call a static function on a portfolio plugin class |
456 | * this will figure out the classname and require the right file and call the function. |
457 | * you can send a variable number of arguments to this function after the first two |
458 | * and they will be passed on to the function you wish to call. |
459 | * |
460 | * @param string $plugin name of plugin |
461 | * @param string $function function to call |
462 | */ |
463 | function portfolio_static_function($plugin, $function) { |
464 | global $CFG; |
465 | |
466 | $pname = null; |
467 | if (is_object($plugin) || is_array($plugin)) { |
468 | $plugin = (object)$plugin; |
469 | $pname = $plugin->name; |
470 | } else { |
471 | $pname = $plugin; |
472 | } |
473 | |
474 | $args = func_get_args(); |
475 | if (count($args) <= 2) { |
476 | $args = array(); |
477 | } |
478 | else { |
479 | array_shift($args); |
480 | array_shift($args); |
481 | } |
482 | |
483 | require_once($CFG->dirroot . '/portfolio/type/' . $plugin . '/lib.php'); |
484 | return call_user_func_array(array('portfolio_plugin_' . $plugin, $function), $args); |
485 | } |
486 | |
487 | /** |
488 | * helper function to check all the plugins for sanity and set any insane ones to invisible. |
489 | * |
490 | * @param array $plugins to check (if null, defaults to all) |
491 | * one string will work too for a single plugin. |
492 | * |
493 | * @return array array of insane instances (keys= id, values = reasons (keys for plugin lang) |
494 | */ |
495 | function portfolio_plugin_sanity_check($plugins=null) { |
496 | global $DB; |
497 | if (is_string($plugins)) { |
498 | $plugins = array($plugins); |
499 | } else if (empty($plugins)) { |
500 | $plugins = get_list_of_plugins('portfolio/type'); |
501 | } |
502 | |
503 | $insane = array(); |
504 | foreach ($plugins as $plugin) { |
505 | if ($result = portfolio_static_function($plugin, 'plugin_sanity_check')) { |
506 | $insane[$plugin] = $result; |
507 | } |
508 | } |
509 | if (empty($insane)) { |
510 | return array(); |
511 | } |
512 | list($where, $params) = $DB->get_in_or_equal(array_keys($insane)); |
513 | $where = ' plugin ' . $where; |
514 | $DB->set_field_select('portfolio_instance', 'visible', 0, $where, $params); |
515 | return $insane; |
516 | } |
517 | |
518 | /** |
519 | * helper function to check all the instances for sanity and set any insane ones to invisible. |
520 | * |
521 | * @param array $instances to check (if null, defaults to all) |
522 | * one instance or id will work too |
523 | * |
524 | * @return array array of insane instances (keys= id, values = reasons (keys for plugin lang) |
525 | */ |
526 | function portfolio_instance_sanity_check($instances=null) { |
527 | global $DB; |
528 | if (empty($instances)) { |
529 | $instances = portfolio_instances(false); |
530 | } else if (!is_array($instances)) { |
531 | $instances = array($instances); |
532 | } |
533 | |
534 | $insane = array(); |
535 | foreach ($instances as $instance) { |
536 | if (is_object($instance) && !($instance instanceof portfolio_plugin_base)) { |
537 | $instance = portfolio_instance($instance->id, $instance); |
538 | } else if (is_numeric($instance)) { |
539 | $instance = portfolio_instance($instance); |
540 | } |
541 | if (!($instance instanceof portfolio_plugin_base)) { |
542 | debugging('something weird passed to portfolio_instance_sanity_check, not subclass or id'); |
543 | continue; |
544 | } |
545 | if ($result = $instance->instance_sanity_check()) { |
546 | $insane[$instance->get('id')] = $result; |
547 | } |
548 | } |
549 | if (empty($insane)) { |
550 | return array(); |
551 | } |
552 | list ($where, $params) = $DB->get_in_or_equal(array_keys($insane)); |
553 | $where = ' id ' . $where; |
554 | $DB->set_field_select('portfolio_instance', 'visible', 0, $where, $params); |
555 | return $insane; |
556 | } |
557 | |
558 | /** |
559 | * helper function to display a table of plugins (or instances) and reasons for disabling |
560 | * |
561 | * @param array $insane array of insane plugins (key = plugin (or instance id), value = reason) |
562 | * @param array $instances if reporting instances rather than whole plugins, pass the array (key = id, value = object) here |
563 | * |
564 | */ |
a50ef3d3 |
565 | function portfolio_report_insane($insane, $instances=false, $return=false) { |
67a87e7d |
566 | if (empty($insane)) { |
567 | return; |
568 | } |
569 | |
570 | static $pluginstr; |
571 | if (empty($pluginstr)) { |
572 | $pluginstr = get_string('plugin', 'portfolio'); |
573 | } |
574 | if ($instances) { |
575 | $headerstr = get_string('someinstancesdisabled', 'portfolio'); |
576 | } else { |
577 | $headerstr = get_string('somepluginsdisabled', 'portfolio'); |
578 | } |
579 | |
a50ef3d3 |
580 | $output = notify($headerstr, 'notifyproblem', 'center', true); |
67a87e7d |
581 | $table = new StdClass; |
582 | $table->head = array($pluginstr, ''); |
583 | $table->data = array(); |
584 | foreach ($insane as $plugin => $reason) { |
585 | if ($instances) { |
586 | // @todo this isn't working |
587 | // because it seems the new recordset object |
588 | // doesn't implement the key correctly. |
0082ed89 |
589 | // see MDL-15798 |
67a87e7d |
590 | $instance = $instances[$plugin]; |
591 | $plugin = $instance->get('plugin'); |
592 | $name = $instance->get('name'); |
593 | } else { |
594 | $name = $plugin; |
595 | } |
596 | $table->data[] = array($name, get_string($reason, 'portfolio_' . $plugin)); |
597 | } |
a50ef3d3 |
598 | $output .= print_table($table, true); |
599 | $output .= '<br /><br /><br />'; |
600 | |
601 | if ($return) { |
602 | return $output; |
603 | } |
604 | echo $output; |
67a87e7d |
605 | } |
606 | |
9eb0a772 |
607 | /** |
608 | * fake the url to portfolio/add.php from data from somewhere else |
609 | * you should use portfolio_add_button instead 99% of the time |
610 | * |
611 | * @param int $instanceid instanceid (optional, will force a new screen if not specified) |
612 | * @param string $classname callback classname |
613 | * @param string $classfile file containing the callback class definition |
614 | * @param array $callbackargs arguments to pass to the callback class |
615 | */ |
616 | function portfolio_fake_add_url($instanceid, $classname, $classfile, $callbackargs) { |
617 | global $CFG; |
618 | $url = $CFG->wwwroot . '/portfolio/add.php?instance=' . $instanceid . '&callbackclass=' . $classname . '&callbackfile=' . $classfile; |
619 | |
620 | if (is_object($callbackargs)) { |
621 | $callbackargs = (array)$callbackargs; |
622 | } |
623 | if (!is_array($callbackargs) || empty($callbackargs)) { |
624 | return $url; |
625 | } |
626 | foreach ($callbackargs as $key => $value) { |
627 | $url .= '&ca_' . $key . '=' . urlencode($value); |
628 | } |
629 | return $url; |
630 | } |
67a87e7d |
631 | |
632 | /** |
633 | * base class for the caller |
634 | * places in moodle that want to display |
635 | * the add form should subclass this for their callback. |
636 | */ |
637 | abstract class portfolio_caller_base { |
638 | |
639 | /** |
640 | * stdclass object |
641 | * course that was active during the caller |
642 | */ |
643 | protected $course; |
644 | |
645 | /** |
646 | * named array of export config |
647 | * use{@link set_export_config} and {@link get_export_config} to access |
648 | */ |
649 | protected $exportconfig; |
650 | |
651 | /** |
652 | * stdclass object |
653 | * user currently exporting content |
654 | */ |
655 | protected $user; |
656 | |
d67bfc32 |
657 | /** |
658 | * a reference to the exporter object |
659 | */ |
660 | protected $exporter; |
84a44985 |
661 | |
04f35360 |
662 | /** |
349242a3 |
663 | * this can be overridden in subclasses constructors if they want |
04f35360 |
664 | */ |
349242a3 |
665 | protected $supportedformats; |
04f35360 |
666 | |
67a87e7d |
667 | /** |
668 | * if this caller wants any additional config items |
669 | * they should be defined here. |
670 | * |
671 | * @param array $mform moodleform object (passed by reference) to add elements to |
672 | * @param object $instance subclass of portfolio_plugin_base |
673 | * @param integer $userid id of user exporting content |
674 | */ |
675 | public function export_config_form(&$mform, $instance) {} |
676 | |
677 | |
678 | /** |
679 | * whether this caller wants any additional |
680 | * config during export (eg options or metadata) |
681 | * |
682 | * @return boolean |
683 | */ |
684 | public function has_export_config() { |
685 | return false; |
686 | } |
687 | |
688 | /** |
689 | * just like the moodle form validation function |
690 | * this is passed in the data array from the form |
691 | * and if a non empty array is returned, form processing will stop. |
692 | * |
693 | * @param array $data data from form. |
694 | * @return array keyvalue pairs - form element => error string |
695 | */ |
696 | public function export_config_validation($data) {} |
697 | |
698 | /** |
699 | * how long does this reasonably expect to take.. |
700 | * should we offer the user the option to wait.. |
701 | * this is deliberately nonstatic so it can take filesize into account |
702 | * the portfolio plugin can override this. |
703 | * (so for exmaple even if a huge file is being sent, |
704 | * the download portfolio plugin doesn't care ) |
705 | * |
706 | * @return string (see PORTFOLIO_TIME_* constants) |
707 | */ |
708 | public abstract function expected_time(); |
709 | |
710 | /** |
711 | * used for displaying the navigation during the export screens. |
712 | * |
713 | * this function must be implemented, but can really return anything. |
714 | * an Exporting.. string will be added on the end. |
715 | * @return array of $extranav and $cm |
716 | * |
717 | * to pass to build_navigation |
718 | * |
719 | */ |
720 | public abstract function get_navigation(); |
721 | |
ffcfd8a7 |
722 | /** |
723 | * |
724 | */ |
725 | public abstract function get_sha1(); |
726 | |
67a87e7d |
727 | /* |
728 | * generic getter for properties belonging to this instance |
729 | * <b>outside</b> the subclasses |
730 | * like name, visible etc. |
67a87e7d |
731 | */ |
9eb0a772 |
732 | public function get($field) { |
67a87e7d |
733 | if (property_exists($this, $field)) { |
734 | return $this->{$field}; |
735 | } |
34035201 |
736 | $a = (object)array('property' => $field, 'class' => get_class($this)); |
737 | throw new portfolio_export_exception($this->get('exporter'), 'invalidproperty', 'portfolio', $this->get_return_url(), $a); |
67a87e7d |
738 | } |
739 | |
740 | /** |
741 | * generic setter for properties belonging to this instance |
742 | * <b>outside</b> the subclass |
743 | * like name, visible, etc. |
744 | * |
67a87e7d |
745 | */ |
d67bfc32 |
746 | public final function set($field, &$value) { |
67a87e7d |
747 | if (property_exists($this, $field)) { |
d67bfc32 |
748 | $this->{$field} =& $value; |
67a87e7d |
749 | $this->dirty = true; |
750 | return true; |
751 | } |
34035201 |
752 | $a = (object)array('property' => $field, 'class' => get_class($this)); |
753 | throw new portfolio_export_exception($this->get('exporter'), 'invalidproperty', 'portfolio', $this->get_return_url(), $a); |
67a87e7d |
754 | } |
755 | |
756 | /** |
757 | * stores the config generated at export time. |
758 | * subclasses can retrieve values using |
759 | * {@link get_export_config} |
760 | * |
761 | * @param array $config formdata |
762 | */ |
763 | public final function set_export_config($config) { |
764 | $allowed = array_merge( |
ffcfd8a7 |
765 | array('wait', 'hidewait', 'format', 'hideformat'), |
67a87e7d |
766 | $this->get_allowed_export_config() |
767 | ); |
768 | foreach ($config as $key => $value) { |
769 | if (!in_array($key, $allowed)) { |
34035201 |
770 | $a = (object)array('property' => $key, 'class' => get_class($this)); |
771 | throw new portfolio_export_exception($this->get('exporter'), 'invalidexportproperty', 'portfolio', $this->get_return_url(), $a); |
67a87e7d |
772 | } |
773 | $this->exportconfig[$key] = $value; |
774 | } |
775 | } |
776 | |
777 | /** |
778 | * returns a particular export config value. |
779 | * subclasses shouldn't need to override this |
780 | * |
781 | * @param string key the config item to fetch |
67a87e7d |
782 | */ |
783 | public final function get_export_config($key) { |
784 | $allowed = array_merge( |
ffcfd8a7 |
785 | array('wait', 'hidewait', 'format', 'hideformat'), |
67a87e7d |
786 | $this->get_allowed_export_config() |
787 | ); |
788 | if (!in_array($key, $allowed)) { |
34035201 |
789 | $a = (object)array('property' => $key, 'class' => get_class($this)); |
790 | throw new portfolio_export_exception($this->get('exporter'), 'invalidexportproperty', 'portfolio', $this->get_return_url(), $a); |
67a87e7d |
791 | } |
792 | if (!array_key_exists($key, $this->exportconfig)) { |
34035201 |
793 | return null; |
67a87e7d |
794 | } |
795 | return $this->exportconfig[$key]; |
796 | } |
797 | |
04f35360 |
798 | |
04f35360 |
799 | |
67a87e7d |
800 | /** |
801 | * Similar to the other allowed_config functions |
802 | * if you need export config, you must provide |
803 | * a list of what the fields are. |
804 | * |
805 | * even if you want to store stuff during export |
806 | * without displaying a form to the user, |
807 | * you can use this. |
808 | * |
809 | * @return array array of allowed keys |
810 | */ |
811 | public function get_allowed_export_config() { |
812 | return array(); |
813 | } |
814 | |
815 | /** |
816 | * after the user submits their config |
817 | * they're given a confirm screen |
818 | * summarising what they've chosen. |
819 | * |
820 | * this function should return a table of nice strings => values |
821 | * of what they've chosen |
822 | * to be displayed in a table. |
823 | * |
824 | * @return array array of config items. |
825 | */ |
826 | public function get_export_summary() { |
827 | return false; |
828 | } |
829 | |
830 | /** |
831 | * called before the portfolio plugin gets control |
832 | * this function should copy all the files it wants to |
d67bfc32 |
833 | * the temporary directory, using {@see copy_existing_file} |
834 | * or {@see write_new_file} |
67a87e7d |
835 | */ |
d67bfc32 |
836 | public abstract function prepare_package(); |
67a87e7d |
837 | |
838 | /** |
839 | * array of formats this caller supports |
840 | * the intersection of what this function returns |
841 | * and what the selected portfolio plugin supports |
842 | * will be used |
843 | * use the constants PORTFOLIO_FORMAT_* |
349242a3 |
844 | * if $caller is passed, that can be used for more specific guesses |
845 | * as this function <b>must</b> be called statically. |
67a87e7d |
846 | * |
847 | * @return array list of formats |
848 | */ |
349242a3 |
849 | public static function supported_formats($caller=null) { |
850 | if ($caller && $caller->get('supportedformats')) { |
851 | return $caller->get('supportedformats'); |
852 | } |
bb63fc3e |
853 | return array(PORTFOLIO_FORMAT_FILE); |
854 | } |
67a87e7d |
855 | |
856 | /** |
857 | * this is the "return to where you were" url |
858 | * |
859 | * @return string url |
860 | */ |
861 | public abstract function get_return_url(); |
862 | |
863 | /** |
864 | * callback to do whatever capability checks required |
865 | * in the caller (called during the export process |
866 | */ |
867 | public abstract function check_permissions(); |
ffcfd8a7 |
868 | |
869 | /** |
870 | * nice name to display to the user about this caller location |
871 | */ |
872 | public abstract static function display_name(); |
192ce92b |
873 | |
874 | /** |
875 | * return a string to put at the header summarising this export |
876 | * by default, just the display name (usually just 'assignment' or something unhelpful |
877 | */ |
878 | public function heading_summary() { |
879 | return get_string('exportingcontentfrom', 'portfolio', $this->display_name()); |
880 | } |
67a87e7d |
881 | } |
882 | |
5071079c |
883 | abstract class portfolio_module_caller_base extends portfolio_caller_base { |
884 | |
885 | protected $cm; |
9eb0a772 |
886 | protected $course; |
5071079c |
887 | |
888 | public function get_navigation() { |
889 | $extranav = array('name' => $this->cm->name, 'link' => $this->get_return_url()); |
890 | return array($extranav, $this->cm); |
891 | } |
892 | |
893 | public function get_return_url() { |
894 | global $CFG; |
895 | return $CFG->wwwroot . '/mod/' . $this->cm->modname . '/view.php?id=' . $this->cm->id; |
896 | } |
9eb0a772 |
897 | |
898 | public function get($key) { |
899 | if ($key != 'course') { |
900 | return parent::get($key); |
901 | } |
902 | global $DB; |
903 | if (empty($this->course)) { |
904 | $this->course = $DB->get_record('course', array('id' => $this->cm->course)); |
905 | } |
906 | return $this->course; |
907 | } |
192ce92b |
908 | |
909 | public function heading_summary() { |
910 | return get_string('exportingcontentfrom', 'portfolio', $this->display_name() . ': ' . $this->cm->name); |
911 | } |
5071079c |
912 | } |
913 | |
67a87e7d |
914 | /** |
2876d73b |
915 | * the base class for portfolio plugins |
67a87e7d |
916 | * all plugins must subclass this. |
917 | */ |
918 | abstract class portfolio_plugin_base { |
919 | |
920 | /** |
921 | * boolean |
922 | * whether this object needs writing out to the database |
923 | */ |
924 | protected $dirty; |
925 | |
926 | /** |
927 | * integer |
928 | * id of instance |
929 | */ |
930 | protected $id; |
931 | |
932 | /** |
933 | * string |
934 | * name of instance |
935 | */ |
936 | protected $name; |
937 | |
938 | /** |
939 | * string |
940 | * plugin this instance belongs to |
941 | */ |
942 | protected $plugin; |
943 | |
944 | /** |
945 | * boolean |
946 | * whether this instance is visible or not |
947 | */ |
948 | protected $visible; |
949 | |
950 | /** |
951 | * named array |
952 | * admin configured config |
953 | * use {@link set_config} and {@get_config} to access |
954 | */ |
955 | protected $config; |
956 | |
957 | /** |
958 | * |
959 | * user config cache |
960 | * named array of named arrays |
961 | * keyed on userid and then on config field => value |
962 | * use {@link get_user_config} and {@link set_user_config} to access. |
963 | */ |
964 | protected $userconfig; |
965 | |
966 | /** |
967 | * named array |
968 | * export config during export |
969 | * use {@link get_export_config} and {@link set export_config} to access. |
970 | */ |
971 | protected $exportconfig; |
972 | |
973 | /** |
974 | * stdclass object |
975 | * user currently exporting data |
976 | */ |
977 | protected $user; |
978 | |
d67bfc32 |
979 | /** |
980 | * a reference to the exporter object |
981 | */ |
982 | protected $exporter; |
67a87e7d |
983 | |
984 | /** |
985 | * array of formats this portfolio supports |
986 | * the intersection of what this function returns |
987 | * and what the caller supports will be used |
988 | * use the constants PORTFOLIO_FORMAT_* |
989 | * |
990 | * @return array list of formats |
991 | */ |
bb63fc3e |
992 | public static function supported_formats() { |
993 | return array(PORTFOLIO_FORMAT_FILE); |
994 | } |
67a87e7d |
995 | |
996 | |
997 | /** |
998 | * how long does this reasonably expect to take.. |
999 | * should we offer the user the option to wait.. |
1000 | * this is deliberately nonstatic so it can take filesize into account |
1001 | * |
1002 | * @param string $callertime - what the caller thinks |
1003 | * the portfolio plugin instance |
1004 | * is given the final say |
1005 | * because it might be (for example) download. |
1006 | * @return string (see PORTFOLIO_TIME_* constants) |
1007 | */ |
1008 | public abstract function expected_time($callertime); |
1009 | |
d96a1acc |
1010 | /** |
1011 | * is this plugin push or pill. |
1012 | * if push, cleanup will be called directly after send_package |
1013 | * if not, cleanup will be called after portfolio/file.php is requested |
1014 | * |
1015 | * @return boolean |
1016 | */ |
1017 | public abstract function is_push(); |
1018 | |
0f71f48b |
1019 | public static abstract function get_name(); |
1020 | |
67a87e7d |
1021 | /** |
1022 | * check sanity of plugin |
1023 | * if this function returns something non empty, ALL instances of your plugin |
1024 | * will be set to invisble and not be able to be set back until it's fixed |
1025 | * |
1026 | * @return mixed - string = error string KEY (must be inside plugin_$yourplugin) or 0/false if you're ok |
1027 | */ |
1028 | public static function plugin_sanity_check() { |
1029 | return 0; |
1030 | } |
1031 | |
1032 | /** |
1033 | * check sanity of instances |
1034 | * if this function returns something non empty, the instance will be |
1035 | * set to invislbe and not be able to be set back until it's fixed. |
1036 | * |
1037 | * @return mixed - string = error string KEY (must be inside plugin_$yourplugin) or 0/false if you're ok |
1038 | */ |
1039 | public function instance_sanity_check() { |
1040 | return 0; |
1041 | } |
1042 | |
1043 | /** |
1044 | * does this plugin need any configuration by the administrator? |
1045 | * |
1046 | * if you override this to return true, |
1047 | * you <b>must</b> implement {@link} admin_config_form |
1048 | */ |
1049 | public static function has_admin_config() { |
1050 | return false; |
1051 | } |
1052 | |
1053 | /** |
1054 | * can this plugin be configured by the user in their profile? |
1055 | * |
1056 | * if you override this to return true, |
1057 | * you <b>must</b> implement {@link user_config_form |
1058 | */ |
1059 | public function has_user_config() { |
1060 | return false; |
1061 | } |
1062 | |
1063 | /** |
1064 | * does this plugin need configuration during export time? |
1065 | * |
1066 | * if you override this to return true, |
1067 | * you <b>must</b> implement {@link export_config_form} |
1068 | */ |
1069 | public function has_export_config() { |
1070 | return false; |
1071 | } |
1072 | |
1073 | /** |
1074 | * just like the moodle form validation function |
1075 | * this is passed in the data array from the form |
1076 | * and if a non empty array is returned, form processing will stop. |
1077 | * |
1078 | * @param array $data data from form. |
1079 | * @return array keyvalue pairs - form element => error string |
1080 | */ |
1081 | public function export_config_validation() {} |
1082 | |
1083 | /** |
1084 | * just like the moodle form validation function |
1085 | * this is passed in the data array from the form |
1086 | * and if a non empty array is returned, form processing will stop. |
1087 | * |
1088 | * @param array $data data from form. |
1089 | * @return array keyvalue pairs - form element => error string |
1090 | */ |
1091 | public function user_config_validation() {} |
1092 | |
1093 | /** |
1094 | * sets the export time config from the moodle form. |
1095 | * you can also use this to set export config that |
1096 | * isn't actually controlled by the user |
1097 | * eg things that your subclasses want to keep in state |
1098 | * across the export. |
1099 | * keys must be in {@link get_allowed_export_config} |
1100 | * |
1101 | * this is deliberately not final (see boxnet plugin) |
1102 | * |
1103 | * @param array $config named array of config items to set. |
1104 | */ |
1105 | public function set_export_config($config) { |
1106 | $allowed = array_merge( |
ffcfd8a7 |
1107 | array('wait', 'hidewait', 'format', 'hideformat'), |
67a87e7d |
1108 | $this->get_allowed_export_config() |
1109 | ); |
1110 | foreach ($config as $key => $value) { |
1111 | if (!in_array($key, $allowed)) { |
34035201 |
1112 | $a = (object)array('property' => $key, 'class' => get_class($this)); |
1113 | throw new portfolio_export_exception($this->get('exporter'), 'invalidexportproperty', 'portfolio', $this->get_return_url(), $a); |
67a87e7d |
1114 | } |
1115 | $this->exportconfig[$key] = $value; |
1116 | } |
1117 | } |
1118 | |
1119 | /** |
1120 | * gets an export time config value. |
1121 | * subclasses should not override this. |
1122 | * |
1123 | * @param string key field to fetch |
1124 | * |
1125 | * @return string config value |
1126 | * |
67a87e7d |
1127 | */ |
1128 | public final function get_export_config($key) { |
1129 | $allowed = array_merge( |
ffcfd8a7 |
1130 | array('hidewait', 'wait', 'format', 'hideformat'), |
67a87e7d |
1131 | $this->get_allowed_export_config() |
1132 | ); |
1133 | if (!in_array($key, $allowed)) { |
34035201 |
1134 | $a = (object)array('property' => $key, 'class' => get_class($this)); |
1135 | throw new portfolio_export_exception($this->get('exporter'), 'invalidexportproperty', 'portfolio', $this->get_return_url(), $a); |
67a87e7d |
1136 | } |
1137 | if (!array_key_exists($key, $this->exportconfig)) { |
34035201 |
1138 | return null; |
67a87e7d |
1139 | } |
1140 | return $this->exportconfig[$key]; |
1141 | } |
1142 | |
1143 | /** |
1144 | * after the user submits their config |
1145 | * they're given a confirm screen |
1146 | * summarising what they've chosen. |
1147 | * |
1148 | * this function should return a table of nice strings => values |
1149 | * of what they've chosen |
1150 | * to be displayed in a table. |
1151 | * |
1152 | * @return array array of config items. |
1153 | */ |
1154 | public function get_export_summary() { |
1155 | return false; |
1156 | } |
1157 | |
1158 | /** |
d67bfc32 |
1159 | * called after the caller has finished having control |
1160 | * of its prepare_package function. |
1161 | * this function should read all the files from the portfolio |
1162 | * working file area and zip them and send them or whatever it wants. |
1163 | * {@see get_tempfiles} to get the list of files. |
67a87e7d |
1164 | * |
67a87e7d |
1165 | */ |
d67bfc32 |
1166 | public abstract function prepare_package(); |
67a87e7d |
1167 | |
1168 | /** |
1169 | * this is the function that is responsible for sending |
1170 | * the package to the remote system, |
1171 | * or whatever request is necessary to initiate the transfer. |
1172 | * |
1173 | * @return boolean success |
1174 | */ |
1175 | public abstract function send_package(); |
1176 | |
1177 | |
1178 | /** |
1179 | * once everything is done and the user |
1180 | * has the finish page displayed to them |
1181 | * the base class takes care of printing them |
1182 | * "return to where you are" or "continue to portfolio" links |
1183 | * this function allows for exta finish options from the plugin |
1184 | * |
1185 | * @return array named array of links => titles |
1186 | */ |
1187 | public function get_extra_finish_options() { |
1188 | return false; |
1189 | } |
1190 | |
1191 | /** |
1192 | * the url for the user to continue to their portfolio |
1193 | * |
1194 | * @return string url or false. |
1195 | */ |
1196 | public abstract function get_continue_url(); |
1197 | |
1198 | /** |
1199 | * mform to display to the user in their profile |
1200 | * if your plugin can't be configured by the user, |
1201 | * (see {@link has_user_config}) |
1202 | * don't bother overriding this function |
1203 | * |
1204 | * @param moodleform $mform passed by reference, add elements to it |
1205 | */ |
1206 | public function user_config_form(&$mform) {} |
1207 | |
1208 | /** |
1209 | * mform to display to the admin configuring the plugin. |
1210 | * if your plugin can't be configured by the admin, |
1211 | * (see {@link} has_admin_config) |
1212 | * don't bother overriding this function |
1213 | * |
1214 | * this function can be called statically or non statically, |
1215 | * depending on whether it's creating a new instance (statically), |
1216 | * or editing an existing one (non statically) |
1217 | * |
1218 | * @param moodleform $mform passed by reference, add elements to it. |
67a87e7d |
1219 | */ |
1220 | public function admin_config_form(&$mform) {} |
1221 | |
1222 | /** |
1223 | * just like the moodle form validation function |
1224 | * this is passed in the data array from the form |
1225 | * and if a non empty array is returned, form processing will stop. |
1226 | * |
1227 | * @param array $data data from form. |
1228 | * @return array keyvalue pairs - form element => error string |
1229 | */ |
d67bfc32 |
1230 | public function admin_config_validation($data) {} |
67a87e7d |
1231 | /** |
1232 | * mform to display to the user exporting data using this plugin. |
1233 | * if your plugin doesn't need user input at this time, |
1234 | * (see {@link has_export_config} |
1235 | * don't bother overrideing this function |
1236 | * |
1237 | * @param moodleform $mform passed by reference, add elements to it. |
1238 | */ |
1239 | public function export_config_form(&$mform) {} |
1240 | |
1241 | /** |
1242 | * override this if your plugin doesn't allow multiple instances |
1243 | * |
1244 | * @return boolean |
1245 | */ |
1246 | public static function allows_multiple() { |
1247 | return true; |
1248 | } |
1249 | |
1250 | /** |
1251 | * |
1252 | * If at any point the caller wants to steal control |
1253 | * it can, by returning something that isn't false |
1254 | * in this function |
1255 | * The controller will redirect to whatever url |
1256 | * this function returns. |
1257 | * Afterwards, you can redirect back to portfolio/add.php?postcontrol=1 |
1258 | * and {@link post_control} is called before the rest of the processing |
1259 | * for the stage is done |
1260 | * |
1261 | * @param int stage to steal control *before* (see constants PARAM_STAGE_*} |
1262 | * |
1263 | * @return boolean or string url |
1264 | */ |
1265 | public function steal_control($stage) { |
1266 | return false; |
1267 | } |
1268 | |
1269 | /** |
1270 | * after a plugin has elected to steal control, |
1271 | * and control returns to portfolio/add.php|postcontrol=1, |
1272 | * this function is called, and passed the stage that was stolen control from |
1273 | * and the request (get and post but not cookie) parameters |
1274 | * this is useful for external systems that need to redirect the user back |
1275 | * with some extra data in the url (like auth tokens etc) |
1276 | * for an example implementation, see boxnet portfolio plugin. |
1277 | * |
1278 | * @param int $stage the stage before control was stolen |
1279 | * @param array $params a merge of $_GET and $_POST |
1280 | * |
1281 | */ |
1282 | |
1283 | public function post_control($stage, $params) { } |
1284 | |
1285 | /** |
1286 | * this function creates a new instance of a plugin |
1287 | * saves it in the database, saves the config |
1288 | * and returns it. |
1289 | * you shouldn't need to override it |
1290 | * unless you're doing something really funky |
1291 | * |
cc8696b2 |
1292 | * @param string $plugin portfolio plugin to create |
1293 | * @param string $name name of new instance |
1294 | * @param array $config what the admin config form returned |
1295 | * |
67a87e7d |
1296 | * @return object subclass of portfolio_plugin_base |
1297 | */ |
1298 | public static function create_instance($plugin, $name, $config) { |
1299 | global $DB, $CFG; |
1300 | $new = (object)array( |
1301 | 'plugin' => $plugin, |
1302 | 'name' => $name, |
1303 | ); |
cc8696b2 |
1304 | if (!portfolio_static_function($plugin, 'allows_multiple')) { |
1305 | // check we don't have one already |
1306 | if ($DB->record_exists('portfolio_instance', array('plugin' => $plugin))) { |
f240dc70 |
1307 | throw new portfolio_exception('multipledisallowed', 'portfolio', '', $plugin); |
cc8696b2 |
1308 | } |
1309 | } |
67a87e7d |
1310 | $newid = $DB->insert_record('portfolio_instance', $new); |
1311 | require_once($CFG->dirroot . '/portfolio/type/' . $plugin . '/lib.php'); |
1312 | $classname = 'portfolio_plugin_' . $plugin; |
1313 | $obj = new $classname($newid); |
1314 | $obj->set_config($config); |
1315 | return $obj; |
1316 | } |
1317 | |
1318 | /** |
1319 | * construct a plugin instance |
1320 | * subclasses should not need to override this unless they're doing something special |
1321 | * and should call parent::__construct afterwards |
1322 | * |
1323 | * @param int $instanceid id of plugin instance to construct |
1324 | * @param mixed $record stdclass object or named array - use this is you already have the record to avoid another query |
1325 | * |
1326 | * @return object subclass of portfolio_plugin_base |
1327 | */ |
1328 | public function __construct($instanceid, $record=null) { |
1329 | global $DB; |
1330 | if (!$record) { |
1331 | if (!$record = $DB->get_record('portfolio_instance', array('id' => $instanceid))) { |
34035201 |
1332 | throw new portfolio_exception('invalidinstance', 'portfolio'); |
67a87e7d |
1333 | } |
1334 | } |
1335 | foreach ((array)$record as $key =>$value) { |
1336 | if (property_exists($this, $key)) { |
1337 | $this->{$key} = $value; |
1338 | } |
1339 | } |
1340 | $this->config = new StdClass; |
1341 | $this->userconfig = array(); |
1342 | $this->exportconfig = array(); |
1343 | foreach ($DB->get_records('portfolio_instance_config', array('instance' => $instanceid)) as $config) { |
1344 | $this->config->{$config->name} = $config->value; |
1345 | } |
1346 | return $this; |
1347 | } |
1348 | |
1349 | /** |
1350 | * a list of fields that can be configured per instance. |
1351 | * this is used for the save handlers of the config form |
1352 | * and as checks in set_config and get_config |
1353 | * |
1354 | * @return array array of strings (config item names) |
1355 | */ |
1356 | public static function get_allowed_config() { |
1357 | return array(); |
1358 | } |
1359 | |
1360 | /** |
1361 | * a list of fields that can be configured by the user. |
1362 | * this is used for the save handlers in the config form |
1363 | * and as checks in set_user_config and get_user_config. |
1364 | * |
1365 | * @return array array of strings (config field names) |
1366 | */ |
1367 | public function get_allowed_user_config() { |
1368 | return array(); |
1369 | } |
1370 | |
1371 | /** |
1372 | * a list of fields that can be configured by the user. |
1373 | * this is used for the save handlers in the config form |
1374 | * and as checks in set_export_config and get_export_config. |
1375 | * |
1376 | * @return array array of strings (config field names) |
1377 | */ |
1378 | public function get_allowed_export_config() { |
1379 | return array(); |
1380 | } |
1381 | |
1382 | /** |
1383 | * saves (or updates) the config stored in portfolio_instance_config. |
1384 | * you shouldn't need to override this unless you're doing something funky. |
1385 | * |
1386 | * @param array $config array of config items. |
1387 | */ |
1388 | public final function set_config($config) { |
1389 | global $DB; |
1390 | foreach ($config as $key => $value) { |
1391 | // try set it in $this first |
23cbde0a |
1392 | try { |
1393 | $this->set($key, $value); |
67a87e7d |
1394 | continue; |
23cbde0a |
1395 | } catch (portfolio_exception $e) { } |
67a87e7d |
1396 | if (!in_array($key, $this->get_allowed_config())) { |
34035201 |
1397 | $a = (object)array('property' => $key, 'class' => get_class($this)); |
1398 | throw new portfolio_export_exception($this->get('exporter'), 'invalidconfigproperty', 'portfolio', null, $a); |
67a87e7d |
1399 | } |
1400 | if (!isset($this->config->{$key})) { |
1401 | $DB->insert_record('portfolio_instance_config', (object)array( |
1402 | 'instance' => $this->id, |
1403 | 'name' => $key, |
1404 | 'value' => $value, |
1405 | )); |
1406 | } else if ($this->config->{$key} != $value) { |
1407 | $DB->set_field('portfolio_instance_config', 'value', $value, array('name' => $key, 'instance' => $this->id)); |
1408 | } |
1409 | $this->config->{$key} = $value; |
1410 | } |
67a87e7d |
1411 | } |
1412 | |
1413 | /** |
1414 | * gets the value of a particular config item |
1415 | * |
1416 | * @param string $key key to fetch |
1417 | * |
1418 | * @return string the corresponding value |
67a87e7d |
1419 | */ |
1420 | public final function get_config($key) { |
1421 | if (!in_array($key, $this->get_allowed_config())) { |
34035201 |
1422 | $a = (object)array('property' => $key, 'class' => get_class($this)); |
1423 | throw new portfolio_export_exception($this->get('exporter'), 'invalidconfigproperty', 'portfolio', null, $a); |
67a87e7d |
1424 | } |
1425 | if (isset($this->config->{$key})) { |
1426 | return $this->config->{$key}; |
1427 | } |
34035201 |
1428 | return null; |
67a87e7d |
1429 | } |
1430 | |
1431 | /** |
1432 | * get the value of a config item for a particular user |
1433 | * |
1434 | * @param string $key key to fetch |
1435 | * @param integer $userid id of user (defaults to current) |
1436 | * |
1437 | * @return string the corresponding value |
1438 | * |
67a87e7d |
1439 | */ |
1440 | public final function get_user_config($key, $userid=0) { |
1441 | global $DB; |
1442 | |
1443 | if (empty($userid)) { |
1444 | $userid = $this->user->id; |
1445 | } |
1446 | |
1447 | if ($key != 'visible') { // handled by the parent class |
1448 | if (!in_array($key, $this->get_allowed_user_config())) { |
34035201 |
1449 | $a = (object)array('property' => $key, 'class' => get_class($this)); |
1450 | throw new portfolio_export_exception($this->get('exporter'), 'invaliduserproperty', 'portfolio', null, $a); |
67a87e7d |
1451 | } |
1452 | } |
1453 | if (!array_key_exists($userid, $this->userconfig)) { |
1454 | $this->userconfig[$userid] = (object)array_fill_keys(array_merge(array('visible'), $this->get_allowed_user_config()), null); |
1455 | foreach ($DB->get_records('portfolio_instance_user', array('instance' => $this->id, 'userid' => $userid)) as $config) { |
1456 | $this->userconfig[$userid]->{$config->name} = $config->value; |
1457 | } |
1458 | } |
1459 | if ($this->userconfig[$userid]->visible === null) { |
1460 | $this->set_user_config(array('visible' => 1), $userid); |
1461 | } |
1462 | return $this->userconfig[$userid]->{$key}; |
1463 | |
1464 | } |
1465 | |
1466 | /** |
1467 | * |
1468 | * sets config options for a given user |
1469 | * |
1470 | * @param mixed $config array or stdclass containing key/value pairs to set |
1471 | * @param integer $userid userid to set config for (defaults to current) |
1472 | * |
67a87e7d |
1473 | */ |
1474 | public final function set_user_config($config, $userid=0) { |
1475 | global $DB; |
1476 | |
1477 | if (empty($userid)) { |
1478 | $userid = $this->user->id; |
1479 | } |
1480 | |
1481 | foreach ($config as $key => $value) { |
1482 | if ($key != 'visible' && !in_array($key, $this->get_allowed_user_config())) { |
34035201 |
1483 | $a = (object)array('property' => $key, 'class' => get_class($this)); |
1484 | throw new portfolio_export_exception($this->get('exporter'), 'invaliduserproperty', 'portfolio', null, $a); |
67a87e7d |
1485 | } |
1486 | if (!$existing = $DB->get_record('portfolio_instance_user', array('instance'=> $this->id, 'userid' => $userid, 'name' => $key))) { |
1487 | $DB->insert_record('portfolio_instance_user', (object)array( |
1488 | 'instance' => $this->id, |
1489 | 'name' => $key, |
1490 | 'value' => $value, |
1491 | 'userid' => $userid, |
1492 | )); |
1493 | } else if ($existing->value != $value) { |
1494 | $DB->set_field('portfolio_instance_user', 'value', $value, array('name' => $key, 'instance' => $this->id, 'userid' => $userid)); |
1495 | } |
1496 | $this->userconfig[$userid]->{$key} = $value; |
1497 | } |
67a87e7d |
1498 | |
1499 | } |
1500 | |
1501 | /** |
1502 | * generic getter for properties belonging to this instance |
1503 | * <b>outside</b> the subclasses |
1504 | * like name, visible etc. |
1505 | * |
67a87e7d |
1506 | */ |
1507 | public final function get($field) { |
1508 | if (property_exists($this, $field)) { |
1509 | return $this->{$field}; |
1510 | } |
34035201 |
1511 | $a = (object)array('property' => $field, 'class' => get_class($this)); |
e3569156 |
1512 | throw new portfolio_export_exception($this->get('exporter'), 'invalidproperty', 'portfolio', null, $a); |
67a87e7d |
1513 | } |
1514 | |
1515 | /** |
1516 | * generic setter for properties belonging to this instance |
1517 | * <b>outside</b> the subclass |
1518 | * like name, visible, etc. |
1519 | * |
67a87e7d |
1520 | */ |
1521 | public final function set($field, $value) { |
1522 | if (property_exists($this, $field)) { |
d67bfc32 |
1523 | $this->{$field} =& $value; |
67a87e7d |
1524 | $this->dirty = true; |
1525 | return true; |
1526 | } |
34035201 |
1527 | $a = (object)array('property' => $field, 'class' => get_class($this)); |
e3569156 |
1528 | throw new portfolio_export_exception($this->get('exporter'), 'invalidproperty', 'portfolio', null, $a); |
67a87e7d |
1529 | |
1530 | } |
1531 | |
1532 | /** |
1533 | * saves stuff that's been stored in the object to the database |
1534 | * you shouldn't need to override this |
1535 | * unless you're doing something really funky. |
1536 | * and if so, call parent::save when you're done. |
1537 | */ |
1538 | public function save() { |
1539 | global $DB; |
1540 | if (!$this->dirty) { |
1541 | return true; |
1542 | } |
1543 | $fordb = new StdClass(); |
1544 | foreach (array('id', 'name', 'plugin', 'visible') as $field) { |
1545 | $fordb->{$field} = $this->{$field}; |
1546 | } |
1547 | $DB->update_record('portfolio_instance', $fordb); |
1548 | $this->dirty = false; |
1549 | return true; |
1550 | } |
1551 | |
1552 | /** |
1553 | * deletes everything from the database about this plugin instance. |
1554 | * you shouldn't need to override this unless you're storing stuff |
1555 | * in your own tables. and if so, call parent::delete when you're done. |
1556 | */ |
1557 | public function delete() { |
1558 | global $DB; |
1559 | $DB->delete_records('portfolio_instance_config', array('instance' => $this->get('id'))); |
1560 | $DB->delete_records('portfolio_instance_user', array('instance' => $this->get('id'))); |
1561 | $DB->delete_records('portfolio_instance', array('id' => $this->get('id'))); |
1562 | $this->dirty = false; |
1563 | return true; |
1564 | } |
83f60058 |
1565 | |
2eb07e79 |
1566 | /** |
1567 | * perform any required cleanup functions |
1568 | */ |
1569 | public function cleanup() { |
1570 | return true; |
1571 | } |
1572 | |
83f60058 |
1573 | public static function mnet_publishes() { |
1574 | return array(); |
1575 | } |
67a87e7d |
1576 | } |
1577 | |
d96a1acc |
1578 | /** |
1579 | * class to inherit from for 'push' type plugins |
1580 | */ |
1581 | abstract class portfolio_plugin_push_base extends portfolio_plugin_base { |
1582 | |
1583 | public function is_push() { |
1584 | return true; |
1585 | } |
1586 | } |
1587 | |
1588 | /** |
1589 | * class to inherit from for 'pull' type plugins |
1590 | */ |
1591 | abstract class portfolio_plugin_pull_base extends portfolio_plugin_base { |
1592 | |
2eb07e79 |
1593 | protected $file; |
d96a1acc |
1594 | |
1595 | public function is_push() { |
1596 | return false; |
1597 | } |
1598 | |
1599 | |
1600 | /** |
1601 | * before sending the file when the pull is requested, verify the request parameters |
1602 | * these might include a token of some sort of whatever |
1603 | * |
1604 | * @param array request parameters (POST wins over GET) |
1605 | */ |
1606 | public abstract function verify_file_request_params($params); |
1607 | |
2eb07e79 |
1608 | /** |
1609 | * called from portfolio/file.php |
1610 | * this function sends the stored file out to the browser |
1611 | * the default is to just use send_stored_file, |
1612 | * but other implementations might do something different |
1613 | * for example, send back the file base64 encoded and encrypted |
1614 | * mahara does this but in the response to an xmlrpc request |
1615 | * rather than through file.php |
1616 | */ |
1617 | public function send_file() { |
1618 | $file = $this->get('file'); |
1619 | if (!($file instanceof stored_file)) { |
1620 | throw new portfolio_export_exception($this->get('exporter'), 'filenotfound', 'portfolio'); |
1621 | } |
1622 | send_stored_file($file, 0, 0, true, null, true); |
1623 | } |
1624 | |
d96a1acc |
1625 | } |
1626 | |
67a87e7d |
1627 | /** |
1628 | * this is the form that is actually used while exporting. |
1629 | * plugins and callers don't get to define their own class |
1630 | * as we have to handle form elements from both places |
1631 | * see the docs for portfolio_plugin_base and portfolio_caller for more information |
1632 | */ |
1633 | final class portfolio_export_form extends moodleform { |
1634 | |
1635 | public function definition() { |
1636 | |
1637 | $mform =& $this->_form; |
1638 | $mform->addElement('hidden', 'stage', PORTFOLIO_STAGE_CONFIG); |
1639 | $mform->addElement('hidden', 'instance', $this->_customdata['instance']->get('id')); |
1640 | |
1641 | if (array_key_exists('formats', $this->_customdata) && is_array($this->_customdata['formats'])) { |
1642 | if (count($this->_customdata['formats']) > 1) { |
1643 | $options = array(); |
1644 | foreach ($this->_customdata['formats'] as $key) { |
1645 | $options[$key] = get_string('format_' . $key, 'portfolio'); |
1646 | } |
1647 | $mform->addElement('select', 'format', get_string('availableformats', 'portfolio'), $options); |
1648 | } else { |
1649 | $f = array_shift($this->_customdata['formats']); |
1650 | $mform->addElement('hidden', 'format', $f); |
1651 | } |
1652 | } |
1653 | |
83f60058 |
1654 | if (array_key_exists('expectedtime', $this->_customdata) && $this->_customdata['expectedtime'] != PORTFOLIO_TIME_LOW && $this->_customdata['expectedtime'] != PORTFOLIO_TIME_FORCEQUEUE) { |
67a87e7d |
1655 | $radioarray = array(); |
1656 | $radioarray[] = &MoodleQuickForm::createElement('radio', 'wait', '', get_string('wait', 'portfolio'), 1); |
1657 | $radioarray[] = &MoodleQuickForm::createElement('radio', 'wait', '', get_string('dontwait', 'portfolio'), 0); |
1658 | $mform->addGroup($radioarray, 'radioar', get_string('wanttowait_' . $this->_customdata['expectedtime'], 'portfolio') , array(' '), false); |
1659 | |
1660 | $mform->setDefault('wait', 0); |
1661 | } |
1662 | else { |
83f60058 |
1663 | if ($this->_customdata['expectedtime'] == PORTFOLIO_TIME_LOW) { |
1664 | $mform->addElement('hidden', 'wait', 1); |
1665 | } else { |
1666 | $mform->addElement('hidden', 'wait', 0); |
1667 | } |
67a87e7d |
1668 | } |
1669 | |
1670 | if (array_key_exists('plugin', $this->_customdata) && is_object($this->_customdata['plugin'])) { |
1671 | $this->_customdata['plugin']->export_config_form($mform, $this->_customdata['userid']); |
1672 | } |
1673 | |
1674 | if (array_key_exists('caller', $this->_customdata) && is_object($this->_customdata['caller'])) { |
1675 | $this->_customdata['caller']->export_config_form($mform, $this->_customdata['instance'], $this->_customdata['userid']); |
1676 | } |
1677 | |
1678 | $this->add_action_buttons(true, get_string('next')); |
1679 | } |
1680 | |
1681 | public function validation($data) { |
1682 | |
1683 | $errors = array(); |
1684 | |
1685 | if (array_key_exists('plugin', $this->_customdata) && is_object($this->_customdata['plugin'])) { |
1686 | $pluginerrors = $this->_customdata['plugin']->export_config_validation($data); |
1687 | if (is_array($pluginerrors)) { |
1688 | $errors = $pluginerrors; |
1689 | } |
1690 | } |
1691 | if (array_key_exists('caller', $this->_customdata) && is_object($this->_customdata['caller'])) { |
1692 | $callererrors = $this->_customdata['caller']->export_config_validation($data); |
1693 | if (is_array($callererrors)) { |
1694 | $errors = array_merge($errors, $callererrors); |
1695 | } |
1696 | } |
1697 | return $errors; |
1698 | } |
1699 | } |
1700 | |
1701 | /** |
1702 | * this form is extendable by plugins |
1703 | * who want the admin to be able to configure |
1704 | * more than just the name of the instance. |
1705 | * this is NOT done by subclassing this class, |
1706 | * see the docs for portfolio_plugin_base for more information |
1707 | */ |
1708 | final class portfolio_admin_form extends moodleform { |
1709 | |
1710 | protected $instance; |
1711 | protected $plugin; |
1712 | |
1713 | public function definition() { |
1714 | global $CFG; |
1715 | $this->plugin = $this->_customdata['plugin']; |
1716 | $this->instance = (isset($this->_customdata['instance']) |
1717 | && is_subclass_of($this->_customdata['instance'], 'portfolio_plugin_base')) |
1718 | ? $this->_customdata['instance'] : null; |
1719 | |
1720 | $mform =& $this->_form; |
1721 | $strrequired = get_string('required'); |
1722 | |
1723 | $mform->addElement('hidden', 'edit', ($this->instance) ? $this->instance->get('id') : 0); |
1724 | $mform->addElement('hidden', 'new', $this->plugin); |
1725 | $mform->addElement('hidden', 'plugin', $this->plugin); |
1726 | |
67a87e7d |
1727 | // let the plugin add the fields they want (either statically or not) |
1728 | if (portfolio_static_function($this->plugin, 'has_admin_config')) { |
1729 | if (!$this->instance) { |
3304eb44 |
1730 | $insane = portfolio_instance_sanity_check($this->instance); |
1731 | portfolio_static_function($this->plugin, 'admin_config_form', $mform); |
67a87e7d |
1732 | } else { |
3304eb44 |
1733 | $insane = portfolio_plugin_sanity_check($this->plugin); |
1734 | $this->instance->admin_config_form($mform); |
67a87e7d |
1735 | } |
1736 | } |
1737 | |
0f71f48b |
1738 | if (isset($insane) && is_array($insane)) { |
3304eb44 |
1739 | $insane = array_shift($insane); |
1740 | } |
1741 | if (isset($insane) && is_string($insane)) { // something went wrong, warn... |
1742 | $mform->addElement('warning', 'insane', null, get_string($insane, 'portfolio_' . $this->plugin)); |
67a87e7d |
1743 | } |
1744 | |
36facf2e |
1745 | $mform->addElement('text', 'name', get_string('name'), 'maxlength="100" size="30"'); |
1746 | $mform->addRule('name', $strrequired, 'required', null, 'client'); |
1747 | |
1748 | |
67a87e7d |
1749 | // and set the data if we have some. |
1750 | if ($this->instance) { |
1751 | $data = array('name' => $this->instance->get('name')); |
1752 | foreach ($this->instance->get_allowed_config() as $config) { |
1753 | $data[$config] = $this->instance->get_config($config); |
1754 | } |
1755 | $this->set_data($data); |
0f71f48b |
1756 | } else { |
1757 | $this->set_data(array('name' => portfolio_static_function($this->plugin, 'get_name'))); |
67a87e7d |
1758 | } |
0f71f48b |
1759 | |
67a87e7d |
1760 | $this->add_action_buttons(true, get_string('save', 'portfolio')); |
1761 | } |
1762 | |
1763 | public function validation($data) { |
1764 | global $DB; |
1765 | |
1766 | $errors = array(); |
1767 | if ($DB->count_records('portfolio_instance', array('name' => $data['name'], 'plugin' => $data['plugin'])) > 1) { |
1768 | $errors = array('name' => get_string('err_uniquename', 'portfolio')); |
1769 | } |
1770 | |
1771 | $pluginerrors = array(); |
1772 | if ($this->instance) { |
1773 | $pluginerrors = $this->instance->admin_config_validation($data); |
1774 | } |
1775 | else { |
1776 | $pluginerrors = portfolio_static_function($this->plugin, 'admin_config_validation', $data); |
1777 | } |
1778 | if (is_array($pluginerrors)) { |
1779 | $errors = array_merge($errors, $pluginerrors); |
1780 | } |
1781 | return $errors; |
1782 | } |
1783 | } |
1784 | |
1785 | /** |
1786 | * this is the form for letting the user configure an instance of a plugin. |
1787 | * in order to extend this, you don't subclass this in the plugin.. |
1788 | * see the docs in portfolio_plugin_base for more information |
1789 | */ |
1790 | final class portfolio_user_form extends moodleform { |
1791 | |
1792 | protected $instance; |
1793 | protected $userid; |
1794 | |
1795 | public function definition() { |
1796 | $this->instance = $this->_customdata['instance']; |
1797 | $this->userid = $this->_customdata['userid']; |
1798 | |
1799 | $this->_form->addElement('hidden', 'config', $this->instance->get('id')); |
1800 | |
1801 | $this->instance->user_config_form($this->_form, $this->userid); |
1802 | |
1803 | $data = array(); |
1804 | foreach ($this->instance->get_allowed_user_config() as $config) { |
1805 | $data[$config] = $this->instance->get_user_config($config, $this->userid); |
1806 | } |
1807 | $this->set_data($data); |
1808 | $this->add_action_buttons(true, get_string('save', 'portfolio')); |
1809 | } |
1810 | |
1811 | public function validation($data) { |
1812 | |
1813 | $errors = $this->instance->user_config_validation($data); |
1814 | |
1815 | } |
1816 | } |
1817 | |
1818 | /** |
1819 | * |
1820 | * Class that handles the various stages of the actual export |
1821 | */ |
77ac22a5 |
1822 | class portfolio_exporter { |
67a87e7d |
1823 | |
15053330 |
1824 | /** |
1825 | * the caller object used during the export |
1826 | */ |
67a87e7d |
1827 | private $caller; |
15053330 |
1828 | |
1829 | /** the portfolio plugin instanced used during the export |
1830 | */ |
67a87e7d |
1831 | private $instance; |
15053330 |
1832 | |
1833 | /** |
1834 | * if there has been no config form displayed to the user |
1835 | */ |
67a87e7d |
1836 | private $noconfig; |
15053330 |
1837 | |
1838 | /** |
1839 | * the navigation to display on the wizard screens |
1840 | * built from build_navigation |
1841 | */ |
67a87e7d |
1842 | private $navigation; |
15053330 |
1843 | |
1844 | /** |
1845 | * the user currently exporting content |
1846 | * always $USER, but more conveniently placed here |
1847 | */ |
67a87e7d |
1848 | private $user; |
1849 | |
15053330 |
1850 | /** the file to include that contains the class defintion |
1851 | * of the portfolio instance plugin |
1852 | * used to re-waken the object after sleep |
1853 | */ |
67a87e7d |
1854 | public $instancefile; |
15053330 |
1855 | |
1856 | /** |
1857 | * the file to include that contains the class definition |
1858 | * of the caller object |
1859 | * used to re-waken the object after sleep |
1860 | */ |
67a87e7d |
1861 | public $callerfile; |
1862 | |
15053330 |
1863 | /** |
1864 | * the current stage of the export |
1865 | */ |
ac6a5492 |
1866 | private $stage; |
1867 | |
15053330 |
1868 | /** |
1869 | * whether something (usually the portfolio plugin) |
1870 | * has forced queuing |
1871 | */ |
f1ebc192 |
1872 | private $forcequeue; |
1873 | |
84a44985 |
1874 | /** |
d67bfc32 |
1875 | * id of this export |
1876 | * matches record in portfolio_tempdata table |
1877 | * and used for itemid for file storage. |
84a44985 |
1878 | */ |
1879 | private $id; |
1880 | |
15053330 |
1881 | /** |
1882 | * the session key during the export |
1883 | * used to avoid hijacking transfers |
1884 | */ |
beb4ac1a |
1885 | private $sesskey; |
1886 | |
15053330 |
1887 | /** |
1888 | * array of stages that have had the portfolio plugin already steal control from them |
1889 | */ |
1890 | private $alreadystolen; |
1891 | |
67a87e7d |
1892 | /** |
1893 | * construct a new exporter for use |
1894 | * |
1895 | * @param portfolio_plugin_base subclass $instance portfolio instance (passed by reference) |
1896 | * @param portfolio_caller_base subclass $caller portfolio caller (passed by reference) |
34035201 |
1897 | * @param string $callerfile path to callerfile (relative to dataroot) |
67a87e7d |
1898 | * @param string $navigation result of build_navigation (passed to print_header) |
1899 | */ |
1900 | public function __construct(&$instance, &$caller, $callerfile, $navigation) { |
1901 | $this->instance =& $instance; |
1902 | $this->caller =& $caller; |
1903 | if ($instance) { |
1904 | $this->instancefile = 'portfolio/type/' . $instance->get('plugin') . '/lib.php'; |
d67bfc32 |
1905 | $this->instance->set('exporter', $this); |
67a87e7d |
1906 | } |
1907 | $this->callerfile = $callerfile; |
1908 | $this->stage = PORTFOLIO_STAGE_CONFIG; |
1909 | $this->navigation = $navigation; |
d67bfc32 |
1910 | $this->caller->set('exporter', $this); |
15053330 |
1911 | $this->alreadystolen = array(); |
67a87e7d |
1912 | } |
1913 | |
1914 | /* |
1915 | * generic getter for properties belonging to this instance |
1916 | * <b>outside</b> the subclasses |
1917 | * like name, visible etc. |
67a87e7d |
1918 | */ |
1919 | public function get($field) { |
1920 | if (property_exists($this, $field)) { |
1921 | return $this->{$field}; |
1922 | } |
34035201 |
1923 | $a = (object)array('property' => $field, 'class' => get_class($this)); |
e3569156 |
1924 | throw new portfolio_export_exception($this, 'invalidproperty', 'portfolio', null, $a); |
67a87e7d |
1925 | } |
1926 | |
1927 | /** |
1928 | * generic setter for properties belonging to this instance |
1929 | * <b>outside</b> the subclass |
1930 | * like name, visible, etc. |
67a87e7d |
1931 | */ |
d67bfc32 |
1932 | public function set($field, &$value) { |
67a87e7d |
1933 | if (property_exists($this, $field)) { |
d67bfc32 |
1934 | $this->{$field} =& $value; |
67a87e7d |
1935 | if ($field == 'instance') { |
1936 | $this->instancefile = 'portfolio/type/' . $this->instance->get('plugin') . '/lib.php'; |
d67bfc32 |
1937 | $this->instance->set('exporter', $this); |
67a87e7d |
1938 | } |
1939 | $this->dirty = true; |
1940 | return true; |
1941 | } |
34035201 |
1942 | $a = (object)array('property' => $field, 'class' => get_class($this)); |
e3569156 |
1943 | throw new portfolio_export_exception($this, 'invalidproperty', 'portfolio', null, $a); |
67a87e7d |
1944 | |
1945 | } |
1946 | /** |
1947 | * process the given stage calling whatever functions are necessary |
1948 | * |
1949 | * @param int $stage (see PORTFOLIO_STAGE_* constants) |
1950 | * @param boolean $alreadystolen used to avoid letting plugins steal control twice. |
1951 | * |
1952 | * @return boolean whether or not to process the next stage. this is important as the function is called recursively. |
1953 | */ |
1954 | public function process_stage($stage, $alreadystolen=false) { |
ac6a5492 |
1955 | $this->set('stage', $stage); |
15053330 |
1956 | if ($alreadystolen) { |
1957 | $this->alreadystolen[$stage] = true; |
1958 | } else { |
1959 | if (!array_key_exists($stage, $this->alreadystolen)) { |
1960 | $this->alreadystolen[$stage] = false; |
1961 | } |
1962 | } |
ac6a5492 |
1963 | $this->save(); |
15053330 |
1964 | if (!$this->alreadystolen[$stage] && $url = $this->instance->steal_control($stage)) { |
67a87e7d |
1965 | redirect($url); |
1966 | break; |
1967 | } |
1968 | |
1969 | $waiting = $this->instance->get_export_config('wait'); |
1970 | if ($stage > PORTFOLIO_STAGE_QUEUEORWAIT && empty($waiting)) { |
1971 | $stage = PORTFOLIO_STAGE_FINISHED; |
1972 | } |
1973 | $functionmap = array( |
1974 | PORTFOLIO_STAGE_CONFIG => 'config', |
1975 | PORTFOLIO_STAGE_CONFIRM => 'confirm', |
1976 | PORTFOLIO_STAGE_QUEUEORWAIT => 'queueorwait', |
1977 | PORTFOLIO_STAGE_PACKAGE => 'package', |
1978 | PORTFOLIO_STAGE_CLEANUP => 'cleanup', |
1979 | PORTFOLIO_STAGE_SEND => 'send', |
1980 | PORTFOLIO_STAGE_FINISHED => 'finished' |
1981 | ); |
1982 | |
1983 | $function = 'process_stage_' . $functionmap[$stage]; |
34035201 |
1984 | try { |
1985 | if ($this->$function()) { |
1986 | // if we get through here it means control was returned |
1987 | // as opposed to wanting to stop processing |
1988 | // eg to wait for user input. |
2eb07e79 |
1989 | $this->save(); |
34035201 |
1990 | $stage++; |
1991 | return $this->process_stage($stage); |
2eb07e79 |
1992 | } else { |
1993 | $this->save(); |
1994 | return false; |
34035201 |
1995 | } |
1996 | } catch (portfolio_caller_exception $e) { |
1997 | portfolio_export_rethrow_exception($this, $e); |
1998 | } catch (portfolio_plugin_exception $e) { |
1999 | portfolio_export_rethrow_exception($this, $e); |
83f60058 |
2000 | } catch (portfolio_export_exception $e) { |
2001 | throw $e; |
34035201 |
2002 | } catch (Exception $e) { |
2003 | debugging(get_string('thirdpartyexception', 'portfolio', get_class($e))); |
2004 | portfolio_export_rethrow_exception($this, $e); |
67a87e7d |
2005 | } |
67a87e7d |
2006 | } |
2007 | |
2008 | /** |
2009 | * helper function to return the portfolio instance |
2010 | * |
2011 | * @return portfolio_plugin_base subclass |
2012 | */ |
2013 | public function instance() { |
2014 | return $this->instance; |
2015 | } |
2016 | |
2017 | /** |
2018 | * helper function to return the caller object |
2019 | * |
2020 | * @return portfolio_caller_base subclass |
2021 | */ |
2022 | public function caller() { |
2023 | return $this->caller; |
2024 | } |
2025 | |
2026 | /** |
2027 | * processes the 'config' stage of the export |
2028 | * |
2029 | * @return boolean whether or not to process the next stage. this is important as the control function is called recursively. |
2030 | */ |
2031 | public function process_stage_config() { |
2032 | |
67a87e7d |
2033 | $pluginobj = $callerobj = null; |
2034 | if ($this->instance->has_export_config()) { |
2035 | $pluginobj = $this->instance; |
2036 | } |
2037 | if ($this->caller->has_export_config()) { |
2038 | $callerobj = $this->caller; |
2039 | } |
349242a3 |
2040 | $formats = portfolio_supported_formats_intersect($this->caller->supported_formats($this->caller), $this->instance->supported_formats()); |
67a87e7d |
2041 | $expectedtime = $this->instance->expected_time($this->caller->expected_time()); |
2042 | if (count($formats) == 0) { |
2043 | // something went wrong, we should not have gotten this far. |
e3569156 |
2044 | throw new portfolio_export_exception($this, 'nocommonformats', 'portfolio', null, get_class($this->caller)); |
67a87e7d |
2045 | } |
2046 | // even if neither plugin or caller wants any config, we have to let the user choose their format, and decide to wait. |
83f60058 |
2047 | if ($pluginobj || $callerobj || count($formats) > 1 || ($expectedtime != PORTFOLIO_TIME_LOW && $expectedtime != PORTFOLIO_TIME_FORCEQUEUE)) { |
67a87e7d |
2048 | $customdata = array( |
2049 | 'instance' => $this->instance, |
2050 | 'plugin' => $pluginobj, |
2051 | 'caller' => $callerobj, |
2052 | 'userid' => $this->user->id, |
2053 | 'formats' => $formats, |
2054 | 'expectedtime' => $expectedtime, |
2055 | ); |
2056 | $mform = new portfolio_export_form('', $customdata); |
2057 | if ($mform->is_cancelled()){ |
84a44985 |
2058 | $this->cancel_request(); |
67a87e7d |
2059 | } else if ($fromform = $mform->get_data()){ |
2060 | if (!confirm_sesskey()) { |
e3569156 |
2061 | throw new portfolio_export_exception($this, 'confirmsesskeybad'); |
67a87e7d |
2062 | } |
2063 | $pluginbits = array(); |
2064 | $callerbits = array(); |
2065 | foreach ($fromform as $key => $value) { |
2066 | if (strpos($key, 'plugin_') === 0) { |
2067 | $pluginbits[substr($key, 7)] = $value; |
2068 | } else if (strpos($key, 'caller_') === 0) { |
2069 | $callerbits[substr($key, 7)] = $value; |
2070 | } |
2071 | } |
2072 | $callerbits['format'] = $pluginbits['format'] = $fromform->format; |
2073 | $pluginbits['wait'] = $fromform->wait; |
294b4928 |
2074 | if ($expectedtime == PORTFOLIO_TIME_LOW) { |
67a87e7d |
2075 | $pluginbits['wait'] = 1; |
2076 | $pluginbits['hidewait'] = 1; |
83f60058 |
2077 | } else if ($expectedtime == PORTFOLIO_TIME_FORCEQUEUE) { |
2078 | $pluginbits['wait'] = 0; |
2079 | $pluginbits['hidewait'] = 1; |
f1ebc192 |
2080 | $this->forcequeue = true; |
67a87e7d |
2081 | } |
ffcfd8a7 |
2082 | $callerbits['hideformat'] = $pluginbits['hideformat'] = (count($formats) == 1); |
67a87e7d |
2083 | $this->caller->set_export_config($callerbits); |
2084 | $this->instance->set_export_config($pluginbits); |
2085 | return true; |
2086 | } else { |
192ce92b |
2087 | $this->print_header('configexport'); |
67a87e7d |
2088 | print_simple_box_start(); |
2089 | $mform->display(); |
2090 | print_simple_box_end(); |
2091 | print_footer(); |
2092 | return false;; |
2093 | } |
2094 | } else { |
2095 | $this->noexportconfig = true; |
ffcfd8a7 |
2096 | $format = array_shift($formats); |
83f60058 |
2097 | $config = array( |
2098 | 'hidewait' => 1, |
2099 | 'wait' => (($expectedtime == PORTFOLIO_TIME_LOW) ? 1 : 0), |
2100 | 'format' => $format, |
2101 | 'hideformat' => 1 |
2102 | ); |
2103 | $this->instance->set_export_config($config); |
ffcfd8a7 |
2104 | $this->caller->set_export_config(array('format' => $format, 'hideformat' => 1)); |
f1ebc192 |
2105 | if ($expectedtime == PORTFOLIO_TIME_FORCEQUEUE) { |
2106 | $this->forcequeue = true; |
2107 | } |
67a87e7d |
2108 | return true; |
2109 | // do not break - fall through to confirm |
2110 | } |
2111 | } |
2112 | |
67a87e7d |
2113 | /** |
2114 | * processes the 'confirm' stage of the export |
2115 | * |
2116 | * @return boolean whether or not to process the next stage. this is important as the control function is called recursively. |
2117 | */ |
2118 | public function process_stage_confirm() { |
ffcfd8a7 |
2119 | global $CFG, $DB; |
2120 | |
2121 | $previous = $DB->get_records( |
2122 | 'portfolio_log', |
2123 | array( |
2124 | 'userid' => $this->user->id, |
2125 | 'portfolio' => $this->instance->get('id'), |
2126 | 'caller_sha1' => $this->caller->get_sha1(), |
2127 | ) |
2128 | ); |
2129 | if (isset($this->noexportconfig) && empty($previous)) { |
67a87e7d |
2130 | return true; |
2131 | } |
2132 | $strconfirm = get_string('confirmexport', 'portfolio'); |
2133 | $yesurl = $CFG->wwwroot . '/portfolio/add.php?stage=' . PORTFOLIO_STAGE_QUEUEORWAIT; |
ffcfd8a7 |
2134 | $nourl = $CFG->wwwroot . '/portfolio/add.php?cancel=1'; |
192ce92b |
2135 | $this->print_header('confirmexport'); |
67a87e7d |
2136 | print_simple_box_start(); |
2137 | print_heading(get_string('confirmsummary', 'portfolio'), '', 4); |
ffcfd8a7 |
2138 | $mainsummary = array(); |
2139 | if (!$this->instance->get_export_config('hideformat')) { |
2140 | $mainsummary[get_string('selectedformat', 'portfolio')] = get_string('format_' . $this->instance->get_export_config('format'), 'portfolio'); |
2141 | } |
67a87e7d |
2142 | if (!$this->instance->get_export_config('hidewait')) { |
ffcfd8a7 |
2143 | $mainsummary[get_string('selectedwait', 'portfolio')] = get_string(($this->instance->get_export_config('wait') ? 'yes' : 'no')); |
2144 | } |
2145 | if ($previous) { |
2146 | $previousstr = ''; |
2147 | foreach ($previous as $row) { |
2148 | $previousstr .= userdate($row->time); |
2149 | if ($row->caller_class != get_class($this->caller)) { |
2150 | require_once($CFG->dirroot . '/' . $row->caller_file); |
2151 | $previousstr .= ' (' . call_user_func(array($row->caller_class, 'display_name')) . ')'; |
2152 | } |
2153 | $previousstr .= '<br />'; |
2154 | } |
2155 | $mainsummary[get_string('exportedpreviously', 'portfolio')] = $previousstr; |
67a87e7d |
2156 | } |
2157 | if (!$csummary = $this->caller->get_export_summary()) { |
2158 | $csummary = array(); |
2159 | } |
2160 | if (!$isummary = $this->instance->get_export_summary()) { |
2161 | $isummary = array(); |
2162 | } |
2163 | $mainsummary = array_merge($mainsummary, $csummary, $isummary); |
ffcfd8a7 |
2164 | $table = new StdClass; |
2165 | $table->data = array(); |
67a87e7d |
2166 | foreach ($mainsummary as $string => $value) { |
ffcfd8a7 |
2167 | $table->data[] = array($string, $value); |
67a87e7d |
2168 | } |
ffcfd8a7 |
2169 | print_table($table); |
67a87e7d |
2170 | notice_yesno($strconfirm, $yesurl, $nourl); |
2171 | print_simple_box_end(); |
2172 | print_footer(); |
2173 | return false; |
2174 | } |
2175 | |
2176 | /** |
2177 | * processes the 'queueornext' stage of the export |
2178 | * |
2179 | * @return boolean whether or not to process the next stage. this is important as the control function is called recursively. |
2180 | */ |
2181 | public function process_stage_queueorwait() { |
2182 | global $SESSION; |
2183 | $wait = $this->instance->get_export_config('wait'); |
2184 | if (empty($wait)) { |
84a44985 |
2185 | events_trigger('portfolio_send', $this->id); |
2186 | unset($SESSION->portfolioexport); |
8f182eef |
2187 | return $this->process_stage_finished(true); |
67a87e7d |
2188 | } |
2189 | return true; |
2190 | } |
2191 | |
2192 | /** |
2193 | * processes the 'package' stage of the export |
2194 | * |
2195 | * @return boolean whether or not to process the next stage. this is important as the control function is called recursively. |
2196 | */ |
2197 | public function process_stage_package() { |
2198 | // now we've agreed on a format, |
2199 | // the caller is given control to package it up however it wants |
2200 | // and then the portfolio plugin is given control to do whatever it wants. |
d67bfc32 |
2201 | if (!$this->caller->prepare_package()) { |
34035201 |
2202 | throw new portfolio_export_exception($this, 'callercouldnotpackage', 'portfolio'); |
67a87e7d |
2203 | } |
d67bfc32 |
2204 | if (!$package = $this->instance->prepare_package()) { |
34035201 |
2205 | throw new portfolio_export_exception($this, 'plugincouldnotpackage', 'portfolio'); |
67a87e7d |
2206 | } |
2207 | return true; |
2208 | } |
2209 | |
2210 | /** |
2211 | * processes the 'cleanup' stage of the export |
2212 | * |
2213 | * @return boolean whether or not to process the next stage. this is important as the control function is called recursively. |
2214 | */ |
d96a1acc |
2215 | public function process_stage_cleanup($pullok=false) { |
84a44985 |
2216 | global $CFG, $DB, $SESSION; |
d96a1acc |
2217 | |
349242a3 |
2218 | if (!$pullok && $this->get('instance') && !$this->get('instance')->is_push()) { |
d96a1acc |
2219 | unset($SESSION->portfolioexport); |
2220 | return true; |
2221 | } |
0f71f48b |
2222 | if ($this->get('instance')) { |
2223 | // might not be set - before export really starts |
2224 | $this->get('instance')->cleanup(); |
2225 | } |
84a44985 |
2226 | $DB->delete_records('portfolio_tempdata', array('id' => $this->id)); |
d67bfc32 |
2227 | $fs = get_file_storage(); |
2228 | $fs->delete_area_files(SYSCONTEXTID, 'portfolio_exporter', $this->id); |
84a44985 |
2229 | unset($SESSION->portfolioexport); |
67a87e7d |
2230 | return true; |
2231 | } |
2232 | |
2233 | /** |
2234 | * processes the 'send' stage of the export |
2235 | * |
2236 | * @return boolean whether or not to process the next stage. this is important as the control function is called recursively. |
2237 | */ |
2238 | public function process_stage_send() { |
2239 | // send the file |
2240 | if (!$this->instance->send_package()) { |
34035201 |
2241 | throw new portfolio_export_exception($this, 'failedtosendpackage', 'portfolio'); |
67a87e7d |
2242 | } |
ffcfd8a7 |
2243 | // log the transfer |
2244 | global $DB; |
2245 | $l = array( |
2246 | 'userid' => $this->user->id, |
2247 | 'portfolio' => $this->instance->get('id'), |
2248 | 'caller_file' => $this->callerfile, |
2249 | 'caller_sha1' => $this->caller->get_sha1(), |
2250 | 'caller_class' => get_class($this->caller), |
2251 | 'time' => time(), |
2252 | ); |
2253 | $DB->insert_record('portfolio_log', $l); |
67a87e7d |
2254 | return true; |
2255 | } |
2256 | |
2257 | /** |
2258 | * processes the 'finish' stage of the export |
2259 | * |
2260 | * @return boolean whether or not to process the next stage. this is important as the control function is called recursively. |
2261 | */ |
8f182eef |
2262 | public function process_stage_finished($queued=false) { |
67a87e7d |
2263 | $returnurl = $this->caller->get_return_url(); |
2264 | $continueurl = $this->instance->get_continue_url(); |
2265 | $extras = $this->instance->get_extra_finish_options(); |
2266 | |
192ce92b |
2267 | $key = 'exportcomplete'; |
8f182eef |
2268 | if ($queued) { |
192ce92b |
2269 | $key = 'exportqueued'; |
f1ebc192 |
2270 | if ($this->forcequeue) { |
2271 | $key = 'exportqueuedforced'; |
2272 | } |
8f182eef |
2273 | } |
192ce92b |
2274 | $this->print_header($key, false); |
67a87e7d |
2275 | if ($returnurl) { |
2276 | echo '<a href="' . $returnurl . '">' . get_string('returntowhereyouwere', 'portfolio') . '</a><br />'; |
2277 | } |
2278 | if ($continueurl) { |
2279 | echo '<a href="' . $continueurl . '">' . get_string('continuetoportfolio', 'portfolio') . '</a><br />'; |
2280 | } |
2281 | if (is_array($extras)) { |
2282 | foreach ($extras as $link => $string) { |
2283 | echo '<a href="' . $link . '">' . $string . '</a><br />'; |
2284 | } |
2285 | } |
2286 | print_footer(); |
67a87e7d |
2287 | return false; |
2288 | } |
2289 | |
2290 | |
2291 | /** |
2292 | * local print header function to be reused across the export |
2293 | * |
2294 | * @param string $titlestring key for a portfolio language string |
2295 | * @param string $headerstring key for a portfolio language string |
2296 | */ |
192ce92b |
2297 | public function print_header($headingstr, $summary=true) { |
2298 | $titlestr = get_string('exporting', 'portfolio'); |
2299 | $headerstr = get_string('exporting', 'portfolio'); |
67a87e7d |
2300 | |
2301 | print_header($titlestr, $headerstr, $this->navigation); |
192ce92b |
2302 | print_heading(get_string($headingstr, 'portfolio')); |
2303 | |
2304 | if (!$summary) { |
2305 | return; |
2306 | } |
2307 | |
2308 | print_simple_box_start(); |
2309 | echo $this->caller->heading_summary(); |
2310 | print_simple_box_end(); |
67a87e7d |
2311 | } |
2312 | |
62a99dea |
2313 | /** |
2314 | * cancels a potfolio request and cleans up the tempdata |
2315 | * and redirects the user back to where they started |
2316 | */ |
84a44985 |
2317 | public function cancel_request() { |
2318 | if (!isset($this)) { |
2319 | return; |
2320 | } |
d96a1acc |
2321 | $this->process_stage_cleanup(true); |
84a44985 |
2322 | redirect($this->caller->get_return_url()); |
2323 | exit; |
2324 | } |
2325 | |
2326 | /** |
2327 | * writes out the contents of this object and all its data to the portfolio_tempdata table and sets the 'id' field. |
2328 | */ |
2329 | public function save() { |
2330 | global $DB; |
2331 | if (empty($this->id)) { |
2332 | $r = (object)array( |
2333 | 'data' => base64_encode(serialize($this)), |
2334 | 'expirytime' => time() + (60*60*24), |
192ce92b |
2335 | 'userid' => $this->user->id, |
84a44985 |
2336 | ); |
2337 | $this->id = $DB->insert_record('portfolio_tempdata', $r); |
d67bfc32 |
2338 | $this->save(); // call again so that id gets added to the save data. |
84a44985 |
2339 | } else { |
2340 | $DB->set_field('portfolio_tempdata', 'data', base64_encode(serialize($this)), array('id' => $this->id)); |
2341 | } |
2342 | } |
2343 | |
62a99dea |
2344 | /** |
2345 | * rewakens the data from the database given the id |
2876d73b |
2346 | * makes sure to load the required files with the class definitions |
62a99dea |
2347 | * |
2348 | * @param int $id id of data |
2349 | * |
2350 | * @return portfolio_exporter |
2351 | */ |
84a44985 |
2352 | public static function rewaken_object($id) { |
2353 | global $DB, $CFG; |
15053330 |
2354 | require_once($CFG->libdir . '/filelib.php'); |
84a44985 |
2355 | if (!$data = $DB->get_record('portfolio_tempdata', array('id' => $id))) { |
34035201 |
2356 | throw new portfolio_exception('invalidtempid', 'portfolio'); |
84a44985 |
2357 | } |
2358 | $exporter = unserialize(base64_decode($data->data)); |
2359 | if ($exporter->instancefile) { |
2360 | require_once($CFG->dirroot . '/' . $exporter->instancefile); |
2361 | } |
2362 | require_once($CFG->dirroot . '/' . $exporter->callerfile); |
2363 | $exporter = unserialize(serialize($exporter)); |
2364 | return $exporter; |
2365 | } |
d67bfc32 |
2366 | |
2367 | /** |
2368 | * copies a file from somewhere else in moodle |
2369 | * to the portfolio temporary working directory |
2370 | * associated with this export |
2371 | * |
2372 | * @param $oldfile stored_file object |
2373 | */ |
2374 | public function copy_existing_file($oldfile) { |
2375 | $fs = get_file_storage(); |
2376 | $file_record = $this->new_file_record_base($oldfile->get_filename()); |
2377 | try { |
2378 | return $fs->create_file_from_storedfile($file_record, $oldfile->get_id()); |
2379 | } catch (file_exception $e) { |
2380 | return false; |
2381 | } |
2382 | } |
2383 | |
2384 | /** |
2385 | * writes out some content to a file in the |
2386 | * portfolio temporary working directory |
2387 | * associated with this export |
2388 | * |
2389 | * @param string $content content to write |
2390 | * @param string $name filename to use |
2391 | */ |
2392 | public function write_new_file($content, $name) { |
2393 | $fs = get_file_storage(); |
2394 | $file_record = $this->new_file_record_base($name); |
2395 | return $fs->create_file_from_string($file_record, $content); |
2396 | } |
2397 | |
2398 | /** |
2399 | * returns an arary of files in the temporary working directory |
2400 | * for this export |
2401 | * always use this instead of the files api directly |
2402 | * |
2403 | * @return arary |
2404 | */ |
2405 | public function get_tempfiles() { |
2406 | $fs = get_file_storage(); |
2407 | $files = $fs->get_area_files(SYSCONTEXTID, 'portfolio_exporter', $this->id, '', false); |
2408 | if (empty($files)) { |
2409 | return array(); |
2410 | } |
d96a1acc |
2411 | $returnfiles = array(); |
2412 | foreach ($files as $f) { |
2413 | $returnfiles[$f->get_filename()] = $f; |
2414 | } |
2415 | return $returnfiles; |
d67bfc32 |
2416 | } |
2417 | |
2418 | /** |
2419 | * helper function to create the beginnings of a file_record object |
2420 | * to create a new file in the portfolio_temporary working directory |
2421 | * use {@see write_new_file} or {@see copy_existing_file} externally |
2422 | * |
2423 | * @param string $name filename of new record |
2424 | */ |
2425 | private function new_file_record_base($name) { |
2426 | return (object)array( |
2427 | 'contextid' => SYSCONTEXTID, |
2428 | 'filearea' => 'portfolio_exporter', |
2429 | 'itemid' => $this->id, |
2430 | 'filepath' => '/', |
2431 | 'filename' => $name, |
2432 | ); |
2433 | } |
2434 | |
beb4ac1a |
2435 | public function verify_rewaken() { |
2436 | global $USER; |
2437 | if ($this->get('user')->id != $USER->id) { |
2438 | throw new portfolio_exception('notyours', 'portfolio'); |
2439 | } |
2440 | if (!confirm_sesskey($this->get('sesskey'))) { |
2441 | throw new portfolio_exception('confirmsesskeybad'); |
2442 | } |
2443 | } |
67a87e7d |
2444 | } |
2445 | |
62a99dea |
2446 | /** |
2447 | * form that just contains the dropdown menu of available instances |
2448 | */ |
6fdd8fa7 |
2449 | class portfolio_instance_select extends moodleform { |
2450 | |
2451 | private $caller; |
2452 | |
2453 | function definition() { |
2454 | $this->caller = $this->_customdata['caller']; |
2455 | $options = portfolio_instance_select( |
2456 | portfolio_instances(), |
349242a3 |
2457 | $this->caller->supported_formats($this->caller), |
6fdd8fa7 |
2458 | get_class($this->caller), |
2459 | 'instance', |
2460 | true, |
2461 | true |
2462 | ); |
2463 | if (empty($options)) { |
34035201 |
2464 | debugging('noavailableplugins', 'portfolio'); |
2465 | return false; |
6fdd8fa7 |
2466 | } |
2467 | $mform =& $this->_form; |
2468 | $mform->addElement('select', 'instance', get_string('selectplugin', 'portfolio'), $options); |
2469 | $this->add_action_buttons(true, get_string('next')); |
2470 | } |
2471 | } |
2472 | |
67a87e7d |
2473 | /** |
2474 | * event handler for the portfolio_send event |
2475 | */ |
2476 | function portfolio_handle_event($eventdata) { |
2477 | global $CFG; |
84a44985 |
2478 | $exporter = portfolio_exporter::rewaken_object($eventdata); |
67a87e7d |
2479 | $exporter->process_stage_package(); |
2480 | $exporter->process_stage_send(); |
d5dfe1b3 |
2481 | $exporter->save(); |
31ca68c4 |
2482 | $exporter->process_stage_cleanup(); |
67a87e7d |
2483 | return true; |
2484 | } |
2485 | |
7e51bbce |
2486 | /** |
2487 | * main portfolio cronjob |
2488 | * |
2489 | */ |
2490 | function portfolio_cron() { |
2491 | global $DB; |
2492 | |
2eb07e79 |
2493 | if ($expired = $DB->get_records_select('portfolio_tempdata', 'expirytime < ?', array(time()), '', 'id')) { |
7e51bbce |
2494 | foreach ($expired as $d) { |
2eb07e79 |
2495 | $e = portfolio_exporter::rewaken_object($d); |
2496 | $e->process_stage_cleanup(true); |
7e51bbce |
2497 | } |
2498 | } |
2eb07e79 |
2499 | // @todo add hooks in the plugins - either per instance or per plugin |
7e51bbce |
2500 | } |
2501 | |
7812e882 |
2502 | /** |
2503 | * this is just used to find an intersection of supported formats |
2504 | * between the caller and portfolio plugins |
2505 | * |
2506 | * the most basic type - pretty much everything is a subtype |
2507 | */ |
2508 | class portfolio_format_file {} |
2509 | |
2510 | /** |
2511 | * this is just used to find an intersection of supported formats |
2512 | * between the caller and portfolio plugins |
2513 | * |
2514 | * added for potential flickr plugin |
2515 | */ |
2516 | class portfolio_format_image extends portfolio_format_file {} |
2517 | |
2518 | /** |
2519 | * this is just used to find an intersection of supported formats |
2520 | * between the caller and portfolio plugins |
2521 | * |
2522 | * in case we want to be really specific. |
2523 | */ |
2524 | class portfolio_format_html extends portfolio_format_file {} |
2525 | |
2526 | /** |
2527 | * this is just used to find an intersection of supported formats |
2528 | * between the caller and portfolio plugins |
2529 | * |
2530 | * later.... a moodle plugin might support this. |
2531 | */ |
2532 | class portfolio_format_mbkp extends portfolio_format_file {} |
7e51bbce |
2533 | |
ebdf8957 |
2534 | /** |
2535 | * top level portfolio exception. |
2536 | * sometimes caught and rethrown as {@see portfolio_export_exception} |
2537 | */ |
2538 | class portfolio_exception extends moodle_exception {} |
2539 | |
34035201 |
2540 | /** |
2541 | * exception to throw during an export - will clean up session and tempdata |
2542 | */ |
2543 | class portfolio_export_exception extends portfolio_exception { |
2544 | |
2545 | /** |
2546 | * constructor. |
2547 | * @param object $exporter instance of portfolio_exporter (will handle null case) |
2548 | * @param string $errorcode language string key |
2549 | * @param string $module language string module (optional, defaults to moodle) |
2550 | * @param string $continue url to continue to (optional, defaults to wwwroot) |
2551 | * @param mixed $a language string data (optional, defaults to null) |
2552 | */ |
2553 | public function __construct($exporter, $errorcode, $module=null, $continue=null, $a=null) { |
2554 | if (!empty($exporter) && $exporter instanceof portfolio_exporter) { |
2555 | if (empty($continue)) { |
2556 | $caller = $exporter->get('caller'); |
2557 | if (!empty($caller) && $caller instanceof portfolio_caller_base) { |
2558 | $continue = $exporter->get('caller')->get_return_url(); |
2559 | } |
2560 | } |
2561 | if (!defined('FULLME') || FULLME != 'cron') { |
2562 | $exporter->process_stage_cleanup(); |
2563 | } |
2564 | } else { |
2565 | global $SESSION; |
2566 | if (!empty($SESSION->portfolioexport)) { |
2567 | debugging(get_string('exportexceptionnoexporter', 'portfolio')); |
2568 | } |
2569 | } |
2570 | parent::__construct($errorcode, $module, $continue, $a); |
2571 | } |
2572 | } |
2573 | |
2574 | /** |
2575 | * exception for callers to throw when they have a problem. |
2576 | * usually caught and rethrown as {@see portfolio_export_exception} |
2577 | */ |
2578 | class portfolio_caller_exception extends portfolio_exception {} |
2579 | |
34035201 |
2580 | /** |
2581 | * exception for portfolio plugins to throw when they have a problem. |
2582 | * usually caught and rethrown as {@see portfolio_export_exception} |
2583 | */ |
2584 | class portfolio_plugin_exception extends portfolio_exception {} |
2585 | |
2586 | /** |
2587 | * helper function to rethrow a caught portfolio_exception as an export exception |
2588 | */ |
2589 | function portfolio_export_rethrow_exception($exporter, $e) { |
2590 | throw new portfolio_export_exception($exporter, $e->errorcode, $e->module, $e->link, $e->a); |
2591 | } |
67a87e7d |
2592 | ?> |