From ccb3e7468e6c8689d29386de315949eb534a12fe Mon Sep 17 00:00:00 2001 From: Adam Stewart Date: Thu, 14 Mar 2024 16:26:33 -0500 Subject: [PATCH 1/5] Duplicates report. --- server/RoboFile.php | 6 + .../scripts/generate-duplicates-report.php | 152 ++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 server/hedley/modules/custom/hedley_admin/scripts/generate-duplicates-report.php diff --git a/server/RoboFile.php b/server/RoboFile.php index 5f188f8bd4..81ef3b630f 100644 --- a/server/RoboFile.php +++ b/server/RoboFile.php @@ -418,6 +418,12 @@ public function reportDemographicsHc($limit_date = NULL, $region = NULL) { $this->_exec("cd /var/www/html/server/www && drush scr profiles/hedley/modules/custom/hedley_admin/scripts/generate-demographics-hc-report.php --limit_date=$limit_date --region=$region"); } + /** + * Generates the duplicates report. + */ + public function reportDuplicates($health_center = NULL) { + $this->_exec("cd /var/www/html/server/www && drush scr profiles/hedley/modules/custom/hedley_admin/scripts/generate-duplicates-report.php --health_center=$health_center"); + } /** * Generates the acute illness report. */ diff --git a/server/hedley/modules/custom/hedley_admin/scripts/generate-duplicates-report.php b/server/hedley/modules/custom/hedley_admin/scripts/generate-duplicates-report.php new file mode 100644 index 0000000000..3652a76de2 --- /dev/null +++ b/server/hedley/modules/custom/hedley_admin/scripts/generate-duplicates-report.php @@ -0,0 +1,152 @@ + $health_center_name, + ':type' => 'health_center', + )); + if (!$results->fetchField()) { + $results = db_query("SELECT nid FROM node WHERE title LIKE :title AND type = :type", array( + ':title' => db_like($health_center_name) . '%', + ':type' => 'health_center', + )); + if (!$results->fetchField()) { + drush_print('No health centers match the name provided'); + exit(1); + } + elseif (!$results->fetchField()) { + return db_query("SELECT nid FROM node WHERE title LIKE :title AND type = :type LIMIT 1", array( + ':title' => db_like($health_center_name) . '%', + ':type' => 'health_center', + ))->fetchField(); + } + else { + $results = db_query("SELECT nid FROM node WHERE title LIKE :title AND type = :type", array( + ':title' => db_like($health_center_name) . '%', + ':type' => 'health_center', + )); + drush_print('Multiple health centers match the name provided including ' . + get_health_center($results->fetchField()) . ', ' . get_health_center($results->fetchField()) . + ", etc. \r\nPlease use a more specific name"); + exit(1); + } + } + else { + return db_query("SELECT nid FROM node WHERE title = :title AND type = :type", array( + ':title' => $health_center_name, + ':type' => 'health_center', + ))->fetchField(); + } + } + else { + return NULL; + } +} + +/** + * Gets the list of duplicates for a given village/health center. + * + * @param int $health_center_id + * The node ID of a health center. + * + * @param string $village + * The name of a village. + * + * @return array + * The node ID of the health center. + */ +function get_duplicates($village = NULL, $health_center_id) { +return db_query ("SELECT node.nid, fn.field_first_name_value, sn.field_second_name_value, bd.field_birth_date_value, v.field_village_value + FROM field_data_field_birth_date bd + LEFT JOIN node ON bd.entity_id = node.nid + LEFT JOIN field_data_field_first_name fn ON node.nid = fn.entity_id + LEFT JOIN field_data_field_second_name sn ON node.nid = sn.entity_id + LEFT JOIN field_data_field_village v ON node.nid = v.entity_id + LEFT JOIN field_data_field_health_center hc ON node.nid = hc.entity_id + WHERE bd.field_birth_date_value + IN (SELECT dbd.field_birth_date_value + FROM field_data_field_birth_date dbd + LEFT JOIN node dnode ON dbd.entity_id = dnode.nid + LEFT JOIN field_data_field_village dv ON dnode.nid = dv.entity_id + LEFT JOIN field_data_field_health_center dhc ON dnode.nid = dhc.entity_id + WHERE UPPER(dv.field_village_value) = UPPER('$village') + AND dhc.field_health_center_target_id= $health_center_id + GROUP BY dbd.field_birth_date_value + HAVING COUNT(dbd.field_birth_date_value) > 1) + AND bd.field_birth_date_value NOT LIKE '%01-01%' + AND UPPER(v.field_village_value) = UPPER('$village') + AND hc.field_health_center_target_id= $health_center_id + ORDER BY `bd`.`field_birth_date_value` DESC")->fetchAll(); +} + +/** + * Gets the list of villages in a health center. + * + * @param int $health_center_id + * The name of a healthcenter. + * + * @return array + * The list of villages. + */ +function get_villages($health_center_id) { + return db_query( "SELECT DISTINCT field_village_value + FROM field_data_field_village v + LEFT JOIN field_data_field_health_center hc ON hc.entity_id = v.entity_id + WHERE hc.field_health_center_target_id = $health_center_id + ORDER BY v.field_village_value ASC")->fetchCol(); +} + +// Get the health center ID from the health center name and find its villages. +$health_center_id = get_health_center_id($health_center); +$villages = get_villages($health_center_id); + +// Iterate over each village. +$data = []; +foreach ($villages as $village) { + // Get the duplicates for this village + $results = get_duplicates($village, $health_center_id); + + // Add teh results to teh $data array. + foreach ($results as $result) { + $data[] = [ + $result->nid, + $result->field_first_name_value, + $result->field_second_name_value, + date("Y-m-d", strtotime($result->field_birth_date_value)), + strtoupper($result->field_village_value), + ]; + } +} + +drush_print("# Duplicates report - " . $health_center . "(" . $health_center_id . ")"); + +$table = new HedleyAdminTextTable(['Node ID', 'First Name', 'Second Name', 'Birth Date', 'Village']); + +drush_print($table->render($data)); From 39840167898d40102a7fd250f90f7c9808b85590 Mon Sep 17 00:00:00 2001 From: Adam Stewart Date: Thu, 14 Mar 2024 16:39:20 -0500 Subject: [PATCH 2/5] Code sniffer. --- .../scripts/generate-duplicates-report.php | 57 ++++++++++--------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/server/hedley/modules/custom/hedley_admin/scripts/generate-duplicates-report.php b/server/hedley/modules/custom/hedley_admin/scripts/generate-duplicates-report.php index 3652a76de2..edf588ca83 100644 --- a/server/hedley/modules/custom/hedley_admin/scripts/generate-duplicates-report.php +++ b/server/hedley/modules/custom/hedley_admin/scripts/generate-duplicates-report.php @@ -75,35 +75,34 @@ function get_health_center_id($health_center_name = NULL) { * * @param int $health_center_id * The node ID of a health center. - * * @param string $village * The name of a village. * * @return array * The node ID of the health center. */ -function get_duplicates($village = NULL, $health_center_id) { -return db_query ("SELECT node.nid, fn.field_first_name_value, sn.field_second_name_value, bd.field_birth_date_value, v.field_village_value - FROM field_data_field_birth_date bd - LEFT JOIN node ON bd.entity_id = node.nid - LEFT JOIN field_data_field_first_name fn ON node.nid = fn.entity_id - LEFT JOIN field_data_field_second_name sn ON node.nid = sn.entity_id - LEFT JOIN field_data_field_village v ON node.nid = v.entity_id - LEFT JOIN field_data_field_health_center hc ON node.nid = hc.entity_id - WHERE bd.field_birth_date_value - IN (SELECT dbd.field_birth_date_value - FROM field_data_field_birth_date dbd - LEFT JOIN node dnode ON dbd.entity_id = dnode.nid - LEFT JOIN field_data_field_village dv ON dnode.nid = dv.entity_id - LEFT JOIN field_data_field_health_center dhc ON dnode.nid = dhc.entity_id - WHERE UPPER(dv.field_village_value) = UPPER('$village') - AND dhc.field_health_center_target_id= $health_center_id - GROUP BY dbd.field_birth_date_value - HAVING COUNT(dbd.field_birth_date_value) > 1) - AND bd.field_birth_date_value NOT LIKE '%01-01%' - AND UPPER(v.field_village_value) = UPPER('$village') - AND hc.field_health_center_target_id= $health_center_id - ORDER BY `bd`.`field_birth_date_value` DESC")->fetchAll(); +function get_duplicates($health_center_id, $village = NULL) { + return db_query("SELECT node.nid, fn.field_first_name_value, sn.field_second_name_value, bd.field_birth_date_value, v.field_village_value + FROM field_data_field_birth_date bd + LEFT JOIN node ON bd.entity_id = node.nid + LEFT JOIN field_data_field_first_name fn ON node.nid = fn.entity_id + LEFT JOIN field_data_field_second_name sn ON node.nid = sn.entity_id + LEFT JOIN field_data_field_village v ON node.nid = v.entity_id + LEFT JOIN field_data_field_health_center hc ON node.nid = hc.entity_id + WHERE bd.field_birth_date_value + IN (SELECT dbd.field_birth_date_value + FROM field_data_field_birth_date dbd + LEFT JOIN node dnode ON dbd.entity_id = dnode.nid + LEFT JOIN field_data_field_village dv ON dnode.nid = dv.entity_id + LEFT JOIN field_data_field_health_center dhc ON dnode.nid = dhc.entity_id + WHERE UPPER(dv.field_village_value) = UPPER('$village') + AND dhc.field_health_center_target_id= $health_center_id + GROUP BY dbd.field_birth_date_value + HAVING COUNT(dbd.field_birth_date_value) > 1) + AND bd.field_birth_date_value NOT LIKE '%01-01%' + AND UPPER(v.field_village_value) = UPPER('$village') + AND hc.field_health_center_target_id= $health_center_id + ORDER BY `bd`.`field_birth_date_value` DESC")->fetchAll(); } /** @@ -116,7 +115,7 @@ function get_duplicates($village = NULL, $health_center_id) { * The list of villages. */ function get_villages($health_center_id) { - return db_query( "SELECT DISTINCT field_village_value + return db_query("SELECT DISTINCT field_village_value FROM field_data_field_village v LEFT JOIN field_data_field_health_center hc ON hc.entity_id = v.entity_id WHERE hc.field_health_center_target_id = $health_center_id @@ -130,7 +129,7 @@ function get_villages($health_center_id) { // Iterate over each village. $data = []; foreach ($villages as $village) { - // Get the duplicates for this village + // Get the duplicates for this village. $results = get_duplicates($village, $health_center_id); // Add teh results to teh $data array. @@ -147,6 +146,12 @@ function get_villages($health_center_id) { drush_print("# Duplicates report - " . $health_center . "(" . $health_center_id . ")"); -$table = new HedleyAdminTextTable(['Node ID', 'First Name', 'Second Name', 'Birth Date', 'Village']); +$table = new HedleyAdminTextTable([ + 'Node ID', + 'First Name', + 'Second Name', + 'Birth Date', + 'Village', +]); drush_print($table->render($data)); From f65693f8e8ab6cfeca582638ee6605c097864e7c Mon Sep 17 00:00:00 2001 From: Adam Stewart Date: Thu, 14 Mar 2024 16:47:45 -0500 Subject: [PATCH 3/5] Reverse order of variables in get_duplicates function. --- .../custom/hedley_admin/scripts/generate-duplicates-report.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/hedley/modules/custom/hedley_admin/scripts/generate-duplicates-report.php b/server/hedley/modules/custom/hedley_admin/scripts/generate-duplicates-report.php index edf588ca83..991ee8bba7 100644 --- a/server/hedley/modules/custom/hedley_admin/scripts/generate-duplicates-report.php +++ b/server/hedley/modules/custom/hedley_admin/scripts/generate-duplicates-report.php @@ -130,7 +130,7 @@ function get_villages($health_center_id) { $data = []; foreach ($villages as $village) { // Get the duplicates for this village. - $results = get_duplicates($village, $health_center_id); + $results = get_duplicates($health_center_id, $village); // Add teh results to teh $data array. foreach ($results as $result) { From 769021e82e71c958c03968ebf9dd498137d6000b Mon Sep 17 00:00:00 2001 From: Adam Stewart Date: Fri, 15 Mar 2024 07:54:00 -0500 Subject: [PATCH 4/5] Code cleanup, efficiency. --- .../custom/hedley_admin/hedley_admin.module | 54 +++++++++++++++++ .../generate-demographics-hc-report.php | 58 +------------------ .../scripts/generate-duplicates-report.php | 54 ----------------- 3 files changed, 56 insertions(+), 110 deletions(-) diff --git a/server/hedley/modules/custom/hedley_admin/hedley_admin.module b/server/hedley/modules/custom/hedley_admin/hedley_admin.module index 1e108f6e2a..919f27b8df 100644 --- a/server/hedley/modules/custom/hedley_admin/hedley_admin.module +++ b/server/hedley/modules/custom/hedley_admin/hedley_admin.module @@ -416,3 +416,57 @@ function hedley_admin_administrators_access() { return user_has_role($admin_role->rid); } + +/** + * Gets the node ID of the health center. + * + * @param string $health_center_name + * The name of a healthcenter. + * + * @return string + * The node ID of the health center. + */ +function get_health_center_id($health_center_name = NULL) { + if ($health_center_name) { + $health_center_name = str_replace(['_', '-', '.'], ' ', $health_center_name); + $results = db_query("SELECT nid FROM node WHERE title = :title AND type = :type", array( + ':title' => $health_center_name, + ':type' => 'health_center', + )); + if (!$results->fetchField()) { + $results = db_query("SELECT nid FROM node WHERE title LIKE :title AND type = :type", array( + ':title' => db_like($health_center_name) . '%', + ':type' => 'health_center', + )); + if (!$results->fetchField()) { + drush_print('No health centers match the name provided'); + exit(1); + } + elseif (!$results->fetchField()) { + return db_query("SELECT nid FROM node WHERE title LIKE :title AND type = :type LIMIT 1", array( + ':title' => db_like($health_center_name) . '%', + ':type' => 'health_center', + ))->fetchField(); + } + else { + $results = db_query("SELECT nid FROM node WHERE title LIKE :title AND type = :type", array( + ':title' => db_like($health_center_name) . '%', + ':type' => 'health_center', + )); + drush_print('Multiple health centers match the name provided including ' . + get_health_center($results->fetchField()) . ', ' . get_health_center($results->fetchField()) . + ", etc. \r\nPlease use a more specific name"); + exit(1); + } + } + else { + return db_query("SELECT nid FROM node WHERE title = :title AND type = :type", array( + ':title' => $health_center_name, + ':type' => 'health_center', + ))->fetchField(); + } + } + else { + return NULL; + } +} \ No newline at end of file diff --git a/server/hedley/modules/custom/hedley_admin/scripts/generate-demographics-hc-report.php b/server/hedley/modules/custom/hedley_admin/scripts/generate-demographics-hc-report.php index 994a61f0d9..0a8d8978df 100644 --- a/server/hedley/modules/custom/hedley_admin/scripts/generate-demographics-hc-report.php +++ b/server/hedley/modules/custom/hedley_admin/scripts/generate-demographics-hc-report.php @@ -17,60 +17,6 @@ exit; } -/** - * Gets the node ID of the health center. - * - * @param string $health_center_name - * The name of a healthcenter. - * - * @return string - * The node ID of the health center. - */ -function get_health_center_id($health_center_name = NULL) { - if ($health_center_name) { - $health_center_name = str_replace(['_', '-', '.'], ' ', $health_center_name); - $results = db_query("SELECT nid FROM node WHERE title = :title AND type = :type", array( - ':title' => $health_center_name, - ':type' => 'health_center', - )); - if (!$results->fetchField()) { - $results = db_query("SELECT nid FROM node WHERE title LIKE :title AND type = :type", array( - ':title' => db_like($health_center_name) . '%', - ':type' => 'health_center', - )); - if (!$results->fetchField()) { - drush_print('No health centers match the name provided'); - exit(1); - } - elseif (!$results->fetchField()) { - return db_query("SELECT nid FROM node WHERE title LIKE :title AND type = :type LIMIT 1", array( - ':title' => db_like($health_center_name) . '%', - ':type' => 'health_center', - ))->fetchField(); - } - else { - $results = db_query("SELECT nid FROM node WHERE title LIKE :title AND type = :type", array( - ':title' => db_like($health_center_name) . '%', - ':type' => 'health_center', - )); - drush_print('Multiple health centers match the name provided including ' . - get_health_center($results->fetchField()) . ', ' . get_health_center($results->fetchField()) . - ", etc. \r\nPlease use a more specific name"); - exit(1); - } - } - else { - return db_query("SELECT nid FROM node WHERE title = :title AND type = :type", array( - ':title' => $health_center_name, - ':type' => 'health_center', - ))->fetchField(); - } - } - else { - return NULL; - } -} - $center_id = get_health_center_id($center_name); // We need to filter for all the measurements at several places, @@ -255,7 +201,7 @@ function encounter_all_count($type, $filter = NULL, $limit = NULL, $center_id = LEFT JOIN field_data_field_individual_participant ip ON e.field_{$type}_encounter_target_id=ip.entity_id LEFT JOIN field_data_field_person person ON ip.field_individual_participant_target_id=person.entity_id LEFT JOIN field_data_field_health_center hc ON person.field_person_target_id=hc.entity_id - WHERE + WHERE FROM_UNIXTIME(node.created) < '$limit' {$center_clause}")->fetchField(); } @@ -316,7 +262,7 @@ function encounter_unique_count($type, $filter = NULL, $limit = NULL, $center_id LEFT JOIN field_data_field_individual_participant ip ON e.field_{$type}_encounter_target_id=ip.entity_id LEFT JOIN field_data_field_person person ON ip.field_individual_participant_target_id=person.entity_id LEFT JOIN field_data_field_health_center hc ON person.field_person_target_id=hc.entity_id - WHERE + WHERE FROM_UNIXTIME(node.created) < '$limit' {$center_clause}")->fetchField(); } diff --git a/server/hedley/modules/custom/hedley_admin/scripts/generate-duplicates-report.php b/server/hedley/modules/custom/hedley_admin/scripts/generate-duplicates-report.php index 991ee8bba7..3d31adb346 100644 --- a/server/hedley/modules/custom/hedley_admin/scripts/generate-duplicates-report.php +++ b/server/hedley/modules/custom/hedley_admin/scripts/generate-duplicates-report.php @@ -16,60 +16,6 @@ exit; } -/** - * Gets the node ID of the health center. - * - * @param string $health_center_name - * The name of a healthcenter. - * - * @return string - * The node ID of the health center. - */ -function get_health_center_id($health_center_name = NULL) { - if ($health_center_name) { - $health_center_name = str_replace(['_', '-', '.'], ' ', $health_center_name); - $results = db_query("SELECT nid FROM node WHERE title = :title AND type = :type", array( - ':title' => $health_center_name, - ':type' => 'health_center', - )); - if (!$results->fetchField()) { - $results = db_query("SELECT nid FROM node WHERE title LIKE :title AND type = :type", array( - ':title' => db_like($health_center_name) . '%', - ':type' => 'health_center', - )); - if (!$results->fetchField()) { - drush_print('No health centers match the name provided'); - exit(1); - } - elseif (!$results->fetchField()) { - return db_query("SELECT nid FROM node WHERE title LIKE :title AND type = :type LIMIT 1", array( - ':title' => db_like($health_center_name) . '%', - ':type' => 'health_center', - ))->fetchField(); - } - else { - $results = db_query("SELECT nid FROM node WHERE title LIKE :title AND type = :type", array( - ':title' => db_like($health_center_name) . '%', - ':type' => 'health_center', - )); - drush_print('Multiple health centers match the name provided including ' . - get_health_center($results->fetchField()) . ', ' . get_health_center($results->fetchField()) . - ", etc. \r\nPlease use a more specific name"); - exit(1); - } - } - else { - return db_query("SELECT nid FROM node WHERE title = :title AND type = :type", array( - ':title' => $health_center_name, - ':type' => 'health_center', - ))->fetchField(); - } - } - else { - return NULL; - } -} - /** * Gets the list of duplicates for a given village/health center. * From 8927a9dd4849637d2304d50d6aaa008daec802cc Mon Sep 17 00:00:00 2001 From: Adam Stewart Date: Fri, 15 Mar 2024 15:02:12 -0500 Subject: [PATCH 5/5] Code format and spelling. --- server/hedley/modules/custom/hedley_admin/hedley_admin.module | 2 +- .../custom/hedley_admin/scripts/generate-duplicates-report.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/hedley/modules/custom/hedley_admin/hedley_admin.module b/server/hedley/modules/custom/hedley_admin/hedley_admin.module index 919f27b8df..bf2e400505 100644 --- a/server/hedley/modules/custom/hedley_admin/hedley_admin.module +++ b/server/hedley/modules/custom/hedley_admin/hedley_admin.module @@ -469,4 +469,4 @@ function get_health_center_id($health_center_name = NULL) { else { return NULL; } -} \ No newline at end of file +} diff --git a/server/hedley/modules/custom/hedley_admin/scripts/generate-duplicates-report.php b/server/hedley/modules/custom/hedley_admin/scripts/generate-duplicates-report.php index 3d31adb346..b8a0b70875 100644 --- a/server/hedley/modules/custom/hedley_admin/scripts/generate-duplicates-report.php +++ b/server/hedley/modules/custom/hedley_admin/scripts/generate-duplicates-report.php @@ -78,7 +78,7 @@ function get_villages($health_center_id) { // Get the duplicates for this village. $results = get_duplicates($health_center_id, $village); - // Add teh results to teh $data array. + // Add the results to the $data array. foreach ($results as $result) { $data[] = [ $result->nid,