MDL-18167 towareds reimplementing of win installer support
[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 {
c737eed8 32 portfolio_exporter::print_expired_export();
50128512 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 {
c737eed8 56
57 if (empty($_GET) && empty($_POST)) {
58 portfolio_exporter::print_expired_export();
59 }
67a87e7d 60 // we'e just posted here for the first time and have might the instance already
61 if ($instance = optional_param('instance', 0, PARAM_INT)) {
9f3ef223 62 // this can throw exceptions but there's no point catching and rethrowing here
63 // as the exporter isn't created yet.
64 $instance = portfolio_instance($instance);
67a87e7d 65 if ($broken = portfolio_instance_sanity_check($instance)) {
34035201 66 throw new portfolio_exception($broken[$instance->get('id')], 'portfolio_' . $instance->get('plugin'));
67a87e7d 67 }
68 $instance->set('user', $USER);
69 } else {
70 $instance = null;
71 }
72
c737eed8 73 $callbackfile = optional_param('callbackfile', null, PARAM_PATH);
74 $callbackclass = optional_param('callbackclass', null, PARAM_ALPHAEXT);
75
76 if (empty($callbackfile) || empty($callbackclass)) {
77 portfolio_exporter::print_expired_export();
78 }
67a87e7d 79
80 $callbackargs = array();
9eb0a772 81 foreach (array_keys(array_merge($_GET, $_POST)) as $key) {
67a87e7d 82 if (strpos($key, 'ca_') === 0) {
83 if (!$value = optional_param($key, false, PARAM_ALPHAEXT)) {
84 if (!$value = optional_param($key, false, PARAM_NUMBER)) {
85 $value = optional_param($key, false, PARAM_PATH);
86 }
87 }
88 $callbackargs[substr($key, 3)] = $value;
89 }
90 }
91 require_once($CFG->dirroot . $callbackfile);
92 $caller = new $callbackclass($callbackargs);
4dc67f1e 93 $caller->set('user', $USER);
0d06b6fd 94 $caller->load_data();
67a87e7d 95 if (!$caller->check_permissions()) {
9f3ef223 96 throw new portfolio_caller_exception('nopermissions', 'portfolio', $caller->get_return_url());
67a87e7d 97 }
67a87e7d 98
99 // for build navigation
100 if (!$course = $caller->get('course')) {
101 $course = optional_param('course', 0, PARAM_INT);
102 }
103
9eb0a772 104 if (!empty($course) && is_numeric($course)) {
9eb0a772 105 $course = $DB->get_record('course', array('id' => $course), 'id,shortname,fullname');
67a87e7d 106 }
107
9f3ef223 108 // this is yuk but used in build_navigation
9eb0a772 109 $COURSE = $course;
110
67a87e7d 111 list($extranav, $cm) = $caller->get_navigation();
112 $extranav[] = array('type' => 'title', 'name' => get_string('exporting', 'portfolio'));
113 $navigation = build_navigation($extranav, $cm);
114
115 $exporter = new portfolio_exporter($instance, $caller, $callbackfile, $navigation);
116 $exporter->set('user', $USER);
50128512 117 $exporter->set('sesskey', sesskey());
84a44985 118 $exporter->save();
119 $SESSION->portfolioexport = $exporter->get('id');
67a87e7d 120}
121
67a87e7d 122if (!$exporter->get('instance')) {
123 // we've just arrived but have no instance
124 // so retrieve everything from the request,
125 // add them as hidden fields in a new form
126 // to select the instance and post back here again
127 // for the next block to catch
6fdd8fa7 128 $mform = new portfolio_instance_select('', array('caller' => $exporter->get('caller')));
129 if ($mform->is_cancelled()) {
84a44985 130 $exporter->cancel_request();
6fdd8fa7 131 } else if ($fromform = $mform->get_data()){
84a44985 132 redirect($CFG->wwwroot . '/portfolio/add.php?instance=' . $fromform->instance . '&amp;id=' . $exporter->get('id'));
6fdd8fa7 133 exit;
134 }
135 else {
349242a3 136 $exporter->print_header('selectplugin');
6fdd8fa7 137 print_simple_box_start();
138 $mform->display();
139 print_simple_box_end();
140 print_footer();
141 exit;
67a87e7d 142 }
67a87e7d 143}
144
ac6a5492 145if (!$stage = optional_param('stage', PORTFOLIO_STAGE_CONFIG)) {
146 $stage = $exporter->get('stage');
147}
148
d67bfc32 149$alreadystolen = false;
150// for places returning control to pass (rather than PORTFOLIO_STAGE_PACKAGE
151// which is unstable if they can't get to the constant (eg external system)
152if ($postcontrol = optional_param('postcontrol', 0, PARAM_INT)) {
34035201 153 try {
154 $exporter->instance()->post_control($stage, array_merge($_GET, $_POST));
155 } catch (portfolio_plugin_exception $e) {
156 portfolio_export_rethrow_exception($exporter, $e);
157 }
d67bfc32 158 $alreadystolen = true;
159}
ac6a5492 160
9f3ef223 161// actually do the work now..
67a87e7d 162$exporter->process_stage($stage, $alreadystolen);
163
164?>