Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[tools] support JSON instruments in fix_candidate_age.php #8286

Merged
merged 1 commit into from
Apr 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 28 additions & 36 deletions tools/data_integrity/fix_candidate_age.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* the NDB_BVL_Instrument::_saveValues() function to safely make changes to ages.
*
* Usage
* - php fix_candidate_age.php;
* - php fix_candidate_age.php check;
* - php fix_candidate_age.php confirm;
*
* PHP Version 7
Expand Down Expand Up @@ -35,76 +35,69 @@
$confirm = true;
}

$incorrectAges = [];
$nullAges = [];
$verifiedTables = [];
$incorrectAges = [];
$nullAges = [];
$verifiedInstruments = [];

foreach ($instruments as $testName=>$instrument) {

$fullName = $instrument->getFullName();
//Check what database table is associated with this instrument
echo "Instrument: $testName ($fullName)\n";

$table = $instrument->table;

if (in_array($table, $verifiedTables, true)) {
echo "\tTable $table has already been verified. skipping.\n";
if (in_array($testName, $verifiedInstruments, true)) {
echo "\tInstrument $testName has already been verified. skipping.\n";
continue;
} else {
//Add table name to repertoire to make sure it is not checked again
//in case 2+ instruments use the same table
$verifiedTables[] = $table;
$verifiedInstruments[] = $testName;
}

if (!$DB->tableExists($table)) {
echo "\tTable $table for instrument $testName does not exist in the ".
"Database\n";
continue;
} else if (strpos('_proband', $testName) !== false) {
if (strpos('_proband', $testName) !== false) {
echo "\t$testName is a Proband instrument and should be handled ".
"separately.\n";
continue;
}

$form = array_keys($instrument->form->form);
// Skip if Date taken does not exist
if (!$DB->columnExists($table, 'Date_taken')) {
if (!in_array('Date_taken', $form)) {
echo "\t"
. "$testName does not use a `Date_taken` field and should be ".
"handled separately."
. "\n";
continue;
}
// Skip if Date taken does not exist
if (!$DB->columnExists($table, 'Candidate_Age')) {
// Skip if Candidate Age does not exist
if (!in_array('Candidate_Age', $form)) {
echo "\t"
. "$testName does not use a `Candidate_Age` field "
. "and should be handled separately.\n";
continue;
}

//Get instrument SQL table
$DBInstTable = $DB->pselect(
"SELECT i.CommentID, Date_taken, Candidate_Age
FROM $table i
LEFT JOIN flag f ON (i.commentID=f.CommentID)
LEFT JOIN session s ON (f.SessionID=s.ID)
LEFT JOIN candidate c ON (s.CandID=c.CandID)
WHERE c.Active='Y' AND s.Active='Y'",
//Get CommentID list
$CommentIDs = $DB->pselectCol(
"SELECT f.CommentID FROM flag f
JOIN session s ON s.ID=f.SessionID
JOIN candidate c ON c.CandID=s.CandID
WHERE c.Active='Y' AND s.Active='Y'",
[]
);

foreach ($DBInstTable as $k => $row) {
foreach ($CommentIDs as $commentID) {
// Get Instrument Instance with commentID
try {
$instrument = NDB_BVL_Instrument::factory(
$lorisInstance,
$testName,
$row['CommentID'],
$commentID,
'',
false
);
} catch (Exception $e) {
echo "\t$testName instrument row with CommentID: ".$row['CommentID'].
echo "\t$testName instrument row with CommentID: ".$commentID.
" was ignored for one of the following reasons:\n".
" - The candidate is inactive.\n".
" - The session is inactive.\n\n";
Expand All @@ -114,21 +107,21 @@
if (!$instrument) {
// instrument does not exist
echo "\t"
. "$testName for CommentID:$row[CommentID] could not be instantiated."
. "$testName for CommentID:$commentID could not be instantiated."
. "\n";
continue;
}

$dateTaken = $row['Date_taken'];
$commentID = $row['CommentID'];
$data = $instrument->getInstanceData();
$dateTaken = $data['Date_taken'] ?? null;

// Flag for problem with date
$trouble =false;
if (!empty($row['Date_taken'])) {
if (!empty($dateTaken)) {
// Check if age is null OR if wrong age
if (empty($row['Candidate_Age'])) {
if (empty($data['Candidate_Age'])) {
// Null age
$nullAges[$testName][$commentID] = $row['CommentID'];
$nullAges[$testName][$commentID] = $commentID;
$trouble =true;
} else {
// get Age from instrument class
Expand All @@ -137,10 +130,9 @@
$calculatedAge
);
//Compare age to value saved in the instrument table
$DBAge = $instrument->getFieldValue('Candidate_Age');
$DBAge = $data['Candidate_Age'];

if ($calculatedAgeMonths != $DBAge) {
//$incorrectAges[] = $row;
$incorrectAges[$testName][$commentID] = [
'cal' => $calculatedAgeMonths,
'db' => $DBAge,
Expand Down