Skip to content

Commit

Permalink
Launch.php finished with new try/catch code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ADLMeganBohland committed Aug 15, 2024
1 parent bce43da commit f6db111
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 57 deletions.
14 changes: 14 additions & 0 deletions classes/local/errorover.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,21 @@ function custom_warningAU($errno, $errstr, $errfile, $errline)
exit;

}
/**
* An warning handler to use to post better warnings to users for troubleshooting.
* @param mixed $errno
* @param mixed $errstr
* @param mixed $errfile
* @param mixed $errline
* @throws \mod_cmi5launch\local\nullException
* @return never
*/
function custom_warning($errno, $errstr, $errfile, $errline)
{

throw new customException('Error launching experience. Report this to system administrator: <br> '. $errstr .' at '. $errfile .' on ' .$errline, 0);

}

/**
* An exception handler to use in AU cases when many different exceptions for data errors may be thrown.
Expand Down
115 changes: 58 additions & 57 deletions launch.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,24 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Launches the experience with the requested registration
*
* Launches the experience with the requested registration number.
* The cmi5 player does the actual playing. This file enables handling of launch url from the player and data saving.
* @copyright 2023 Megan Bohland
* @copyright Based on work by 2013 Andrew Downes
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

use mod_cmi5launch\local\cmi5_connectors;
use mod_cmi5launch\local\au_helpers;
use mod_cmi5launch\local\customException;
use mod_cmi5launch\local\session_helpers;

require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
require('header.php');
require_once("$CFG->dirroot/lib/outputcomponents.php");
require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
require_once('header.php');

// Include the errorover (error override) funcs.
require_once ($CFG->dirroot . '/mod/cmi5launch/classes/local/errorover.php');

require_login($course, false, $cm);

Expand All @@ -56,83 +58,82 @@
$savesession = $sessionhelper->cmi5launch_get_create_session();
$cmi5launchretrieveurl = $connectors->cmi5launch_get_retrieve_url();
$retrieveaus = $auhelper->get_cmi5launch_retrieve_aus_from_db();
/*
// Retrieve registration id and au index (from AUview.php).
$fromauview = required_param('launchform_registration', PARAM_TEXT);

// Break it into array (AU or session id is first index).
$idandstatus = explode(",", $fromauview);
// Retrieve AU OR session id.
$id = array_shift($idandstatus);
*/
// Retrieve the registration id from previous page.
$id = required_param('launchform_registration', PARAM_TEXT);

// Reload cmi5 instance.
$record = $DB->get_record('cmi5launch', array('id' => $cmi5launch->id));

// Retrieve user's course record.
$userscourse = $DB->get_record('cmi5launch_usercourse', ['courseid' => $record->courseid, 'userid' => $USER->id]);

// Retrieve registration id.
$registrationid = $userscourse->registrationid;
// To hold launch url.
$location = "";

// Most of the functions below have their own error handling. We will encapsulate here in case there are any php errors, such
// as json_decode not working, etc.
// Set error and exception handler to catch and override the default PHP error messages, to make messages more user friendly.
set_error_handler('mod_cmi5launch\local\custom_warning', E_WARNING);
//set_exception_handler('mod_cmi5launch\local\customException');

if (empty($registrationid)) {
echo "<div class='alert alert-error'>" . get_string('cmi5launch_regidempty', 'cmi5launch') . "</div>";
try {
// Retrieve AUs.
$au = $retrieveaus($id);

// Failed to connect to LRS.
if ($CFG->debug == 32767) {
// Retrieve the au index.
$auindex = $au->auindex;

echo "<p>Error attempting to get registration id querystring parameter.</p>";
// Pass in the au index to retrieve a launchurl and session id.
$urldecoded = $cmi5launchretrieveurl($cmi5launch->id, $auindex);

die();
}
}
// Retrieve and store session id in the aus table.
$sessionid = intval($urldecoded['id']);

// To hold launch url.
$location = "";
// Check if there are previous sessions.
if (!$au->sessions == null) {
// We don't want to overwrite so retrieve the sessions before changing them.
$sessionlist = json_decode($au->sessions);
// Now add the new session number.
$sessionlist[] = $sessionid;

// Retrieve AUs.
$au = $retrieveaus($id);
} else {
// If it is null just start fresh.
$sessionlist = array();
$sessionlist[] = $sessionid;
}

// Retrieve the au index.
$auindex = $au->auindex;
// Save sessions.
$au->sessions = json_encode($sessionlist);

// Pass in the au index to retrieve a launchurl and session id.
$urldecoded = $cmi5launchretrieveurl($cmi5launch->id, $auindex);
// The record needs to updated in DB.
$updated = $DB->update_record('cmi5launch_aus', $au, true);

// Retrieve and store session id in the aus table.
$sessionid = intval($urldecoded['id']);
// Retrieve the launch url.
$location = $urldecoded['url'];
// And launch method.
$launchmethod = $urldecoded['launchMethod'];

// Check if there are previous sessions.
if (!$au->sessions == null) {
// We don't want to overwrite so retrieve the sessions before changing them.
$sessionlist = json_decode($au->sessions);
// Now add the new session number.
$sessionlist[] = $sessionid;
} catch (\Throwable $e) {
// Restore default handlers.
restore_exception_handler();
restore_error_handler();

} else {
// If it is null just start fresh.
$sessionlist = array();
$sessionlist[] = $sessionid;
// If there is an error, return the error.
throw new customException("Error in launching experience. Report this error to system administrator: ". $e->getMessage());
}
// Create and save session object to session table.
$savesession($sessionid, $location, $launchmethod);

// Save sessions.
$au->sessions = json_encode($sessionlist);

// The record needs to updated in DB.
$updated = $DB->update_record('cmi5launch_aus', $au, true);
// Last thing check for updates.
cmi5launch_update_grades($cmi5launch, $USER->id);

// Retrieve the launch url.
$location = $urldecoded['url'];
// And launch method.
$launchmethod = $urldecoded['launchMethod'];
header("Location: " . $location);

// Create and save session object to session table.
$savesession($sessionid, $location, $launchmethod);

// Last thing check for updates.
cmi5launch_update_grades($cmi5launch, $USER->id);

header("Location: ". $location);
// Restore default handlers.
restore_exception_handler();
restore_error_handler();

exit;

0 comments on commit f6db111

Please sign in to comment.