2 // @TODO think about making some more of the functions final.
5 * {@link portfolio_add_button} -entry point for callers
6 * {@link class portfolio_plugin_base} - class plugins extend
7 * {@link class portfolio_caller_base} - class callers extend
8 * {@link class portfolio_admin_form} - base moodleform class for plugin administration
9 * {@link class portfolio_user_form} - base moodleform class for plugin instance user config
10 * {@link class portfolio_export_form} - base moodleform class for during-export configuration (eg metadata)
11 * {@link class portfolio_exporter} - class used during export process
13 * and some helper functions:
14 * {@link portfolio_instances - returns an array of all configured instances
15 * {@link portfolio_instance - returns an instance of the right class given an id
16 * {@link portfolio_instance_select} - returns a drop menu of available instances
17 * {@link portfolio_static_function - requires the file, and calls a static function on the given class
18 " {@link portfolio_plugin_sanity_check - polls given (or all) portfolio_plugins for sanity and disables insane ones
19 " {@link portfolio_instance_sanity_check - polls given (or all) portfolio instances for sanity and disables insane ones
20 * {@link portfolio_report_instane} - returns a table of insane plugins and the reasons (used for plugins or instances thereof)
21 * {@link portfolio_supported_formats - returns array of all available formats for plugins and callers to use
22 * {@link portfolio_handle_event} - event handler for queued transfers that get triggered on cron
25 require_once ($CFG->libdir.'/formslib.php');
27 // **** EXPORT STAGE CONSTANTS **** //
30 * display a form to the user
31 * this one might not be used if neither
32 * the plugin, or the caller has any config.
34 define('PORTFOLIO_STAGE_CONFIG', 1);
37 * summarise the form and ask for confirmation
38 * if we skipped PORTFOLIO_STAGE_CONFIG,
39 * just confirm the send.
41 define('PORTFOLIO_STAGE_CONFIRM', 2);
44 * either queue the event and skip to PORTFOLIO_STAGE_FINISHED
45 * or continue to PORTFOLIO_STAGE_PACKAGE
48 define('PORTFOLIO_STAGE_QUEUEORWAIT', 3);
51 * package up the various bits
52 * during this stage both the caller
53 * and the plugin get their package methods called
55 define('PORTFOLIO_STAGE_PACKAGE', 4);
58 * the portfolio plugin must send the file
60 define('PORTFOLIO_STAGE_SEND', 5);
63 * cleanup the temporary area
65 define('PORTFOLIO_STAGE_CLEANUP', 6);
68 * display the "finished notification"
70 define('PORTFOLIO_STAGE_FINISHED', 7);
74 // **** EXPORT FORMAT CONSTANTS **** //
75 // these should always correspond to a string
76 // in the portfolio module, called format_{$value}
81 * file - the most basic fallback format.
82 * this should always be supported
85 define('PORTFOLIO_FORMAT_FILE', 'file');
88 * moodle backup - the plugin needs to be able to write a complete backup
89 * the caller need to be able to export the particular XML bits to insert
90 * into moodle.xml (?and the file bits if necessary)
92 define('PORTFOLIO_FORMAT_MBKP', 'mbkp');
95 // **** EXPORT TIME LEVELS **** //
96 // these should correspond to a string
97 // in the portfolio module, called time_{$value}
100 * no delay. don't even offer the user the option
101 * of not waiting for the transfer
103 define('PORTFOLIO_TIME_LOW', 'low');
106 * a small delay. user can still easily opt to
107 * watch this transfer and wait.
109 define('PORTFOLIO_TIME_MODERATE', 'moderate');
112 * slow. the user really should not be given the option
115 define('PORTFOLIO_TIME_HIGH', 'high');
119 * entry point to add an 'add to portfolio' button somewhere in moodle
120 * this function does not check permissions. the caller must check permissions first.
121 * later, during the export process, the caller class is instantiated and the check_permissions method is called
122 * but not in this function.
124 * @param string $callbackclass name of the class containing the callback functions
125 * activity modules should ALWAYS use their name_portfolio_caller
126 * other locations must use something unique
127 * @param mixed $callbackargs this can be an array or hash of arguments to pass
128 * back to the callback functions (passed by reference)
129 * these MUST be primatives to be added as hidden form fields.
130 * and the values get cleaned to PARAM_ALPHAEXT or PARAM_NUMBER or PARAM_PATH
131 * @param string $callbackfile this can be autodetected if it's in the same file as your caller,
132 * but more often, the caller is a script.php and the class in a lib.php
133 * so you can pass it here if necessary.
134 * this path should be relative (ie, not include) dirroot
135 * @param boolean $fullform either display the fullform with the dropmenu of available instances
136 * or just a small icon (which will trigger instance selection in a new screen)
137 * optional, defaults to true.
138 * @param boolean $return whether to echo or return content (optional defaults to false (echo)
140 function portfolio_add_button($callbackclass, $callbackargs, $callbackfile=null, $fullform=true, $return=false) {
142 global $SESSION, $CFG, $COURSE, $USER;
144 if (empty($CFG->portfolioenabled)) {
148 if (!$instances = portfolio_instances()) {
152 if (empty($callbackfile)) {
153 $backtrace = debug_backtrace();
154 if (!array_key_exists(0, $backtrace) || !array_key_exists('file', $backtrace[0]) || !is_readable($backtrace[0]['file'])) {
155 debugging(get_string('nocallbackfile', 'portfolio'));
159 $callbackfile = substr($backtrace[0]['file'], strlen($CFG->dirroot));
161 if (!is_readable($CFG->dirroot . $callbackfile)) {
162 debugging(get_string('nocallbackfile', 'portfolio'));
167 require_once($CFG->dirroot . $callbackfile);
169 $callersupports = call_user_func(array($callbackclass, 'supported_formats'));
171 if (isset($SESSION->portfolio)) {
172 return portfolio_exporter::raise_error('alreadyexporting', 'portfolio');
175 $output = '<form method="post" action="' . $CFG->wwwroot . '/portfolio/add.php" id="portfolio-add-button">' . "\n";
176 foreach ($callbackargs as $key => $value) {
177 if (!empty($value) && !is_string($value) && !is_numeric($value)) {
179 $a->value = print_r($value, true);
180 debugging(get_string('nonprimative', 'portfolio', $a));
183 $output .= "\n" . '<input type="hidden" name="ca_' . $key . '" value="' . $value . '" />';
185 $output .= "\n" . '<input type="hidden" name="callbackfile" value="' . $callbackfile . '" />';
186 $output .= "\n" . '<input type="hidden" name="callbackclass" value="' . $callbackclass . '" />';
187 $output .= "\n" . '<input type="hidden" name="course" value="' . (!empty($COURSE) ? $COURSE->id : 0) . '" />';
189 if (count($instances) == 1) {
190 $instance = array_shift($instances);
191 if (count(array_intersect($callersupports, $instance->supported_formats())) == 0) {
192 // bail. no common formats.
193 debugging(get_string('nocommonformats', 'portfolio', $callbackclass));
196 if ($error = portfolio_instance_sanity_check($instance)) {
197 // bail, plugin is misconfigured
198 debugging(get_string('instancemisconfigured', 'portfolio', get_string($error[$instance->get('id')], 'portfolio_' . $instance->get('plugin'))));
201 $output .= "\n" . '<input type="hidden" name="instance" value="' . $instance->get('id') . '" />';
204 $selectoutput = portfolio_instance_select($instances, $callersupports, $callbackclass);
208 $output .= $selectoutput;
209 $output .= "\n" . '<input type="submit" value="' . get_string('addtoportfolio', 'portfolio') .'" />';
211 $output .= "\n" . '<input type="image" src="' . $CFG->pixpath . '/t/portfolio.gif" alt=' . get_string('addtoportfolio', 'portfolio') .'" />';
212 //@todo replace this with a little icon
215 $output .= "\n" . '</form>';
226 * returns a drop menu with a list of available instances.
228 * @param array $instances the instances to put in the menu
229 * @param array $callerformats the formats the caller supports
230 (this is used to filter plugins)
231 * @param array $callbackclass the callback class name
233 * @return string the html, from <select> to </select> inclusive.
235 function portfolio_instance_select($instances, $callerformats, $callbackclass) {
236 $insane = portfolio_instance_sanity_check();
238 $selectoutput = "\n" . '<select name="instance">' . "\n";
239 foreach ($instances as $instance) {
240 if (count(array_intersect($callerformats, $instance->supported_formats())) == 0) {
241 // bail. no common formats.
244 if (array_key_exists($instance->get('id'), $insane)) {
245 // bail, plugin is misconfigured
246 debugging(get_string('instancemisconfigured', 'portfolio', get_string($insane[$instance->get('id')], 'portfolio_' . $instance->get('plugin'))));
250 $selectoutput .= "\n" . '<option value="' . $instance->get('id') . '">' . $instance->get('name') . '</a>' . "\n";
253 // bail. no common formats.
254 debugging(get_string('nocommonformats', 'portfolio', $callbackclass));
257 $selectoutput .= "\n" . "</select>\n";
258 return $selectoutput;
262 * return all portfolio instances
264 * @param boolean visibleonly don't include hidden instances (defaults to true and will be overridden to true if the next parameter is true)
265 * @param boolean useronly check the visibility preferences and permissions of the logged in user
266 * @return array of portfolio instances (full objects, not just database records)
268 function portfolio_instances($visibleonly=true, $useronly=true) {
273 $sql = 'SELECT * FROM {portfolio_instance}';
275 if ($visibleonly || $useronly) {
277 $sql .= ' WHERE visible = ?';
280 $sql .= ' AND id NOT IN (
281 SELECT instance FROM {portfolio_instance_user}
282 WHERE userid = ? AND name = ? AND value = ?
284 $values = array_merge($values, array($USER->id, 'visible', 0));
286 $sql .= ' ORDER BY name';
288 $instances = array();
289 foreach ($DB->get_records_sql($sql, $values) as $instance) {
290 $instances[$instance->id] = portfolio_instance($instance->id, $instance);
292 // @todo check capabilities here - see MDL-15768
297 * supported formats that portfolio plugins and callers
298 * can use for exporting content
300 * @return array of all the available export formats
302 function portfolio_supported_formats() {
304 PORTFOLIO_FORMAT_FILE,
305 /*PORTFOLIO_FORMAT_MBKP, */ // later
306 /*PORTFOLIO_FORMAT_PIOP, */ // also later
311 * helper function to return an instance of a plugin (with config loaded)
313 * @param int $instance id of instance
314 * @param array $record database row that corresponds to this instance
315 * this is passed to avoid unnecessary lookups
317 * @return subclass of portfolio_plugin_base
319 function portfolio_instance($instanceid, $record=null) {
325 if (!$instance = $DB->get_record('portfolio_instance', array('id' => $instanceid))) {
326 return false; // @todo throw exception?
329 require_once($CFG->dirroot . '/portfolio/type/'. $instance->plugin . '/lib.php');
330 $classname = 'portfolio_plugin_' . $instance->plugin;
331 return new $classname($instanceid, $instance);
335 * helper function to call a static function on a portfolio plugin class
336 * this will figure out the classname and require the right file and call the function.
337 * you can send a variable number of arguments to this function after the first two
338 * and they will be passed on to the function you wish to call.
340 * @param string $plugin name of plugin
341 * @param string $function function to call
343 function portfolio_static_function($plugin, $function) {
347 if (is_object($plugin) || is_array($plugin)) {
348 $plugin = (object)$plugin;
349 $pname = $plugin->name;
354 $args = func_get_args();
355 if (count($args) <= 2) {
363 require_once($CFG->dirroot . '/portfolio/type/' . $plugin . '/lib.php');
364 return call_user_func_array(array('portfolio_plugin_' . $plugin, $function), $args);
368 * helper function to check all the plugins for sanity and set any insane ones to invisible.
370 * @param array $plugins to check (if null, defaults to all)
371 * one string will work too for a single plugin.
373 * @return array array of insane instances (keys= id, values = reasons (keys for plugin lang)
375 function portfolio_plugin_sanity_check($plugins=null) {
377 if (is_string($plugins)) {
378 $plugins = array($plugins);
379 } else if (empty($plugins)) {
380 $plugins = get_list_of_plugins('portfolio/type');
384 foreach ($plugins as $plugin) {
385 if ($result = portfolio_static_function($plugin, 'plugin_sanity_check')) {
386 $insane[$plugin] = $result;
389 if (empty($insane)) {
392 list($where, $params) = $DB->get_in_or_equal(array_keys($insane));
393 $where = ' plugin ' . $where;
394 $DB->set_field_select('portfolio_instance', 'visible', 0, $where, $params);
399 * helper function to check all the instances for sanity and set any insane ones to invisible.
401 * @param array $instances to check (if null, defaults to all)
402 * one instance or id will work too
404 * @return array array of insane instances (keys= id, values = reasons (keys for plugin lang)
406 function portfolio_instance_sanity_check($instances=null) {
408 if (empty($instances)) {
409 $instances = portfolio_instances(false);
410 } else if (!is_array($instances)) {
411 $instances = array($instances);
415 foreach ($instances as $instance) {
416 if (is_object($instance) && !($instance instanceof portfolio_plugin_base)) {
417 $instance = portfolio_instance($instance->id, $instance);
418 } else if (is_numeric($instance)) {
419 $instance = portfolio_instance($instance);
421 if (!($instance instanceof portfolio_plugin_base)) {
422 debugging('something weird passed to portfolio_instance_sanity_check, not subclass or id');
425 if ($result = $instance->instance_sanity_check()) {
426 $insane[$instance->get('id')] = $result;
429 if (empty($insane)) {
432 list ($where, $params) = $DB->get_in_or_equal(array_keys($insane));
433 $where = ' id ' . $where;
434 $DB->set_field_select('portfolio_instance', 'visible', 0, $where, $params);
439 * helper function to display a table of plugins (or instances) and reasons for disabling
441 * @param array $insane array of insane plugins (key = plugin (or instance id), value = reason)
442 * @param array $instances if reporting instances rather than whole plugins, pass the array (key = id, value = object) here
445 function portfolio_report_insane($insane, $instances=false, $return=false) {
446 if (empty($insane)) {
451 if (empty($pluginstr)) {
452 $pluginstr = get_string('plugin', 'portfolio');
455 $headerstr = get_string('someinstancesdisabled', 'portfolio');
457 $headerstr = get_string('somepluginsdisabled', 'portfolio');
460 $output = notify($headerstr, 'notifyproblem', 'center', true);
461 $table = new StdClass;
462 $table->head = array($pluginstr, '');
463 $table->data = array();
464 foreach ($insane as $plugin => $reason) {
466 // @todo this isn't working
467 // because it seems the new recordset object
468 // doesn't implement the key correctly.
470 $instance = $instances[$plugin];
471 $plugin = $instance->get('plugin');
472 $name = $instance->get('name');
476 $table->data[] = array($name, get_string($reason, 'portfolio_' . $plugin));
478 $output .= print_table($table, true);
479 $output .= '<br /><br /><br />';
488 * temporary functions until the File API settles
489 * to do with moving files around
491 function temp_portfolio_working_directory($unique) {
492 return make_upload_directory('temp/portfolio/export/' . $unique);
495 function temp_portfolio_usertemp_directory($userid) {
496 return make_upload_directory('userdata/' . $userid . '/temp');
500 *cleans up the working directory
502 function temp_portfolio_cleanup($unique) {
503 $workdir = temp_portfolio_working_directory($unique);
504 return remove_dir($workdir);
509 * base class for the caller
510 * places in moodle that want to display
511 * the add form should subclass this for their callback.
513 abstract class portfolio_caller_base {
517 * course that was active during the caller
522 * named array of export config
523 * use{@link set_export_config} and {@link get_export_config} to access
525 protected $exportconfig;
529 * user currently exporting content
534 * if this caller wants any additional config items
535 * they should be defined here.
537 * @param array $mform moodleform object (passed by reference) to add elements to
538 * @param object $instance subclass of portfolio_plugin_base
539 * @param integer $userid id of user exporting content
541 public function export_config_form(&$mform, $instance) {}
545 * whether this caller wants any additional
546 * config during export (eg options or metadata)
550 public function has_export_config() {
555 * just like the moodle form validation function
556 * this is passed in the data array from the form
557 * and if a non empty array is returned, form processing will stop.
559 * @param array $data data from form.
560 * @return array keyvalue pairs - form element => error string
562 public function export_config_validation($data) {}
565 * how long does this reasonably expect to take..
566 * should we offer the user the option to wait..
567 * this is deliberately nonstatic so it can take filesize into account
568 * the portfolio plugin can override this.
569 * (so for exmaple even if a huge file is being sent,
570 * the download portfolio plugin doesn't care )
572 * @return string (see PORTFOLIO_TIME_* constants)
574 public abstract function expected_time();
577 * used for displaying the navigation during the export screens.
579 * this function must be implemented, but can really return anything.
580 * an Exporting.. string will be added on the end.
581 * @return array of $extranav and $cm
583 * to pass to build_navigation
586 public abstract function get_navigation();
589 * generic getter for properties belonging to this instance
590 * <b>outside</b> the subclasses
591 * like name, visible etc.
593 * @todo determine what to return in the error case
595 public final function get($field) {
596 if (property_exists($this, $field)) {
597 return $this->{$field};
599 return false; // @todo throw exception?
603 * generic setter for properties belonging to this instance
604 * <b>outside</b> the subclass
605 * like name, visible, etc.
607 * @todo determine what to return in the error case
609 public final function set($field, $value) {
610 if (property_exists($this, $field)) {
611 $this->{$field} = $value;
615 return false; // @todo throw exception?
620 * stores the config generated at export time.
621 * subclasses can retrieve values using
622 * {@link get_export_config}
624 * @param array $config formdata
626 public final function set_export_config($config) {
627 $allowed = array_merge(
628 array('wait', 'hidewait', 'format'),
629 $this->get_allowed_export_config()
631 foreach ($config as $key => $value) {
632 if (!in_array($key, $allowed)) {
633 continue; // @ todo throw exception
635 $this->exportconfig[$key] = $value;
640 * returns a particular export config value.
641 * subclasses shouldn't need to override this
643 * @param string key the config item to fetch
644 * @todo figure out the error cases (item not found or not allowed)
646 public final function get_export_config($key) {
647 $allowed = array_merge(
648 array('wait', 'hidewait', 'format'),
649 $this->get_allowed_export_config()
651 if (!in_array($key, $allowed)) {
652 return false; // @todo throw exception?
654 if (!array_key_exists($key, $this->exportconfig)) {
655 return null; // @todo what to return|
657 return $this->exportconfig[$key];
661 * Similar to the other allowed_config functions
662 * if you need export config, you must provide
663 * a list of what the fields are.
665 * even if you want to store stuff during export
666 * without displaying a form to the user,
669 * @return array array of allowed keys
671 public function get_allowed_export_config() {
676 * after the user submits their config
677 * they're given a confirm screen
678 * summarising what they've chosen.
680 * this function should return a table of nice strings => values
681 * of what they've chosen
682 * to be displayed in a table.
684 * @return array array of config items.
686 public function get_export_summary() {
691 * called before the portfolio plugin gets control
692 * this function should copy all the files it wants to
693 * the temporary directory.
695 * @param string $tempdir path to tempdir to put files in
697 public abstract function prepare_package($tempdir);
700 * array of formats this caller supports
701 * the intersection of what this function returns
702 * and what the selected portfolio plugin supports
704 * use the constants PORTFOLIO_FORMAT_*
706 * @return array list of formats
708 public abstract static function supported_formats();
711 * this is the "return to where you were" url
715 public abstract function get_return_url();
718 * callback to do whatever capability checks required
719 * in the caller (called during the export process
721 public abstract function check_permissions();
724 abstract class portfolio_module_caller_base extends portfolio_caller_base {
728 public function get_navigation() {
729 $extranav = array('name' => $this->cm->name, 'link' => $this->get_return_url());
730 return array($extranav, $this->cm);
733 public function get_return_url() {
735 return $CFG->wwwroot . '/mod/' . $this->cm->modname . '/view.php?id=' . $this->cm->id;
740 * the base class for portfolio plguins
741 * all plugins must subclass this.
743 abstract class portfolio_plugin_base {
747 * whether this object needs writing out to the database
765 * plugin this instance belongs to
771 * whether this instance is visible or not
777 * admin configured config
778 * use {@link set_config} and {@get_config} to access
785 * named array of named arrays
786 * keyed on userid and then on config field => value
787 * use {@link get_user_config} and {@link set_user_config} to access.
789 protected $userconfig;
793 * export config during export
794 * use {@link get_export_config} and {@link set export_config} to access.
796 protected $exportconfig;
800 * user currently exporting data
806 * array of formats this portfolio supports
807 * the intersection of what this function returns
808 * and what the caller supports will be used
809 * use the constants PORTFOLIO_FORMAT_*
811 * @return array list of formats
813 public abstract static function supported_formats();
817 * how long does this reasonably expect to take..
818 * should we offer the user the option to wait..
819 * this is deliberately nonstatic so it can take filesize into account
821 * @param string $callertime - what the caller thinks
822 * the portfolio plugin instance
823 * is given the final say
824 * because it might be (for example) download.
825 * @return string (see PORTFOLIO_TIME_* constants)
827 public abstract function expected_time($callertime);
830 * check sanity of plugin
831 * if this function returns something non empty, ALL instances of your plugin
832 * will be set to invisble and not be able to be set back until it's fixed
834 * @return mixed - string = error string KEY (must be inside plugin_$yourplugin) or 0/false if you're ok
836 public static function plugin_sanity_check() {
841 * check sanity of instances
842 * if this function returns something non empty, the instance will be
843 * set to invislbe and not be able to be set back until it's fixed.
845 * @return mixed - string = error string KEY (must be inside plugin_$yourplugin) or 0/false if you're ok
847 public function instance_sanity_check() {
852 * does this plugin need any configuration by the administrator?
854 * if you override this to return true,
855 * you <b>must</b> implement {@link} admin_config_form
857 public static function has_admin_config() {
862 * can this plugin be configured by the user in their profile?
864 * if you override this to return true,
865 * you <b>must</b> implement {@link user_config_form
867 public function has_user_config() {
872 * does this plugin need configuration during export time?
874 * if you override this to return true,
875 * you <b>must</b> implement {@link export_config_form}
877 public function has_export_config() {
882 * just like the moodle form validation function
883 * this is passed in the data array from the form
884 * and if a non empty array is returned, form processing will stop.
886 * @param array $data data from form.
887 * @return array keyvalue pairs - form element => error string
889 public function export_config_validation() {}
892 * just like the moodle form validation function
893 * this is passed in the data array from the form
894 * and if a non empty array is returned, form processing will stop.
896 * @param array $data data from form.
897 * @return array keyvalue pairs - form element => error string
899 public function user_config_validation() {}
902 * sets the export time config from the moodle form.
903 * you can also use this to set export config that
904 * isn't actually controlled by the user
905 * eg things that your subclasses want to keep in state
907 * keys must be in {@link get_allowed_export_config}
909 * this is deliberately not final (see boxnet plugin)
911 * @param array $config named array of config items to set.
913 public function set_export_config($config) {
914 $allowed = array_merge(
915 array('wait', 'format'),
916 $this->get_allowed_export_config()
918 foreach ($config as $key => $value) {
919 if (!in_array($key, $allowed)) {
920 continue; // @ todo throw exception
922 $this->exportconfig[$key] = $value;
927 * gets an export time config value.
928 * subclasses should not override this.
930 * @param string key field to fetch
932 * @return string config value
934 * @todo figure out the error cases
936 public final function get_export_config($key) {
937 $allowed = array_merge(
938 array('wait', 'format'),
939 $this->get_allowed_export_config()
941 if (!in_array($key, $allowed)) {
942 return false; // @todo throw exception?
944 if (!array_key_exists($key, $this->exportconfig)) {
945 return null; // @todo what to return|
947 return $this->exportconfig[$key];
951 * after the user submits their config
952 * they're given a confirm screen
953 * summarising what they've chosen.
955 * this function should return a table of nice strings => values
956 * of what they've chosen
957 * to be displayed in a table.
959 * @return array array of config items.
961 public function get_export_summary() {
966 * called before the portfolio plugin gets control
967 * this function should copy all the files it wants to
968 * the temporary directory.
970 * @param string $tempdir path to temporary directory
972 public abstract function prepare_package($tempdir);
975 * this is the function that is responsible for sending
976 * the package to the remote system,
977 * or whatever request is necessary to initiate the transfer.
979 * @return boolean success
981 public abstract function send_package();
985 * once everything is done and the user
986 * has the finish page displayed to them
987 * the base class takes care of printing them
988 * "return to where you are" or "continue to portfolio" links
989 * this function allows for exta finish options from the plugin
991 * @return array named array of links => titles
993 public function get_extra_finish_options() {
998 * the url for the user to continue to their portfolio
1000 * @return string url or false.
1002 public abstract function get_continue_url();
1005 * mform to display to the user in their profile
1006 * if your plugin can't be configured by the user,
1007 * (see {@link has_user_config})
1008 * don't bother overriding this function
1010 * @param moodleform $mform passed by reference, add elements to it
1012 public function user_config_form(&$mform) {}
1015 * mform to display to the admin configuring the plugin.
1016 * if your plugin can't be configured by the admin,
1017 * (see {@link} has_admin_config)
1018 * don't bother overriding this function
1020 * this function can be called statically or non statically,
1021 * depending on whether it's creating a new instance (statically),
1022 * or editing an existing one (non statically)
1024 * @param moodleform $mform passed by reference, add elements to it.
1025 * @return mixed - if a string is returned, it means the plugin cannot create an instance
1026 * and the string is an error code
1028 public function admin_config_form(&$mform) {}
1031 * just like the moodle form validation function
1032 * this is passed in the data array from the form
1033 * and if a non empty array is returned, form processing will stop.
1035 * @param array $data data from form.
1036 * @return array keyvalue pairs - form element => error string
1038 public static function admin_config_validation($data) {}
1040 * mform to display to the user exporting data using this plugin.
1041 * if your plugin doesn't need user input at this time,
1042 * (see {@link has_export_config}
1043 * don't bother overrideing this function
1045 * @param moodleform $mform passed by reference, add elements to it.
1047 public function export_config_form(&$mform) {}
1050 * override this if your plugin doesn't allow multiple instances
1054 public static function allows_multiple() {
1060 * If at any point the caller wants to steal control
1061 * it can, by returning something that isn't false
1063 * The controller will redirect to whatever url
1064 * this function returns.
1065 * Afterwards, you can redirect back to portfolio/add.php?postcontrol=1
1066 * and {@link post_control} is called before the rest of the processing
1067 * for the stage is done
1069 * @param int stage to steal control *before* (see constants PARAM_STAGE_*}
1071 * @return boolean or string url
1073 public function steal_control($stage) {
1078 * after a plugin has elected to steal control,
1079 * and control returns to portfolio/add.php|postcontrol=1,
1080 * this function is called, and passed the stage that was stolen control from
1081 * and the request (get and post but not cookie) parameters
1082 * this is useful for external systems that need to redirect the user back
1083 * with some extra data in the url (like auth tokens etc)
1084 * for an example implementation, see boxnet portfolio plugin.
1086 * @param int $stage the stage before control was stolen
1087 * @param array $params a merge of $_GET and $_POST
1091 public function post_control($stage, $params) { }
1094 * this function creates a new instance of a plugin
1095 * saves it in the database, saves the config
1097 * you shouldn't need to override it
1098 * unless you're doing something really funky
1100 * @return object subclass of portfolio_plugin_base
1102 public static function create_instance($plugin, $name, $config) {
1104 $new = (object)array(
1105 'plugin' => $plugin,
1108 $newid = $DB->insert_record('portfolio_instance', $new);
1109 require_once($CFG->dirroot . '/portfolio/type/' . $plugin . '/lib.php');
1110 $classname = 'portfolio_plugin_' . $plugin;
1111 $obj = new $classname($newid);
1112 $obj->set_config($config);
1117 * construct a plugin instance
1118 * subclasses should not need to override this unless they're doing something special
1119 * and should call parent::__construct afterwards
1121 * @param int $instanceid id of plugin instance to construct
1122 * @param mixed $record stdclass object or named array - use this is you already have the record to avoid another query
1124 * @return object subclass of portfolio_plugin_base
1126 public function __construct($instanceid, $record=null) {
1129 if (!$record = $DB->get_record('portfolio_instance', array('id' => $instanceid))) {
1130 return false; // @todo throw exception?
1133 foreach ((array)$record as $key =>$value) {
1134 if (property_exists($this, $key)) {
1135 $this->{$key} = $value;
1138 $this->config = new StdClass;
1139 $this->userconfig = array();
1140 $this->exportconfig = array();
1141 foreach ($DB->get_records('portfolio_instance_config', array('instance' => $instanceid)) as $config) {
1142 $this->config->{$config->name} = $config->value;
1148 * a list of fields that can be configured per instance.
1149 * this is used for the save handlers of the config form
1150 * and as checks in set_config and get_config
1152 * @return array array of strings (config item names)
1154 public static function get_allowed_config() {
1159 * a list of fields that can be configured by the user.
1160 * this is used for the save handlers in the config form
1161 * and as checks in set_user_config and get_user_config.
1163 * @return array array of strings (config field names)
1165 public function get_allowed_user_config() {
1170 * a list of fields that can be configured by the user.
1171 * this is used for the save handlers in the config form
1172 * and as checks in set_export_config and get_export_config.
1174 * @return array array of strings (config field names)
1176 public function get_allowed_export_config() {
1181 * saves (or updates) the config stored in portfolio_instance_config.
1182 * you shouldn't need to override this unless you're doing something funky.
1184 * @param array $config array of config items.
1186 public final function set_config($config) {
1188 foreach ($config as $key => $value) {
1189 // try set it in $this first
1190 if ($this->set($key, $value)) {
1193 if (!in_array($key, $this->get_allowed_config())) {
1194 continue; // @todo throw exception?
1196 if (!isset($this->config->{$key})) {
1197 $DB->insert_record('portfolio_instance_config', (object)array(
1198 'instance' => $this->id,
1202 } else if ($this->config->{$key} != $value) {
1203 $DB->set_field('portfolio_instance_config', 'value', $value, array('name' => $key, 'instance' => $this->id));
1205 $this->config->{$key} = $value;
1207 return true; // @todo - if we're going to change here to throw exceptions, this can change
1211 * gets the value of a particular config item
1213 * @param string $key key to fetch
1215 * @return string the corresponding value
1217 * @todo determine what to return in the error case.
1219 public final function get_config($key) {
1220 if (!in_array($key, $this->get_allowed_config())) {
1221 return false; // @todo throw exception?
1223 if (isset($this->config->{$key})) {
1224 return $this->config->{$key};
1226 return false; // @todo null?
1230 * get the value of a config item for a particular user
1232 * @param string $key key to fetch
1233 * @param integer $userid id of user (defaults to current)
1235 * @return string the corresponding value
1237 * @todo determine what to return in the error case
1239 public final function get_user_config($key, $userid=0) {
1242 if (empty($userid)) {
1243 $userid = $this->user->id;
1246 if ($key != 'visible') { // handled by the parent class
1247 if (!in_array($key, $this->get_allowed_user_config())) {
1248 return false; // @todo throw exception?
1251 if (!array_key_exists($userid, $this->userconfig)) {
1252 $this->userconfig[$userid] = (object)array_fill_keys(array_merge(array('visible'), $this->get_allowed_user_config()), null);
1253 foreach ($DB->get_records('portfolio_instance_user', array('instance' => $this->id, 'userid' => $userid)) as $config) {
1254 $this->userconfig[$userid]->{$config->name} = $config->value;
1257 if ($this->userconfig[$userid]->visible === null) {
1258 $this->set_user_config(array('visible' => 1), $userid);
1260 return $this->userconfig[$userid]->{$key};
1266 * sets config options for a given user
1268 * @param mixed $config array or stdclass containing key/value pairs to set
1269 * @param integer $userid userid to set config for (defaults to current)
1271 * @todo determine what to return in the error case
1273 public final function set_user_config($config, $userid=0) {
1276 if (empty($userid)) {
1277 $userid = $this->user->id;
1280 foreach ($config as $key => $value) {
1281 if ($key != 'visible' && !in_array($key, $this->get_allowed_user_config())) {
1282 continue; // @todo throw exception?
1284 if (!$existing = $DB->get_record('portfolio_instance_user', array('instance'=> $this->id, 'userid' => $userid, 'name' => $key))) {
1285 $DB->insert_record('portfolio_instance_user', (object)array(
1286 'instance' => $this->id,
1289 'userid' => $userid,
1291 } else if ($existing->value != $value) {
1292 $DB->set_field('portfolio_instance_user', 'value', $value, array('name' => $key, 'instance' => $this->id, 'userid' => $userid));
1294 $this->userconfig[$userid]->{$key} = $value;
1296 return true; // @todo
1301 * generic getter for properties belonging to this instance
1302 * <b>outside</b> the subclasses
1303 * like name, visible etc.
1305 * @todo determine what to return in the error case
1307 public final function get($field) {
1308 if (property_exists($this, $field)) {
1309 return $this->{$field};
1311 return false; // @todo throw exception?
1315 * generic setter for properties belonging to this instance
1316 * <b>outside</b> the subclass
1317 * like name, visible, etc.
1319 * @todo determine what to return in the error case
1321 public final function set($field, $value) {
1322 if (property_exists($this, $field)) {
1323 $this->{$field} = $value;
1324 $this->dirty = true;
1327 return false; // @todo throw exception?
1332 * saves stuff that's been stored in the object to the database
1333 * you shouldn't need to override this
1334 * unless you're doing something really funky.
1335 * and if so, call parent::save when you're done.
1337 public function save() {
1339 if (!$this->dirty) {
1342 $fordb = new StdClass();
1343 foreach (array('id', 'name', 'plugin', 'visible') as $field) {
1344 $fordb->{$field} = $this->{$field};
1346 $DB->update_record('portfolio_instance', $fordb);
1347 $this->dirty = false;
1352 * deletes everything from the database about this plugin instance.
1353 * you shouldn't need to override this unless you're storing stuff
1354 * in your own tables. and if so, call parent::delete when you're done.
1356 public function delete() {
1358 $DB->delete_records('portfolio_instance_config', array('instance' => $this->get('id')));
1359 $DB->delete_records('portfolio_instance_user', array('instance' => $this->get('id')));
1360 $DB->delete_records('portfolio_instance', array('id' => $this->get('id')));
1361 $this->dirty = false;
1367 * this is the form that is actually used while exporting.
1368 * plugins and callers don't get to define their own class
1369 * as we have to handle form elements from both places
1370 * see the docs for portfolio_plugin_base and portfolio_caller for more information
1372 final class portfolio_export_form extends moodleform {
1374 public function definition() {
1376 $mform =& $this->_form;
1377 $mform->addElement('hidden', 'stage', PORTFOLIO_STAGE_CONFIG);
1378 $mform->addElement('hidden', 'instance', $this->_customdata['instance']->get('id'));
1380 if (array_key_exists('formats', $this->_customdata) && is_array($this->_customdata['formats'])) {
1381 if (count($this->_customdata['formats']) > 1) {
1383 foreach ($this->_customdata['formats'] as $key) {
1384 $options[$key] = get_string('format_' . $key, 'portfolio');
1386 $mform->addElement('select', 'format', get_string('availableformats', 'portfolio'), $options);
1388 $f = array_shift($this->_customdata['formats']);
1389 $mform->addElement('hidden', 'format', $f);
1393 if (array_key_exists('expectedtime', $this->_customdata) && $this->_customdata['expectedtime'] != PORTFOLIO_TIME_LOW) {
1394 //$mform->addElement('select', 'wait', get_string('waitlevel_' . $this->_customdata['expectedtime'], 'portfolio'), $options);
1397 $radioarray = array();
1398 $radioarray[] = &MoodleQuickForm::createElement('radio', 'wait', '', get_string('wait', 'portfolio'), 1);
1399 $radioarray[] = &MoodleQuickForm::createElement('radio', 'wait', '', get_string('dontwait', 'portfolio'), 0);
1400 $mform->addGroup($radioarray, 'radioar', get_string('wanttowait_' . $this->_customdata['expectedtime'], 'portfolio') , array(' '), false);
1402 $mform->setDefault('wait', 0);
1405 $mform->addElement('hidden', 'wait', 1);
1408 if (array_key_exists('plugin', $this->_customdata) && is_object($this->_customdata['plugin'])) {
1409 $this->_customdata['plugin']->export_config_form($mform, $this->_customdata['userid']);
1412 if (array_key_exists('caller', $this->_customdata) && is_object($this->_customdata['caller'])) {
1413 $this->_customdata['caller']->export_config_form($mform, $this->_customdata['instance'], $this->_customdata['userid']);
1416 $this->add_action_buttons(true, get_string('next'));
1419 public function validation($data) {
1423 if (array_key_exists('plugin', $this->_customdata) && is_object($this->_customdata['plugin'])) {
1424 $pluginerrors = $this->_customdata['plugin']->export_config_validation($data);
1425 if (is_array($pluginerrors)) {
1426 $errors = $pluginerrors;
1429 if (array_key_exists('caller', $this->_customdata) && is_object($this->_customdata['caller'])) {
1430 $callererrors = $this->_customdata['caller']->export_config_validation($data);
1431 if (is_array($callererrors)) {
1432 $errors = array_merge($errors, $callererrors);
1440 * this form is extendable by plugins
1441 * who want the admin to be able to configure
1442 * more than just the name of the instance.
1443 * this is NOT done by subclassing this class,
1444 * see the docs for portfolio_plugin_base for more information
1446 final class portfolio_admin_form extends moodleform {
1448 protected $instance;
1451 public function definition() {
1453 $this->plugin = $this->_customdata['plugin'];
1454 $this->instance = (isset($this->_customdata['instance'])
1455 && is_subclass_of($this->_customdata['instance'], 'portfolio_plugin_base'))
1456 ? $this->_customdata['instance'] : null;
1458 $mform =& $this->_form;
1459 $strrequired = get_string('required');
1461 $mform->addElement('hidden', 'edit', ($this->instance) ? $this->instance->get('id') : 0);
1462 $mform->addElement('hidden', 'new', $this->plugin);
1463 $mform->addElement('hidden', 'plugin', $this->plugin);
1465 $mform->addElement('text', 'name', get_string('name'), 'maxlength="100" size="30"');
1466 $mform->addRule('name', $strrequired, 'required', null, 'client');
1468 // let the plugin add the fields they want (either statically or not)
1469 if (portfolio_static_function($this->plugin, 'has_admin_config')) {
1470 if (!$this->instance) {
1471 $result = portfolio_static_function($this->plugin, 'admin_config_form', $mform);
1473 $result = $this->instance->admin_config_form($mform);
1477 if (isset($result) && is_string($result)) { // something went wrong, stop
1478 return $this->raise_error($result, 'portfolio_' . $this->plugin, $CFG->wwwroot . '/' . $CFG->admin . '/portfolio.php');
1481 // and set the data if we have some.
1482 if ($this->instance) {
1483 $data = array('name' => $this->instance->get('name'));
1484 foreach ($this->instance->get_allowed_config() as $config) {
1485 $data[$config] = $this->instance->get_config($config);
1487 $this->set_data($data);
1489 $this->add_action_buttons(true, get_string('save', 'portfolio'));
1492 public function validation($data) {
1496 if ($DB->count_records('portfolio_instance', array('name' => $data['name'], 'plugin' => $data['plugin'])) > 1) {
1497 $errors = array('name' => get_string('err_uniquename', 'portfolio'));
1500 $pluginerrors = array();
1501 if ($this->instance) {
1502 $pluginerrors = $this->instance->admin_config_validation($data);
1505 $pluginerrors = portfolio_static_function($this->plugin, 'admin_config_validation', $data);
1507 if (is_array($pluginerrors)) {
1508 $errors = array_merge($errors, $pluginerrors);
1515 * this is the form for letting the user configure an instance of a plugin.
1516 * in order to extend this, you don't subclass this in the plugin..
1517 * see the docs in portfolio_plugin_base for more information
1519 final class portfolio_user_form extends moodleform {
1521 protected $instance;
1524 public function definition() {
1525 $this->instance = $this->_customdata['instance'];
1526 $this->userid = $this->_customdata['userid'];
1528 $this->_form->addElement('hidden', 'config', $this->instance->get('id'));
1530 $this->instance->user_config_form($this->_form, $this->userid);
1533 foreach ($this->instance->get_allowed_user_config() as $config) {
1534 $data[$config] = $this->instance->get_user_config($config, $this->userid);
1536 $this->set_data($data);
1537 $this->add_action_buttons(true, get_string('save', 'portfolio'));
1540 public function validation($data) {
1542 $errors = $this->instance->user_config_validation($data);
1549 * Class that handles the various stages of the actual export
1551 final class portfolio_exporter {
1553 private $currentstage;
1557 private $navigation;
1562 public $instancefile;
1566 * construct a new exporter for use
1568 * @param portfolio_plugin_base subclass $instance portfolio instance (passed by reference)
1569 * @param portfolio_caller_base subclass $caller portfolio caller (passed by reference)
1570 * @param string $navigation result of build_navigation (passed to print_header)
1572 public function __construct(&$instance, &$caller, $callerfile, $navigation) {
1573 $this->instance =& $instance;
1574 $this->caller =& $caller;
1576 $this->instancefile = 'portfolio/type/' . $instance->get('plugin') . '/lib.php';
1578 $this->callerfile = $callerfile;
1579 $this->stage = PORTFOLIO_STAGE_CONFIG;
1580 $this->navigation = $navigation;
1584 * generic getter for properties belonging to this instance
1585 * <b>outside</b> the subclasses
1586 * like name, visible etc.
1588 * @todo determine what to return in the error case
1590 public function get($field) {
1591 if (property_exists($this, $field)) {
1592 return $this->{$field};
1594 return false; // @todo throw exception?
1598 * generic setter for properties belonging to this instance
1599 * <b>outside</b> the subclass
1600 * like name, visible, etc.
1602 * @todo determine what to return in the error case
1605 public function set($field, $value) {
1606 if (property_exists($this, $field)) {
1607 $this->{$field} = $value;
1608 if ($field == 'instance') {
1609 $this->instancefile = 'portfolio/type/' . $this->instance->get('plugin') . '/lib.php';
1611 $this->dirty = true;
1614 return false; // @todo throw exception?
1618 * process the given stage calling whatever functions are necessary
1620 * @param int $stage (see PORTFOLIO_STAGE_* constants)
1621 * @param boolean $alreadystolen used to avoid letting plugins steal control twice.
1623 * @return boolean whether or not to process the next stage. this is important as the function is called recursively.
1625 public function process_stage($stage, $alreadystolen=false) {
1627 if (!$alreadystolen && $url = $this->instance->steal_control($stage)) {
1628 $SESSION->portfolio->stagepresteal = $stage;
1633 $waiting = $this->instance->get_export_config('wait');
1634 if ($stage > PORTFOLIO_STAGE_QUEUEORWAIT && empty($waiting)) {
1635 $stage = PORTFOLIO_STAGE_FINISHED;
1637 $functionmap = array(
1638 PORTFOLIO_STAGE_CONFIG => 'config',
1639 PORTFOLIO_STAGE_CONFIRM => 'confirm',
1640 PORTFOLIO_STAGE_QUEUEORWAIT => 'queueorwait',
1641 PORTFOLIO_STAGE_PACKAGE => 'package',
1642 PORTFOLIO_STAGE_CLEANUP => 'cleanup',
1643 PORTFOLIO_STAGE_SEND => 'send',
1644 PORTFOLIO_STAGE_FINISHED => 'finished'
1647 $function = 'process_stage_' . $functionmap[$stage];
1648 if ($this->$function()) {
1649 // if we get through here it means control was returned
1650 // as opposed to wanting to stop processing
1651 // eg to wait for user input.
1653 return $this->process_stage($stage);
1659 * helper function to return the portfolio instance
1661 * @return portfolio_plugin_base subclass
1663 public function instance() {
1664 return $this->instance;
1668 * helper function to return the caller object
1670 * @return portfolio_caller_base subclass
1672 public function caller() {
1673 return $this->caller;
1677 * processes the 'config' stage of the export
1679 * @return boolean whether or not to process the next stage. this is important as the control function is called recursively.
1681 public function process_stage_config() {
1685 $pluginobj = $callerobj = null;
1686 if ($this->instance->has_export_config()) {
1687 $pluginobj = $this->instance;
1689 if ($this->caller->has_export_config()) {
1690 $callerobj = $this->caller;
1692 $formats = array_intersect($this->instance->supported_formats(), $this->caller->supported_formats());
1693 $allsupported = portfolio_supported_formats();
1694 foreach ($formats as $key => $format) {
1695 if (!in_array($format, $allsupported)) {
1696 debugging(get_string('invalidformat', 'portfolio', $format));
1697 unset($formats[$key]);
1700 $expectedtime = $this->instance->expected_time($this->caller->expected_time());
1701 if (count($formats) == 0) {
1702 // something went wrong, we should not have gotten this far.
1703 return $this->raise_error('nocommonformats', 'portfolio', get_class($caller));
1705 // even if neither plugin or caller wants any config, we have to let the user choose their format, and decide to wait.
1706 if ($pluginobj || $callerobj || count($formats) > 1 || $expectedtime != PORTFOLIO_TIME_LOW) {
1707 $customdata = array(
1708 'instance' => $this->instance,
1709 'plugin' => $pluginobj,
1710 'caller' => $callerobj,
1711 'userid' => $this->user->id,
1712 'formats' => $formats,
1713 'expectedtime' => $expectedtime,
1715 $mform = new portfolio_export_form('', $customdata);
1716 if ($mform->is_cancelled()){
1717 unset($SESSION->portfolio);
1718 redirect($this->caller->get_return_url());
1720 } else if ($fromform = $mform->get_data()){
1721 if (!confirm_sesskey()) {
1722 return $this->raise_error('confirmsesskeybad', '', $caller->get_return_url());
1724 $pluginbits = array();
1725 $callerbits = array();
1726 foreach ($fromform as $key => $value) {
1727 if (strpos($key, 'plugin_') === 0) {
1728 $pluginbits[substr($key, 7)] = $value;
1729 } else if (strpos($key, 'caller_') === 0) {
1730 $callerbits[substr($key, 7)] = $value;
1733 $callerbits['format'] = $pluginbits['format'] = $fromform->format;
1734 $pluginbits['wait'] = $fromform->wait;
1735 if ($expectedtime = PORTFOLIO_TIME_LOW) {
1736 $pluginbits['wait'] = 1;
1737 $pluginbits['hidewait'] = 1;
1739 $this->caller->set_export_config($callerbits);
1740 $this->instance->set_export_config($pluginbits);
1743 $this->print_header();
1744 print_heading(get_string('configexport' ,'portfolio'));
1745 print_simple_box_start();
1747 print_simple_box_end();
1752 $this->noexportconfig = true;
1753 $this->instance->set_export_config(array('wait' => 1));
1755 // do not break - fall through to confirm
1761 * processes the 'confirm' stage of the export
1763 * @return boolean whether or not to process the next stage. this is important as the control function is called recursively.
1765 public function process_stage_confirm() {
1767 if (isset($this->noexportconfig)) {
1770 $strconfirm = get_string('confirmexport', 'portfolio');
1771 $yesurl = $CFG->wwwroot . '/portfolio/add.php?stage=' . PORTFOLIO_STAGE_QUEUEORWAIT;
1772 $nourl = $this->caller->get_return_url();
1773 $this->print_header();
1774 print_heading($strconfirm);
1775 print_simple_box_start();
1776 print_heading(get_string('confirmsummary', 'portfolio'), '', 4);
1777 $mainsummary = array(
1778 // @todo do something cleverer about wait
1779 get_string('selectedformat', 'portfolio') => get_string('format_' . $this->instance->get_export_config('format'), 'portfolio'),
1781 if (!$this->instance->get_export_config('hidewait')) {
1782 $mainsummary[get_string('selectedwait', 'portfolio')] = get_string($this->instance->get_export_config('wait') ? 'yes' : 'no');
1784 if (!$csummary = $this->caller->get_export_summary()) {
1785 $csummary = array();
1787 if (!$isummary = $this->instance->get_export_summary()) {
1788 $isummary = array();
1790 $mainsummary = array_merge($mainsummary, $csummary, $isummary);
1791 foreach ($mainsummary as $string => $value) {
1792 echo '<b>' . $string . '</b>:' . $value . '<br />' . "\n";
1794 notice_yesno($strconfirm, $yesurl, $nourl);
1795 print_simple_box_end();
1801 * processes the 'queueornext' stage of the export
1803 * @return boolean whether or not to process the next stage. this is important as the control function is called recursively.
1805 public function process_stage_queueorwait() {
1807 $wait = $this->instance->get_export_config('wait');
1809 error_log(print_r(serialize($this), true));
1810 events_trigger('portfolio_send', $this);
1811 unset($SESSION->portfolio);
1812 return $this->process_stage_finished();
1818 * processes the 'package' stage of the export
1820 * @return boolean whether or not to process the next stage. this is important as the control function is called recursively.
1822 public function process_stage_package() {
1823 // now we've agreed on a format,
1824 // the caller is given control to package it up however it wants
1825 // and then the portfolio plugin is given control to do whatever it wants.
1826 $unique = $this->user->id . '-' . time();
1827 $tempdir = temp_portfolio_working_directory($unique);
1828 $this->uniquekey = $unique;
1829 $this->tempdir = $tempdir;
1830 if (!$this->caller->prepare_package($tempdir)) {
1831 return $this->raise_error('callercouldnotpackage', 'portfolio', $this->caller->get_return_url());
1833 if (!$package = $this->instance->prepare_package($tempdir)) {
1834 return $this->raise_error('plugincouldnotpackage', 'portfolio', $this->caller->get_return_url());
1840 * processes the 'cleanup' stage of the export
1842 * @return boolean whether or not to process the next stage. this is important as the control function is called recursively.
1844 public function process_stage_cleanup() {
1846 // @todo this is unpleasant. fix it.
1847 require_once($CFG->dirroot . '/backup/lib.php');
1848 delete_dir_contents($this->tempdir);
1849 // @todo maybe add a hook in the plugin(s)
1854 * processes the 'send' stage of the export
1856 * @return boolean whether or not to process the next stage. this is important as the control function is called recursively.
1858 public function process_stage_send() {
1860 if (!$this->instance->send_package()) {
1861 return $this->raise_error('failedtosendpackage', 'portfolio');
1867 * processes the 'finish' stage of the export
1869 * @return boolean whether or not to process the next stage. this is important as the control function is called recursively.
1871 public function process_stage_finished() {
1873 $returnurl = $this->caller->get_return_url();
1874 $continueurl = $this->instance->get_continue_url();
1875 $extras = $this->instance->get_extra_finish_options();
1877 $this->print_header();
1878 //@todo do something different here if we're queueing.
1879 print_heading(get_string('exportcomplete', 'portfolio'));
1881 echo '<a href="' . $returnurl . '">' . get_string('returntowhereyouwere', 'portfolio') . '</a><br />';
1884 echo '<a href="' . $continueurl . '">' . get_string('continuetoportfolio', 'portfolio') . '</a><br />';
1886 if (is_array($extras)) {
1887 foreach ($extras as $link => $string) {
1888 echo '<a href="' . $link . '">' . $string . '</a><br />';
1892 unset($SESSION->portfolio);
1898 * local print header function to be reused across the export
1900 * @param string $titlestring key for a portfolio language string
1901 * @param string $headerstring key for a portfolio language string
1903 public function print_header($titlestr='exporting', $headerstr='exporting') {
1904 $titlestr = get_string($titlestr, 'portfolio');
1905 $headerstr = get_string($headerstr, 'portfolio');
1907 print_header($titlestr, $headerstr, $this->navigation);
1911 * error handler - decides whether we're running interactively or not
1912 * and behaves accordingly
1914 public static function raise_error($string, $module, $continue=null) {
1915 if (defined('FULLME') && FULLME == 'cron') {
1916 debugging(get_string($string, $module));
1920 unset($SESSION->portfolio);
1921 print_error($string, $module, $continue);
1926 * event handler for the portfolio_send event
1928 function portfolio_handle_event($eventdata) {
1930 require_once($CFG->dirroot . '/' . $eventdata->instancefile);
1931 require_once($CFG->dirroot . '/' . $eventdata->callerfile);
1932 $exporter = unserialize(serialize($eventdata));
1933 $exporter->process_stage_package();
1934 $exporter->process_stage_send();
1935 $exporter->process_stage_cleanup();