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:
$errstr at $errfile on $errline: Check that session information is present in DB and session id is correct."; + + exit; + +} + + /** * An exception handler to use in AU cases when many different exceptions for data errors may be thrown. * @param mixed $errno @@ -219,3 +237,42 @@ public function customFunction() { // $this->getTraceAsString(); } } + +/** + * Define a custom exception class, this will make pour tests meaningful + * This is a catchall custom + * from php webpage: "Custom exception classes can allow you to write tests that prove your exceptions + * are meaningful. Usually testing exceptions, you either assert the message equals +*something in which case you can't change the message format without refactoring, +*or not make any assertions at all in which case you can get misleading messages +*later down the line. Especially if your $e->getMessage is something complicated +*like a var_dump'ed context array." + */ +class customException extends \Exception +{ + // Redefine the exception so message isn't optional + // I want an exception that takkkes what is missing and adds it to messsssage? + // Is this possivlbe? + public function __construct($message, $code = 0, Throwable $previous = null) { + // some code + + // Ah maybe here is where I can differentiate them + $playermessage = "Caught error. Something went wrong " . $message; + // make sure everything is assigned properly + parent::__construct($playermessage, $code, $previous); + + echo"$playermessage"; + } + + + // custom string representation of object (what is returned with echo) + public function __toString(): string { + return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; + // maybe here? + } + + public function customFunction() { + // echo " This error to string :"; + // $this->getTraceAsString(); + } +} diff --git a/cmi5PHP/tests/progressTest.php b/cmi5PHP/tests/progressTest.php index 209ac39..b6a7bb8 100644 --- a/cmi5PHP/tests/progressTest.php +++ b/cmi5PHP/tests/progressTest.php @@ -79,11 +79,11 @@ protected function setUp(): void protected function tearDown(): void { - global $sessionids; + global $sessionids, $testcourseid, $cmi5launchsettings; // Restore overridden global variable. unset($GLOBALS['USER']); unset($GLOBALS['cmi5launchsettings']); - + deletetestcmi5launch_usercourse($testcourseid); deletetestcmi5launch_sessions($sessionids); } @@ -974,9 +974,6 @@ public function testcmi5launch_retrieve_statements() { global $CFG, $DB, $sessionids; - // First create a fake session to pass to the function. - $sessions = maketestsessions(); - // Retrieve a sessionid, we'll just use the first one. $sessionid = $sessionids[1]; @@ -1039,9 +1036,6 @@ public function testcmi5launch_retrieve_statements_excep_ext() { global $CFG, $DB, $sessionids; - // First create a fake session to pass to the function. - $sessions = maketestsessions(); - // Retrieve a sessionid, we'll just use the first one. $sessionid = $sessionids[1]; @@ -1103,9 +1097,6 @@ public function testcmi5launch_retrieve_statements_excep() { global $CFG, $DB, $sessionids; - // First create a fake session to pass to the function. - $sessions = maketestsessions(); - // Retrieve a sessionid, we'll just use the first one. $sessionid = $sessionids[1]; diff --git a/launch.php b/launch.php index e00846b..2c13de1 100755 --- a/launch.php +++ b/launch.php @@ -56,7 +56,7 @@ $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); @@ -65,7 +65,8 @@ // Retrieve AU OR session id. $id = array_shift($idandstatus); - +*/ +$id = required_param('launchform_registration', PARAM_TEXT); // Reload cmi5 instance. $record = $DB->get_record('cmi5launch', array('id' => $cmi5launch->id));