]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-16134 - exception handling in portfolio code
authormjollnir_ <mjollnir_>
Tue, 19 Aug 2008 14:20:32 +0000 (14:20 +0000)
committermjollnir_ <mjollnir_>
Tue, 19 Aug 2008 14:20:32 +0000 (14:20 +0000)
lang/en_utf8/portfolio.php
lib/portfoliolib.php
mod/chat/lib.php
mod/data/lib.php
mod/forum/lib.php
mod/glossary/lib.php
portfolio/add.php
portfolio/file.php
portfolio/type/boxnet/lib.php

index b2b8553270558f14b8d2b48e1497400e578c2569..3bcd5e76b0d5101fc5b8b7ecb9ff24a1b7931064 100644 (file)
@@ -23,6 +23,7 @@ $string['exporting'] = 'Exporting to portfolio';
 $string['exportcomplete'] = 'Portfolio export complete!';
 $string['exportqueued'] = 'Portfolio export has been successfully queued for transfer';
 $string['exportedpreviously'] = 'Previous exports';
+$string['exportexceptionnoexporter'] = 'A portfolio_export_exception was thrown with an active session but no exporter object';
 $string['failedtosendpackage'] = 'Failed to send your data to the selected portfolio system!';
 $string['filedenied'] = 'Access denied to this file';
 $string['filenotfound'] = 'File not found';
@@ -40,6 +41,10 @@ $string['invalidaddformat'] = 'Invalid add format passed to portfolio_add_button
 $string['invalidtempid'] = 'Invalid export id. maybe it has expired';
 $string['invalidformat'] = 'Something is exporting an invalid format, $a';
 $string['invalidinstance'] = 'Could not find that portfolio instance';
+$string['invalidproperty'] = 'Could not find that property ($a->property of $a->class)';
+$string['invalidexportproperty'] = 'Could not find that export config property ($a->property of $a->class)';
+$string['invaliduserproperty'] = 'Could not find that user config property ($a->property of $a->class)';
+$string['invalidconfigproperty'] = 'Could not find that config property ($a->property of $a->class)';
 $string['manageportfolios'] = 'Manage portfolios';
 $string['manageyourportfolios'] = 'Manage your portfolios';
 $string['noavailableplugins'] = 'Sorry, but there are no available portfolios for you to export to';
@@ -62,6 +67,7 @@ $string['selectplugin'] = 'Select portfolio plugin to export to';
 $string['someinstancesdisabled'] = 'Some configured plugin instances have been disabled either because they are misconfigured or rely on something else that is';
 $string['somepluginsdisabled']  = 'Some entire plugins have been disabled because they are either misconfigured or rely on something else that is:';
 $string['sure'] = 'Are you sure you want to delete \'$a\'?  This cannot be undone.';
+$string['thirdpartyexception'] = 'A third party exception was thrown during portfolio export ($a). Caught and rethrown but this should really be fixed';
 $string['wanttowait_moderate'] = 'Do you want to wait for this transfer? It might take a few minutes';
 $string['wanttowait_high'] = 'It is not recommended that you wait for this transfer to complete, but you can if you\'re sure and know what you\'re doing';
 $string['wait'] = 'Wait';
index d722bb7dbaf32a8b8e63b73695b3972fa3d572f5..869c7646acf1d304e5b40e05700b074a27322fc4 100644 (file)
@@ -400,13 +400,14 @@ function portfolio_supported_formats_intersect($callerformats, $pluginformats) {
             debugging(get_string('invalidformat', 'portfolio', $cf));
             continue;
         }
