2008-07-25 08:14:11 +00:00
< ? php
2009-05-02 13:23:22 +00:00
/**
* Moodle - Modular Object - Oriented Dynamic Learning Environment
* http :// moodle . org
* Copyright ( C ) 1999 onwards Martin Dougiamas http :// dougiamas . com
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 2 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http :// www . gnu . org / licenses />.
*
* @ package moodle
* @ subpackage portfolio
* @ author Penny Leach < penny @ catalyst . net . nz >
* @ license http :// www . gnu . org / copyleft / gpl . html GNU GPL
* @ copyright ( C ) 1999 onwards Martin Dougiamas http :// dougiamas . com
*
* This file is the main controller to do with the portfolio export wizard .
*/
2008-07-25 08:14:11 +00:00
require_once ( dirname ( dirname ( __FILE__ )) . '/config.php' );
2008-07-29 11:27:16 +00:00
2008-08-26 05:45:07 +00:00
if ( empty ( $CFG -> enableportfolios )) {
2008-07-29 11:27:16 +00:00
print_error ( 'disabled' , 'portfolio' );
}
2009-05-02 13:23:22 +00:00
// this will pull in all the other required libraries
2008-07-25 08:14:11 +00:00
require_once ( $CFG -> libdir . '/portfoliolib.php' );
2009-05-02 13:23:22 +00:00
// so plugins don't have to.
2008-08-04 16:07:37 +00:00
require_once ( $CFG -> libdir . '/formslib.php' );
2008-08-22 13:25:53 +00:00
2009-05-02 13:23:22 +00:00
$cancel = optional_param ( 'cancel' , 0 , PARAM_RAW ); // user has cancelled the request
$dataid = optional_param ( 'id' , 0 , PARAM_INT ); // id of partially completed export (in session, everything else in portfolio_tempdata
$instanceid = optional_param ( 'instance' , 0 , PARAM_INT ); // instanceof of configured portfolio plugin
$courseid = optional_param ( 'course' , 0 , PARAM_INT ); // courseid the data being exported belongs to (caller object should provide this later)
$stage = optional_param ( 'stage' , PORTFOLIO_STAGE_CONFIG , PARAM_INT ); // stage of the export we're at (stored in the exporter)
$postcontrol = optional_param ( 'postcontrol' , 0 , PARAM_INT ); // when returning from some bounce to an external system, this gets passed
$callbackfile = optional_param ( 'callbackfile' , null , PARAM_PATH ); // callback file eg /mod/forum/lib.php - the location of the exporting content
$callbackclass = optional_param ( 'callbackclass' , null , PARAM_ALPHAEXT ); // callback class eg forum_portfolio_caller - the class to handle the exporting content.
2008-08-22 13:25:53 +00:00
2009-05-02 13:23:22 +00:00
require_login (); // this is selectively called again with $course later when we know for sure which one we're in.
2009-03-28 22:02:00 +00:00
2008-07-25 08:14:11 +00:00
$exporter = null ;
2008-08-06 15:27:42 +00:00
2009-05-02 13:23:22 +00:00
// try and find a partial export id in the session if it's not passed explicitly
if ( empty ( $dataid )) {
2008-08-06 15:27:42 +00:00
if ( isset ( $SESSION -> portfolioexport )) {
$dataid = $SESSION -> portfolioexport ;
2008-07-25 08:14:11 +00:00
}
2008-08-06 15:27:42 +00:00
}
2008-09-04 10:32:17 +00:00
2009-05-02 13:23:22 +00:00
// if we have a dataid, it means we're in the middle of an export,
// so rewaken it and continue.
if ( ! empty ( $dataid )) {
2008-08-22 13:25:53 +00:00
try {
$exporter = portfolio_exporter :: rewaken_object ( $dataid );
} catch ( portfolio_exception $e ) {
2008-09-04 10:32:17 +00:00
// this can happen in some cases, a cancel request is sent when something is already broken
// so process it elegantly and move on.
2008-08-22 13:25:53 +00:00
if ( $cancel ) {
unset ( $SESSION -> portfolioexport );
redirect ( $CFG -> wwwroot );
} else {
2008-09-16 17:50:55 +00:00
portfolio_exporter :: print_expired_export ();
2008-08-22 13:25:53 +00:00
}
}
2009-05-02 13:23:22 +00:00
// we have to wake it up first before we can cancel it
// so temporary directories etc get cleaned up.
2008-08-22 13:25:53 +00:00
if ( $cancel ) {
2008-08-06 15:27:42 +00:00
$exporter -> cancel_request ();
2008-07-25 08:14:11 +00:00
}
2008-09-04 10:32:17 +00:00
// verify we still belong to the correct user and session
2008-08-22 13:25:53 +00:00
$exporter -> verify_rewaken ();
2009-05-02 13:23:22 +00:00
// if we don't have an instanceid in the exporter
// it means we've just posted from the 'choose portfolio instance' page
// so process that and start up the portfolio plugin
2008-08-04 16:07:37 +00:00
if ( ! $exporter -> get ( 'instance' )) {
2009-05-02 13:23:22 +00:00
if ( $instanceid ) {
2008-08-19 14:20:32 +00:00
try {
2009-05-02 13:23:22 +00:00
$instance = portfolio_instance ( $instanceid );
2008-08-19 14:20:32 +00:00
} catch ( portfolio_exception $e ) {
portfolio_export_rethrow_exception ( $exporter , $e );
2008-08-04 16:07:37 +00:00
}
2009-05-02 13:23:22 +00:00
// this technically shouldn't happen but make sure anyway
2008-08-04 16:07:37 +00:00
if ( $broken = portfolio_instance_sanity_check ( $instance )) {
2008-09-04 10:32:17 +00:00
throw new portfolio_export_exception ( $exporter , $broken [ $instance -> get ( 'id' )], 'portfolio_' . $instance -> get ( 'plugin' ));
2008-08-04 16:07:37 +00:00
}
2009-05-02 13:23:22 +00:00
// now we're all set up, ready to go
2008-08-04 16:07:37 +00:00
$instance -> set ( 'user' , $USER );
$exporter -> set ( 'instance' , $instance );
2008-08-09 14:24:58 +00:00
$exporter -> save ();
2008-08-04 16:07:37 +00:00
}
}
2009-05-02 13:23:22 +00:00
// completely new request, look to see what information we've been passed and set up the exporter object.
2008-07-25 08:14:11 +00:00
} else {
2009-05-02 13:23:22 +00:00
// you cannot get here with no information for us, we must at least have the caller.
2008-09-16 17:50:55 +00:00
if ( empty ( $_GET ) && empty ( $_POST )) {
portfolio_exporter :: print_expired_export ();
}
2008-07-25 08:14:11 +00:00
// we'e just posted here for the first time and have might the instance already
2009-05-02 13:23:22 +00:00
if ( $instanceid ) {
2008-09-04 10:32:17 +00:00
// this can throw exceptions but there's no point catching and rethrowing here
// as the exporter isn't created yet.
2009-05-02 13:23:22 +00:00
$instance = portfolio_instance ( $instanceid );
2008-07-25 08:14:11 +00:00
if ( $broken = portfolio_instance_sanity_check ( $instance )) {
2008-08-19 14:20:32 +00:00
throw new portfolio_exception ( $broken [ $instance -> get ( 'id' )], 'portfolio_' . $instance -> get ( 'plugin' ));
2008-07-25 08:14:11 +00:00
}
$instance -> set ( 'user' , $USER );
} else {
$instance = null ;
}
2009-05-02 13:23:22 +00:00
// we must be passed this from the caller, we cannot start a new export
// without knowing information about what part of moodle we come from.
2008-09-16 17:50:55 +00:00
if ( empty ( $callbackfile ) || empty ( $callbackclass )) {
portfolio_exporter :: print_expired_export ();
}
2008-07-25 08:14:11 +00:00
2009-05-02 13:23:22 +00:00
// so each place in moodle can pass callback args here
// process the entire request looking for ca_*
// be as lenient as possible while still being secure
// so only accept certain parameter types.
2008-07-25 08:14:11 +00:00
$callbackargs = array ();
2008-08-01 12:23:32 +00:00
foreach ( array_keys ( array_merge ( $_GET , $_POST )) as $key ) {
2008-07-25 08:14:11 +00:00
if ( strpos ( $key , 'ca_' ) === 0 ) {
if ( ! $value = optional_param ( $key , false , PARAM_ALPHAEXT )) {
if ( ! $value = optional_param ( $key , false , PARAM_NUMBER )) {
$value = optional_param ( $key , false , PARAM_PATH );
}
}
2009-05-02 13:23:22 +00:00
// strip off ca_ for niceness
2008-07-25 08:14:11 +00:00
$callbackargs [ substr ( $key , 3 )] = $value ;
}
}
2009-06-01 17:06:28 +00:00
if ( ! confirm_sesskey ()) {
throw new portfolio_caller_exception ( 'confirmsesskeybad' , 'error' );
}
2009-05-02 13:23:22 +00:00
// righto, now we have the callback args set up
// load up the caller file and class and tell it to set up all the data
// it needs
2008-07-25 08:14:11 +00:00
require_once ( $CFG -> dirroot . $callbackfile );
2009-06-01 17:06:28 +00:00
if ( ! class_exists ( $callbackclass ) || ! is_subclass_of ( $callbackclass , 'portfolio_caller_base' )) {
throw new portfolio_caller_exception ( 'callbackclassinvalid' , 'portfolio' );
}
2008-07-25 08:14:11 +00:00
$caller = new $callbackclass ( $callbackargs );
2008-08-22 16:35:26 +00:00
$caller -> set ( 'user' , $USER );
2008-09-11 13:42:58 +00:00
$caller -> load_data ();
2009-05-02 13:23:22 +00:00
// this must check capabilities and either throw an exception or return false.
2008-07-25 08:14:11 +00:00
if ( ! $caller -> check_permissions ()) {
2008-09-04 10:32:17 +00:00
throw new portfolio_caller_exception ( 'nopermissions' , 'portfolio' , $caller -> get_return_url ());
2008-07-25 08:14:11 +00:00
}
// for build navigation
if ( ! $course = $caller -> get ( 'course' )) {
2009-05-02 13:23:22 +00:00
$course = $courseid ;
2008-07-25 08:14:11 +00:00
}
2009-05-02 13:23:22 +00:00
// set up the course so that build_navigation works nice
2009-05-09 13:15:41 +00:00
$PAGE -> set_course ( $course );
2008-07-25 08:14:11 +00:00
list ( $extranav , $cm ) = $caller -> get_navigation ();
2009-06-01 18:53:54 +00:00
// and now we know the course for sure and maybe the cm, call require_login with it
// todo this will have to change when we have things exporting content outside the course context (eg blogs)
require_login ( $course , false , $cm );
2008-07-25 08:14:11 +00:00
$extranav [] = array ( 'type' => 'title' , 'name' => get_string ( 'exporting' , 'portfolio' ));
$navigation = build_navigation ( $extranav , $cm );
2009-05-02 13:23:22 +00:00
// finally! set up the exporter object with the portfolio instance, caller information, and navigation elements
2008-07-25 08:14:11 +00:00
$exporter = new portfolio_exporter ( $instance , $caller , $callbackfile , $navigation );
2009-05-02 13:23:22 +00:00
// set the export-specific variables, and save.
2008-07-25 08:14:11 +00:00
$exporter -> set ( 'user' , $USER );
2008-08-22 13:25:53 +00:00
$exporter -> set ( 'sesskey' , sesskey ());
2008-08-06 15:27:42 +00:00
$exporter -> save ();
2009-05-02 13:23:22 +00:00
// and finally, put it in the session for waking up again later.
2008-08-06 15:27:42 +00:00
$SESSION -> portfolioexport = $exporter -> get ( 'id' );
2008-07-25 08:14:11 +00:00
}
if ( ! $exporter -> get ( 'instance' )) {
// we've just arrived but have no instance
2009-05-02 13:23:22 +00:00
// in this case the exporter object and the caller object have been set up above
// so just make a little form to select the portfolio plugin instance,
// which is the last thing to do before starting the export.
2008-08-04 16:07:37 +00:00
$mform = new portfolio_instance_select ( '' , array ( 'caller' => $exporter -> get ( 'caller' )));
if ( $mform -> is_cancelled ()) {
2008-08-06 15:27:42 +00:00
$exporter -> cancel_request ();
2008-08-04 16:07:37 +00:00
} else if ( $fromform = $mform -> get_data ()){
2008-08-06 15:27:42 +00:00
redirect ( $CFG -> wwwroot . '/portfolio/add.php?instance=' . $fromform -> instance . '&id=' . $exporter -> get ( 'id' ));
2008-08-04 16:07:37 +00:00
exit ;
}
else {
2008-08-20 13:30:37 +00:00
$exporter -> print_header ( 'selectplugin' );
2008-08-04 16:07:37 +00:00
print_simple_box_start ();
$mform -> display ();
print_simple_box_end ();
print_footer ();
exit ;
2008-07-25 08:14:11 +00:00
}
}
2009-05-02 13:23:22 +00:00
// if we haven't been passed &stage= grab it from the exporter.
if ( ! $stage ) {
2008-08-19 10:17:39 +00:00
$stage = $exporter -> get ( 'stage' );
}
2008-08-09 14:24:58 +00:00
// for places returning control to pass (rather than PORTFOLIO_STAGE_PACKAGE
// which is unstable if they can't get to the constant (eg external system)
2009-05-02 13:23:22 +00:00
$alreadystolen = false ;
if ( $postcontrol ) { // the magic request variable plugins must pass on returning here
2008-08-19 14:20:32 +00:00
try {
2009-05-02 13:23:22 +00:00
// allow it to read whatever gets sent back in the request
// this is useful for plugins that redirect away and back again
// adding a token to the end of the url, for example box.net
2008-08-19 14:20:32 +00:00
$exporter -> instance () -> post_control ( $stage , array_merge ( $_GET , $_POST ));
} catch ( portfolio_plugin_exception $e ) {
portfolio_export_rethrow_exception ( $exporter , $e );
}
2009-05-02 13:23:22 +00:00
$alreadystolen = true ; // remember this so we don't get caught in a steal control loop!
2008-08-09 14:24:58 +00:00
}
2008-08-19 10:17:39 +00:00
2008-09-04 10:32:17 +00:00
// actually do the work now..
2008-07-25 08:14:11 +00:00
$exporter -> process_stage ( $stage , $alreadystolen );
?>