portfolio: MDL-20850 fixed some navigation/output
[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
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 48require_login(); // this is selectively called again with $course later when we know for sure which one we're in.
aa379865 49$PAGE->set_url('/portfolio/add.php', array('id' => $dataid));
67a87e7d 50$exporter = null;
84a44985 51
4c7a4ef9 52// try and find a partial export id in the session if it's not passed explicitly
53if (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.
61if (!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 }
f1d2641d
PL
101
102 portfolio_export_pagesetup($PAGE, $exporter->get('caller')); // this calls require_login($course) if it can..
103
4c7a4ef9 104// completely new request, look to see what information we've been passed and set up the exporter object.
67a87e7d 105} else {
4c7a4ef9 106 // you cannot get here with no information for us, we must at least have the caller.
c737eed8 107 if (empty($_GET) && empty($_POST)) {
108 portfolio_exporter::print_expired_export();
109 }
67a87e7d 110 // we'e just posted here for the first time and have might the instance already
4c7a4ef9 111 if ($instanceid) {
9f3ef223 112 // this can throw exceptions but there's no point catching and rethrowing here
113 // as the exporter isn't created yet.
4c7a4ef9 114 $instance = portfolio_instance($instanceid);
67a87e7d 115 if ($broken = portfolio_instance_sanity_check($instance)) {
34035201 116 throw new portfolio_exception($broken[$instance->get('id')], 'portfolio_' . $instance->get('plugin'));
67a87e7d 117 }
118 $instance->set('user', $USER);
119 } else {
120 $instance = null;
121 }
122
4c7a4ef9 123 // we must be passed this from the caller, we cannot start a new export
124 // without knowing information about what part of moodle we come from.
c737eed8 125 if (empty($callbackfile) || empty($callbackclass)) {
126 portfolio_exporter::print_expired_export();
127 }
67a87e7d 128
4c7a4ef9 129 // so each place in moodle can pass callback args here
130 // process the entire request looking for ca_*
131 // be as lenient as possible while still being secure
132 // so only accept certain parameter types.
67a87e7d 133 $callbackargs = array();
9eb0a772 134 foreach (array_keys(array_merge($_GET, $_POST)) as $key) {
67a87e7d 135 if (strpos($key, 'ca_') === 0) {
136 if (!$value = optional_param($key, false, PARAM_ALPHAEXT)) {
137 if (!$value = optional_param($key, false, PARAM_NUMBER)) {
138 $value = optional_param($key, false, PARAM_PATH);
139 }
140 }
4c7a4ef9 141 // strip off ca_ for niceness
67a87e7d 142 $callbackargs[substr($key, 3)] = $value;
143 }
144 }
766d61cb 145 if (!confirm_sesskey()) {
146 throw new portfolio_caller_exception('confirmsesskeybad', 'error');
147 }
4c7a4ef9 148 // righto, now we have the callback args set up
149 // load up the caller file and class and tell it to set up all the data
150 // it needs
67a87e7d 151 require_once($CFG->dirroot . $callbackfile);
766d61cb 152 if (!class_exists($callbackclass) || !is_subclass_of($callbackclass, 'portfolio_caller_base')) {
153 throw new portfolio_caller_exception('callbackclassinvalid', 'portfolio');
154 }
67a87e7d 155 $caller = new $callbackclass($callbackargs);
4dc67f1e 156 $caller->set('user', $USER);
0d06b6fd 157 $caller->load_data();
4c7a4ef9 158 // this must check capabilities and either throw an exception or return false.
67a87e7d 159 if (!$caller->check_permissions()) {
9f3ef223 160 throw new portfolio_caller_exception('nopermissions', 'portfolio', $caller->get_return_url());
67a87e7d 161 }
67a87e7d 162
f1d2641d 163 portfolio_export_pagesetup($PAGE, $caller); // this calls require_login($course) if it can..
67a87e7d 164
de6d81e6 165 // finally! set up the exporter object with the portfolio instance, and caller information elements
166 $exporter = new portfolio_exporter($instance, $caller, $callbackfile);
4c7a4ef9 167
168 // set the export-specific variables, and save.
67a87e7d 169 $exporter->set('user', $USER);
50128512 170 $exporter->set('sesskey', sesskey());
84a44985 171 $exporter->save();
4c7a4ef9 172
173 // and finally, put it in the session for waking up again later.
84a44985 174 $SESSION->portfolioexport = $exporter->get('id');
67a87e7d 175}
176
67a87e7d 177if (!$exporter->get('instance')) {
178 // we've just arrived but have no instance
4c7a4ef9 179 // in this case the exporter object and the caller object have been set up above
180 // so just make a little form to select the portfolio plugin instance,
181 // which is the last thing to do before starting the export.
6fdd8fa7 182 $mform = new portfolio_instance_select('', array('caller' => $exporter->get('caller')));
183 if ($mform->is_cancelled()) {
84a44985 184 $exporter->cancel_request();
6fdd8fa7 185 } else if ($fromform = $mform->get_data()){
84a44985 186 redirect($CFG->wwwroot . '/portfolio/add.php?instance=' . $fromform->instance . '&amp;id=' . $exporter->get('id'));
6fdd8fa7 187 exit;
188 }
189 else {
349242a3 190 $exporter->print_header('selectplugin');
de2bd9df 191 echo $OUTPUT->box_start();
6fdd8fa7 192 $mform->display();
de2bd9df 193 echo $OUTPUT->box_end();
e6e565ab 194 echo $OUTPUT->footer();
6fdd8fa7 195 exit;
67a87e7d 196 }
67a87e7d 197}
198
4c7a4ef9 199// if we haven't been passed &stage= grab it from the exporter.
200if (!$stage) {
ac6a5492 201 $stage = $exporter->get('stage');
202}
203
d67bfc32 204// for places returning control to pass (rather than PORTFOLIO_STAGE_PACKAGE
205// which is unstable if they can't get to the constant (eg external system)
4c7a4ef9 206$alreadystolen = false;
207if ($postcontrol) { // the magic request variable plugins must pass on returning here
34035201 208 try {
4c7a4ef9 209 // allow it to read whatever gets sent back in the request
210 // this is useful for plugins that redirect away and back again
211 // adding a token to the end of the url, for example box.net
34035201 212 $exporter->instance()->post_control($stage, array_merge($_GET, $_POST));
213 } catch (portfolio_plugin_exception $e) {
214 portfolio_export_rethrow_exception($exporter, $e);
215 }
4c7a4ef9 216 $alreadystolen = true; // remember this so we don't get caught in a steal control loop!
d67bfc32 217}
ac6a5492 218
9f3ef223 219// actually do the work now..
67a87e7d 220$exporter->process_stage($stage, $alreadystolen);
221
4317f92f 222