"MDL-16596, fixed upload in non-js filepicker"
[moodle.git] / portfolio / add.php
CommitLineData
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 28require_once(dirname(dirname(__FILE__)) . '/config.php');
a239f01e 29
90658eef 30if (empty($CFG->enableportfolios)) {
a239f01e 31 print_error('disabled', 'portfolio');
32}
33
4c7a4ef9 34// this will pull in all the other required libraries
67a87e7d 35require_once($CFG->libdir . '/portfoliolib.php');
4c7a4ef9 36// so plugins don't have to.
6fdd8fa7 37require_once($CFG->libdir . '/formslib.php');
50128512 38
c95a6095
PL
39$dataid = optional_param('id', 0, PARAM_INT); // id of partially completed export. corresponds to a record in portfolio_tempdata
40$type = optional_param('type', null, PARAM_SAFEDIR); // if we're returning from an external system (postcontrol) for a single-export only plugin
4c7a4ef9 41$cancel = optional_param('cancel', 0, PARAM_RAW); // user has cancelled the request
c95a6095
PL
42$cancelsure = optional_param('cancelsure', 0, PARAM_BOOL); // make sure they confirm first
43$logreturn = optional_param('logreturn', 0, PARAM_BOOL); // when cancelling, we can also come from the log page, rather than the caller
4c7a4ef9 44$instanceid = optional_param('instance', 0, PARAM_INT); // instanceof of configured portfolio plugin
45$courseid = optional_param('course', 0, PARAM_INT); // courseid the data being exported belongs to (caller object should provide this later)
46$stage = optional_param('stage', PORTFOLIO_STAGE_CONFIG, PARAM_INT); // stage of the export we're at (stored in the exporter)
47$postcontrol = optional_param('postcontrol', 0, PARAM_INT); // when returning from some bounce to an external system, this gets passed
48$callbackfile = optional_param('callbackfile', null, PARAM_PATH); // callback file eg /mod/forum/lib.php - the location of the exporting content
49$callbackclass = optional_param('callbackclass', null, PARAM_ALPHAEXT); // callback class eg forum_portfolio_caller - the class to handle the exporting content.
50128512 50
4c7a4ef9 51require_login(); // this is selectively called again with $course later when we know for sure which one we're in.
c95a6095 52$PAGE->set_url('/portfolio/add.php', array('id' => $dataid, 'sesskey' => sesskey()));
67a87e7d 53$exporter = null;
84a44985 54
c95a6095
PL
55if ($postcontrol && $type && !$dataid) {
56 // we're returning from an external system that can't construct dynamic return urls
57 // this is a special "one export of this type only per session" case
58 if (portfolio_static_function($type, 'allows_multiple_exports')) {
59 throw new portfolio_exception('multiplesingleresume', 'portfolio');
67a87e7d 60 }
c95a6095 61
b12fbe8e 62 if (!$dataid = portfolio_export_type_to_id($type, $USER->id)) {
c95a6095
PL
63 throw new portfolio_exception('invalidtempid', 'portfolio');
64 }
65} else {
66 // we can't do this in the above case, because we're redirecting straight back from an external system
67 // this is not really ideal, but since we're in a "staged" wizard, the session key is checked in other stages.
68 require_sesskey(); // pretty much everything in this page is a write that could be hijacked, so just do this at the top here
84a44985 69}
9f3ef223 70
4c7a4ef9 71// if we have a dataid, it means we're in the middle of an export,
72// so rewaken it and continue.
73if (!empty($dataid)) {
50128512 74 try {
75 $exporter = portfolio_exporter::rewaken_object($dataid);
76 } catch (portfolio_exception $e) {
9f3ef223 77 // this can happen in some cases, a cancel request is sent when something is already broken
78 // so process it elegantly and move on.
50128512 79 if ($cancel) {
c95a6095
PL
80 if ($logreturn) {
81 redirect($CFG->wwwroot . '/user/portfoliologs.php');
82 }
50128512 83 redirect($CFG->wwwroot);
84 } else {
c95a6095 85 throw $e;
50128512 86 }
87 }
4c7a4ef9 88 // we have to wake it up first before we can cancel it
89 // so temporary directories etc get cleaned up.
50128512 90 if ($cancel) {
c95a6095
PL
91 if ($cancelsure) {
92 $exporter->cancel_request($logreturn);
93 } else {
94 $yesurl = $CFG->wwwroot . '/portfolio/add.php?id=' . $dataid . '&cancel=1&cancelsure=1&logreturn=' . $logreturn . '&sesskey=' . sesskey();
95 $nourl = $CFG->wwwroot . '/portfolio/add.php?id=' . $dataid . '&sesskey=' . sesskey();
96 if ($logreturn) {
97 $nourl = $CFG->wwwroot . '/user/portfoliologs.php';
98 }
99 $exporter->print_header('confirmcancel');
100 echo $OUTPUT->box_start();
101 echo $OUTPUT->confirm(get_string('confirmcancel', 'portfolio'), $yesurl, $nourl);
102 echo $OUTPUT->box_end();
103 echo $OUTPUT->footer();
104 exit;
105 }
67a87e7d 106 }
c95a6095 107 // verify we still belong to the correct user and permissions are still ok
50128512 108 $exporter->verify_rewaken();
4c7a4ef9 109 // if we don't have an instanceid in the exporter
110 // it means we've just posted from the 'choose portfolio instance' page
111 // so process that and start up the portfolio plugin
6fdd8fa7 112 if (!$exporter->get('instance')) {
4c7a4ef9 113 if ($instanceid) {
34035201 114 try {
4c7a4ef9 115 $instance = portfolio_instance($instanceid);
34035201 116 } catch (portfolio_exception $e) {
117 portfolio_export_rethrow_exception($exporter, $e);
6fdd8fa7 118 }
4c7a4ef9 119 // this technically shouldn't happen but make sure anyway
6fdd8fa7 120 if ($broken = portfolio_instance_sanity_check($instance)) {
9f3ef223 121 throw new portfolio_export_exception($exporter, $broken[$instance->get('id')], 'portfolio_' . $instance->get('plugin'));
6fdd8fa7 122 }
4c7a4ef9 123 // now we're all set up, ready to go
6fdd8fa7 124 $instance->set('user', $USER);
125 $exporter->set('instance', $instance);
d67bfc32 126 $exporter->save();
6fdd8fa7 127 }
128 }
f1d2641d
PL
129
130 portfolio_export_pagesetup($PAGE, $exporter->get('caller')); // this calls require_login($course) if it can..
131
4c7a4ef9 132// completely new request, look to see what information we've been passed and set up the exporter object.
67a87e7d 133} else {
4c7a4ef9 134 // you cannot get here with no information for us, we must at least have the caller.
c737eed8 135 if (empty($_GET) && empty($_POST)) {
136 portfolio_exporter::print_expired_export();
137 }
67a87e7d 138 // we'e just posted here for the first time and have might the instance already
4c7a4ef9 139 if ($instanceid) {
9f3ef223 140 // this can throw exceptions but there's no point catching and rethrowing here
141 // as the exporter isn't created yet.
4c7a4ef9 142 $instance = portfolio_instance($instanceid);
67a87e7d 143 if ($broken = portfolio_instance_sanity_check($instance)) {
34035201 144 throw new portfolio_exception($broken[$instance->get('id')], 'portfolio_' . $instance->get('plugin'));
67a87e7d 145 }
146 $instance->set('user', $USER);
147 } else {
148 $instance = null;
149 }
150
4c7a4ef9 151 // we must be passed this from the caller, we cannot start a new export
152 // without knowing information about what part of moodle we come from.
c737eed8 153 if (empty($callbackfile) || empty($callbackclass)) {
154 portfolio_exporter::print_expired_export();
155 }
67a87e7d 156
4c7a4ef9 157 // so each place in moodle can pass callback args here
158 // process the entire request looking for ca_*
159 // be as lenient as possible while still being secure
160 // so only accept certain parameter types.
67a87e7d 161 $callbackargs = array();
9eb0a772 162 foreach (array_keys(array_merge($_GET, $_POST)) as $key) {
67a87e7d 163 if (strpos($key, 'ca_') === 0) {
164 if (!$value = optional_param($key, false, PARAM_ALPHAEXT)) {
165 if (!$value = optional_param($key, false, PARAM_NUMBER)) {
166 $value = optional_param($key, false, PARAM_PATH);
167 }
168 }
4c7a4ef9 169 // strip off ca_ for niceness
67a87e7d 170 $callbackargs[substr($key, 3)] = $value;
171 }
172 }
4c7a4ef9 173 // righto, now we have the callback args set up
174 // load up the caller file and class and tell it to set up all the data
175 // it needs
67a87e7d 176 require_once($CFG->dirroot . $callbackfile);
766d61cb 177 if (!class_exists($callbackclass) || !is_subclass_of($callbackclass, 'portfolio_caller_base')) {
178 throw new portfolio_caller_exception('callbackclassinvalid', 'portfolio');
179 }
67a87e7d 180 $caller = new $callbackclass($callbackargs);
4dc67f1e 181 $caller->set('user', $USER);
0d06b6fd 182 $caller->load_data();
4c7a4ef9 183 // this must check capabilities and either throw an exception or return false.
67a87e7d 184 if (!$caller->check_permissions()) {
9f3ef223 185 throw new portfolio_caller_exception('nopermissions', 'portfolio', $caller->get_return_url());
67a87e7d 186 }
67a87e7d 187
f1d2641d 188 portfolio_export_pagesetup($PAGE, $caller); // this calls require_login($course) if it can..
67a87e7d 189
de6d81e6 190 // finally! set up the exporter object with the portfolio instance, and caller information elements
191 $exporter = new portfolio_exporter($instance, $caller, $callbackfile);
4c7a4ef9 192
193 // set the export-specific variables, and save.
67a87e7d 194 $exporter->set('user', $USER);
84a44985 195 $exporter->save();
67a87e7d 196}
197
67a87e7d 198if (!$exporter->get('instance')) {
199 // we've just arrived but have no instance
4c7a4ef9 200 // in this case the exporter object and the caller object have been set up above
201 // so just make a little form to select the portfolio plugin instance,
202 // which is the last thing to do before starting the export.
c95a6095 203 $mform = new portfolio_instance_select('', array('id' => $exporter->get('id'), 'caller' => $exporter->get('caller')));
6fdd8fa7 204 if ($mform->is_cancelled()) {
84a44985 205 $exporter->cancel_request();
6fdd8fa7 206 } else if ($fromform = $mform->get_data()){
84a44985 207 redirect($CFG->wwwroot . '/portfolio/add.php?instance=' . $fromform->instance . '&amp;id=' . $exporter->get('id'));
6fdd8fa7 208 exit;
209 }
210 else {
349242a3 211 $exporter->print_header('selectplugin');
de2bd9df 212 echo $OUTPUT->box_start();
6fdd8fa7 213 $mform->display();
de2bd9df 214 echo $OUTPUT->box_end();
e6e565ab 215 echo $OUTPUT->footer();
6fdd8fa7 216 exit;
67a87e7d 217 }
67a87e7d 218}
219
4c7a4ef9 220// if we haven't been passed &stage= grab it from the exporter.
221if (!$stage) {
ac6a5492 222 $stage = $exporter->get('stage');
223}
224
d67bfc32 225// for places returning control to pass (rather than PORTFOLIO_STAGE_PACKAGE
226// which is unstable if they can't get to the constant (eg external system)
4c7a4ef9 227$alreadystolen = false;
228if ($postcontrol) { // the magic request variable plugins must pass on returning here
34035201 229 try {
4c7a4ef9 230 // allow it to read whatever gets sent back in the request
231 // this is useful for plugins that redirect away and back again
232 // adding a token to the end of the url, for example box.net
34035201 233 $exporter->instance()->post_control($stage, array_merge($_GET, $_POST));
234 } catch (portfolio_plugin_exception $e) {
235 portfolio_export_rethrow_exception($exporter, $e);
236 }
4c7a4ef9 237 $alreadystolen = true; // remember this so we don't get caught in a steal control loop!
d67bfc32 238}
ac6a5492 239
9f3ef223 240// actually do the work now..
67a87e7d 241$exporter->process_stage($stage, $alreadystolen);
242
4317f92f 243