diff --git a/AUview.php b/AUview.php index a385868..4f628c3 100755 --- a/AUview.php +++ b/AUview.php @@ -15,7 +15,7 @@ // along with Moodle. If not, see . /** - * Prints an AUs session information annd allows start of new one. + * Prints an AUs session information and allows start of new one. * @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 @@ -36,7 +36,6 @@ $auhelper = new au_helpers; $sessionhelper = new session_helpers; $retrievesession = $sessionhelper->cmi5launch_get_retrieve_sessions_from_db(); -//$updatesession = $sessionhelper->cmi5launch_get_update_session(); $retrieveaus = $auhelper->get_cmi5launch_retrieve_aus_from_db(); // MB - Not currently using events, but may in future. @@ -154,16 +153,23 @@ function mod_cmi5launch_launchexperience(registration) { // Array to hold data for table. $sessioninfo = array(); - // Retrieve createdAt and format. - $date = new DateTime($session->createdat, new DateTimeZone('US/Eastern')); - $date->setTimezone(new DateTimeZone('America/New_York')); - $sessioninfo[] = $date->format('D d M Y H:i:s'); + + if ($session->createdat != 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'); + // 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) ) . "
"); diff --git a/classes/local/au_helpers.php b/classes/local/au_helpers.php index 4ecdbff..259081a 100644 --- a/classes/local/au_helpers.php +++ b/classes/local/au_helpers.php @@ -155,6 +155,10 @@ public function cmi5launch_save_aus($auobjectarray) //Check it's not null. if ($auobjectarray == null) { + // Restore default hadlers. + restore_exception_handler(); + restore_error_handler(); + throw new nullException('Cannot save AU information. AU object array is: null', 0); } else { @@ -229,6 +233,9 @@ public function cmi5launch_save_aus($auobjectarray) // Now use the found missing value to give feedback to user. echo " One of the fields is incorrect. Check data for field '$missing'. " . $e->getMessage() . "\n"; + // Restore default hadlers. + restore_exception_handler(); + restore_error_handler(); } } diff --git a/classes/local/cmi5_connectors.php b/classes/local/cmi5_connectors.php index 8a51360..b79b551 100755 --- a/classes/local/cmi5_connectors.php +++ b/classes/local/cmi5_connectors.php @@ -504,8 +504,11 @@ public function cmi5launch_send_request_to_cmi5_player_post($cmi5launch_stream_a // Return response. return $result; - }catch(\Throwable $e) { - + }catch(\Throwable $e) { + + // Restore default hadlers. + restore_exception_handler(); + restore_error_handler(); // throw new playerException("communicating with player, sending or crafting a POST request: " . $e); } @@ -614,7 +617,7 @@ public function cmi5launch_connectors_error_message($resulttotest, $type) { if ($resulttest === false ){ - $errormessage = $type . ". CMI5 Player is not communicating. Is it running?"; + $errormessage = $type . " CMI5 Player is not communicating. Is it running?"; throw new playerException($errormessage); } diff --git a/classes/local/errorover.php b/classes/local/errorover.php index 8fba574..1cc8679 100644 --- a/classes/local/errorover.php +++ b/classes/local/errorover.php @@ -15,9 +15,9 @@ // along with Moodle. If not, see . /** - * Lets see if this works. making an ovrride error class, or several + * Error class with overridden functions for error and warning handling. * - * @copyright 2023 Megan Bohland + * @copyright 2024 Megan Bohland * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -34,16 +34,11 @@ */ function exception_au(\Throwable $exception) { - // echo"Error stirn --- $errstr"; - // echo"Error number --- $errno"; - - // Maybe we can construct the new errors here. This would allow the error personalization? And keep main code clean - - throw new fieldException('Error OVER: ' . $exception->getMessage(), 0); - // exit; + throw new nullException('Error OVER: ' . $exception->getMessage(), 0); } + /** - * An exception handler to use in AU cases when many different exceptions for data errors may be thrown. + * An exception handler to use in grade cases when many different exceptions for data errors may be thrown. * @param mixed $errno * @param mixed $errstr * @param mixed $errfile @@ -53,31 +48,23 @@ function exception_au(\Throwable $exception) */ function exception_grade(\Throwable $exception) { - throw new nullException('Error in checking user grades: ' . $exception->getMessage(), 0); - // exit; -} -function progress_warning($errno, $errstr, $errfile, $errline) -{ - // echo"Error stirn --- $errstr"; - // echo"Error number --- $errno"; -//echo"Error errfile --- $errfile"; - // echo"Error errline --- $errline"; - // Maybe we can construct the new errors here. This would allow the error personalization? And keep main code clean - echo " unable to get name "; - // throw new nullException('Unable to communicate with LRS. Caught exception: ' . $exception->getMessage() . " Check LRS is up, username and password are correct, and LRS endpoint is correct.", 0); - // exit; } + +/** + * An error handler to use in progress cases when many different exceptions for data errors may be thrown. + * @param mixed $errno + * @param mixed $errstr + * @param mixed $errfile + * @param mixed $errline + * @throws \mod_cmi5launch\local\nullException + * @return never + */ function progresslrsreq_warning($errno, $errstr, $errfile, $errline) { - // echo"Error stirn --- $errstr"; - // echo"Error number --- $errno"; -//echo"Error errfile --- $errfile"; - // echo"Error errline --- $errline"; - // Maybe we can construct the new errors here. This would allow the error personalization? And keep main code clean throw new nullException('Unable to communicate with LRS. Caught exception: ' . $errstr. " Check LRS is up, username and password are correct, and LRS endpoint is correct.", 0); - // exit; + } /** @@ -93,19 +80,21 @@ function exception_progresslrsreq(\Throwable $exception) { throw new nullException('Unable to communicate with LRS. Caught exception: ' . $exception->getMessage() . " Check LRS is up, username and password are correct, and LRS endpoint is correct.", 0); - // exit; } +/** + * A warning handler to use in AU cases when many different exceptions for data errors may be thrown. + * @param mixed $errno + * @param mixed $errstr + * @param mixed $errfile + * @param mixed $errline + * @throws \mod_cmi5launch\local\nullException + * @return never + */ function progresslrs_warning($errno, $errstr, $errfile, $errline) { - // echo"Error stirn --- $errstr"; - // echo"Error number --- $errno"; -//echo"Error errfile --- $errfile"; - // echo"Error errline --- $errline"; - // Maybe we can construct the new errors here. This would allow the error personalization? And keep main code clean - throw new nullException('Error in retrieving statements from LRS ' . $errstr, 0); - // exit; } + /** * An exception handler to use in AU cases when many different exceptions for data errors may be thrown. * @param mixed $errno @@ -119,11 +108,10 @@ function exception_progresslrs(\Throwable $exception) { throw new nullException('Error in retrieving statements from LRS ' . $exception->getMessage(), 0); - // exit; } /** - * An exception handler to use in AU cases when many different exceptions for data errors may be thrown. + * A warning handler to use in AU cases when many different exceptions for data errors may be thrown. * @param mixed $errno * @param mixed $errstr * @param mixed $errfile @@ -131,88 +119,42 @@ function exception_progresslrs(\Throwable $exception) * @throws \mod_cmi5launch\local\nullException * @return never */ -function session_warning(\Throwable $exception) -{ - - throw new nullException('Unable to communicate with LRS. Caught exception: ' . $exception->getMessage() . " Check LRS is up, username and password are correct, and LRS endpoint is correct.", 0); - // exit; -} -function sesssion_exception($errno, $errstr, $errfile, $errline) -{ - // echo"Error stirn --- $errstr"; - // echo"Error number --- $errno"; -//echo"Error errfile --- $errfile"; - // echo"Error errline --- $errline"; - // Maybe we can construct the new errors here. This would allow the error personalization? And keep main code clean - - throw new nullException('Error in retrieving statements from LRS ' . $errstr, 0); - // exit; -} - function sifting_data_warning($errno, $errstr, $errfile, $errline) { - // echo"Error stirn --- $errstr"; - // echo"Error number --- $errno"; -//echo"Error errfile --- $errfile"; - // echo"Error errline --- $errline"; - // Maybe we can construct the new errors here. This would allow the error personalization? And keep main code clean - - throw new fieldException('Error: ' . $errstr, 0); + throw new nullException('Error: ' . $errstr, 0); // exit; } +/** + * An exception handler to use in AU cases when many different exceptions for data errors may be thrown. + * @param mixed $errno + * @param mixed $errstr + * @param mixed $errfile + * @param mixed $errline + * @throws \mod_cmi5launch\local\nullException + * @return never + */ function array_chunk_warning($errno, $errstr, $errfile, $errline) { - // echo"Error stirn --- $errstr"; - // echo"Error number --- $errno"; - - // Maybe we can construct the new errors here. This would allow the error personalization? And keep main code clean - throw new nullException('Cannot parse array. Error: ' . $errstr, 0); - // exit; } - /// Ok, this i a different error handler - function grade_warning($errno, $errstr, $errfile, $errline) - { - // echo"Error stirn --- $errstr"; - // echo"Error number --- $errno"; - //echo"Error errfile --- $errfile"; - // echo"Error errline --- $errline"; - // Maybe we can construct the new errors here. This would allow the error personalization? And keep main code clean - - throw new nullException('Error in checking user grades: ' . $errstr, 0); - // exit; - } - /** - * Define a custom exception class, this will make pour tests meaningful - * 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." + * An grade handler to use in AU cases when many different exceptions for data errors may be thrown. + * @param mixed $errno + * @param mixed $errstr + * @param mixed $errfile + * @param mixed $errline + * @throws \mod_cmi5launch\local\nullException + * @return never */ -class nullException extends \Exception -{ - // Redefine the exception so message isn't optional - public function __construct($message, $code = 0, Throwable $previous = null) { - // some code + function grade_warning($errno, $errstr, $errfile, $errline) + { - // make sure everything is assigned properly - parent::__construct($message, $code, $previous); + throw new nullException('Error in checking user grades: ' . $errstr, 0); } - // custom string representation of object (what is returned with echo) - public function __toString(): string { - return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; - } - public function customFunction() { - echo "A custom function for this type of exception\n"; - } -} /** * Define a custom exception class, this will make pour tests meaningful * from php webpage: "Custom exception classes can allow you to write tests that prove your exceptions @@ -222,11 +164,9 @@ public function customFunction() { *later down the line. Especially if your $e->getMessage is something complicated *like a var_dump'ed context array." */ -class missingException extends \Exception +class nullException 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 @@ -234,51 +174,16 @@ public function __construct($message, $code = 0, Throwable $previous = null) { parent::__construct($message, $code, $previous); } - // 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(); + echo "A custom function for this type of exception\n"; } } -/** - * Define a custom exception class, this will make pour tests meaningful - * 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 fieldException 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 - - // make sure everything is assigned properly - parent::__construct($message, $code, $previous); - } - - - // 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(); - } -} /** * Define a custom exception class, this will make pour tests meaningful * from php webpage: "Custom exception classes can allow you to write tests that prove your exceptions @@ -314,5 +219,3 @@ public function customFunction() { // $this->getTraceAsString(); } } - -// If all my exceptions are the same, just diff names, are they necessary? Do any of my try/catches really differentiate? \ No newline at end of file diff --git a/classes/local/token_form.php b/classes/local/token_form.php index c98e7f4..990e10c 100644 --- a/classes/local/token_form.php +++ b/classes/local/token_form.php @@ -32,9 +32,6 @@ public function definition() { // A common convention is to store it in a variable, such as `$mform`. $mform = $this->_form; // Don't forget the underscore! - // Elements are diff inputs in form - - // Add elements to your form. Second arg is the name of element $mform->addElement('text', 'cmi5token', get_string('cmi5launchtenanttoken', 'cmi5launch')); // Set type of element. diff --git a/cmi5PHP/tests/ausHelpersTest.php b/cmi5PHP/tests/ausHelpersTest.php index 3d4584e..e5513de 100644 --- a/cmi5PHP/tests/ausHelpersTest.php +++ b/cmi5PHP/tests/ausHelpersTest.php @@ -1,8 +1,6 @@ willReturn($errormessage); // Expected exceptions - $exceptionmessage = "Player communication error. Something went wrong retrieving the registration information. CMI5 Player returned 400 error. With message 'website not found'." ; + $exceptionmessage = "Player communication error. Something went wrong creating the tenant CMI5 Player returned 400 error. With message 'website not found'." ; // the correct output not an exception @@ -469,7 +485,7 @@ public function testcmi5launch_retrieve_registration_with_get_fail() ->willReturn($errormessage); // Expected exceptions - $exceptionmessage = "Player communication error. Something went wrong retrieving the registration. CMI5 Player returned 404 error. With message 'testmessage'" ; + $exceptionmessage = "Player communication error. Something went wrong retrieving the registration CMI5 Player returned 404 error. With message 'testmessage'." ; // Expected exceptions and messages $this->expectExceptionMessage($exceptionmessage); @@ -689,7 +705,7 @@ public function testcmi5launch_retrieve_registration_with_post_fail() ->willReturn($errormessage); // Expected exceptions - $exceptionmessage = "Player communication error. Something went wrong retrieving the registration. CMI5 Player returned 404 error. With message 'testmessage'" ; + $exceptionmessage = "Player communication error. Something went wrong retrieving the registration CMI5 Player returned 404 error. With message 'testmessage'" ; // Expected exceptions and messages $this->expectExceptionMessage($exceptionmessage); @@ -1126,8 +1142,8 @@ public function testcmi5launch_send_request_to_cmi5_player_post_with_one_arg() // Call the method under test. $test = $helper->cmi5launch_send_request_to_cmi5_player_post($testfunction, $data, $url, $filetype, $token); - // If the right message is displayed the try/catch wworked! - $this->expectOutputString($exceptionmessage); + // If the right message is displayed the try/catch worked. + $this->assertEquals($returnvalue, $test, "The return value should be the same as the return value from the mocked method."); } @@ -1141,11 +1157,7 @@ public function testcmi5launch_send_request_to_cmi5_player_post_with_one_arg_fai // We send the TEST function to the function under test now! $testfunction = 'cmi5Test\cmi5launch_test_stream_and_send_excep'; // Which returns the 'options' parameter passed to it. - // The player returns a string under normal circumstances. - $returnvalue = json_encode(array( - "statusCode" => 200, - "Response" => "Successful Post", - )); + // The data to be passed to the mocked method. $data = array( @@ -1180,11 +1192,6 @@ public function testcmi5launch_send_request_to_cmi5_player_post_with_one_arg_fai // Call the method under test. // Note: by not sending an actual function, this will cause an exception and allow testing of try/catch and error override. $test = $helper->cmi5launch_send_request_to_cmi5_player_post('testfunction', $data, $url, $filetype, $token); - - // And the return should be a string. - $this->assertIsString($test); - // And it should be the same as the return value. - $this->assertEquals($test, $returnvalue); } @@ -1226,9 +1233,6 @@ public function testcmi5launch_send_request_to_cmi5_player_post_with_two_args() $username = "testname"; $password = "testpassword"; - - - // This is the SUT? $helper = new cmi5_connectors; $test = $helper->cmi5launch_send_request_to_cmi5_player_post($testfunction, $data, $url, $filetype, $username, $password); @@ -1318,11 +1322,11 @@ public function testcmi5launch_send_request_to_cmi5_player_with_get_pass() // Get class and the function under test. $helper = new cmi5_connectors; $test = $helper->cmi5launch_send_request_to_cmi5_player_get($testfunction, $token, $url); - + // And the return should be an array. $this->assertIsString($test); // And it should be the same as the return value. - $this->assertEquals($test, json_decode($returnvalue, true) ); + $this->assertEquals($test, $returnvalue, true) ; } @@ -1559,7 +1563,7 @@ public function testcmi5launch_connectors_error_message_path_one() // The expected error message to be output. - $exceptionmessage ="Something went wrong " . $type . ". CMI5 Player is not communicating. Is it running?"; + $exceptionmessage ="Something went wrong " . $type . " CMI5 Player is not communicating. Is it running?"; // Expected exceptions and messages diff --git a/cmi5PHP/tests/errorOverTest.php b/cmi5PHP/tests/errorOverTest.php deleted file mode 100644 index 38d4c7c..0000000 --- a/cmi5PHP/tests/errorOverTest.php +++ /dev/null @@ -1,36 +0,0 @@ -expectException(nullException::class); - - //can we just call it? - // is this necessary? errorover::array_chunk_warning(E_WARNING, "This is a test error", "testfile.php", 1); - - - } -} \ No newline at end of file diff --git a/cmi5PHP/tests/grade_helpersTest.php b/cmi5PHP/tests/grade_helpersTest.php index 1286f4d..b683cb9 100644 --- a/cmi5PHP/tests/grade_helpersTest.php +++ b/cmi5PHP/tests/grade_helpersTest.php @@ -84,11 +84,15 @@ protected function setUp(): void // Assign the AUs to the course. assign_aus_to_courses($testcourseid, $testcourseausids); + + // Delete testcoursesessionids. + deletetestcmi5launch_sessions($testcoursesessionids); } protected function tearDown(): void { - + global $sessionids; + deletetestcmi5launch_sessions($sessionids); }