From ac6a5492193020d336438faf18ddd48faf424bd9 Mon Sep 17 00:00:00 2001
From: mjollnir_ <mjollnir_>
Date: Tue, 19 Aug 2008 10:17:39 +0000
Subject: [PATCH] MDL-16047 - nicer handling of interupted exports

---
 lang/en_utf8/portfolio.php |  2 +-
 lib/portfoliolib.php       | 11 ++++++++---
 portfolio/add.php          |  7 +++++--
 3 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/lang/en_utf8/portfolio.php b/lang/en_utf8/portfolio.php
index 6f303f8398..a7049b0cc8 100644
--- a/lang/en_utf8/portfolio.php
+++ b/lang/en_utf8/portfolio.php
@@ -3,7 +3,7 @@
 $string['addnewportfolio'] = 'Add a new portfolio';
 $string['addtoportfolio'] = 'Add to portfolio';
 $string['addalltoportfolio'] = 'Add all to portfolio';
-$string['alreadyexporting'] = 'You already have an active portfolio export in this session. Please complete that first, or <a href=\"$a\">click here</a> to cancel it.';
+$string['alreadyexporting'] = 'You already have an active portfolio export in this session. Please <a href=\"$a->finish\">complete that first</a>, or <a href=\"$a->cancel\">click here</a> to cancel it.';
 $string['availableformats'] = 'Available export formats';
 $string['callercouldnotpackage'] = 'Failed to package up your data for export';
 $string['cannotsetvisible'] = 'Cannot set this to visible - the plugin has been completely disabled because of a misconfiguration';
diff --git a/lib/portfoliolib.php b/lib/portfoliolib.php
index b0fe301fb8..3da55544da 100644
--- a/lib/portfoliolib.php
+++ b/lib/portfoliolib.php
@@ -185,7 +185,10 @@ function portfolio_add_button($callbackclass, $callbackargs, $callbackfile=null,
     }
 
     if (isset($SESSION->portfolioexport)) {
-        print_error('alreadyexporting', 'portfolio', null, $CFG->wwwroot . '/portfolio/add.php?cancel=1');
+        $a = new StdClass;
+        $a->cancel = $CFG->wwwroot . '/portfolio/add.php?cancel=1';
+        $a->finish = $CFG->wwwroot . '/portfolio/add.php?id=' . $SESSION->portfolioexport;
+        print_error('alreadyexporting', 'portfolio', null, $a);
     }
 
     if (empty($callbackfile)) {
@@ -1715,6 +1718,8 @@ final class portfolio_exporter {
     public $instancefile;
     public $callerfile;
 
+    private $stage;
+
     /**
     * id of this export
     * matches record in portfolio_tempdata table
@@ -1787,9 +1792,9 @@ final class portfolio_exporter {
     * @return boolean whether or not to process the next stage. this is important as the function is called recursively.
     */
     public function process_stage($stage, $alreadystolen=false) {
+        $this->set('stage', $stage);
+        $this->save();
         if (!$alreadystolen && $url = $this->instance->steal_control($stage)) {
-            $this->set('stage', $stage);
-            $this->save();
             redirect($url);
             break;
         }
diff --git a/portfolio/add.php b/portfolio/add.php
index fb6607436b..80438ea305 100644
--- a/portfolio/add.php
+++ b/portfolio/add.php
@@ -114,15 +114,18 @@ if (!$exporter->get('instance')) {
     }
 }
 
-$stage = optional_param('stage', PORTFOLIO_STAGE_CONFIG);
+if (!$stage = optional_param('stage', PORTFOLIO_STAGE_CONFIG)) {
+    $stage = $exporter->get('stage');
+}
+
 $alreadystolen = false;
 // 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)
 if ($postcontrol = optional_param('postcontrol', 0, PARAM_INT)) {
-    $stage = $exporter->get('stage');
     $exporter->instance()->post_control($stage, array_merge($_GET, $_POST));
     $alreadystolen = true;
 }
+
 $exporter->process_stage($stage, $alreadystolen);
 
 ?>
-- 
2.39.5