Skip to content

Commit

Permalink
Merge pull request #46 from ADLMeganBohland/Adding-try-and-catch
Browse files Browse the repository at this point in the history
Adding try and catch
  • Loading branch information
ADLMeganBohland authored Jul 25, 2024
2 parents cd59aa3 + 96369b5 commit 32901db
Show file tree
Hide file tree
Showing 23 changed files with 1,707 additions and 489 deletions.
11 changes: 11 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"github.copilot.enable": {

"*": true,
"plaintext": false,
"markdown": false,
"scminput": false
},
"github.copilot.advanced": {},
"files.eol": "\n"
}
3 changes: 1 addition & 2 deletions AUview.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,13 @@ 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');

// Retrieve lastRequestTime and format.
$date = new DateTime($session->updatedat, new DateTimeZone('US/Eastern'));
$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');

Expand Down
10 changes: 10 additions & 0 deletions classes/local/au.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
*/
namespace mod_cmi5launch\local;

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

class au {

// Lowercase values are for saving to DB.
Expand All @@ -34,6 +37,13 @@ class au {
// Constructs AUs. Is fed array and where array key matches property, sets the property.
public function __construct($statement) {

// What can go wrong here? It could be that a statement is null
// or that the statement is not an array or not an object.
if (is_null($statement) || (!is_array($statement) && !is_object($statement) )) {

throw new nullException('Statement to build AU is null or not an array/object.', 0);
}
// If it is an array, create the object.
foreach ($statement as $key => $value) {

$this->$key = ($value);
Expand Down
208 changes: 150 additions & 58 deletions classes/local/au_helpers.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
namespace mod_cmi5launch\local;

use mod_cmi5launch\local\au;
use mod_cmi5launch\local\errorover;

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

defined('MOODLE_INTERNAL') || die();

class au_helpers {
Expand All @@ -46,38 +52,67 @@ public function get_cmi5launch_retrieve_aus_from_db() {
* @return array
*/
public function cmi5launch_retrieve_aus($returnedinfo) {

$resultchunked = "";


// Use our own more specific error handler, to give better info tto user.
set_error_handler('mod_cmi5launch\local\array_chunk_warning', E_WARNING);

// The results come back as nested array under more then just AUs.
// We only want the info pertaining to the AU.
$resultchunked = array_chunk($returnedinfo["metadata"]["aus"], 1, );
// We only want the info pertaining to the AU. However, if the wrong info is passed array_chunk will through an exception.
try {
$resultchunked = array_chunk($returnedinfo["metadata"]["aus"], 1, );
}
catch (\Exception $e) {

echo "Cannot retrieve AUs. Error found when trying to parse them from course creation: " .
"Please check the connection to player or course format and try again. \n"
. $e->getMessage() . "\n";

//exit;
}

// Restore the error handler.
restore_error_handler();

return $resultchunked;
}


/**
* So it should be fed an array of statements that then assigns the values to
* several aus, and then returns them as au objects.
* @param mixed $austatements
* @return array<au>
*/
public function cmi5launch_create_aus($austatements) {

public function cmi5launch_create_aus($austatements)
{
// Needs to return our new AU objects.
$newaus = array();

foreach ($austatements as $int => $info) {
// We should not be able to get here but what if null is pulled from record and passed in?
// So in case it is given null.
if ($austatements == null) {

throw new nullException('Cannot retrieve AU information. AU statements from DB are: ' . $austatements, 0);

// The aus come back decoded from DB nestled in an array.
// So they are the first key, which is '0'.
$statement = $info[0];
} else {
foreach ($austatements as $int => $info) {

$au = new au($statement);
// The aus come back decoded from DB nestled in an array.
// So they are the first key, which is '0'.
$statement = $info[0];

// Assign the newly created au to the return array.
$newaus[] = $au;
}
$au = new au($statement);

// Assign the newly created au to the return array.
$newaus[] = $au;
}

// Return our new list of AU.
return $newaus;
// Return our new list of AU.
return $newaus;
}
}

/**
Expand All @@ -89,50 +124,105 @@ public function cmi5launch_save_aus($auobjectarray) {
// Add userid to the record.
global $DB, $USER, $cmi5launch;
$table = "cmi5launch_aus";

// An array to hold the created ids.
$auids = array();

// For each AU in array build a new record and save it.
// Because of so many nested variables this needs to be done manually.
foreach ($auobjectarray as $auobject) {

// Make a newrecord to save.
$newrecord = new \stdClass();

$newrecord->userid = $USER->id;
$newrecord->attempt = $auobject->attempt;
$newrecord->auid = $auobject->id;
$newrecord->launchmethod = $auobject->launchMethod;
$newrecord->lmsid = json_decode(json_encode($auobject->lmsId, true) );
$newrecord->url = $auobject->url;
$newrecord->type = $auobject->type;
$title = json_decode(json_encode($auobject->title), true);
$newrecord->title = $title[0]['text'];
$newrecord->moveon = $auobject->moveOn;
$newrecord->auindex = $auobject->auIndex;
$newrecord->parents = json_encode($auobject->parents, true);
$newrecord->objectives = json_encode($auobject->objectives);
$desc = json_decode(json_encode($auobject->description), true);
$newrecord->description = $desc[0]['text'];
$newrecord->activitytype = $auobject->activityType;
$newrecord->masteryscore = $auobject->masteryscore;
$newrecord->completed = $auobject->completed;
$newrecord->passed = $auobject->passed;
$newrecord->inprogress = $auobject->inprogress;
$newrecord->noattempt = $auobject->noattempt;
$newrecord->satisfied = $auobject->satisfied;
// And HERE we can add the moodlecourseid.
$newrecord->moodlecourseid = $cmi5launch->id;

// Save the record and get the new id.
$newid = $DB->insert_record($table, $newrecord, true);
// Save new id to list to pass back.
$auids[] = $newid;
}
// Variables for error over and exception handling.
// Array of all items in new record, this will be useful for troubleshooting.
$newrecorditems = array('id', 'attempt', 'auid', 'launchmethod', 'lmsid', 'url', 'type', 'title', 'moveon', 'auindex', 'parents', 'objectives', 'description', 'activitytype', 'masteryscore', 'completed', 'passed', 'inprogress', 'noattempt', 'satisfied', 'moodlecourseid');
$currentrecord = 1;
$newid = "";
$newrecord = "";

// 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\sifting_data_warning', E_WARNING);
set_exception_handler('mod_cmi5launch\local\exception_au');

//Check it's not null.
if ($auobjectarray == null) {

return $auids;
throw new nullException('Cannot save AU information. AU object array is: null' , 0);

} else {
// For each AU in array build a new record and save it.
// Because of so many nested variables this needs to be done manually.
foreach ($auobjectarray as $auobject) {

// A try statement to catch any errors that may be thrown.
try {
// Make a newrecord to save.
$newrecord = new \stdClass();

// Assign the values to the new record.
$newrecord->userid = $USER->id;
$newrecord->attempt = $auobject->attempt;
$newrecord->auid = $auobject->id;
$newrecord->launchmethod = $auobject->launchMethod;
$newrecord->lmsid = json_decode(json_encode($auobject->lmsId, true));
$newrecord->url = $auobject->url;
$newrecord->type = $auobject->type;
$title = json_decode(json_encode($auobject->title), true);
$newrecord->title = $title[0]['text'];
$newrecord->moveon = $auobject->moveOn;
$newrecord->auindex = $auobject->auIndex;
$newrecord->parents = json_encode($auobject->parents, true);
$newrecord->objectives = json_encode($auobject->objectives);
$desc = json_decode(json_encode($auobject->description), true);
$newrecord->description = $desc[0]['text'];
$newrecord->activitytype = $auobject->activityType;
$newrecord->masteryscore = $auobject->masteryscore;
$newrecord->completed = $auobject->completed;
$newrecord->passed = $auobject->passed;
$newrecord->inprogress = $auobject->inprogress;
$newrecord->noattempt = $auobject->noattempt;
$newrecord->satisfied = $auobject->satisfied;
$newrecord->moodlecourseid = $cmi5launch->id;

// Save the record and get the new id.
$newid = $DB->insert_record($table, $newrecord, true);

// Save new id to list to pass back.
$auids[] = $newid;

// This is for troubleshooting, so we know where the error is.
$currentrecord++;

// The set exception handler catches exceptionas that SLIP by,
// so maybe DONT make it throwable and catch type errror
} catch (\Throwable $e) {


echo "Cannot save to DB. Stopped at record with ID number " . ($currentrecord) . ".";

// This is the tricky part, we need to find out which field is missing. But because the error is thrown ON the field, we need to do some
// manuevering to find out which field is missing.
// Typecast to array to grab the list item.
$items = (array) $newrecord;

// Get the last ley of array
$lastkey = array_key_last($items);

// Heres thhe tricky part, the lastkey here is somewhere in the array we earlier made and the NEXT one would be the one that threw the error.
// So now we can grab the key after the last one.
$key = array_search($lastkey, $newrecorditems) + 1;

// Ok, NOW the missin element is key in newrecorditems.
$missing = $newrecorditems[$key];

// 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();

return $auids;
}
}


/**
* Retrieves AU info from DB, converts to AU object, and returns it.
Expand All @@ -145,22 +235,24 @@ public function cmi5launch_retrieve_aus_from_db($auid) {

$check = $DB->record_exists( 'cmi5launch_aus', ['id' => $auid], '*', IGNORE_MISSING);

// If check is negative, the record does not exist. It should so throw error.

// If check is negative, the record does not exist. It should also throw error.
// Moodle will throw the error, but we want to pass this message back ot user.
if (!$check) {

echo "<p>Error attempting to get AU data from DB. Check AU id. AU id is: " . $auid ."</p>";
throw new nullException("Error attempting to get AU data from DB. Check AU id. AU id is: " . $auid ."</p>", 0);

return false;
} else {

$auitem = $DB->get_record('cmi5launch_aus', array('id' => $auid));

$au = new au($auitem);

// Return our new list of AU.
return $au;
}

// Return our new list of AU.
return $au;

}

}
Loading

0 comments on commit 32901db

Please sign in to comment.