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