diff --git a/test/run-php-linter.sh b/test/run-php-linter.sh index d367bb2e8df..a10f21c9b9f 100755 --- a/test/run-php-linter.sh +++ b/test/run-php-linter.sh @@ -36,6 +36,7 @@ declare -a tools_list=( 'raisinbread_refresh.php' 'recreate_conflicts.php' 'resetpassword.php' + 'detect_conflicts.php' 'setconfig.php' 'single_use/Cleanup_multiple_firstVisits.php' 'single_use/Convert_LorisMenuID_to_ModuleID.php' diff --git a/tools/detect_conflicts.php b/tools/detect_conflicts.php index 61a81366bc1..74d05dd7c68 100755 --- a/tools/detect_conflicts.php +++ b/tools/detect_conflicts.php @@ -1,18 +1,17 @@ #!/usr/bin/php * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 - * @license Loris License * @link https://github.com/aces/Loris */ require_once __DIR__ . "/../vendor/autoload.php"; @@ -25,50 +24,53 @@ require_once "NDB_Client.class.inc"; require_once "TimePoint.class.inc"; -/* - * The minimum number of arguments required to run this script. +/* + * The minimum number of arguments required to run this script. * - * @var int + * @var int */ const MIN_NUMBER_OF_ARGS = 2; /* - * The maximum number of arguments to run this script. + * The maximum number of arguments to run this script. * - * @var int + * @var int */ const MAX_NUMBER_OF_ARGS = 6; /// User prompt - -if ((count($argv) < MIN_NUMBER_OF_ARGS) || (count($argv) > MAX_NUMBER_OF_ARGS)) { - - echo "Usage: php detect_conflicts.php -i [instrument] -t [timepoint]\n"; - echo "Example: php detect_conflicts.php -i bdi -t 3month \n\n"; - - echo "Usage: php detect_conflicts.php -i [instrument] \n"; - echo "Example: php detect_conflicts.php -i bdi \n\n"; - echo "Usage: php detect_conflicts.php -r [instrument/all] [-y to confirm]\n"; - echo "Example: php detect_conflicts.php -r all -y\n"; - echo "Example: php detect_conflicts.php -r bdi -y \n\n"; - - echo "to insert the conflicts into conflicts_unresolved table type -c -i \n"; - echo "to remove all/re-insert the conflicts into conflicts_unresolved table type -m \n"; - echo "to run the script for all the instruments simply type -i all \n"; - echo "to remove ignored conflicts type -r\n\n"; - echo "NOTE: ONLY THE FAILED VISITS ARE EXCLUDED BY THIS SCRIPT \n"; +if ((count($argv) < MIN_NUMBER_OF_ARGS) + || (count($argv) > MAX_NUMBER_OF_ARGS) +) { + echo <<getSetting('DoubleDataEntryInstruments'); -$db_config = $config->getSetting('database'); -$paths = $config->getSetting('paths'); -$dataDir = $paths['base'] . $config->getSetting('log'); -$diff = null; -$new_conflicts = array(); -$detected_conflicts = array(); -$current_conflicts = array(); -$conflicts_to_be_excluded = array(); +$db_config = $config->getSetting('database'); +$paths = $config->getSetting('paths'); +$dataDir = $paths['base'] . $config->getSetting('log'); +$diff = null; +$new_conflicts = []; +$detected_conflicts = []; +$current_conflicts = []; +$conflicts_to_be_excluded = []; if ($delete_ignored_conflicts) { if ($instrument_ignored[0] == "all") { @@ -130,49 +137,42 @@ if (isset($instrument_ignored)) { if ($confirm) { echo "Removing ignored conflicts\n\n"; - } - else { + } else { echo "Detecting ignored conflicts\n\n"; } detectIgnoreColumns($instrument_ignored, $confirm); if ($confirm) { echo "Finished.\n\n"; - } - else { + } else { echo "\n\nRun this tool again with the argument '-y' to ". "perform the changes\n\n"; } } else { echo "No DDE instruments found to remove ignored columns."; } -} -else { - +} else { // Check to see if the variable instrument is set if (($instrument=='all') ||($instrument=='All')) { - $Factory = NDB_Factory::singleton(); - $DB = $Factory->Database(); //=& Database::singleton(); + $DB = $Factory->Database(); $instruments_q = $DB->pselect( "SELECT Test_name FROM test_names", - array() + [] ); - $instruments = array(); + $instruments = []; foreach ($instruments_q as $row) { if (isset($row['Test_name'])) { $instruments[$row['Test_name']] =$row['Test_name']; } } - } else { - $instruments = array($instrument=>$instrument); + $instruments = [$instrument => $instrument]; } foreach ($instruments as $instrument) { if (isset($instrument)) { - include_once $paths['base'] . "project/instruments/NDB_BVL_Instrument_$instrument.class.inc"; @@ -185,18 +185,24 @@ //check to make sure that the commentids are set if (isset($commentids)) { //get the current conflicts - $current_conflicts = getCurrentUnresolvedConflicts( - $instrument, $visit_label + $current_conflicts = getCurrentUnresolvedConflicts( + $instrument, + $visit_label ); $detected_conflicts = detectConflicts( - $instrument, $commentids, $current_conflicts + $instrument, + $commentids, + $current_conflicts ); - $new_conflicts = getNewConflicts( - $current_conflicts, $detected_conflicts + $new_conflicts = getNewConflicts( + $current_conflicts, + $detected_conflicts ); $conflicts_to_be_excluded = detectConflictsTobeExcluded( - $instrument, $commentids, $current_conflicts + $instrument, + $commentids, + $current_conflicts ); /** * Only Create report for the confliclits @@ -204,34 +210,42 @@ * 2) that should be deleted from the conflicts_unresolved table */ if ((!$change) && (!$change_all)) { - //just show the conflicts print "This will only display the conflicts needed to be inserted \n"; print "To re-create the conflict, run this script with -c option \n"; - if ((empty($new_conflicts)) && empty($conflicts_to_be_excluded)) { + if ((empty($new_conflicts)) + && empty($conflicts_to_be_excluded) + ) { print "No new conflicts or current conflicts to be removed are detected for instrument $instrument \n"; } else { writeCSV( - $new_conflicts, $dataDir, $instrument, $visit_label, + $new_conflicts, + $dataDir, + $instrument, + $visit_label, "Conflicts_to_be_inserted" ); writeCSV( - $conflicts_to_be_excluded, $dataDir, $instrument, - $visit_label, "Conflicts_to_be_removed" + $conflicts_to_be_excluded, + $dataDir, + $instrument, + $visit_label, + "Conflicts_to_be_removed" ); } } /** - * Only insert those conflicts which are new and are not currently - * in the conflict_uresolved + * Only insert those conflicts which are new and are not + * currently in the conflict_uresolved * 1)remove/clear the existing conflicts * 2) And then re-create the conflicts */ - ///if the instrument is not provided, run it only for all the instruments + //if the instrument is not provided, run it only for all the + //instruments if ($change) { if ((empty($new_conflicts)) || (!isset($new_conflicts))) { die("There are No new conflicts to be inserted "); @@ -249,22 +263,23 @@ /** - * Remove all the current conflicts for the givent instrument/visit_label - * And re-insert them + * Remove all the current conflicts for the givent + * instrument/visit_label and re-insert them */ if ($change_all) { foreach ($commentids as $cid) { - ConflictDetector::clearConflictsForInstance($cid['CommentID']); + ConflictDetector::clearConflictsForInstance( + $cid['CommentID'] + ); $diff = ConflictDetector::detectConflictsForCommentIds( - $test_name, $cid['CommentID'], $cid['DDECommentID'] + $test_name, + $cid['CommentID'], + $cid['DDECommentID'] ); ConflictDetector::recordUnresolvedConflicts($diff); } } - - } - } else { die("the instrument is not set or there are no commentids \n"); } @@ -273,24 +288,23 @@ /** -*Get the commentids for the given instrument, candidate and visit_label -* -* @param String $test_name The instrument been searched -* @param string $visit_label The visit_label been searched -* @param string $candid The candid been searched -* -* @return array $commentids An array of commentids found -*/ - -function getCommentIDs($test_name, $visit_label=null, $candid=null) + * Get the commentids for the given instrument, candidate and visit_label + * + * @param String $test_name The instrument been searched + * @param ?string $visit_label The visit_label been searched + * @param ?string $candid The candid been searched + * + * @return array $commentids An array of commentids found + */ +function getCommentIDs($test_name, $visit_label = null, $candid = null) { - $db =& Database::singleton(); - $params = array(); - $query = "SELECT CommentID, s.visit_label,Test_name, + $db =& Database::singleton(); + $params = []; + $query = "SELECT CommentID, s.visit_label,Test_name, CONCAT('DDE_', CommentID) AS DDECommentID FROM flag f JOIN session s ON (s.ID=f.SessionID) JOIN candidate c ON (c.CandID=s.CandID)"; - $where = " WHERE CommentID NOT LIKE 'DDE%' + $where = " WHERE CommentID NOT LIKE 'DDE%' AND s.Active='Y' AND c.Active='Y' AND s.Visit <> 'Failure'"; if ($test_name!=null) { @@ -298,33 +312,29 @@ function getCommentIDs($test_name, $visit_label=null, $candid=null) $params['instrument'] = $test_name; if (($visit_label!=null) && (isset($visit_label))) { - $where .= " AND s.visit_label= :vlabel"; + $where .= " AND s.visit_label= :vlabel"; $params['vlabel'] = $visit_label ; } } - $query .=$where; + $query .=$where; $commentids = $GLOBALS['DB']->pselect($query, $params); return $commentids; } - - /** -*Get the conflicts currently in the conflicts_unresolved table -* -* @param String $test_name The instrument been searched -* @param string $visit_label The visit_label been searched -* -* @return array $conflicts An array of conflicts detected -*/ - - -function getCurrentUnresolvedConflicts($test_name,$visit_label=null) + * Get the conflicts currently in the conflicts_unresolved table + * + * @param String $test_name The instrument been searched + * @param ?string $visit_label The visit_label been searched + * + * @return array $conflicts An array of conflicts detected + */ +function getCurrentUnresolvedConflicts($test_name, $visit_label = null): array { - $db =& Database::singleton(); - $params = array(); - $query = "SELECT cu.* FROM conflicts_unresolved cu + $db =& Database::singleton(); + $params = []; + $query = "SELECT cu.* FROM conflicts_unresolved cu JOIN flag f on (f.commentid = cu.commentid1) JOIN session s on (s.id = f.sessionid) JOIN candidate c on (c.CandID = s.CandID)"; @@ -336,31 +346,29 @@ function getCurrentUnresolvedConflicts($test_name,$visit_label=null) $params['instrument'] = $test_name; if ($visit_label!=null) { - $where .= " AND s.visit_label= :visit"; + $where .= " AND s.visit_label= :visit"; $params['visit'] = $visit_label ; } } - $query .=$where; + $query .=$where; $conflicts = $db->pselect($query, $params); return $conflicts; } - /** -* detect the conflicts by -* 1) instantiating the instrument for each given commmentid -* 2) and then taking the diff -* -* @param String $test_name The instrument been searched -* @param Array $commentids An array of commentids used as haystack -* @param Array $current_conflicts An array of current conflicts detected -* -* @return Array $detected_conflicts List of newly detected conflicts -*/ - -function detectConflicts($test_name,$commentids,$current_conflicts) + * Detect the conflicts by + * 1) instantiating the instrument for each given commmentid + * 2) and then taking the diff + * + * @param String $test_name The instrument been searched + * @param Array $commentids An array of commentids used as haystack + * @param Array $current_conflicts An array of current conflicts detected + * + * @return Array $detected_conflicts List of newly detected conflicts + */ +function detectConflicts($test_name, $commentids, $current_conflicts) { - $detected_conflicts = array(); + $detected_conflicts = []; /** * Go through each commentid */ @@ -368,8 +376,10 @@ function detectConflicts($test_name,$commentids,$current_conflicts) /** * Detect new conflicts */ - $diff=ConflictDetector::detectConflictsForCommentIds( - $test_name, $cid['CommentID'], $cid['DDECommentID'] + $diff =ConflictDetector::detectConflictsForCommentIds( + $test_name, + $cid['CommentID'], + $cid['DDECommentID'] ); if ($diff!=null) { foreach ($diff as $row) { @@ -383,18 +393,17 @@ function detectConflicts($test_name,$commentids,$current_conflicts) /** -* Write the data into a csv file -* -* @param String $output Array of data been written into csv -* @param String $path The file path -* @param String $instrument The name of the instrument -* @param String $visit_label The name of the visit -* @param String $prefix The type of csv file -* -* @return Null -*/ - -function writeCSV($output,$path,$instrument,$visit_label,$prefix) + * Write the data into a csv file + * + * @param String $output Array of data been written into csv + * @param String $path The file path + * @param String $instrument The name of the instrument + * @param String $visit_label The name of the visit + * @param String $prefix The type of csv file + * + * @return Null + */ +function writeCSV($output, $path, $instrument, $visit_label, $prefix) { /** @@ -415,7 +424,7 @@ function writeCSV($output,$path,$instrument,$visit_label,$prefix) unset($column_headers['ExtraKey1']); unset($column_headers['ExtraKey2']); - $column_headers = array_keys($column_headers); + $column_headers = array_keys($column_headers); $column_headers[] = "Visit_label"; $column_headers[] = "PSCID"; $column_headers[] = "CandID"; @@ -428,8 +437,8 @@ function writeCSV($output,$path,$instrument,$visit_label,$prefix) unset($data['ExtraKey2']); $info = getInfoUsingCommentID($data['CommentId1']); $data['Visit_label'] = $info['Visit_label']; - $data['CandID'] = $info['CandID']; - $data['PSCID'] = $info['PSCID']; + $data['CandID'] = $info['CandID']; + $data['PSCID'] = $info['PSCID']; fputcsv($fp, $data, "\t"); //write the headers to the CSV file } fclose($fp); @@ -441,21 +450,21 @@ function writeCSV($output,$path,$instrument,$visit_label,$prefix) /** -* Return data using the commentid -* -* @param String $commentid extract data using commentid -* -* @return Array $data Result of the query -*/ - + * Return data using the commentid + * + * @param String $commentid extract data using commentid + * + * @return Array $data Result of the query + */ function getInfoUsingCommentID($commentid) { - $db =& Database::singleton(); + $db =& Database::singleton(); $data = $db->pselectRow( "SELECT c.PSCID, c.CandID, s.Visit_label FROM flag f JOIN session s on (f.sessionid=s.ID) JOIN candidate c on (c.CandID=s.CandID) - WHERE f.CommentID = :cid", array('cid'=>$commentid) + WHERE f.CommentID = :cid", + ['cid' => $commentid] ); return $data; } @@ -463,29 +472,28 @@ function getInfoUsingCommentID($commentid) /** -* *Detect those conflicts which are currently in the conflicts_unresolved table -* But should be excluded based on the _doubleDataEntryDiffIgnoreColumns array -* -* @param String $instrument The instrument used for conflict detection -* @param Array $commentids Used for creation of instrument-instantiation -* @param Array $current_conflicts An array of current conflicts detected -* -* @return Array $conflicts_to_excluded Array of confllicts to be execluded -*/ -function detectConflictsTobeExcluded($instrument,$commentids,$current_conflicts) + * Detect those conflicts which are currently in the conflicts_unresolved table + * But should be excluded based on the _doubleDataEntryDiffIgnoreColumns array + * + * @param String $instrument The instrument used for conflict detection + * @param Array $commentids Used for creation of instrument-instantiation + * @param Array $current_conflicts An array of current conflicts detected + * + * @return Array $conflicts_to_excluded Array of confllicts to be execluded + */ +function detectConflictsTobeExcluded($instrument, $commentids, $current_conflicts) { - - $conflicts_to_excluded = array(); - $instance1 =& NDB_BVL_Instrument::factory( - $instrument, $commentids[0]['CommentID'], null + $conflicts_to_excluded = []; + $instance1 =& NDB_BVL_Instrument::factory( + $instrument, + $commentids[0]['CommentID'], + null ); $ignore_columns = $instance1->_doubleDataEntryDiffIgnoreColumns; foreach ($current_conflicts as $conflict) { - /** - * if the field is part of the ignore_columns, - * and it doesn exist in the conflict array - * then track it - */ + // if the field is part of the ignore_columns, + // and it doesn exist in the conflict array + // then track it if (in_array($conflict['FieldName'], $ignore_columns)) { $conflicts_to_excluded[] = $conflict; } @@ -493,33 +501,30 @@ function detectConflictsTobeExcluded($instrument,$commentids,$current_conflicts) return $conflicts_to_excluded; } - /** * Detect those conflicts not currently in conflicts unresolved table * - * @param Array $current_conflicts The list of current conflicts detected + * @param Array $current_conflicts The list of current conflicts detected * @param Array $detected_conflicts The list of newly created conflicts - * + * * @return Array $new_conflicts The list of conflicts to be included */ - -function getNewConflicts ($current_conflicts, $detected_conflicts) +function getNewConflicts($current_conflicts, $detected_conflicts) { - $new_conflicts = array(); + $new_conflicts = []; /** * 1) Make sure that the $detected_conflicts is not empty - * 2) if the current_conflicts is empty, then the + * 2) if the current_conflicts is empty, then the * new conflicts = $detected_conflicts */ if (!(empty($detected_conflicts))) { /** * 1) go through the current conflicts - * 2) and detect those conflicts in recreated conflicts + * 2) and detect those conflicts in recreated conflicts * which are not in the current_conflicts */ foreach ($detected_conflicts as $re_conflict) { - if (empty($current_conflicts)) {///if the conflict doesn't exist $new_conflicts[] = $re_conflict; } else { @@ -532,21 +537,20 @@ function getNewConflicts ($current_conflicts, $detected_conflicts) return $new_conflicts; } - /** - * check to see if the commentid and fieldname exist in the array of conflicts + * Check to see if the commentid and fieldname exist in the array of conflicts * if it doesn't then return false * and if they do, then check to see if the values are the same, * if not the same, return false * * @param String $conflict The needle - * @param Array $conflicts The Haysta - * + * @param Array $conflicts The Haystack + * * @return boolean true if needle exists , else false */ -function findConflict($conflict,$conflicts) +function findConflict($conflict, $conflicts) { - $found=false; + $found =false; foreach ($conflicts as $cf) { ////////if the value exists then set found to true//// if ((in_array($conflict['CommentId1'], $cf)) @@ -563,65 +567,82 @@ function findConflict($conflict,$conflicts) return false; } - - - - /** * Populates the DDE ignore fields for each instrument and runs * the ignoreColumn function on the instrument for the given fields - * @param $instruments + * + * @param array $instruments The instruments to check + * @param bool $confirm Whether to execute + * + * @return void + * * @throws Exception */ function detectIgnoreColumns($instruments, $confirm) { - $instrumentFields = array(); + $instrumentFields = []; foreach ($instruments as $instrument) { $file = "../project/instruments/NDB_BVL_Instrument_$instrument.class.inc"; if (file_exists($file)) { include_once $file; - $commentids = getCommentIDs($instrument, null); - $instance =& NDB_BVL_Instrument::factory($instrument, $commentids[0]['CommentID'], null); + $commentids = getCommentIDs($instrument, null); + $instance =& NDB_BVL_Instrument::factory( + $instrument, + $commentids[0]['CommentID'], + null + ); $DDEIgnoreFields = $instance->_doubleDataEntryDiffIgnoreColumns; if ($DDEIgnoreFields != null) { foreach ($DDEIgnoreFields as $key => $DDEField) { - $instrumentFields = array_merge($instrumentFields, array($DDEField => $instrument)); + $instrumentFields = array_merge( + $instrumentFields, + [$DDEField => $instrument] + ); } } else { echo "No DDE ignore fields found for " . $instrument; } ignoreColumn($instrument, $instrumentFields, $confirm); - } - else { + } else { echo $file . " was not found.\n"; } } } -/* +/** * Prints the instrument-specific ignore columns to be removed * Removes the fields if confirmation is set + * + * @param string $instrument The instrument name + * @param array $instrumentFields The instrument fields + * @param bool $confirm whether to execute the command or not + * + * @return void */ -function ignoreColumn($instrument, $instrumentFields, $confirm) { +function ignoreColumn($instrument, $instrumentFields, $confirm) +{ $db =& Database::singleton(); foreach ($instrumentFields as $field => $instr) { - - - $query = "SELECT TableName, FieldName, Value1, Value2 FROM conflicts_unresolved - WHERE TableName = '$instrument' AND FieldName = '$field'"; - $ignoreColumn = $db->pselectOne($query, array()); + $query = "SELECT TableName, FieldName, Value1, Value2 + FROM conflicts_unresolved + WHERE TableName = '$instrument' AND FieldName = '$field'"; + $ignoreColumn = $db->pselectOne($query, []); if (!empty($ignoreColumn)) { - $query = "SELECT TableName, FieldName, CommentId1, Value1, CommentId2, Value2 FROM conflicts_unresolved WHERE TableName = '$instrument' AND FieldName = '$field'"; - $conflictsToRemove = $db->pselect($query, array()); + $query = "SELECT + TableName, FieldName, CommentId1, Value1, CommentId2, Value2 + FROM conflicts_unresolved + WHERE TableName = '$instrument' AND FieldName = '$field'"; + $conflictsToRemove = $db->pselect($query, []); print_r($conflictsToRemove); if ($confirm) { - $query = "DELETE FROM conflicts_unresolved WHERE TableName = '$instrument' AND FieldName = '$field'"; + $query = "DELETE FROM conflicts_unresolved + WHERE TableName = '$instrument' AND FieldName = '$field'"; $db->run($query); } }