Skip to content

Commit

Permalink
Improving error msg, data validation and fixing notices
Browse files Browse the repository at this point in the history
  • Loading branch information
laemtl committed Dec 16, 2020
1 parent a6aab75 commit b13c8f9
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 49 deletions.
62 changes: 61 additions & 1 deletion php/libraries/AnonymousUser.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,73 @@ namespace LORIS;
*/
class AnonymousUser extends \User
{
/**
* An anonymous user username
*
* @return string
*/
function getUsername(): string
{
return "";
}

/**
* An anonymous user is not a member of any site.
*
* @return array The empty string
* @return array An empty array
*/
public function getSiteNames() : array
{
return [];
}

/**
* An anonymous user real name
*
* @return string
*/
function getFullname(): string
{
return "";
}

/**
* An anonymous user is not a member of any site.
*
* @return array An empty array
*/
function getCenterIDs(): array
{
return [];
}

/**
* An anonymous user is not a member of any project.
*
* @return array An empty array
*/
function getProjectIDs(): array
{
return [];
}

/**
* An anonymous user's language preference
*
* @return int
*/
function getLanguagePreference(): int
{
return -1;
}

/**
* Returns all sites where Examiner is active
*
* @return array
*/
function getExaminerSites(): array
{
return [];
}
}
6 changes: 3 additions & 3 deletions php/libraries/TimePoint.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -484,11 +484,11 @@ class TimePoint
/**
* Gets this timepoint's screening status.
*
* @return ?string 'Pass', 'Failure', 'Withdrawal', or 'In Progress'
* @return string 'Pass', 'Failure', 'Withdrawal', or 'In Progress'
*/
function getScreeningStatus(): ?string
function getScreeningStatus(): string
{
return $this->_timePointInfo["Screening"];
return $this->_timePointInfo["Screening"] ?? '';
}

/**
Expand Down
174 changes: 129 additions & 45 deletions tools/fix_timepoint_date_problems.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@
* @package Loris
* @author Loris Team <loris-dev@bic.mni.mcgill.ca>
* @license Loris license
* @link https://www.github.com/aces/Loris-Trunk/
* @link https://www.github.com/aces/Loris/
*/
use LORIS\StudyEntities\Candidate\CandID;

set_include_path(get_include_path().":../project/libraries:../php/libraries:");

// path to config file
$configFile = "../project/config.xml";
$configFile = dirname(__FILE__) . "/../project/config.xml";

