Skip to content

Commit

Permalink
Corrected variable and file names, cleaned up code
Browse files Browse the repository at this point in the history
  • Loading branch information
ADLMeganBohland committed Jul 10, 2024
1 parent 6125efe commit 6081009
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 302 deletions.
126 changes: 48 additions & 78 deletions classes/local/au_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
namespace mod_cmi5launch\local;

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

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

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

Expand All @@ -51,48 +51,32 @@ public function get_cmi5launch_retrieve_aus_from_db() {
* @return array
*/
public function cmi5launch_retrieve_aus($returnedinfo) {
// The results come back as nested array under more then just AUs.
// We only want the info pertaining to the AU.

$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);

//Lets add a try catch, and see if it catches when fed wrong array info
// The results come back as nested array under more then just AUs.
// 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, );
}
// If this doesnt work catch the exception or erro and throw our own!
catch (\Exception $e) {

// It's nt bein thrown!! OMG it's not being throuwn!!!!
// lololololololol
// But it is bein thorw but the array chunk over ride, this is the problem
// So maybe catch the error? The warning huh
// We need to throw HERE for it to be cauht
// Whats happening is the ARRAY chunk is throwing trhe null exception
// not the code under test
// So, what does this mean? Do we want to just test output?????
// I think so, because the exception handling is anothe system under test
// What is happening here is the error is turned into exception, the exception is autothrown or thrown to be a null exception,
// If the null exception is thrown THEN the catch operates, but because catch is operiting the tests are picking up IT, not the thing that causedd it!!!

//They problme is the try/catch syntax I think


// What is php unit is throwing ANOTHER exceptuion and thats what is
// being cauhy and thats why me invented one is missed
// The message returned is what I decded in erroverride, combined with below I can make it apecif for certain areas

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_error_handler();
return $resultchunked;

}

// 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.
Expand All @@ -101,16 +85,15 @@ public function cmi5launch_retrieve_aus($returnedinfo) {
*/
public function cmi5launch_create_aus($austatements)
{

// Needs to return our new AU objects.
$newaus = array();
// /SHould this be where the build in is? Or where this func is called?

// 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
// So in case it is given null.
if ($austatements == null) {

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

} else {
foreach ($austatements as $int => $info) {

Expand Down Expand Up @@ -138,34 +121,37 @@ public function cmi5launch_save_aus($auobjectarray) {
// Add userid to the record.
global $DB, $USER, $cmi5launch;
$table = "cmi5launch_aus";
$newid = "";
$newrecord = "";

// An array to hold the created ids.
$auids = array();
// Array of all items in new record, this will be useful for
// troubleshooting.

// 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');
$currentrecord = 1;

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

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) {

// I suppose another thing that could o wrong is the retrieval or subsequent encoding of these items.
// Sooo maybe wrap hte whole thing in a try/cath?
// Can we make an exception that points to whos is missing? Like title vs id?
// Now here we need an exception catchy thing, because theres no way to know WHAT wmight be good
// 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;
Expand All @@ -188,65 +174,51 @@ public function cmi5launch_save_aus($auobjectarray) {
$newrecord->inprogress = $auobject->inprogress;
$newrecord->noattempt = $auobject->noattempt;
$newrecord->satisfied = $auobject->satisfied;
// And HERE we can add the moodlecourseid.
$newrecord->moodlecourseid = $cmi5launch->id;

// echo" What are the newrecords? " . var_dump($auobject->moveOn) . "\n";

// What could go wrong here? The record is not saved? The record is not saved correctly?
// 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++;
// echo" What are the newrecords? " . print_r($newrecord) . "\n";

// Thats right, the error handler is throwing the exception, not the code under test, so catch ANY error
// A type error cn also be thrown here, throwable catches it, so we either need to set an exception handler as well
// or catch throwable


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

// Maybe we can construct the new errors here. This would allow the error personalization? And keep main code clean
// maybe the catch is just what is suppossed to happen when the
//error is thrown, any additional stuff
//the current new record is
// echo "New idis ";

// Its plus one because it would be the recor that couldnt save, so the last id would be the the previous one that went throuh

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

// ok so what is e?
// echo"Error stirn --- " . $e->getMessage();
// Typecast tp array to grab the list item
// 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;
// This will almost work but it is the NEXT one after, so maybe I should make array and then grab the one after it
// 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 thre th error

// So now we can grab the key after the last one
// 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
// Ok, NOW the missin element is key in newrecorditems.
$missing = $newrecorditems[$key];

// It worked! Now in the test we can make say three mock statements, each one with a bad field, and then make sure the
// error messae matches the bad output!!! Woot!
// yup I'm a dummy....
// 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";
// exit;

}
}

// Restore default hadlers.
restore_exception_handler();
restore_error_handler();

return $auids;
}
}
}


/**
Expand All @@ -261,14 +233,12 @@ 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) {

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

// return false;
} else {

$auitem = $DB->get_record('cmi5launch_aus', array('id' => $auid));
Expand Down
File renamed without changes.
Loading

0 comments on commit 6081009

Please sign in to comment.