MDL-15777 more fixes for portfolio to use file api
[moodle.git] / portfolio / add.php
CommitLineData
67a87e7d 1<?php
2require_once(dirname(dirname(__FILE__)) . '/config.php');
a239f01e 3
4if (empty($CFG->portfolioenabled)) {
5 print_error('disabled', 'portfolio');
6}
7
67a87e7d 8require_once($CFG->libdir . '/portfoliolib.php');
6fdd8fa7 9require_once($CFG->libdir . '/formslib.php');
67a87e7d 10$exporter = null;
84a44985 11$dataid = 0;
12
13if (!$dataid = optional_param('id') ) {
14 if (isset($SESSION->portfolioexport)) {
15 $dataid = $SESSION->portfolioexport;
67a87e7d 16 }
84a44985 17}
18if ($dataid) {
19 $exporter = portfolio_exporter::rewaken_object($dataid);
6fdd8fa7 20 if ($cancel = optional_param('cancel', 0, PARAM_RAW)) {
84a44985 21 $exporter->cancel_request();
67a87e7d 22 }
6fdd8fa7 23 if (!$exporter->get('instance')) {
24 if ($instance = optional_param('instance', '', PARAM_INT)) {
25 if (!$instance = portfolio_instance($instance)) {
26 $exporter->raise_error('invalidinstance', 'portfolio');
27 }
28 if ($broken = portfolio_instance_sanity_check($instance)) {
29 print_error(get_string($broken[$instance->get('id')], 'portfolio_' . $instance->get('plugin')));
30 }
31 $instance->set('user', $USER);
32 $exporter->set('instance', $instance);
d67bfc32 33 $exporter->save();
6fdd8fa7 34 }
35 }
67a87e7d 36} else {
37 // we'e just posted here for the first time and have might the instance already
38 if ($instance = optional_param('instance', 0, PARAM_INT)) {
9eb0a772 39 if (!$instance = portfolio_instance($instance)) {
40 portfolio_exporter::raise_error('invalidinstance', 'portfolio');
41 }
67a87e7d 42 if ($broken = portfolio_instance_sanity_check($instance)) {
43 print_error(get_string($broken[$instance->get('id')], 'portfolio_' . $instance->get('plugin')));
44 }
45 $instance->set('user', $USER);
46 } else {
47 $instance = null;
48 }
49
50 $callbackfile = required_param('callbackfile', PARAM_PATH);
51 $callbackclass = required_param('callbackclass', PARAM_ALPHAEXT);
52
53 $callbackargs = array();
9eb0a772 54 foreach (array_keys(array_merge($_GET, $_POST)) as $key) {
67a87e7d 55 if (strpos($key, 'ca_') === 0) {
56 if (!$value = optional_param($key, false, PARAM_ALPHAEXT)) {
57 if (!$value = optional_param($key, false, PARAM_NUMBER)) {
58 $value = optional_param($key, false, PARAM_PATH);
59 }
60 }
61 $callbackargs[substr($key, 3)] = $value;
62 }
63 }
64 require_once($CFG->dirroot . $callbackfile);
65 $caller = new $callbackclass($callbackargs);
66 if (!$caller->check_permissions()) {
67 print_error('nopermissions', 'portfolio', $caller->get_return_url());
68 }
69 $caller->set('user', $USER);
70
71 // for build navigation
72 if (!$course = $caller->get('course')) {
73 $course = optional_param('course', 0, PARAM_INT);
74 }
75
9eb0a772 76 if (!empty($course) && is_numeric($course)) {
9eb0a772 77 $course = $DB->get_record('course', array('id' => $course), 'id,shortname,fullname');
67a87e7d 78 // this is yuk but used in build_navigation
79 }
80
9eb0a772 81 $COURSE = $course;
82
67a87e7d 83 list($extranav, $cm) = $caller->get_navigation();
84 $extranav[] = array('type' => 'title', 'name' => get_string('exporting', 'portfolio'));
85 $navigation = build_navigation($extranav, $cm);
86
87 $exporter = new portfolio_exporter($instance, $caller, $callbackfile, $navigation);
88 $exporter->set('user', $USER);
84a44985 89 $exporter->save();
90 $SESSION->portfolioexport = $exporter->get('id');
67a87e7d 91}
92
67a87e7d 93if (!$exporter->get('instance')) {
d67bfc32 94 print_object($exporter);
67a87e7d 95 // we've just arrived but have no instance
96 // so retrieve everything from the request,
97 // add them as hidden fields in a new form
98 // to select the instance and post back here again
99 // for the next block to catch
6fdd8fa7 100 $mform = new portfolio_instance_select('', array('caller' => $exporter->get('caller')));
101 if ($mform->is_cancelled()) {
84a44985 102 $exporter->cancel_request();
6fdd8fa7 103 } else if ($fromform = $mform->get_data()){
84a44985 104 redirect($CFG->wwwroot . '/portfolio/add.php?instance=' . $fromform->instance . '&amp;id=' . $exporter->get('id'));
6fdd8fa7 105 exit;
106 }
107 else {
108 $exporter->print_header();
109 print_heading(get_string('selectplugin', 'portfolio'));
110 print_simple_box_start();
111 $mform->display();
112 print_simple_box_end();
113 print_footer();
114 exit;
67a87e7d 115 }
67a87e7d 116}
117
d67bfc32 118$stage = optional_param('stage', PORTFOLIO_STAGE_CONFIG);
119$alreadystolen = false;
120// for places returning control to pass (rather than PORTFOLIO_STAGE_PACKAGE
121// which is unstable if they can't get to the constant (eg external system)
122if ($postcontrol = optional_param('postcontrol', 0, PARAM_INT)) {
123 $stage = $exporter->get('stage');
124 $exporter->instance()->post_control($stage, array_merge($_GET, $_POST));
125 $alreadystolen = true;
126}
67a87e7d 127$exporter->process_stage($stage, $alreadystolen);
128
129?>