diff --git a/AUview.php b/AUview.php index 4f628c3..a5dfcbf 100755 --- a/AUview.php +++ b/AUview.php @@ -22,12 +22,17 @@ */ use mod_cmi5launch\local\session_helpers; +use mod_cmi5launch\local\customException; use mod_cmi5launch\local\au_helpers; - require_once(dirname(dirname(dirname(__FILE__))) . '/config.php'); require('header.php'); require_once("$CFG->dirroot/lib/outputcomponents.php"); + +// Include the errorover (error override) funcs. +require_once ($CFG->dirroot . '/mod/cmi5launch/classes/local/errorover.php'); + + require_login($course, false, $cm); global $cmi5launch, $USER; @@ -99,12 +104,10 @@ function mod_cmi5launch_launchexperience(registration) { id); @@ -121,65 +124,78 @@ function mod_cmi5launch_launchexperience(registration) { // Reload user course instance. $userscourse = $DB->get_record('cmi5launch_usercourse', ['courseid' => $record->courseid, 'userid' => $USER->id]); -// Retrieve the registration id. -$regid = $userscourse->registrationid; - // If it is null there have been no previous sessions. if (!$au->sessions == null) { - // Array to hold info for table population. - $tabledata = array(); - - // Build table. - $table = new html_table(); - $table->id = 'cmi5launch_auSessionTable'; - $table->caption = get_string('modulenameplural', 'cmi5launch'); - $table->head = array( - get_string('cmi5launchviewfirstlaunched', 'cmi5launch'), - get_string('cmi5launchviewlastlaunched', 'cmi5launch'), - get_string('cmi5launchviewprogress', 'cmi5launch'), - get_string('cmi5launchviewgradeheader', 'cmi5launch'), - ); - - // Retrieve session ids. - $sessionids = json_decode($au->sessions); - - // Iterate through each session by id. - foreach ($sessionids as $key => $sessionid) { - - // Get the session from DB with session id. - $session = $DB->get_record('cmi5launch_sessions', array('sessionid' => $sessionid)); - - // Array to hold data for table. - $sessioninfo = array(); - - - if ($session->createdat != null) { - - // Retrieve createdAt and format. - $date = new DateTime($session->createdat, new DateTimeZone('US/Eastern')); - $date->setTimezone(new DateTimeZone('America/New_York')); - // date_timezone_set($date, new DateTimeZone('America/New_York')); - $sessioninfo[] = $date->format('D d M Y H:i:s'); - } + try{ - if ($session->lastrequesttime != null) { + // 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_warningAU', E_WARNING); + set_exception_handler('mod_cmi5launch\local\custom_warningAU'); - // Retrieve lastRequestTime and format. - $date = new DateTime($session->lastrequesttime, new DateTimeZone('US/Eastern')); - $date->setTimezone(new DateTimeZone('America/New_York')); - $sessioninfo[] = $date->format('D d M Y H:i:s'); - } - // Add progress to table. - $sessioninfo[] = ("
" . implode("\n ", json_decode($session->progress) ) . ""); + // Array to hold info for table population. + $tabledata = array(); + + // Build table. + $table = new html_table(); + $table->id = 'cmi5launch_auSessionTable'; + $table->caption = get_string('modulenameplural', 'cmi5launch'); + $table->head = array( + get_string('cmi5launchviewfirstlaunched', 'cmi5launch'), + get_string('cmi5launchviewlastlaunched', 'cmi5launch'), + get_string('cmi5launchviewprogress', 'cmi5launch'), + get_string('cmi5launchviewgradeheader', 'cmi5launch'), + ); + + + // Retrieve session ids. + $sessionids = json_decode($au->sessions); + + // Iterate through each session by id. + foreach ($sessionids as $key => $sessionid) { + + // Get the session from DB with session id. + $session = $DB->get_record('cmi5launch_sessions', array('sessionid' => $sessionid)); - // Add score to table. - $sessioninfo[] = $session->score; - // Add score to array for AU. - $sessionscores[] = $session->score; + // Array to hold data for table. + $sessioninfo = array(); - // Add to be fed to table. - $tabledata[] = $sessioninfo; + + if ($session->createdat != null) { + + // Retrieve createdAt and format. + $date = new DateTime($session->createdat, new DateTimeZone('US/Eastern')); + $date->setTimezone(new DateTimeZone('America/New_York')); + // date_timezone_set($date, new DateTimeZone('America/New_York')); + $sessioninfo[] = $date->format('D d M Y H:i:s'); + } + + if ($session->lastrequesttime != null) { + + // Retrieve lastRequestTime and format. + $date = new DateTime($session->lastrequesttime, new DateTimeZone('US/Eastern')); + $date->setTimezone(new DateTimeZone('America/New_York')); + $sessioninfo[] = $date->format('D d M Y H:i:s'); + } + // Add progress to table. + $sessioninfo[] = ("
" . implode("\n ", json_decode($session->progress)) . ""); + + // Add score to table. + $sessioninfo[] = $session->score; + // Add score to array for AU. + $sessionscores[] = $session->score; + + // Add to be fed to table. + $tabledata[] = $sessioninfo; + } + } catch (Exception $e) { + + // Restore default hadlers. + restore_exception_handler(); + restore_error_handler(); + + // Throw an exception. + throw new customException('loading session table on AUview page. Report this to system administrator: ' . $e->getMessage() . 'Check that session information is present in DB and session id is correct.' , 0); } // Write table. @@ -188,17 +204,17 @@ function mod_cmi5launch_launchexperience(registration) { // Update AU in table with new info. $DB->update_record('cmi5launch_aus', $au); -} -// Build the new session link. -$newsession = "true"; -// Create a string to pass the auid and new session info to next page (launch.php). -$infofornextpage = $auid . "," . $newsession; + // Restore default hadlers. + restore_exception_handler(); + restore_error_handler(); +} +// Pass the auid and new session info to next page (launch.php). // New attempt button. echo ""; diff --git a/classes/local/errorover.php b/classes/local/errorover.php index 1cc8679..5b2fcba 100644 --- a/classes/local/errorover.php +++ b/classes/local/errorover.php @@ -67,6 +67,24 @@ function progresslrsreq_warning($errno, $errstr, $errfile, $errline) } +/** + * 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_warningAU($errno, $errstr, $errfile, $errline) +{ + echo "Error loading session table on AUview page. Report this to system administrator: