Skip to content

Commit

Permalink
AUview page updated
Browse files Browse the repository at this point in the history
  • Loading branch information
ADLMeganBohland committed Aug 15, 2024
1 parent ce5fd27 commit bce43da
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 78 deletions.
146 changes: 81 additions & 65 deletions AUview.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@
*/

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

require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
require('header.php');
require_once("$CFG->dirroot/lib/outputcomponents.php");


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


require_login($course, false, $cm);

global $cmi5launch, $USER;
Expand Down Expand Up @@ -99,12 +104,10 @@ function mod_cmi5launch_launchexperience(registration) {
</script>
<?php

// Is this all necessary? Cant the data come through on its own

// Retrieve the registration and AU ID from view.php.
$fromview = required_param('AU_view', PARAM_TEXT);
// Break it into array (AU is first index).
$fromview = explode(",", $fromview);
// Retrieve AU ID.
$auid = array_shift($fromview);
$auid = required_param('AU_view', PARAM_TEXT);

// First thing check for updates.
cmi5launch_update_grades($cmi5launch, $USER->id);
Expand All @@ -121,65 +124,78 @@ function mod_cmi5launch_launchexperience(registration) {
// Reload user course instance.
$userscourse = $DB->get_record('cmi5launch_usercourse', ['courseid' => $record->courseid, 'userid' => $USER->id]);

// Retrieve the registration id.
$regid = $userscourse->registrationid;

// If it is null there have been no previous sessions.
if (!$au->sessions == null) {

// Array to hold info for table population.
$tabledata = array();

// Build table.
$table = new html_table();
$table->id = 'cmi5launch_auSessionTable';
$table->caption = get_string('modulenameplural', 'cmi5launch');
$table->head = array(
get_string('cmi5launchviewfirstlaunched', 'cmi5launch'),
get_string('cmi5launchviewlastlaunched', 'cmi5launch'),
get_string('cmi5launchviewprogress', 'cmi5launch'),
get_string('cmi5launchviewgradeheader', 'cmi5launch'),
);

// Retrieve session ids.
$sessionids = json_decode($au->sessions);

// Iterate through each session by id.
foreach ($sessionids as $key => $sessionid) {

// Get the session from DB with session id.
$session = $DB->get_record('cmi5launch_sessions', array('sessionid' => $sessionid));

// Array to hold data for table.
$sessioninfo = array();


if ($session->createdat != null) {

// 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');
}
try{

if ($session->lastrequesttime != null) {
// 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\custom_warningAU', E_WARNING);
set_exception_handler('mod_cmi5launch\local\custom_warningAU');

// 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>");
// Array to hold info for table population.
$tabledata = array();

// Build table.
$table = new html_table();
$table->id = 'cmi5launch_auSessionTable';
$table->caption = get_string('modulenameplural', 'cmi5launch');
$table->head = array(
get_string('cmi5launchviewfirstlaunched', 'cmi5launch'),
get_string('cmi5launchviewlastlaunched', 'cmi5launch'),
get_string('cmi5launchviewprogress', 'cmi5launch'),
get_string('cmi5launchviewgradeheader', 'cmi5launch'),
);


// Retrieve session ids.
$sessionids = json_decode($au->sessions);

// Iterate through each session by id.
foreach ($sessionids as $key => $sessionid) {

// Get the session from DB with session id.
$session = $DB->get_record('cmi5launch_sessions', array('sessionid' => $sessionid));

// Add score to table.
$sessioninfo[] = $session->score;
// Add score to array for AU.
$sessionscores[] = $session->score;
// Array to hold data for table.
$sessioninfo = array();

// Add to be fed to table.
$tabledata[] = $sessioninfo;

if ($session->createdat != null) {

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

// Add score to table.
$sessioninfo[] = $session->score;
// Add score to array for AU.
$sessionscores[] = $session->score;

// Add to be fed to table.
$tabledata[] = $sessioninfo;
}
} catch (Exception $e) {

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

// Throw an exception.
throw new customException('loading session table on AUview page. Report this to system administrator: ' . $e->getMessage() . 'Check that session information is present in DB and session id is correct.' , 0);
}

// Write table.
Expand All @@ -188,17 +204,17 @@ function mod_cmi5launch_launchexperience(registration) {

// Update AU in table with new info.
$DB->update_record('cmi5launch_aus', $au);
}

// Build the new session link.
$newsession = "true";
// Create a string to pass the auid and new session info to next page (launch.php).
$infofornextpage = $auid . "," . $newsession;
// Restore default hadlers.
restore_exception_handler();
restore_error_handler();
}

// Pass the auid and new session info to next page (launch.php).
// New attempt button.
echo "<p tabindex=\"0\"onkeyup=\"key_test('"
. $infofornextpage . "')\"id='cmi5launch_newattempt'><button onclick=\"mod_cmi5launch_launchexperience('"
. $infofornextpage
. $auid . "')\"id='cmi5launch_newattempt'><button onclick=\"mod_cmi5launch_launchexperience('"
. $auid
. "')\" style=\"cursor: pointer;\">"
. get_string('cmi5launch_attempt', 'cmi5launch')
. "</button></p>";
Expand Down
57 changes: 57 additions & 0 deletions classes/local/errorover.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ function progresslrsreq_warning($errno, $errstr, $errfile, $errline)

}

/**
* An warning handler to use to post better warnings to users for troubleshooting.
* @param mixed $errno
* @param mixed $errstr
* @param mixed $errfile
* @param mixed $errline
* @throws \mod_cmi5launch\local\nullException
* @return never
*/
function custom_warningAU($errno, $errstr, $errfile, $errline)
{
echo "Error loading session table on AUview page. Report this to system administrator: <br> $errstr at $errfile on $errline: Check that session information is present in DB and session id is correct.";

exit;

}


/**
* An exception handler to use in AU cases when many different exceptions for data errors may be thrown.
* @param mixed $errno
Expand Down Expand Up @@ -219,3 +237,42 @@ public function customFunction() {
// $this->getTraceAsString();
}
}

/**
* Define a custom exception class, this will make pour tests meaningful
* This is a catchall custom
* 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 customException 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

// Ah maybe here is where I can differentiate them
$playermessage = "Caught error. Something went wrong " . $message;
// make sure everything is assigned properly
parent::__construct($playermessage, $code, $previous);

echo"$playermessage";
}


// 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();
}
}
13 changes: 2 additions & 11 deletions cmi5PHP/tests/progressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ protected function setUp(): void

protected function tearDown(): void
{
global $sessionids;
global $sessionids, $testcourseid, $cmi5launchsettings;
// Restore overridden global variable.
unset($GLOBALS['USER']);
unset($GLOBALS['cmi5launchsettings']);

deletetestcmi5launch_usercourse($testcourseid);
deletetestcmi5launch_sessions($sessionids);
}

Expand Down Expand Up @@ -974,9 +974,6 @@ public function testcmi5launch_retrieve_statements()
{
global $CFG, $DB, $sessionids;

// First create a fake session to pass to the function.
$sessions = maketestsessions();

// Retrieve a sessionid, we'll just use the first one.
$sessionid = $sessionids[1];

Expand Down Expand Up @@ -1039,9 +1036,6 @@ public function testcmi5launch_retrieve_statements_excep_ext()
{
global $CFG, $DB, $sessionids;

// First create a fake session to pass to the function.
$sessions = maketestsessions();

// Retrieve a sessionid, we'll just use the first one.
$sessionid = $sessionids[1];

Expand Down Expand Up @@ -1103,9 +1097,6 @@ public function testcmi5launch_retrieve_statements_excep()
{
global $CFG, $DB, $sessionids;

// First create a fake session to pass to the function.
$sessions = maketestsessions();

// Retrieve a sessionid, we'll just use the first one.
$sessionid = $sessionids[1];

Expand Down
5 changes: 3 additions & 2 deletions launch.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
$savesession = $sessionhelper->cmi5launch_get_create_session();
$cmi5launchretrieveurl = $connectors->cmi5launch_get_retrieve_url();
$retrieveaus = $auhelper->get_cmi5launch_retrieve_aus_from_db();

/*
// Retrieve registration id and au index (from AUview.php).
$fromauview = required_param('launchform_registration', PARAM_TEXT);
Expand All @@ -65,7 +65,8 @@
// Retrieve AU OR session id.
$id = array_shift($idandstatus);

*/
$id = required_param('launchform_registration', PARAM_TEXT);
// Reload cmi5 instance.
$record = $DB->get_record('cmi5launch', array('id' => $cmi5launch->id));

Expand Down

0 comments on commit bce43da

Please sign in to comment.