67a87e7d |
1 | <?php |
2 | require_once(dirname(dirname(__FILE__)) . '/config.php'); |
a239f01e |
3 | |
4 | if (empty($CFG->portfolioenabled)) { |
5 | print_error('disabled', 'portfolio'); |
6 | } |
7 | |
67a87e7d |
8 | require_once($CFG->libdir . '/portfoliolib.php'); |
6fdd8fa7 |
9 | require_once($CFG->libdir . '/formslib.php'); |
67a87e7d |
10 | $exporter = null; |
84a44985 |
11 | $dataid = 0; |
12 | |
beb4ac1a |
13 | if (!$dataid = optional_param('id', '', PARAM_INT) ) { |
84a44985 |
14 | if (isset($SESSION->portfolioexport)) { |
15 | $dataid = $SESSION->portfolioexport; |
67a87e7d |
16 | } |
84a44985 |
17 | } |
18 | if ($dataid) { |
19 | $exporter = portfolio_exporter::rewaken_object($dataid); |
beb4ac1a |
20 | $exporter->verify_rewaken(); |
6fdd8fa7 |
21 | if ($cancel = optional_param('cancel', 0, PARAM_RAW)) { |
84a44985 |
22 | $exporter->cancel_request(); |
67a87e7d |
23 | } |
6fdd8fa7 |
24 | if (!$exporter->get('instance')) { |
25 | if ($instance = optional_param('instance', '', PARAM_INT)) { |
34035201 |
26 | try { |
27 | $instance = portfolio_instance($instance); |
28 | } catch (portfolio_exception $e) { |
29 | portfolio_export_rethrow_exception($exporter, $e); |
6fdd8fa7 |
30 | } |
31 | if ($broken = portfolio_instance_sanity_check($instance)) { |
32 | print_error(get_string($broken[$instance->get('id')], 'portfolio_' . $instance->get('plugin'))); |
33 | } |
34 | $instance->set('user', $USER); |
35 | $exporter->set('instance', $instance); |
beb4ac1a |
36 | $exporter->set('sesskey', sesskey()); |
d67bfc32 |
37 | $exporter->save(); |
6fdd8fa7 |
38 | } |
39 | } |
67a87e7d |
40 | } else { |
41 | // we'e just posted here for the first time and have might the instance already |
42 | if ($instance = optional_param('instance', 0, PARAM_INT)) { |
34035201 |
43 | try { |
44 | $instance = portfolio_instance($instance); |
45 | } catch (portfolio_exception $e) { |
46 | portfolio_export_rethrow_exception($exporter, $e); |
9eb0a772 |
47 | } |
67a87e7d |
48 | if ($broken = portfolio_instance_sanity_check($instance)) { |
34035201 |
49 | throw new portfolio_exception($broken[$instance->get('id')], 'portfolio_' . $instance->get('plugin')); |
67a87e7d |
50 | } |
51 | $instance->set('user', $USER); |
52 | } else { |
53 | $instance = null; |
54 | } |
55 | |
56 | $callbackfile = required_param('callbackfile', PARAM_PATH); |
57 | $callbackclass = required_param('callbackclass', PARAM_ALPHAEXT); |
58 | |
59 | $callbackargs = array(); |
9eb0a772 |
60 | foreach (array_keys(array_merge($_GET, $_POST)) as $key) { |
67a87e7d |
61 | if (strpos($key, 'ca_') === 0) { |
62 | if (!$value = optional_param($key, false, PARAM_ALPHAEXT)) { |
63 | if (!$value = optional_param($key, false, PARAM_NUMBER)) { |
64 | $value = optional_param($key, false, PARAM_PATH); |
65 | } |
66 | } |
67 | $callbackargs[substr($key, 3)] = $value; |
68 | } |
69 | } |
70 | require_once($CFG->dirroot . $callbackfile); |
71 | $caller = new $callbackclass($callbackargs); |
72 | if (!$caller->check_permissions()) { |
73 | print_error('nopermissions', 'portfolio', $caller->get_return_url()); |
74 | } |
75 | $caller->set('user', $USER); |
76 | |
77 | // for build navigation |
78 | if (!$course = $caller->get('course')) { |
79 | $course = optional_param('course', 0, PARAM_INT); |
80 | } |
81 | |
9eb0a772 |
82 | if (!empty($course) && is_numeric($course)) { |
9eb0a772 |
83 | $course = $DB->get_record('course', array('id' => $course), 'id,shortname,fullname'); |
67a87e7d |
84 | // this is yuk but used in build_navigation |
85 | } |
86 | |
9eb0a772 |
87 | $COURSE = $course; |
88 | |
67a87e7d |
89 | list($extranav, $cm) = $caller->get_navigation(); |
90 | $extranav[] = array('type' => 'title', 'name' => get_string('exporting', 'portfolio')); |
91 | $navigation = build_navigation($extranav, $cm); |
92 | |
93 | $exporter = new portfolio_exporter($instance, $caller, $callbackfile, $navigation); |
94 | $exporter->set('user', $USER); |
84a44985 |
95 | $exporter->save(); |
96 | $SESSION->portfolioexport = $exporter->get('id'); |
67a87e7d |
97 | } |
98 | |
67a87e7d |
99 | if (!$exporter->get('instance')) { |
100 | // we've just arrived but have no instance |
101 | // so retrieve everything from the request, |
102 | // add them as hidden fields in a new form |
103 | // to select the instance and post back here again |
104 | // for the next block to catch |
6fdd8fa7 |
105 | $mform = new portfolio_instance_select('', array('caller' => $exporter->get('caller'))); |
106 | if ($mform->is_cancelled()) { |
84a44985 |
107 | $exporter->cancel_request(); |
6fdd8fa7 |
108 | } else if ($fromform = $mform->get_data()){ |
84a44985 |
109 | redirect($CFG->wwwroot . '/portfolio/add.php?instance=' . $fromform->instance . '&id=' . $exporter->get('id')); |
6fdd8fa7 |
110 | exit; |
111 | } |
112 | else { |
349242a3 |
113 | $exporter->print_header('selectplugin'); |
6fdd8fa7 |
114 | print_simple_box_start(); |
115 | $mform->display(); |
116 | print_simple_box_end(); |
117 | print_footer(); |
118 | exit; |
67a87e7d |
119 | } |
67a87e7d |
120 | } |
121 | |
ac6a5492 |
122 | if (!$stage = optional_param('stage', PORTFOLIO_STAGE_CONFIG)) { |
123 | $stage = $exporter->get('stage'); |
124 | } |
125 | |
d67bfc32 |
126 | $alreadystolen = false; |
127 | // for places returning control to pass (rather than PORTFOLIO_STAGE_PACKAGE |
128 | // which is unstable if they can't get to the constant (eg external system) |
129 | if ($postcontrol = optional_param('postcontrol', 0, PARAM_INT)) { |
34035201 |
130 | try { |
131 | $exporter->instance()->post_control($stage, array_merge($_GET, $_POST)); |
132 | } catch (portfolio_plugin_exception $e) { |
133 | portfolio_export_rethrow_exception($exporter, $e); |
134 | } |
d67bfc32 |
135 | $alreadystolen = true; |
136 | } |
ac6a5492 |
137 | |
67a87e7d |
138 | $exporter->process_stage($stage, $alreadystolen); |
139 | |
140 | ?> |