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