|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00028 require_once(dirname(dirname(__FILE__)) . '/config.php'); 00029 00030 if (empty($CFG->enableportfolios)) { 00031 print_error('disabled', 'portfolio'); 00032 } 00033 00034 require_once($CFG->libdir . '/portfoliolib.php'); 00035 require_once($CFG->libdir . '/portfolio/exporter.php'); 00036 require_once($CFG->libdir . '/portfolio/caller.php'); 00037 require_once($CFG->libdir . '/portfolio/plugin.php'); 00038 00039 $dataid = optional_param('id', 0, PARAM_INT); // id of partially completed export. corresponds to a record in portfolio_tempdata 00040 $type = optional_param('type', null, PARAM_SAFEDIR); // if we're returning from an external system (postcontrol) for a single-export only plugin 00041 $cancel = optional_param('cancel', 0, PARAM_RAW); // user has cancelled the request 00042 $cancelsure = optional_param('cancelsure', 0, PARAM_BOOL); // make sure they confirm first 00043 $logreturn = optional_param('logreturn', 0, PARAM_BOOL); // when cancelling, we can also come from the log page, rather than the caller 00044 $instanceid = optional_param('instance', 0, PARAM_INT); // instanceof of configured portfolio plugin 00045 $courseid = optional_param('course', 0, PARAM_INT); // courseid the data being exported belongs to (caller object should provide this later) 00046 $stage = optional_param('stage', PORTFOLIO_STAGE_CONFIG, PARAM_INT); // stage of the export we're at (stored in the exporter) 00047 $postcontrol = optional_param('postcontrol', 0, PARAM_INT); // when returning from some bounce to an external system, this gets passed 00048 $callbackfile = optional_param('callbackfile', null, PARAM_PATH); // callback file eg /mod/forum/lib.php - the location of the exporting content 00049 $callbackclass = optional_param('callbackclass', null, PARAM_ALPHAEXT); // callback class eg forum_portfolio_caller - the class to handle the exporting content. 00050 $callerformats = optional_param('callerformats', null, PARAM_TAGLIST); // comma separated list of formats the specific place exporting content supports 00051 00052 require_login(); // this is selectively called again with $course later when we know for sure which one we're in. 00053 $PAGE->set_context(get_system_context()); 00054 $PAGE->set_url('/portfolio/add.php', array('id' => $dataid, 'sesskey' => sesskey())); 00055 $PAGE->set_pagelayout('standard'); 00056 $exporter = null; 00057 00058 if ($postcontrol && $type && !$dataid) { 00059 // we're returning from an external system that can't construct dynamic return urls 00060 // this is a special "one export of this type only per session" case 00061 if (portfolio_static_function($type, 'allows_multiple_exports')) { 00062 throw new portfolio_exception('multiplesingleresume', 'portfolio'); 00063 } 00064 00065 if (!$dataid = portfolio_export_type_to_id($type, $USER->id)) { 00066 throw new portfolio_exception('invalidtempid', 'portfolio'); 00067 } 00068 } else { 00069 // we can't do this in the above case, because we're redirecting straight back from an external system 00070 // this is not really ideal, but since we're in a "staged" wizard, the session key is checked in other stages. 00071 require_sesskey(); // pretty much everything in this page is a write that could be hijacked, so just do this at the top here 00072 } 00073 00074 // if we have a dataid, it means we're in the middle of an export, 00075 // so rewaken it and continue. 00076 if (!empty($dataid)) { 00077 try { 00078 $exporter = portfolio_exporter::rewaken_object($dataid); 00079 } catch (portfolio_exception $e) { 00080 // this can happen in some cases, a cancel request is sent when something is already broken 00081 // so process it elegantly and move on. 00082 if ($cancel) { 00083 if ($logreturn) { 00084 redirect($CFG->wwwroot . '/user/portfoliologs.php'); 00085 } 00086 redirect($CFG->wwwroot); 00087 } else { 00088 throw $e; 00089 } 00090 } 00091 // we have to wake it up first before we can cancel it 00092 // so temporary directories etc get cleaned up. 00093 if ($cancel) { 00094 if ($cancelsure) { 00095 $exporter->cancel_request($logreturn); 00096 } else { 00097 portfolio_export_pagesetup($PAGE, $exporter->get('caller')); 00098 $exporter->print_header(get_string('confirmcancel', 'portfolio')); 00099 echo $OUTPUT->box_start(); 00100 $yesbutton = new single_button(new moodle_url('/portfolio/add.php', array('id' => $dataid, 'cancel' => 1, 'cancelsure' => 1, 'logreturn' => $logreturn)), get_string('yes')); 00101 if ($logreturn) { 00102 $nobutton = new single_button(new moodle_url('/user/portfoliologs.php'), get_string('no')); 00103 } else { 00104 $nobutton = new single_button(new moodle_url('/portfolio/add.php', array('id' => $dataid)), get_string('no')); 00105 } 00106 echo $OUTPUT->confirm(get_string('confirmcancel', 'portfolio'), $yesbutton, $nobutton); 00107 echo $OUTPUT->box_end(); 00108 echo $OUTPUT->footer(); 00109 exit; 00110 } 00111 } 00112 // verify we still belong to the correct user and permissions are still ok 00113 $exporter->verify_rewaken(); 00114 // if we don't have an instanceid in the exporter 00115 // it means we've just posted from the 'choose portfolio instance' page 00116 // so process that and start up the portfolio plugin 00117 if (!$exporter->get('instance')) { 00118 if ($instanceid) { 00119 try { 00120 $instance = portfolio_instance($instanceid); 00121 } catch (portfolio_exception $e) { 00122 portfolio_export_rethrow_exception($exporter, $e); 00123 } 00124 // this technically shouldn't happen but make sure anyway 00125 if ($broken = portfolio_instance_sanity_check($instance)) { 00126 throw new portfolio_export_exception($exporter, $broken[$instance->get('id')], 'portfolio_' . $instance->get('plugin')); 00127 } 00128 // now we're all set up, ready to go 00129 $instance->set('user', $USER); 00130 $exporter->set('instance', $instance); 00131 $exporter->save(); 00132 } 00133 } 00134 00135 portfolio_export_pagesetup($PAGE, $exporter->get('caller')); // this calls require_login($course) if it can.. 00136 00137 // completely new request, look to see what information we've been passed and set up the exporter object. 00138 } else { 00139 // you cannot get here with no information for us, we must at least have the caller. 00140 if (empty($_GET) && empty($_POST)) { 00141 portfolio_exporter::print_expired_export(); 00142 } 00143 // we'e just posted here for the first time and have might the instance already 00144 if ($instanceid) { 00145 // this can throw exceptions but there's no point catching and rethrowing here 00146 // as the exporter isn't created yet. 00147 $instance = portfolio_instance($instanceid); 00148 if ($broken = portfolio_instance_sanity_check($instance)) { 00149 throw new portfolio_exception($broken[$instance->get('id')], 'portfolio_' . $instance->get('plugin')); 00150 } 00151 $instance->set('user', $USER); 00152 } else { 00153 $instance = null; 00154 } 00155 00156 // we must be passed this from the caller, we cannot start a new export 00157 // without knowing information about what part of moodle we come from. 00158 if (empty($callbackfile) || empty($callbackclass)) { 00159 debugging('no callback file or class'); 00160 portfolio_exporter::print_expired_export(); 00161 } 00162 00163 // so each place in moodle can pass callback args here 00164 // process the entire request looking for ca_* 00165 // be as lenient as possible while still being secure 00166 // so only accept certain parameter types. 00167 $callbackargs = array(); 00168 foreach (array_keys(array_merge($_GET, $_POST)) as $key) { 00169 if (strpos($key, 'ca_') === 0) { 00170 if (!$value = optional_param($key, false, PARAM_ALPHAEXT)) { 00171 if (!$value = optional_param($key, false, PARAM_NUMBER)) { 00172 $value = optional_param($key, false, PARAM_PATH); 00173 } 00174 } 00175 // strip off ca_ for niceness 00176 $callbackargs[substr($key, 3)] = $value; 00177 } 00178 } 00179 // righto, now we have the callback args set up 00180 // load up the caller file and class and tell it to set up all the data 00181 // it needs 00182 require_once($CFG->dirroot . $callbackfile); 00183 if (!class_exists($callbackclass) || !is_subclass_of($callbackclass, 'portfolio_caller_base')) { 00184 throw new portfolio_caller_exception('callbackclassinvalid', 'portfolio'); 00185 } 00186 $caller = new $callbackclass($callbackargs); 00187 $caller->set('user', $USER); 00188 if ($formats = explode(',', $callerformats)) { 00189 $caller->set_formats_from_button($formats); 00190 } 00191 $caller->load_data(); 00192 // this must check capabilities and either throw an exception or return false. 00193 if (!$caller->check_permissions()) { 00194 throw new portfolio_caller_exception('nopermissions', 'portfolio', $caller->get_return_url()); 00195 } 00196 00197 portfolio_export_pagesetup($PAGE, $caller); // this calls require_login($course) if it can.. 00198 00199 // finally! set up the exporter object with the portfolio instance, and caller information elements 00200 $exporter = new portfolio_exporter($instance, $caller, $callbackfile); 00201 00202 // set the export-specific variables, and save. 00203 $exporter->set('user', $USER); 00204 $exporter->save(); 00205 } 00206 00207 if (!$exporter->get('instance')) { 00208 // we've just arrived but have no instance 00209 // in this case the exporter object and the caller object have been set up above 00210 // so just make a little form to select the portfolio plugin instance, 00211 // which is the last thing to do before starting the export. 00212 // 00213 // first check to make sure there is actually a point 00214 $options = portfolio_instance_select( 00215 portfolio_instances(), 00216 $exporter->get('caller')->supported_formats(), 00217 get_class($exporter->get('caller')), 00218 $exporter->get('caller')->get_mimetype(), 00219 'instance', 00220 true, 00221 true 00222 ); 00223 if (empty($options)) { 00224 throw new portfolio_export_exception($exporter, 'noavailableplugins', 'portfolio'); 00225 } else if (count($options) == 1) { 00226 // no point displaying a form, just redirect. 00227 $instance = array_shift(array_keys($options)); 00228 redirect($CFG->wwwroot . '/portfolio/add.php?id= ' . $exporter->get('id') . '&instance=' . $instance . '&sesskey=' . sesskey()); 00229 } 00230 // be very selective about not including this unless we really need to 00231 require_once($CFG->libdir . '/portfolio/forms.php'); 00232 $mform = new portfolio_instance_select('', array('id' => $exporter->get('id'), 'caller' => $exporter->get('caller'), 'options' => $options)); 00233 if ($mform->is_cancelled()) { 00234 $exporter->cancel_request(); 00235 } else if ($fromform = $mform->get_data()){ 00236 redirect($CFG->wwwroot . '/portfolio/add.php?instance=' . $fromform->instance . '&id=' . $exporter->get('id')); 00237 exit; 00238 } 00239 else { 00240 $exporter->print_header(get_string('selectplugin', 'portfolio')); 00241 echo $OUTPUT->box_start(); 00242 $mform->display(); 00243 echo $OUTPUT->box_end(); 00244 echo $OUTPUT->footer(); 00245 exit; 00246 } 00247 } 00248 00249 // if we haven't been passed &stage= grab it from the exporter. 00250 if (!$stage) { 00251 $stage = $exporter->get('stage'); 00252 } 00253 00254 // for places returning control to pass (rather than PORTFOLIO_STAGE_PACKAGE 00255 // which is unstable if they can't get to the constant (eg external system) 00256 $alreadystolen = false; 00257 if ($postcontrol) { // the magic request variable plugins must pass on returning here 00258 try { 00259 // allow it to read whatever gets sent back in the request 00260 // this is useful for plugins that redirect away and back again 00261 // adding a token to the end of the url, for example box.net 00262 $exporter->instance()->post_control($stage, array_merge($_GET, $_POST)); 00263 } catch (portfolio_plugin_exception $e) { 00264 portfolio_export_rethrow_exception($exporter, $e); 00265 } 00266 $alreadystolen = true; // remember this so we don't get caught in a steal control loop! 00267 } 00268 00269 // actually do the work now.. 00270 $exporter->process_stage($stage, $alreadystolen); 00271 00272