require_once __DIR__ . "/../vendor/autoload.php";
$client = new NDB_Client();
Expand All @@ -66,25 +66,13 @@
/**
* HELP SCREEN
* display and stop processing if action=help
* or if we have missing arguments
*/
if (empty($argv[1]) || $argv[1] == 'help') {
//phpcs:disable
echo <<<USAGE
Usage:
fix_timepoint_date_problems.php help - displays this msg
fix_timepoint_date_problems.php fix_date <candID> <newCorrectDate> <dob/edc>
fix_timepoint_date_problems.php fix_date <candID> <newCorrectDate> <screening/visit> <sessionID>
fix_timepoint_date_problems.php diagnose <candID>
fix_timepoint_date_problems.php diagnose <candID> [<newCorrectDate> <dob/edc/>]
fix_timepoint_date_problems.php diagnose <candID> [<newCorrectDate> <screening/visit> <sessionID>]\n");
fix_timepoint_date_problems.php add_instrument <candID> <sessionID> <test_name>
NOTES: The date format is: YYYY-MM-DD.
fix_date: updates the dates in candidate or session table.
Does not alter the BVL battery
USAGE;
//phpcs:enable
if (empty($argv[1]) || empty($argv[2]) || $argv[1] == 'help') {
printUsage();
}


/**
* Get cmd-line arguments
*/
Expand All @@ -97,12 +85,20 @@
// get the rest of the arguments
switch ($action) {
case 'fix_date':
if (empty($argv[3]) || empty($argv[4])) {
printUsage();
}

// new date
$newDate = $argv[3];
// date type
$dateType = strtolower($argv[4]);
// sessionID
if (in_array($dateType, ['screening', 'visit'])) {
if (empty($argv[5])) {
printUsage();
}

$sessionID = $argv[5];
}
break;
Expand All @@ -117,12 +113,20 @@
$dateType = strtolower($argv[4]);
// sessionID only present when dateType defined
if (in_array($dateType, ['screening', 'visit'])) {
if (empty($argv[5])) {
printUsage();
}

$sessionID = $argv[5];
}
}
break;

case 'add_instrument':
if (empty($argv[3]) || empty($argv[4])) {
printUsage();
}

// sessionID
$sessionID = $argv[3];
// test name
Expand Down Expand Up @@ -205,7 +209,17 @@
case 'add_instrument':
// add a missing instrument (sessionID and test name are checked inside the
// function)
$success = addInstrument($sessionID, $testName);
try {
$success = addInstrument($sessionID, $testName);
} catch (LorisException $e) {
fwrite(
STDERR,
"Error: failed to add the instrument $testName for " .
"timepoint ($sessionID):\n"
);
fwrite(STDERR, $e->getMessage(). "\n");
}

break;

/**
Expand All @@ -216,11 +230,20 @@
// fix the date (arguments are checked by the function
// wrapping in an if/else statement to avoid PHP Notice when $sessionID is
// empty
if (!empty($sessionID)) {
$success = fixDate($candID, $dateType, $newDate, $sessionID);
} else {
$success = fixDate($candID, $dateType, $newDate);
try {
if (!empty($sessionID)) {
$success = fixDate($candID, $dateType, $newDate, $sessionID);
} else {
$success = fixDate($candID, $dateType, $newDate);
}
} catch (LorisException $e) {
fwrite(
STDERR,
"Error: failed to fix the date for candidate ($candID):\n"
);
fwrite(STDERR, $e->getMessage(). "\n");
}

break;

/**
Expand Down Expand Up @@ -269,14 +292,14 @@
"Error: failed to get the list of needed instruments for " .
"candidate ($candID), timepoint ($sessionID):\n"
);
//print error message from dianose function
//print error message from diagnose function
fwrite(STDERR, $e->getMessage(). "\n");
continue;
}

// if there are missing instruments
//if (count($listNewInstruments) > 0) {
if (!empty($listNewInstruments) >0) {
if (!empty($listNewInstruments) > 0) {
fwrite(STDERR, "\n Missing instruments are:\n");

//print out the list of missing instruments
Expand All @@ -290,6 +313,52 @@
break;
} // end switch ($action)


/**
* Print usage
*
* @return void
*/
function printUsage()
{
fwrite(STDERR, "Usage: \n\n");
fwrite(STDERR, "fix_timepoint_date_problems.php help\n");
fwrite(
STDERR,
"fix_timepoint_date_problems.php fix_date "
. "<candID> <newCorrectDate> <dob/edc>\n"
);
fwrite(
STDERR,
"fix_timepoint_date_problems.php fix_date "
. "<candID> <newCorrectDate> <screening/visit> <sessionID>\n"
);
fwrite(STDERR, "fix_timepoint_date_problems.php diagnose <candID> \n");
fwrite(
STDERR,
"fix_timepoint_date_problems.php diagnose <candID> "
. "[<newCorrectDate> <dob/edc/>]\n"
);
fwrite(
STDERR,
"fix_timepoint_date_problems.php diagnose "
. "<candID> [<newCorrectDate> <screening/visit> <sessionID>]\n"
);
fwrite(
STDERR,
"fix_timepoint_date_problems.php add_instrument "
. "<candID> <sessionID> <test_name>\n"
);
fwrite(STDERR, "NOTES: The date format is: YYYY-MM-DD.\n");
fwrite(
STDERR,
"fix_date: updates the dates in candidate or session table. "
. "Does not alter the BVL battery\n"
);

exit();
}

/**
* Adds a bvl instrument to the battery
* the function checks the args, add the instrument (if valid), creates a bvl
Expand All @@ -306,7 +375,7 @@ function addInstrument($sessionID, $testName)
{
// check the user $_ENV['USER']
$user =& User::singleton(getenv('USER'));
if ($user->getUsername() == null) {
if (empty($user->getUsername())) {
throw new LorisException(
"Error: Database user named " . getenv('USER')
. " does not exist. Please create and then retry script\n"
Expand Down Expand Up @@ -411,11 +480,10 @@ function fixDate($candID, $dateType, $newDate, $sessionID = null)
{
// check the user $_ENV['USER']
$user =& User::singleton(getenv('USER'));

if ($user->getUsername() == null) {
if (empty($user->getUsername())) {
throw new LorisException(
"Error: Database user named " . getenv('USER')
. " does not exist. Please create and then retry script\n"
"Error: A loris user named " . getenv('USER')
. " does not exist. Please create it and then retry the script.\n"
);
}

Expand Down Expand Up @@ -600,7 +668,7 @@ function diagnose($sessionID, $dateType = null, $newDate = null)
if (($dateType=='dob' && $subProjectID==1)
|| ($dateType=='edc' && $subProjectID==2)
) {
$dateBirth =$newDate;
$dateBirth = $newDate;
} else {
$dateBirth = $timePoint->getEffectiveDateOfBirth();
}
Expand All @@ -610,8 +678,12 @@ function diagnose($sessionID, $dateType = null, $newDate = null)
|| empty($stageList['screening']['status'])
) {
throw new LorisException(
"Error: Cannot diagnose the non-started timepoint ($sessionID) " .
"for candidate (".$timePoint->getCandID().")!"
"Error: Cannot diagnose the timepoint ($sessionID) " .
"for candidate (".$timePoint->getCandID()."). "
. "Make sure that the timepoint is started ("
. $timePoint->getCurrentStage().") "
. "and screening status is set ("
. $stageList['screening']['status'].")."
);
}
// check the subProjectID
Expand All @@ -628,25 +700,37 @@ function diagnose($sessionID, $dateType = null, $newDate = null)
foreach ($stageList as $stage => $stageData) {
// if the stage is started
if (!empty($stageData['status'])) {
$dateOfStage = (!empty($newDate) && strtolower($dateType)==$stage) ?
$newDate
: $stageList[$stage]['date'];

// compute subject age for the current stage
$ageArray = Utility::calculateAge($dateBirth, $dateOfStage);
$age = ($ageArray['year'] * 12 + $ageArray['mon']) * 30
+ $ageArray['day'];
if ($age < 0) {
$age = 0;
if (!empty($newDate) && strtolower($dateType)==$stage) {
$dateOfStage = $newDate;
} else if (!empty($stageList[$stage]['date'])) {
$dateOfStage = $stageList[$stage]['date'];
} else {
$dateOfStage = null;
$age = 0;
fwrite(
STDERR,
"ERROR: Stage $stage for sessionID $sessionID"
. " does not have a date.\n"
);
}

if (!empty($dateOfStage)) {
// compute subject age for the current stage
$ageArray = Utility::calculateAge($dateBirth, $dateOfStage);
$age = ($ageArray['year'] * 12 + $ageArray['mon']) * 30
+ $ageArray['day'];
if ($age < 0) {
$age = 0;
}
unset($ageArray);
}
unset($ageArray);
fwrite(STDERR, "Age at $stage: $age [ $dateBirth $dateOfStage]\n");

// create battery object
$battery = new NDB_BVL_Battery();

// set the SessionID for the battery
$success = $battery->selectBattery($sessionID);
$success = $battery->selectBattery(new \SessionID(strval($sessionID)));

// get the existing battery for the stage
$existingTests = $battery->getBattery($stage);
Expand Down

0 comments on commit b13c8f9

Please sign in to comment.