Skip to content

Commit

Permalink
Finished updating gradehelpers with try/catch and new unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ADLMeganBohland committed Aug 6, 2024
1 parent 921e3a5 commit 5e5de75
Show file tree
Hide file tree
Showing 6 changed files with 521 additions and 326 deletions.
4 changes: 2 additions & 2 deletions classes/local/au_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
global $CFG;
// Include the errorover (error override) funcs.
require_once ($CFG->dirroot . '/mod/cmi5launch/classes/local/errorover.php');
//require 'errorover.php';

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

Expand Down Expand Up @@ -166,7 +167,7 @@ public function cmi5launch_save_aus($auobjectarray)
try {
// Make a newrecord to save.
$newrecord = new \stdClass();

// Assign the values to the new record.
$newrecord->userid = $USER->id;
$newrecord->attempt = $auobject->attempt;
Expand Down Expand Up @@ -195,7 +196,6 @@ public function cmi5launch_save_aus($auobjectarray)
$newrecord->satisfied = $auobject->satisfied;
$newrecord->moodlecourseid = $cmi5launch->id;


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

Expand Down
1 change: 0 additions & 1 deletion classes/local/cmi5_connectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use mod_cmi5launch\local\cmi5launch_helpers;
// Include the errorover (error override) funcs.
require_once ($CFG->dirroot . '/mod/cmi5launch/classes/local/errorover.php');

class cmi5_connectors {

public function cmi5launch_get_create_tenant() {
Expand Down
24 changes: 12 additions & 12 deletions classes/local/errorover.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,19 @@ function array_chunk_warning($errno, $errstr, $errfile, $errline)
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;
}

/// 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
Expand Down
202 changes: 104 additions & 98 deletions classes/local/grade_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
defined('MOODLE_INTERNAL') || die();

use mod_cmi5launch\local\session_helpers;
use mod_cmi5launch\local\au_helpers;

require_once ($CFG->dirroot . '/mod/cmi5launch/classes/local/errorover.php');

class grade_helpers
{
Expand Down Expand Up @@ -122,24 +121,25 @@ public function cmi5launch_highest_grade($scores)
// Now apply intval.
$highestgrade = intval($highestgrade);



return $highestgrade;
}


/**
* Parses and retrieves AUs and their sessions from the returned info from CMI5 player and LRS and updates them.
* @param array $user - the user whose grades are being updated.
* @return array
*/
public function cmi5launch_check_user_grades_for_updates($user)
public function cmi5launch_check_user_grades_for_updates($user, )
{

global $cmi5launch, $USER, $DB;

// what is cmi5launch here in actual func?
// 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\grade_warning', E_WARNING);
set_exception_handler('mod_cmi5launch\local\exception_grade');
// 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\grade_warning', E_WARNING);
set_exception_handler('mod_cmi5launch\local\exception_grade');

// Check if record already exists.
$exists = $DB->record_exists('cmi5launch_usercourse', ['courseid' => $cmi5launch->courseid, 'userid' => $user->id]);
Expand All @@ -153,19 +153,17 @@ public function cmi5launch_check_user_grades_for_updates($user)

$auids = json_decode($userscourse->aus);

// Bring in functions and classes.
$sessionhelper = new session_helpers;


// Update the AUs
$returnedinfo = $this->cmi5launch_update_au_for_user_grades($auids, $user);

$returnedinfo = $this->cmi5launch_update_au_for_user_grades($sessionhelper, $auids, $user);
// Array to hold AU scores.
$auscores = $returnedinfo[0];
$overallgrade = $returnedinfo[1];

// Update course record.
$userscourse->ausgrades = json_encode($auscores);
$DB->update_record("cmi5launch_usercourse", $userscourse);
//echo" 1 branch";
// Restore default hadlers.
restore_exception_handler();
restore_error_handler();
Expand All @@ -174,12 +172,11 @@ public function cmi5launch_check_user_grades_for_updates($user)

} else {

// Should we return SOMEthing>
$nograde = array(0 => 'No grades to update. No record for user found in this course.');
// Do nothing, there is no record for this user in this course.
// Restore default hadlers.
restore_exception_handler();
restore_error_handler();
restore_exception_handler();
restore_error_handler();
return $nograde;

}
Expand All @@ -199,118 +196,127 @@ public function cmi5launch_check_user_grades_for_updates($user)
* @param mixed $auid
* @return au|bool
*/
public function cmi5launch_update_au_for_user_grades($auids, $user)
public function cmi5launch_update_au_for_user_grades($session_helpers, $auids, $user)
{
global $cmi5launch, $USER, $DB;

echo " HELLO I SHOULDN'T BE HERE?";

$cmi5launchsettings = cmi5launch_settings($cmi5launch->id);

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

// Array to hold AU scores.
$auscores = array();
$overallgrade = array();
try {
// Bring in functions and classes.
$sessionhelper = $session_helpers;

// Functions from other classes.
$updatesession = $sessionhelper->cmi5launch_get_update_session();

// Bring in functions and classes.
$sessionhelper = new session_helpers;
// Go through each Au, each Au will be responsible for updating its own session.
foreach ($auids as $key => $auid) {

// Functions from other classes.
$updatesession = $sessionhelper->cmi5launch_get_update_session();
// Array to hold session scores for update.
$sessiongrades = array();

// Go through each Au, each Au will be responsible for updating its own session.
foreach ($auids as $key => $auid) {


// Array to hold session scores for update.
$sessiongrades = array();
// This uses the auid to pull the right record from the aus table.
$aurecord = $DB->get_record('cmi5launch_aus', ['id' => $auid]);

// This uses the auid to pull the right record from the aus table.
$aurecord = $DB->get_record('cmi5launch_aus', ['id' => $auid]);
// When it is null it is because the user has not launched the AU yet.
if (!$aurecord == null || false) {

// When it is null it is because the user has not launched the AU yet.
if (!$aurecord == null || false) {
// Check if there are sessions.
if (!$aurecord->sessions == null) {

// Check if there are sessions.
if (!$aurecord->sessions == null) {
// Retrieve session ids for this course. There may be more than one session.
$sessions = json_decode($aurecord->sessions, true);

// Retrieve session ids for this course. There may be more than one session.
$sessions = json_decode($aurecord->sessions, true);

// Iterate through each session.
foreach ($sessions as $sessionid) {
// Iterate through each session.
foreach ($sessions as $sessionid) {

// Using current session id, retrieve session from DB.
$session = $DB->get_record('cmi5launch_sessions', ['sessionid' => $sessionid]);
// Using current session id, retrieve session from DB.
$session = $DB->get_record('cmi5launch_sessions', ['sessionid' => $sessionid]);

// the update session is call the ission
// we need to moick IT
// Retrieve new info (if any) from CMI5 player and LRS on session.
$session = $updatesession($sessionid, $cmi5launch->id, $user);

// Retrieve new info (if any) from CMI5 player and LRS on session.
$session = $updatesession($sessionid, $cmi5launch->id, $user);
// Now if the session is complete, passed, or terminated, we want to update the AU.
// These come in order, so the last one is the current status, so update on each one,
// overwrite as you go, and the last one if final.
if ($session->iscompleted == 1) {
// 0 is no 1 is yes, these are from players
$aurecord->completed = 1;
}
if ($session->ispassed == 1) {
// 0 is no 1 is yes, these are from players
$aurecord->passed = 1;
}
if ($session->isterminated == 1) {
// 0 is no 1 is yes, these are from players
$aurecord->terminated = 1;
}

// Now if the session is complete, passed, or terminated, we want to update the AU.
// These come in order, so the last one is the current status, so update on each one,
// overwrite as you go, and the last one if final.
if ($session->iscompleted == 1) {
// 0 is no 1 is yes, these are from players
$aurecord->completed = 1;
}
if ($session->ispassed == 1) {
// 0 is no 1 is yes, these are from players
$aurecord->passed = 1;
}
if ($session->isterminated == 1) {
// 0 is no 1 is yes, these are from players
$aurecord->terminated = 1;
// Add the session grade to array.
$sessiongrades[] = $session->score;
}
// Save the session scores to AU, it is ok to overwrite.
$aurecord->scores = json_encode($sessiongrades, JSON_NUMERIC_CHECK);

// Add the session grade to array.
$sessiongrades[] = $session->score;
}
// Save the session scores to AU, it is ok to overwrite.
$aurecord->scores = json_encode($sessiongrades, JSON_NUMERIC_CHECK);

// Determine gradetype and use it to save overall grade to AU.
$gradetype = $cmi5launchsettings["grademethod"];

switch ($gradetype) {

// GRADE_AUS_CMI5 = 0.
// GRADE_HIGHEST_CMI5 = 1.
// GRADE_AVERAGE_CMI5 = 2.
// GRADE_SUM_CMI5 = 3.

case 1:
$aurecord->grade = $this->cmi5launch_highest_grade($sessiongrades);
break;
case 2:
$aurecord->grade = $this->cmi5launch_average_grade($sessiongrades);
break;
default:
echo "Gradetype not found.";
}
// Determine gradetype and use it to save overall grade to AU.
$gradetype = $cmi5launchsettings["grademethod"];

switch ($gradetype) {

// Save AU scores to corresponding title.
$auscores[$aurecord->lmsid] = array($aurecord->title => $aurecord->scores);
// GRADE_AUS_CMI5 = 0.
// GRADE_HIGHEST_CMI5 = 1.
// GRADE_AVERAGE_CMI5 = 2.
// GRADE_SUM_CMI5 = 3.

// Save an overall grade to array to be passed out to grade_update.
$overallgrade[] = $aurecord->grade;
case 1:
$aurecord->grade = $this->cmi5launch_highest_grade($sessiongrades);
break;
case 2:
$aurecord->grade = $this->cmi5launch_average_grade($sessiongrades);
break;
default:

echo("Gradetype not found.");
}

// Save Au title and their scores to AU.
// Save updates to DB.
$aurecord = $DB->update_record('cmi5launch_aus', $aurecord);
// Save AU scores to corresponding title.
$auscores[$aurecord->lmsid] = array($aurecord->title => $aurecord->scores);

// Save an overall grade \to be passed out to grade_update.
$overallgrade = $aurecord->grade;


// Save Au title and their scores to AU.
// Save updates to DB.
$aurecord = $DB->update_record('cmi5launch_aus', $aurecord);

}
}
}
}

//Array to hold answer
$toreturn = array(0 => $auscores, 1 => $overallgrade);
// we need to return auscores and overallgrade.
// Array to hold answer.
$toreturn = array(0 => $auscores, 1 => $overallgrade);

return $toreturn;
}
// Restore default hadlers.
restore_exception_handler();
restore_error_handler();

return $toreturn;
} catch (\Throwable $e) {

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

// If there is an error, return the error.
throw new nullException(" Error in updating or checking user grades. Report this error to system administrator: ". $e->getMessage());
}
}
}
Loading

0 comments on commit 5e5de75

Please sign in to comment.