Skip to content

Commit

Permalink
All unit tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
ADLMeganBohland committed Aug 14, 2024
1 parent a922e69 commit ce5fd27
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 225 deletions.
26 changes: 16 additions & 10 deletions AUview.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* 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
Expand All @@ -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.
Expand Down Expand Up @@ -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[] = ("<pre>" . implode("\n ", json_decode($session->progress) ) . "</pre>");

Expand Down
7 changes: 7 additions & 0 deletions classes/local/au_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}
}

Expand Down
9 changes: 6 additions & 3 deletions classes/local/cmi5_connectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
195 changes: 49 additions & 146 deletions classes/local/errorover.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* 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
*/

Expand All @@ -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
Expand All @@ -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;

}

/**
Expand All @@ -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
Expand All @@ -119,100 +108,53 @@ 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
* @param mixed $errline
* @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
Expand All @@ -222,63 +164,26 @@ 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

// 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();
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
Expand Down Expand Up @@ -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?
Loading

0 comments on commit ce5fd27

Please sign in to comment.