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/hedley_admin.module b/server/hedley/modules/custom/hedley_admin/hedley_admin.module index 1e108f6e2a..bf2e400505 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; + } +} 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 new file mode 100644 index 0000000000..b8a0b70875 --- /dev/null +++ b/server/hedley/modules/custom/hedley_admin/scripts/generate-duplicates-report.php @@ -0,0 +1,103 @@ + 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($health_center_id, $village); + + // Add the results to the $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));