67a87e7d |
1 | <?php |
4c7a4ef9 |
2 | /** |
3 | * Moodle - Modular Object-Oriented Dynamic Learning Environment |
4 | * http://moodle.org |
5 | * Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com |
6 | * |
7 | * This program is free software: you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by |
9 | * the Free Software Foundation, either version 2 of the License, or |
10 | * (at your option) any later version. |
11 | * |
12 | * This program is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * GNU General Public License for more details. |
16 | * |
17 | * You should have received a copy of the GNU General Public License |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | * |
20 | * @package moodle |
21 | * @subpackage portfolio |
22 | * @author Penny Leach <penny@catalyst.net.nz> |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL |
24 | * @copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com |
25 | * |
26 | * This file is the main controller to do with the portfolio export wizard. |
27 | */ |
67a87e7d |
28 | require_once(dirname(dirname(__FILE__)) . '/config.php'); |
a239f01e |
29 | |
90658eef |
30 | if (empty($CFG->enableportfolios)) { |
a239f01e |
31 | print_error('disabled', 'portfolio'); |
32 | } |
33 | |
4c7a4ef9 |
34 | // this will pull in all the other required libraries |
67a87e7d |
35 | require_once($CFG->libdir . '/portfoliolib.php'); |
4c7a4ef9 |
36 | // so plugins don't have to. |
6fdd8fa7 |
37 | require_once($CFG->libdir . '/formslib.php'); |
50128512 |
38 | |
4c7a4ef9 |
39 | $cancel = optional_param('cancel', 0, PARAM_RAW); // user has cancelled the request |
40 | $dataid = optional_param('id', 0, PARAM_INT); // id of partially completed export (in session, everything else in portfolio_tempdata |
41 | $instanceid = optional_param('instance', 0, PARAM_INT); // instanceof of configured portfolio plugin |
42 | $courseid = optional_param('course', 0, PARAM_INT); // courseid the data being exported belongs to (caller object should provide this later) |
43 | $stage = optional_param('stage', PORTFOLIO_STAGE_CONFIG, PARAM_INT); // stage of the export we're at (stored in the exporter) |
44 | $postcontrol = optional_param('postcontrol', 0, PARAM_INT); // when returning from some bounce to an external system, this gets passed |
45 | $callbackfile = optional_param('callbackfile', null, PARAM_PATH); // callback file eg /mod/forum/lib.php - the location of the exporting content |
46 | $callbackclass = optional_param('callbackclass', null, PARAM_ALPHAEXT); // callback class eg forum_portfolio_caller - the class to handle the exporting content. |
50128512 |
47 | |
4c7a4ef9 |
48 | require_login(); // this is selectively called again with $course later when we know for sure which one we're in. |
98335b2d |
49 | |
67a87e7d |
50 | $exporter = null; |
84a44985 |
51 | |
4c7a4ef9 |
52 | // try and find a partial export id in the session if it's not passed explicitly |
53 | if (empty($dataid)) { |
84a44985 |
54 | if (isset($SESSION->portfolioexport)) { |
55 | $dataid = $SESSION->portfolioexport; |
67a87e7d |
56 | } |
84a44985 |
57 | } |
9f3ef223 |
58 | |
4c7a4ef9 |
59 | // if we have a dataid, it means we're in the middle of an export, |
60 | // so rewaken it and continue. |
61 | if (!empty($dataid)) { |
50128512 |
62 | try { |
63 | $exporter = portfolio_exporter::rewaken_object($dataid); |
64 | } catch (portfolio_exception $e) { |
9f3ef223 |
65 | // this can happen in some cases, a cancel request is sent when something is already broken |
66 | // so process it elegantly and move on. |
50128512 |
67 | if ($cancel) { |
68 | unset($SESSION->portfolioexport); |
69 | redirect($CFG->wwwroot); |
70 | } else { |
c737eed8 |
71 | portfolio_exporter::print_expired_export(); |
50128512 |
72 | } |
73 | } |
4c7a4ef9 |
74 | // we have to wake it up first before we can cancel it |
75 | // so temporary directories etc get cleaned up. |
50128512 |
76 | if ($cancel) { |
84a44985 |
77 | $exporter->cancel_request(); |
67a87e7d |
78 | } |
9f3ef223 |
79 | // verify we still belong to the correct user and session |
50128512 |
80 | $exporter->verify_rewaken(); |
4c7a4ef9 |
81 | // if we don't have an instanceid in the exporter |
82 | // it means we've just posted from the 'choose portfolio instance' page |
83 | // so process that and start up the portfolio plugin |
6fdd8fa7 |
84 | if (!$exporter->get('instance')) { |
4c7a4ef9 |
85 | if ($instanceid) { |
34035201 |
86 | try { |
4c7a4ef9 |
87 | $instance = portfolio_instance($instanceid); |
34035201 |
88 | } catch (portfolio_exception $e) { |
89 | portfolio_export_rethrow_exception($exporter, $e); |
6fdd8fa7 |
90 | } |
4c7a4ef9 |
91 | // this technically shouldn't happen but make sure anyway |
6fdd8fa7 |
92 | if ($broken = portfolio_instance_sanity_check($instance)) { |
9f3ef223 |
93 | throw new portfolio_export_exception($exporter, $broken[$instance->get('id')], 'portfolio_' . $instance->get('plugin')); |
6fdd8fa7 |
94 | } |
4c7a4ef9 |
95 | // now we're all set up, ready to go |
6fdd8fa7 |
96 | $instance->set('user', $USER); |
97 | $exporter->set('instance', $instance); |
d67bfc32 |
98 | $exporter->save(); |
6fdd8fa7 |
99 | } |
100 | } |
4c7a4ef9 |
101 | // completely new request, look to see what information we've been passed and set up the exporter object. |
67a87e7d |
102 | } else { |
4c7a4ef9 |
103 | // you cannot get here with no information for us, we must at least have the caller. |
c737eed8 |
104 | if (empty($_GET) && empty($_POST)) { |
105 | portfolio_exporter::print_expired_export(); |
106 | } |
67a87e7d |
107 | // we'e just posted here for the first time and have might the instance already |
4c7a4ef9 |
108 | if ($instanceid) { |
9f3ef223 |
109 | // this can throw exceptions but there's no point catching and rethrowing here |
110 | // as the exporter isn't created yet. |
4c7a4ef9 |
111 | $instance = portfolio_instance($instanceid); |
67a87e7d |
112 | if ($broken = portfolio_instance_sanity_check($instance)) { |
34035201 |
113 | throw new portfolio_exception($broken[$instance->get('id')], 'portfolio_' . $instance->get('plugin')); |
67a87e7d |
114 | } |
115 | $instance->set('user', $USER); |
116 | } else { |
117 | $instance = null; |
118 | } |
119 | |
4c7a4ef9 |
120 | // we must be passed this from the caller, we cannot start a new export |
121 | // without knowing information about what part of moodle we come from. |
c737eed8 |
122 | if (empty($callbackfile) || empty($callbackclass)) { |
123 | portfolio_exporter::print_expired_export(); |
124 | } |
67a87e7d |
125 | |
4c7a4ef9 |
126 | // so each place in moodle can pass callback args here |
127 | // process the entire request looking for ca_* |
128 | // be as lenient as possible while still being secure |
129 | // so only accept certain parameter types. |
67a87e7d |
130 | $callbackargs = array(); |
9eb0a772 |
131 | foreach (array_keys(array_merge($_GET, $_POST)) as $key) { |
67a87e7d |
132 | if (strpos($key, 'ca_') === 0) { |
133 | if (!$value = optional_param($key, false, PARAM_ALPHAEXT)) { |
134 | if (!$value = optional_param($key, false, PARAM_NUMBER)) { |
135 | $value = optional_param($key, false, PARAM_PATH); |
136 | } |
137 | } |
4c7a4ef9 |
138 | // strip off ca_ for niceness |
67a87e7d |
139 | $callbackargs[substr($key, 3)] = $value; |
140 | } |
141 | } |
4c7a4ef9 |
142 | // righto, now we have the callback args set up |
143 | // load up the caller file and class and tell it to set up all the data |
144 | // it needs |
67a87e7d |
145 | require_once($CFG->dirroot . $callbackfile); |
146 | $caller = new $callbackclass($callbackargs); |
4dc67f1e |
147 | $caller->set('user', $USER); |
0d06b6fd |
148 | $caller->load_data(); |
4c7a4ef9 |
149 | // this must check capabilities and either throw an exception or return false. |
67a87e7d |
150 | if (!$caller->check_permissions()) { |
9f3ef223 |
151 | throw new portfolio_caller_exception('nopermissions', 'portfolio', $caller->get_return_url()); |
67a87e7d |
152 | } |
67a87e7d |
153 | |
154 | // for build navigation |
155 | if (!$course = $caller->get('course')) { |
4c7a4ef9 |
156 | $course = $courseid; |
67a87e7d |
157 | } |
158 | |
4c7a4ef9 |
159 | // set up the course so that build_navigation works nice |
9e7d38b1 |
160 | $PAGE->set_course($course); |
67a87e7d |
161 | |
4c7a4ef9 |
162 | // and now we know the course for sure, call require_login with it |
163 | require_login($course); |
9eb0a772 |
164 | |
67a87e7d |
165 | list($extranav, $cm) = $caller->get_navigation(); |
166 | $extranav[] = array('type' => 'title', 'name' => get_string('exporting', 'portfolio')); |
167 | $navigation = build_navigation($extranav, $cm); |
168 | |
4c7a4ef9 |
169 | // finally! set up the exporter object with the portfolio instance, caller information, and navigation elements |
67a87e7d |
170 | $exporter = new portfolio_exporter($instance, $caller, $callbackfile, $navigation); |
4c7a4ef9 |
171 | |
172 | // set the export-specific variables, and save. |
67a87e7d |
173 | $exporter->set('user', $USER); |
50128512 |
174 | $exporter->set('sesskey', sesskey()); |
84a44985 |
175 | $exporter->save(); |
4c7a4ef9 |
176 | |
177 | // and finally, put it in the session for waking up again later. |
84a44985 |
178 | $SESSION->portfolioexport = $exporter->get('id'); |
67a87e7d |
179 | } |
180 | |
67a87e7d |
181 | if (!$exporter->get('instance')) { |
182 | // we've just arrived but have no instance |
4c7a4ef9 |
183 | // in this case the exporter object and the caller object have been set up above |
184 | // so just make a little form to select the portfolio plugin instance, |
185 | // which is the last thing to do before starting the export. |
6fdd8fa7 |
186 | $mform = new portfolio_instance_select('', array('caller' => $exporter->get('caller'))); |
187 | if ($mform->is_cancelled()) { |
84a44985 |
188 | $exporter->cancel_request(); |
6fdd8fa7 |
189 | } else if ($fromform = $mform->get_data()){ |
84a44985 |
190 | redirect($CFG->wwwroot . '/portfolio/add.php?instance=' . $fromform->instance . '&id=' . $exporter->get('id')); |
6fdd8fa7 |
191 | exit; |
192 | } |
193 | else { |
349242a3 |
194 | $exporter->print_header('selectplugin'); |
6fdd8fa7 |
195 | print_simple_box_start(); |
196 | $mform->display(); |
197 | print_simple_box_end(); |
198 | print_footer(); |
199 | exit; |
67a87e7d |
200 | } |
67a87e7d |
201 | } |
202 | |
4c7a4ef9 |
203 | // if we haven't been passed &stage= grab it from the exporter. |
204 | if (!$stage) { |
ac6a5492 |
205 | $stage = $exporter->get('stage'); |
206 | } |
207 | |
d67bfc32 |
208 | // for places returning control to pass (rather than PORTFOLIO_STAGE_PACKAGE |
209 | // which is unstable if they can't get to the constant (eg external system) |
4c7a4ef9 |
210 | $alreadystolen = false; |
211 | if ($postcontrol) { // the magic request variable plugins must pass on returning here |
34035201 |
212 | try { |
4c7a4ef9 |
213 | // allow it to read whatever gets sent back in the request |
214 | // this is useful for plugins that redirect away and back again |
215 | // adding a token to the end of the url, for example box.net |
34035201 |
216 | $exporter->instance()->post_control($stage, array_merge($_GET, $_POST)); |
217 | } catch (portfolio_plugin_exception $e) { |
218 | portfolio_export_rethrow_exception($exporter, $e); |
219 | } |
4c7a4ef9 |
220 | $alreadystolen = true; // remember this so we don't get caught in a steal control loop! |
d67bfc32 |
221 | } |
ac6a5492 |
222 | |
9f3ef223 |
223 | // actually do the work now.. |
67a87e7d |
224 | $exporter->process_stage($stage, $alreadystolen); |
225 | |
226 | ?> |