From b13c8f91227c7d4d33fdbb043c3d97ba09f294b2 Mon Sep 17 00:00:00 2001 From: Laetitia Fesselier Date: Thu, 8 Oct 2020 00:33:40 -0400 Subject: [PATCH] Improving error msg, data validation and fixing notices --- php/libraries/AnonymousUser.class.inc | 62 ++++++++- php/libraries/TimePoint.class.inc | 6 +- tools/fix_timepoint_date_problems.php | 174 +++++++++++++++++++------- 3 files changed, 193 insertions(+), 49 deletions(-) diff --git a/php/libraries/AnonymousUser.class.inc b/php/libraries/AnonymousUser.class.inc index caced1e8817..cd7b922a3ec 100644 --- a/php/libraries/AnonymousUser.class.inc +++ b/php/libraries/AnonymousUser.class.inc @@ -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 []; + } } diff --git a/php/libraries/TimePoint.class.inc b/php/libraries/TimePoint.class.inc index 15dc177dac9..6dd6c5aab12 100644 --- a/php/libraries/TimePoint.class.inc +++ b/php/libraries/TimePoint.class.inc @@ -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"] ?? ''; } /** diff --git a/tools/fix_timepoint_date_problems.php b/tools/fix_timepoint_date_problems.php index aac3f561022..7b9a31d8ffa 100755 --- a/tools/fix_timepoint_date_problems.php +++ b/tools/fix_timepoint_date_problems.php @@ -47,14 +47,14 @@ * @package Loris * @author Loris Team * @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(); @@ -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 << - fix_timepoint_date_problems.php fix_date - fix_timepoint_date_problems.php diagnose - fix_timepoint_date_problems.php diagnose [ ] - fix_timepoint_date_problems.php diagnose [ ]\n"); - fix_timepoint_date_problems.php add_instrument - 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 */ @@ -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; @@ -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 @@ -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; /** @@ -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; /** @@ -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 @@ -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 " + . " \n" + ); + fwrite( + STDERR, + "fix_timepoint_date_problems.php fix_date " + . " \n" + ); + fwrite(STDERR, "fix_timepoint_date_problems.php diagnose \n"); + fwrite( + STDERR, + "fix_timepoint_date_problems.php diagnose " + . "[ ]\n" + ); + fwrite( + STDERR, + "fix_timepoint_date_problems.php diagnose " + . " [ ]\n" + ); + fwrite( + STDERR, + "fix_timepoint_date_problems.php add_instrument " + . " \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 @@ -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" @@ -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" ); } @@ -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(); } @@ -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 @@ -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);