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