+        $cfobj = new $allformats[$cf]();
         foreach ($pluginformats as $p => $pf) {
             if (!array_key_exists($pf, $allformats)) {
                 debugging(get_string('invalidformat', 'portfolio', $pf));
                 unset($pluginformats[$p]); // to avoid the same warning over and over
                 continue;
             }
-            if ($cf instanceof $pf) {
+            if ($cfobj instanceof $allformats[$pf]) {
                 $intersection[] = $cf;
             }
         }
@@ -430,7 +431,7 @@ function portfolio_instance($instanceid, $record=null) {
         $instance  = $record;
     } else {
         if (!$instance = $DB->get_record('portfolio_instance', array('id' => $instanceid))) {
-            return false; // @todo throw exception?
+            throw new portfolio_exception('invalidinstance', 'portfolio');
         }
     }
     require_once($CFG->dirroot . '/portfolio/type/'. $instance->plugin . '/lib.php');
@@ -715,14 +716,13 @@ abstract class portfolio_caller_base {
     * generic getter for properties belonging to this instance
     * <b>outside</b> the subclasses
     * like name, visible etc.
-    *
-    * @todo  determine what to return in the error case
     */
     public function get($field) {
         if (property_exists($this, $field)) {
             return $this->{$field};
         }
-        return false; // @todo throw exception?
+        $a = (object)array('property' => $field, 'class' => get_class($this));
+        throw new portfolio_export_exception($this->get('exporter'), 'invalidproperty', 'portfolio', $this->get_return_url(), $a);
     }
 
     /**
@@ -730,7 +730,6 @@ abstract class portfolio_caller_base {
     * <b>outside</b> the subclass
     * like name, visible, etc.
     *
-    * @todo  determine what to return in the error case
     */
     public final function set($field, &$value) {
         if (property_exists($this, $field)) {
@@ -738,8 +737,8 @@ abstract class portfolio_caller_base {
             $this->dirty = true;
             return true;
         }
-        return false; // @todo throw exception?
-
+        $a = (object)array('property' => $field, 'class' => get_class($this));
+        throw new portfolio_export_exception($this->get('exporter'), 'invalidproperty', 'portfolio', $this->get_return_url(), $a);
     }
 
     /**
@@ -756,7 +755,8 @@ abstract class portfolio_caller_base {
         );
         foreach ($config as $key => $value) {
             if (!in_array($key, $allowed)) {
-                continue; // @ todo throw exception
+                $a = (object)array('property' => $key, 'class' => get_class($this));
+                throw new portfolio_export_exception($this->get('exporter'), 'invalidexportproperty', 'portfolio', $this->get_return_url(), $a);
             }
             $this->exportconfig[$key] = $value;
         }
@@ -767,7 +767,6 @@ abstract class portfolio_caller_base {
     * subclasses shouldn't need to override this
     *
     * @param string key the config item to fetch
-    * @todo figure out the error cases (item not found or not allowed)
     */
     public final function get_export_config($key) {
         $allowed = array_merge(
@@ -775,10 +774,11 @@ abstract class portfolio_caller_base {
             $this->get_allowed_export_config()
         );
         if (!in_array($key, $allowed)) {
-            return false; // @todo throw exception?
+            $a = (object)array('property' => $key, 'class' => get_class($this));
+            throw new portfolio_export_exception($this->get('exporter'), 'invalidexportproperty', 'portfolio', $this->get_return_url(), $a);
         }
         if (!array_key_exists($key, $this->exportconfig)) {
-            return null; // @todo what to return|
+            return null;
         }
         return $this->exportconfig[$key];
     }
@@ -1078,7 +1078,8 @@ abstract class portfolio_plugin_base {
         );
         foreach ($config as $key => $value) {
             if (!in_array($key, $allowed)) {
-                continue; // @ todo throw exception
+                $a = (object)array('property' => $key, 'class' => get_class($this));
+                throw new portfolio_export_exception($this->get('exporter'), 'invalidexportproperty', 'portfolio', $this->get_return_url(), $a);
             }
             $this->exportconfig[$key] = $value;
         }
@@ -1092,7 +1093,6 @@ abstract class portfolio_plugin_base {
     *
     * @return string config value
     *
-    * @todo figure out the error cases
     */
     public final function get_export_config($key) {
         $allowed = array_merge(
@@ -1100,10 +1100,11 @@ abstract class portfolio_plugin_base {
             $this->get_allowed_export_config()
         );
         if (!in_array($key, $allowed)) {
-            return false; // @todo throw exception?
+            $a = (object)array('property' => $key, 'class' => get_class($this));
+            throw new portfolio_export_exception($this->get('exporter'), 'invalidexportproperty', 'portfolio', $this->get_return_url(), $a);
         }
         if (!array_key_exists($key, $this->exportconfig)) {
-            return null; // @todo what to return|
+            return null;
         }
         return $this->exportconfig[$key];
     }
@@ -1289,7 +1290,7 @@ abstract class portfolio_plugin_base {
         global $DB;
         if (!$record) {
             if (!$record = $DB->get_record('portfolio_instance', array('id' => $instanceid))) {
-                return false; // @todo throw exception?
+                throw new portfolio_exception('invalidinstance', 'portfolio');
             }
         }
         foreach ((array)$record as $key =>$value) {
@@ -1353,7 +1354,8 @@ abstract class portfolio_plugin_base {
                 continue;
             }
             if (!in_array($key, $this->get_allowed_config())) {
-                continue; // @todo throw exception?
+                $a = (object)array('property' => $key, 'class' => get_class($this));
+                throw new portfolio_export_exception($this->get('exporter'), 'invalidconfigproperty', 'portfolio', null, $a);
             }
             if (!isset($this->config->{$key})) {
                 $DB->insert_record('portfolio_instance_config', (object)array(
@@ -1366,7 +1368,6 @@ abstract class portfolio_plugin_base {
             }
             $this->config->{$key} = $value;
         }
-        return true; // @todo - if we're going to change here to throw exceptions, this can change
     }
 
     /**
@@ -1375,17 +1376,16 @@ abstract class portfolio_plugin_base {
     * @param string $key key to fetch
     *
     * @return string the corresponding value
-    *
-    * @todo determine what to return in the error case.
     */
     public final function get_config($key) {
         if (!in_array($key, $this->get_allowed_config())) {
-            return false; // @todo throw exception?
+            $a = (object)array('property' => $key, 'class' => get_class($this));
+            throw new portfolio_export_exception($this->get('exporter'), 'invalidconfigproperty', 'portfolio', null, $a);
         }
         if (isset($this->config->{$key})) {
             return $this->config->{$key};
         }
-        return false; // @todo null?
+        return null;
     }
 
     /**
@@ -1396,7 +1396,6 @@ abstract class portfolio_plugin_base {
     *
     * @return string the corresponding value
     *
-    * @todo determine what to return in the error case
     */
     public final function get_user_config($key, $userid=0) {
         global $DB;
@@ -1407,7 +1406,8 @@ abstract class portfolio_plugin_base {
 
         if ($key != 'visible') { // handled by the parent class
             if (!in_array($key, $this->get_allowed_user_config())) {
-                return false; // @todo throw exception?
+                $a = (object)array('property' => $key, 'class' => get_class($this));
+                throw new portfolio_export_exception($this->get('exporter'), 'invaliduserproperty', 'portfolio', null, $a);
             }
         }
         if (!array_key_exists($userid, $this->userconfig)) {
@@ -1430,7 +1430,6 @@ abstract class portfolio_plugin_base {
     * @param mixed $config array or stdclass containing key/value pairs to set
     * @param integer $userid userid to set config for (defaults to current)
     *
-    * @todo determine what to return in the error case
     */
     public final function set_user_config($config, $userid=0) {
         global $DB;
@@ -1441,7 +1440,8 @@ abstract class portfolio_plugin_base {
 
         foreach ($config as $key => $value) {
             if ($key != 'visible' && !in_array($key, $this->get_allowed_user_config())) {
-                continue; // @todo throw exception?
+                $a = (object)array('property' => $key, 'class' => get_class($this));
+                throw new portfolio_export_exception($this->get('exporter'), 'invaliduserproperty', 'portfolio', null, $a);
             }
             if (!$existing = $DB->get_record('portfolio_instance_user', array('instance'=> $this->id, 'userid' => $userid, 'name' => $key))) {
                 $DB->insert_record('portfolio_instance_user', (object)array(
@@ -1455,7 +1455,6 @@ abstract class portfolio_plugin_base {
             }
             $this->userconfig[$userid]->{$key} = $value;
         }
-        return true; // @todo
 
     }
 
@@ -1464,13 +1463,13 @@ abstract class portfolio_plugin_base {
     * <b>outside</b> the subclasses
     * like name, visible etc.
     *
-    * @todo  determine what to return in the error case
     */
     public final function get($field) {
         if (property_exists($this, $field)) {
             return $this->{$field};
         }
-        return false; // @todo throw exception?
+        $a = (object)array('property' => $field, 'class' => get_class($this));
+        throw new portfolio_export_exception($this->get('exporter'), 'invalidproperty', 'portfolio', $a);
     }
 
     /**
@@ -1478,7 +1477,6 @@ abstract class portfolio_plugin_base {
     * <b>outside</b> the subclass
     * like name, visible, etc.
     *
-    * @todo  determine what to return in the error case
     */
     public final function set($field, $value) {
         if (property_exists($this, $field)) {
@@ -1486,7 +1484,8 @@ abstract class portfolio_plugin_base {
             $this->dirty = true;
             return true;
         }
-        return false; // @todo throw exception?
+        $a = (object)array('property' => $field, 'class' => get_class($this));
+        throw new portfolio_export_exception($this->get('exporter'), 'invalidproperty', 'portfolio', $a);
 
     }
 
@@ -1666,7 +1665,7 @@ final class portfolio_admin_form extends moodleform {
         }
 
         if (isset($result) && is_string($result)) { // something went wrong, stop
-            return $this->raise_error($result, 'portfolio_' . $this->plugin, $CFG->wwwroot . '/' . $CFG->admin . '/portfolio.php');
+            throw new portfolio_exception($result, 'portfolio_' . $this->plugin, $CFG->wwwroot . '/' . $CFG->admin . '/portfolio.php');
         }
 
         // and set the data if we have some.
@@ -1765,7 +1764,7 @@ final class portfolio_exporter {
     *
     * @param portfolio_plugin_base subclass $instance portfolio instance (passed by reference)
     * @param portfolio_caller_base subclass $caller portfolio caller (passed by reference)
-    * @param string $callerfile @todo document
+    * @param string $callerfile path to callerfile (relative to dataroot)
     * @param string $navigation result of build_navigation (passed to print_header)
     */
     public function __construct(&$instance, &$caller, $callerfile, $navigation) {
@@ -1785,24 +1784,20 @@ final class portfolio_exporter {
     * generic getter for properties belonging to this instance
     * <b>outside</b> the subclasses
     * like name, visible etc.
-    *
-    * @todo  determine what to return in the error case
     */
     public function get($field) {
         if (property_exists($this, $field)) {
             return $this->{$field};
         }
-        return false; // @todo throw exception?
+        $a = (object)array('property' => $field, 'class' => get_class($this));
+        throw new portfolio_export_exception($this, 'invalidproperty', 'portfolio', $a);
     }
 
     /**
     * generic setter for properties belonging to this instance
     * <b>outside</b> the subclass
     * like name, visible, etc.
-    *
-    * @todo  determine what to return in the error case
     */
-
     public function set($field, &$value) {
         if (property_exists($this, $field)) {
             $this->{$field} =& $value;
@@ -1813,7 +1808,8 @@ final class portfolio_exporter {
             $this->dirty = true;
             return true;
         }
-        return false; // @todo throw exception?
+        $a = (object)array('property' => $field, 'class' => get_class($this));
+        throw new portfolio_export_exception($this, 'invalidproperty', 'portfolio', $a);
 
     }
     /**
@@ -1847,12 +1843,21 @@ final class portfolio_exporter {
         );
 
         $function = 'process_stage_' . $functionmap[$stage];
-        if ($this->$function()) {
-            // if we get through here it means control was returned
-            // as opposed to wanting to stop processing
-            // eg to wait for user input.
-            $stage++;
-            return $this->process_stage($stage);
+        try {
+            if ($this->$function()) {
+                // if we get through here it means control was returned
+                // as opposed to wanting to stop processing
+                // eg to wait for user input.
+                $stage++;
+                return $this->process_stage($stage);
+            }
+        } catch (portfolio_caller_exception $e) {
+            portfolio_export_rethrow_exception($this, $e);
+        } catch (portfolio_plugin_exception $e) {
+            portfolio_export_rethrow_exception($this, $e);
+        } catch (Exception $e) {
+            debugging(get_string('thirdpartyexception', 'portfolio', get_class($e)));
+            portfolio_export_rethrow_exception($this, $e);
         }
         $this->save();
         return false;
@@ -1894,7 +1899,7 @@ final class portfolio_exporter {
         $expectedtime = $this->instance->expected_time($this->caller->expected_time());
         if (count($formats) == 0) {
             // something went wrong, we should not have gotten this far.
-            return $this->raise_error('nocommonformats', 'portfolio', get_class($caller));
+            throw new portfolio_export_exception($this, 'nocommonformats', 'portfolio', get_class($this->caller));
         }
         // even if neither plugin or caller wants any config, we have to let the user choose their format, and decide to wait.
         if ($pluginobj || $callerobj || count($formats) > 1 || $expectedtime != PORTFOLIO_TIME_LOW) {
@@ -1911,7 +1916,7 @@ final class portfolio_exporter {
                 $this->cancel_request();
             } else if ($fromform = $mform->get_data()){
                 if (!confirm_sesskey()) {
-                    return $this->raise_error('confirmsesskeybad', '', $caller->get_return_url());
+                    throw new portfolio_export_exception($this, 'confirmsesskeybad', '');
                 }
                 $pluginbits = array();
                 $callerbits = array();
@@ -2041,10 +2046,10 @@ final class portfolio_exporter {
         // the caller is given control to package it up however it wants
         // and then the portfolio plugin is given control to do whatever it wants.
         if (!$this->caller->prepare_package()) {
-            return $this->raise_error('callercouldnotpackage', 'portfolio', $this->caller->get_return_url());
+            throw new portfolio_export_exception($this, 'callercouldnotpackage', 'portfolio');
         }
         if (!$package = $this->instance->prepare_package()) {
-            return $this->raise_error('plugincouldnotpackage', 'portfolio', $this->caller->get_return_url());
+            throw new portfolio_export_exception($this, 'plugincouldnotpackage', 'portfolio');
         }
         return true;
     }
@@ -2077,7 +2082,7 @@ final class portfolio_exporter {
     public function process_stage_send() {
         // send the file
         if (!$this->instance->send_package()) {
-            return $this->raise_error('failedtosendpackage', 'portfolio');
+            throw new portfolio_export_exception($this, 'failedtosendpackage', 'portfolio');
         }
         // log the transfer
         global $DB;
@@ -2138,24 +2143,6 @@ final class portfolio_exporter {
         print_header($titlestr, $headerstr, $this->navigation);
     }
 
-    /**
-    * error handler - decides whether we're running interactively or not
-    * and behaves accordingly
-    */
-    public function raise_error($string, $module='moodle', $continue=null, $a=null) {
-        if (defined('FULLME') && FULLME == 'cron') {
-            debugging(get_string($string, $module));
-            return false;
-        }
-        if (isset($this) && $this instanceof portfolio_exporter) {
-            // apparently even calling statically (with :: rather than ->
-            // causes $this to be set in some php versions
-            // (assuming this function is called from some other object - wtF?!)
-            $this->process_stage_cleanup(true);
-        }
-        print_error($string, $module, $continue, $a);
-    }
-
     /**
     * cancels a potfolio request and cleans up the tempdata
     * and redirects the user back to where they started
@@ -2197,7 +2184,7 @@ final class portfolio_exporter {
     public static function rewaken_object($id) {
         global $DB, $CFG;
         if (!$data = $DB->get_record('portfolio_tempdata', array('id' => $id))) {
-            portfolio_exporter::raise_error('invalidtempid', 'portfolio');
+            throw new portfolio_exception('invalidtempid', 'portfolio');
         }
         $exporter = unserialize(base64_decode($data->data));
         if ($exporter->instancefile) {
@@ -2296,7 +2283,8 @@ class portfolio_instance_select extends moodleform {
             true
         );
         if (empty($options)) {
-            portfolio_exporter::raise_error('noavailableplugins', 'portfolio');
+            debugging('noavailableplugins', 'portfolio');
+            return false;
         }
         $mform =& $this->_form;
         $mform->addElement('select', 'instance', get_string('selectplugin', 'portfolio'), $options);
@@ -2366,4 +2354,62 @@ class portfolio_format_html extends portfolio_format_file {}
 */
 class portfolio_format_mbkp extends portfolio_format_file {}
 
+/**
+* exception to throw during an export - will clean up session and tempdata
+*/
+class portfolio_export_exception extends portfolio_exception {
+
+    /**
+    * constructor.
+    * @param object $exporter instance of portfolio_exporter (will handle null case)
+    * @param string $errorcode language string key
+    * @param string $module language string module (optional, defaults to moodle)
+    * @param string $continue url to continue to (optional, defaults to wwwroot)
+    * @param mixed $a language string data (optional, defaults to  null)
+    */
+    public function __construct($exporter, $errorcode, $module=null, $continue=null, $a=null) {
+        if (!empty($exporter) && $exporter instanceof portfolio_exporter) {
+            if (empty($continue)) {
+                $caller = $exporter->get('caller');
+                if (!empty($caller) && $caller instanceof portfolio_caller_base) {
+                    $continue = $exporter->get('caller')->get_return_url();
+                }
+            }
+            if (!defined('FULLME') || FULLME != 'cron') {
+                $exporter->process_stage_cleanup();
+            }
+        } else {
+            global $SESSION;
+            if (!empty($SESSION->portfolioexport)) {
+                debugging(get_string('exportexceptionnoexporter', 'portfolio'));
+            }
+        }
+        parent::__construct($errorcode, $module, $continue, $a);
+    }
+}
+
+/**
+* exception for callers to throw when they have a problem.
+* usually caught and rethrown as {@see portfolio_export_exception}
+*/
+class portfolio_caller_exception extends portfolio_exception {}
+
+/**
+* top level portfolio exception.
+* sometimes caught and rethrown as {@see portfolio_export_exception}
+*/
+class portfolio_exception extends moodle_exception {}
+
+/**
+* exception for portfolio plugins to throw when they have a problem.
+* usually caught and rethrown as {@see portfolio_export_exception}
+*/
+class portfolio_plugin_exception extends portfolio_exception {}
+
+/**
+* helper function to rethrow a caught portfolio_exception as an export exception
+*/
+function portfolio_export_rethrow_exception($exporter, $e) {
+    throw new portfolio_export_exception($exporter, $e->errorcode, $e->module, $e->link, $e->a);
+}
 ?>
index f794995da39ed0d4c5be5a56245957973d6078be..4f7b6785816a5941db792328db882bf09d79fd71 100644 (file)
@@ -829,7 +829,7 @@ class chat_portfolio_caller extends portfolio_module_caller_base {
     public function __construct($callbackargs) {
         global $DB, $USER;
         if (!$this->cm = get_coursemodule_from_id('chat', $callbackargs['id'])) {
-            portfolio_exporter::raise_error('invalidid', 'chat');
+            throw new portfolio_caller_exception('invalidid', 'chat');
         }
         $this->chat = $DB->get_record('chat', array('id' => $this->cm->instance));
         $select = 'chatid = ?';
index 807a835968fc6682fd5c8a35cc4f37cbcfef17ac..4ad04dd59060359bcdb848b6f34ea3f21e5e0339 100755 (executable)
@@ -2448,7 +2448,7 @@ class data_portfolio_caller extends portfolio_module_caller_base {
     public function __construct($callbackargs) {
         global $DB;
         if (!$this->cm = get_coursemodule_from_id('data', $callbackargs['id'])) {
-            portfolio_exporter::raise_error('invalidid', 'data');
+            throw new portfolio_caller_exception('invalidid', 'data');
         }
         $this->data = $DB->get_record('data', array('id' => $this->cm->instance));
         $fieldrecords = $DB->get_records('data_fields', array('dataid'=>$this->cm->instance), 'id');
@@ -2532,15 +2532,15 @@ class data_portfolio_caller extends portfolio_module_caller_base {
                 $filename = clean_filename($this->cm->name . '.csv');
                 break;
             case 'xls':
-                portfolio_exporter::raise_error('notimplemented', 'portfolio');
+                throw new portfolio_caller_exception('notimplemented', 'portfolio', '', 'xls');
                 $content = data_export_xls($this->exportdata, $this->cm->name, $count, true);
                 break;
             case 'ods':
-                portfolio_exporter::raise_error('notimplemented', 'portfolio');
+                throw new portfolio_caller_exception('notimplemented', 'portfolio', '', 'ods');
                 $content = data_export_ods($this->exportdata, $this->cm->name, $count, true);
                 break;
             default:
-                portfolio_exporter::raise_error('notimplemented', 'portfolio', '', $this->exporttype);
+                throw new portfolio_caller_exception('notimplemented', 'portfolio', '', $this->exporttype);
             break;
         }
         return $this->exporter->write_new_file($content, $filename);
index 0eee748f90393bbaac6ab4828ea13048a20c6b6a..f4c476e0c1666f22df90ea5886f16b2cbe874870 100644 (file)
@@ -7133,8 +7133,7 @@ class forum_portfolio_caller extends portfolio_module_caller_base {
         // a single post, with or without attachment
         // or just an attachment with no post
         if (!$this->post) { // whole discussion
-            portfolio_exporter::raise_error('exoprting whole discussion not implemented - see MDL-15758');
-            // @todo see MDL-15758
+            throw new portfolio_caller_exception('TODO: exporting whole discussion not implemented - see MDL-15758');
         } else {
             $status = true;
             if ($this->files) {
@@ -7229,8 +7228,7 @@ class forum_portfolio_caller extends portfolio_module_caller_base {
             }
             return sha1($attachsha1 . ',' . $this->post->subject . ',' . $this->post->message);
         }
-        portfolio_exporter::raise_error('exporting whole discussion not implemented - see MDL-15758');
-        // @todo see MDL-15758
+        throw new portfolio_caller_exception('TODO: exporting whole discussion not implemented - see MDL-15758');
     }
 
     function expected_time() {
index d8b43e66e4ea8eaf610a030944d38957574bd83f..af21ac05f4d56a95dc93a60c12ebd91ded646899 100644 (file)
@@ -2379,10 +2379,10 @@ class glossary_csv_portfolio_caller extends portfolio_module_caller_base {
     public function __construct($callbackargs) {
         global $DB;
         if (!$this->cm = get_coursemodule_from_id('glossary', $callbackargs['id'])) {
-            portfolio_exporter::raise_error('invalidid', 'glossary');
+            throw new portfolio_caller_exception('invalidid', 'glossary');
         }
         if (!$this->glossary = $DB->get_record('glossary', array('id' => $this->cm->instance))) {
-            portfolio_exporter::raise_error('invalidid', 'glossary');
+            throw new portfolio_caller_exception('invalidid', 'glossary');
         }
         $entries = $DB->get_records('glossary_entries', array('glossaryid' => $this->glossary->id));
         list($where, $params) = $DB->get_in_or_equal(array_keys($entries));
@@ -2446,14 +2446,14 @@ class glossary_entry_portfolio_caller extends portfolio_module_caller_base {
     public function __construct($callbackargs) {
         global $DB;
         if (!$this->cm = get_coursemodule_from_id('glossary', $callbackargs['id'])) {
-            portfolio_exporter::raise_error('invalidid', 'glossary');
+            throw new portfolio_caller_exception('invalidid', 'glossary');
         }
         if (!$this->glossary = $DB->get_record('glossary', array('id' => $this->cm->instance))) {
-            portfolio_exporter::raise_error('invalidid', 'glossary');
+            throw new portfolio_caller_exception('invalidid', 'glossary');
         }
         if (!array_key_exists('entryid', $callbackargs)
             || !$this->entry = $DB->get_record('glossary_entries', array('id' => $callbackargs['entryid']))) {
-            portfolio_exporter::raise_error('noentry', 'glossary');
+            throw new portfolio_caller_exception('noentry', 'glossary');
         }
     }
 
index 80438ea3058a330ccbc38ce65fbff8a1e657e6e4..c731799cbf867b1e7663920b785d57b4794535b0 100644 (file)
@@ -22,8 +22,10 @@ if ($dataid) {
     }
     if (!$exporter->get('instance')) {
         if ($instance = optional_param('instance', '', PARAM_INT)) {
-            if (!$instance = portfolio_instance($instance)) {
-                $exporter->raise_error('invalidinstance', 'portfolio');
+            try {
+                $instance = portfolio_instance($instance);
+            } catch (portfolio_exception $e) {
+                portfolio_export_rethrow_exception($exporter, $e);
             }
             if ($broken = portfolio_instance_sanity_check($instance)) {
                 print_error(get_string($broken[$instance->get('id')], 'portfolio_' . $instance->get('plugin')));
@@ -36,11 +38,13 @@ if ($dataid) {
 } else {
     // we'e just posted here for the first time and have might the instance already
     if ($instance = optional_param('instance', 0, PARAM_INT)) {
-        if (!$instance = portfolio_instance($instance)) {
-            portfolio_exporter::raise_error('invalidinstance', 'portfolio');
+        try {
+            $instance = portfolio_instance($instance);
+        } catch (portfolio_exception $e) {
+            portfolio_export_rethrow_exception($exporter, $e);
         }
         if ($broken = portfolio_instance_sanity_check($instance)) {
-            print_error(get_string($broken[$instance->get('id')], 'portfolio_' . $instance->get('plugin')));
+            throw new portfolio_exception($broken[$instance->get('id')], 'portfolio_' . $instance->get('plugin'));
         }
         $instance->set('user', $USER);
     } else {
@@ -122,7 +126,11 @@ $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)) {
-    $exporter->instance()->post_control($stage, array_merge($_GET, $_POST));
+    try {
+        $exporter->instance()->post_control($stage, array_merge($_GET, $_POST));
+    } catch (portfolio_plugin_exception $e) {
+        portfolio_export_rethrow_exception($exporter, $e);
+    }
     $alreadystolen = true;
 }
 
index b53c9f560a8d1ca620ed04f68ac60314990009a5..3135bc6656341bb8b6dcbc0a249e65f0b89a59b8 100644 (file)
@@ -9,16 +9,16 @@ $id = required_param('id', PARAM_INT);
 $exporter = portfolio_exporter::rewaken_object($id);
 
 if ($exporter->get('instance')->is_push()) {
-    $exporter->raise_error('filedenied', 'portfolio');
+    throw new portfolio_export_exception($exporter, 'filedenied', 'portfolio');
 }
 
 if (!$exporter->get('instance')->verify_file_request_params(array_merge($_GET, $_POST))) {
-    $exporter->raise_error('filedenied', 'portfolio');
+    throw new portfolio_export_exception($exporter, 'filedenied', 'portfolio');
 }
 
 $file = $exporter->get('instance')->get('file');
 if (!($file instanceof stored_file)) {
-    $exporter->raise_error('filenotfound', 'portfolio');
+    throw new portfolio_export_exception($exporter, 'filenotfound', 'portfolio');
 }
 
 send_stored_file($file, 0, 0, true, null, true);
index 0d302a73d3de22cb1201d1ad37408deade454756..15b2f1f2d99a7320ae2fd78be43cda19ce943fe5 100644 (file)
@@ -39,7 +39,7 @@ class portfolio_plugin_boxnet extends portfolio_plugin_push_base {
         parent::set_export_config($config);
         if (array_key_exists('newfolder', $config) && !empty($config['newfolder'])) {
             if (!$created = $this->boxclient->createFolder($config['newfolder'])) {
-                portfolio_exporter::raise_error('foldercreatefailed', 'portfolio_boxnet');
+                throw new portfolio_plugin_exception('foldercreatefailed', 'portfolio_boxnet');
             }
             $this->folders[$created['folder_id']] = $created['folder_type'];
             parent::set_export_config(array('folder' => $created['folder_id']));
@@ -123,7 +123,7 @@ class portfolio_plugin_boxnet extends portfolio_plugin_push_base {
             return false;
         }
         if (!$this->ensure_ticket()) {
-            portfolio_exporter::raise_error('noticket', 'portfolio_boxnet');
+            throw new portfolio_plugin_exception('noticket', 'portfolio_boxnet');
         }
         $token = $this->get_user_config('authtoken', $this->get('user')->id);
         $ctime= $this->get_user_config('authtokenctime', $this->get('user')->id);
@@ -140,7 +140,7 @@ class portfolio_plugin_boxnet extends portfolio_plugin_push_base {
             return;
         }
         if (!array_key_exists('auth_token', $params) || empty($params['auth_token'])) {
-            portfolio_exporter::raise_error('noauthtoken', 'portfolio_boxnet');
+            throw new portfolio_plugin_exception('noauthtoken', 'portfolio_boxnet');
         }
         $this->authtoken = $params['auth_token'];
         $this->boxclient->auth_token = $this->authtoken;
@@ -154,7 +154,7 @@ class portfolio_plugin_boxnet extends portfolio_plugin_push_base {
         $this->boxclient = new boxclient($this->get_config('apikey'), '');
         $ticket_return = $this->boxclient->getTicket();
         if ($this->boxclient->isError() || empty($ticket_return)) {
-            portfolio_exporter::raise_error('noticket', 'portfolio_boxnet');
+            throw new portfolio_plugin_exception('noticket', 'portfolio_boxnet');
         }
         $this->ticket = $ticket_return['ticket'];
         return $this->ticket;
@@ -168,12 +168,12 @@ class portfolio_plugin_boxnet extends portfolio_plugin_push_base {
             || empty($this->authtoken)
             || empty($this->boxclient)) {
             // if we don't have these we're pretty much screwed
-            portfolio_exporter::raise_error('folderlistfailed', 'portfolio_boxnet');
+            throw new portfolio_plugin_exception('folderlistfailed', 'portfolio_boxnet');
             return false;
         }
         $this->accounttree = $this->boxclient->getAccountTree();
         if ($this->boxclient->isError()) {
-            portfolio_exporter::raise_error('folderlistfailed', 'portfolio_boxnet');
+            throw new portfolio_plugin_exception('folderlistfailed', 'portfolio_boxnet');
         }
         if (!is_array($this->accounttree)) {
             return false;