diff --git a/server/elm/src/Pages/Scoreboard/View.elm b/server/elm/src/Pages/Scoreboard/View.elm index fffb3817df..0146807990 100644 --- a/server/elm/src/Pages/Scoreboard/View.elm +++ b/server/elm/src/Pages/Scoreboard/View.elm @@ -269,42 +269,6 @@ viewAcuteMalnutritionPane language currentDate yearSelectorGap monthsGap childre -- Making sure patient was already created during examination month. Date.compare record.created targetDateForMonth == LT - stuntingSevereAsAgeInMonths = - List.filter (\date -> equalByYearAndMonth date targetDateForMonth) - record.nutrition.stunting.severe - - stuntingModerateAsAgeInMonths = - List.filter (\date -> equalByYearAndMonth date targetDateForMonth) - record.nutrition.stunting.moderate - - stuntingNormalAsAgeInMonths = - List.filter (\date -> equalByYearAndMonth date targetDateForMonth) - record.nutrition.stunting.normal - - underweightSevereAsAgeInMonths = - List.filter (\date -> equalByYearAndMonth date targetDateForMonth) - record.nutrition.underweight.severe - - underweightModerateAsAgeInMonths = - List.filter (\date -> equalByYearAndMonth date targetDateForMonth) - record.nutrition.underweight.moderate - - underweightNormalAsAgeInMonths = - List.filter (\date -> equalByYearAndMonth date targetDateForMonth) - record.nutrition.underweight.normal - - wastingSevereAsAgeInMonths = - List.filter (\date -> equalByYearAndMonth date targetDateForMonth) - record.nutrition.wasting.severe - - wastingModerateAsAgeInMonths = - List.filter (\date -> equalByYearAndMonth date targetDateForMonth) - record.nutrition.wasting.moderate - - wastingNormalAsAgeInMonths = - List.filter (\date -> equalByYearAndMonth date targetDateForMonth) - record.nutrition.wasting.normal - muacSevereAsAgeInMonths = List.filter (\date -> equalByYearAndMonth date targetDateForMonth) record.nutrition.muac.severe @@ -320,31 +284,19 @@ viewAcuteMalnutritionPane language currentDate yearSelectorGap monthsGap childre ( row1, row2, row3 ) = if existedDuringExaminationMonth - && ((not <| List.isEmpty stuntingSevereAsAgeInMonths) - || (not <| List.isEmpty underweightSevereAsAgeInMonths) - || (not <| List.isEmpty wastingSevereAsAgeInMonths) - || (not <| List.isEmpty muacSevereAsAgeInMonths) - ) + && (not <| List.isEmpty muacSevereAsAgeInMonths) then ( accumValue.row1 + 1, accumValue.row2, accumValue.row3 ) else if existedDuringExaminationMonth - && ((not <| List.isEmpty stuntingModerateAsAgeInMonths) - || (not <| List.isEmpty underweightModerateAsAgeInMonths) - || (not <| List.isEmpty wastingModerateAsAgeInMonths) - || (not <| List.isEmpty muacModerateAsAgeInMonths) - ) + && (not <| List.isEmpty muacModerateAsAgeInMonths) then ( accumValue.row1, accumValue.row2 + 1, accumValue.row3 ) else if existedDuringExaminationMonth - && ((not <| List.isEmpty stuntingNormalAsAgeInMonths) - || (not <| List.isEmpty underweightNormalAsAgeInMonths) - || (not <| List.isEmpty wastingNormalAsAgeInMonths) - || (not <| List.isEmpty muacNormalAsAgeInMonths) - ) + && (not <| List.isEmpty muacNormalAsAgeInMonths) then ( accumValue.row1, accumValue.row2, accumValue.row3 + 1 ) @@ -503,10 +455,6 @@ viewANCNewbornPane language currentDate yearSelectorGap monthsGap childrenUnder2 targetDateForMonth = resolveTargetDateForMonth gapInMonths currentDate - existedDuringExaminationMonth = - -- Making sure patient was already created during examination month. - Date.compare record.created targetDateForMonth == LT - ageInMonths = -- Using EDD date to properly resolve the month of -- prgnancy (as child may have been borm premature). @@ -520,10 +468,10 @@ viewANCNewbornPane language currentDate yearSelectorGap monthsGap childrenUnder2 gapInMonths - ageInMonths row1 = - if - existedDuringExaminationMonth - && (not <| List.isEmpty row1AsAgeInMonths) - then + -- We do not a condition to check if child existed during + -- examination month because we're examining pregnancy months + -- and it's likely that child did not existy on the system. + if not <| List.isEmpty row1AsAgeInMonths then accumValue.row1 + 1 else @@ -531,8 +479,10 @@ viewANCNewbornPane language currentDate yearSelectorGap monthsGap childrenUnder2 row2 = if - existedDuringExaminationMonth - && (record.ncda.ancNewborn.row2 && gap > 0) + -- We do not a condition to check if child existed during + -- examination month because we're examining pregnancy months + -- and it's likely that child did not existy on the system. + (record.ncda.ancNewborn.row2 && gap > 0) && (gap < 10) then accumValue.row2 + 1 diff --git a/server/hedley/modules/custom/hedley_general/hedley_general.module b/server/hedley/modules/custom/hedley_general/hedley_general.module index cd4872ab53..7e1f76109f 100644 --- a/server/hedley/modules/custom/hedley_general/hedley_general.module +++ b/server/hedley/modules/custom/hedley_general/hedley_general.module @@ -288,7 +288,7 @@ function hedley_general_get_content_type_fields($type) { /** * Implements hook_query_TAG_alter(). * - * Add to a query which to get all entities that have field_deleted set + * Adds a condition to select entities that have field_deleted set * to False, or not set at all (an indication that entity is not deleted). */ function hedley_general_query_exclude_deleted_alter(QueryAlterableInterface $query) { diff --git a/server/hedley/modules/custom/hedley_ncda/hedley_ncda.module b/server/hedley/modules/custom/hedley_ncda/hedley_ncda.module index 673444b5a8..1417236ddd 100644 --- a/server/hedley/modules/custom/hedley_ncda/hedley_ncda.module +++ b/server/hedley/modules/custom/hedley_ncda/hedley_ncda.module @@ -81,6 +81,13 @@ function hedley_ncda_node_insert($node) { return; } + // Generate initial NCDA data for newly created person. + // We need this to properly count 'children under 2'. + if ($node->type == 'person') { + hedley_ncda_trigger_recalculation_for_created_person($node); + return; + } + if ($node->type == 'village') { hedley_ncda_trigger_recalculation_for_new_village($node); return; @@ -355,6 +362,9 @@ function hedley_ncda_generate_results_data($province, $district, $sector, $cell, ->fieldCondition('field_province', 'value', $province) ->fieldCondition('field_district', 'value', $district) ->fieldCondition('field_ncda_data', 'value', NULL, 'IS NOT NULL') + // Since we show data starting Nov 23, it's enough to select only those + // born during the past 3 years. + ->fieldCondition('field_birth_date', 'value', date('Y-m-d', strtotime('-3 years')), '>') ->addTag('exclude_deleted'); if (!empty($sector)) { @@ -428,10 +438,10 @@ function hedley_ncda_calculate_aggregated_data_for_person($person) { return FALSE; } - if ($birth_date < strtotime("-9 year")) { + if ($birth_date < strtotime("2016-01-01")) { // We generate data for children only. // Our first records are from Jan 2018, so going back - // 9 years to make sure we cover all children. + // Jan 2016 to make sure we cover all children. return FALSE; } @@ -1015,11 +1025,39 @@ function hedley_ncda_trigger_recalculation_for_new_village($node) { } } +/** + * Triggers calculation of NCDA data, for newly created person. + * + * @param object $node + * The node object of a village. + * + * @throws EntityMetadataWrapperException + */ +function hedley_ncda_trigger_recalculation_for_created_person($node) { + $birth_date = strtotime($node->field_birth_date[LANGUAGE_NONE][0]['value']); + if (empty($birth_date)) { + // We must know the birthdate to decide if to generate NCDA data. + return; + } + + if ($birth_date < strtotime("2016-01-01")) { + // Our first records are from 01 Jan 2018. NCDA is for children + // under 2y. Therefore, recalculation is needed only for those + // born starting 01 Jan 2016. + return FALSE; + } + + // If we got this far, schedule generating NCDA data using AQ. + hedley_general_add_task_to_advanced_queue_by_id(HEDLEY_NCDA_CALCULATE_AGGREGATED_DATA, $node->nid, [ + 'person_id' => $node->nid, + ]); +} + /** * Triggers recalculation of person's NCDA data, when edited. * * Recalculation is required only when birth date is edited. - * Note: currently, editing geo fields of person is not allowed. + * Note: Currently, editing geo fields of person is not allowed. * * @param object $node * The node object of a village. @@ -1037,6 +1075,13 @@ function hedley_ncda_trigger_recalculation_by_edited_person($node) { return; } + if ($birth_date < strtotime("2016-01-01")) { + // Our first records are from 01 Jan 2018. NCDA is for children + // under 2y. Therefore, recalculation is needed only for those + // born starting 01 Jan 2016. + return; + } + hedley_general_add_task_to_advanced_queue_by_id(HEDLEY_NCDA_CALCULATE_AGGREGATED_DATA, $node->nid, [ 'person_id' => $node->nid, ]); diff --git a/server/hedley/modules/custom/hedley_ncda/js/elm-main.js b/server/hedley/modules/custom/hedley_ncda/js/elm-main.js index ecc048d553..998ec3d9f1 100644 --- a/server/hedley/modules/custom/hedley_ncda/js/elm-main.js +++ b/server/hedley/modules/custom/hedley_ncda/js/elm-main.js @@ -28668,16 +28668,13 @@ var $author$project$Pages$Scoreboard$View$viewANCNewbornPane = F7( return A2($author$project$Utils$NominalDate$equalByYearAndMonth, date, targetDateForMonth); }, record.ncda.ancNewborn.row1); - var existedDuringExaminationMonth = _Utils_eq( - A2($justinmimbs$date$Date$compare, record.created, targetDateForMonth), - $elm$core$Basics$LT); - var row1 = (existedDuringExaminationMonth && (!$elm$core$List$isEmpty(row1AsAgeInMonths))) ? (accumValue.row1 + 1) : accumValue.row1; + var row1 = (!$elm$core$List$isEmpty(row1AsAgeInMonths)) ? (accumValue.row1 + 1) : accumValue.row1; var ageInMonths = A2( $author$project$Gizra$NominalDate$diffMonths, A2($justinmimbs$date$Date$floor, $justinmimbs$date$Date$Month, record.eddDate), targetDateForMonth); var gap = gapInMonths - ageInMonths; - var row2 = (existedDuringExaminationMonth && ((record.ncda.ancNewborn.row2 && (gap > 0)) && (gap < 10))) ? (accumValue.row2 + 1) : accumValue.row2; + var row2 = ((record.ncda.ancNewborn.row2 && (gap > 0)) && (gap < 10)) ? (accumValue.row2 + 1) : accumValue.row2; return {row1: row1, row2: row2}; }, A2($pzp1997$assoc_list$AssocList$get, index, monthsGap))); @@ -28765,60 +28762,6 @@ var $author$project$Pages$Scoreboard$View$viewAcuteMalnutritionPane = F7( $elm$core$Maybe$map, function (gapInMonths) { var targetDateForMonth = A2($author$project$Pages$Scoreboard$View$resolveTargetDateForMonth, gapInMonths, currentDate); - var underweightModerateAsAgeInMonths = A2( - $elm$core$List$filter, - function (date) { - return A2($author$project$Utils$NominalDate$equalByYearAndMonth, date, targetDateForMonth); - }, - record.nutrition.underweight.moderate); - var underweightNormalAsAgeInMonths = A2( - $elm$core$List$filter, - function (date) { - return A2($author$project$Utils$NominalDate$equalByYearAndMonth, date, targetDateForMonth); - }, - record.nutrition.underweight.normal); - var underweightSevereAsAgeInMonths = A2( - $elm$core$List$filter, - function (date) { - return A2($author$project$Utils$NominalDate$equalByYearAndMonth, date, targetDateForMonth); - }, - record.nutrition.underweight.severe); - var wastingModerateAsAgeInMonths = A2( - $elm$core$List$filter, - function (date) { - return A2($author$project$Utils$NominalDate$equalByYearAndMonth, date, targetDateForMonth); - }, - record.nutrition.wasting.moderate); - var wastingNormalAsAgeInMonths = A2( - $elm$core$List$filter, - function (date) { - return A2($author$project$Utils$NominalDate$equalByYearAndMonth, date, targetDateForMonth); - }, - record.nutrition.wasting.normal); - var wastingSevereAsAgeInMonths = A2( - $elm$core$List$filter, - function (date) { - return A2($author$project$Utils$NominalDate$equalByYearAndMonth, date, targetDateForMonth); - }, - record.nutrition.wasting.severe); - var stuntingSevereAsAgeInMonths = A2( - $elm$core$List$filter, - function (date) { - return A2($author$project$Utils$NominalDate$equalByYearAndMonth, date, targetDateForMonth); - }, - record.nutrition.stunting.severe); - var stuntingNormalAsAgeInMonths = A2( - $elm$core$List$filter, - function (date) { - return A2($author$project$Utils$NominalDate$equalByYearAndMonth, date, targetDateForMonth); - }, - record.nutrition.stunting.normal); - var stuntingModerateAsAgeInMonths = A2( - $elm$core$List$filter, - function (date) { - return A2($author$project$Utils$NominalDate$equalByYearAndMonth, date, targetDateForMonth); - }, - record.nutrition.stunting.moderate); var muacSevereAsAgeInMonths = A2( $elm$core$List$filter, function (date) { @@ -28840,7 +28783,7 @@ var $author$project$Pages$Scoreboard$View$viewAcuteMalnutritionPane = F7( var existedDuringExaminationMonth = _Utils_eq( A2($justinmimbs$date$Date$compare, record.created, targetDateForMonth), $elm$core$Basics$LT); - var _v0 = (existedDuringExaminationMonth && ((!$elm$core$List$isEmpty(stuntingSevereAsAgeInMonths)) || ((!$elm$core$List$isEmpty(underweightSevereAsAgeInMonths)) || ((!$elm$core$List$isEmpty(wastingSevereAsAgeInMonths)) || (!$elm$core$List$isEmpty(muacSevereAsAgeInMonths)))))) ? _Utils_Tuple3(accumValue.row1 + 1, accumValue.row2, accumValue.row3) : ((existedDuringExaminationMonth && ((!$elm$core$List$isEmpty(stuntingModerateAsAgeInMonths)) || ((!$elm$core$List$isEmpty(underweightModerateAsAgeInMonths)) || ((!$elm$core$List$isEmpty(wastingModerateAsAgeInMonths)) || (!$elm$core$List$isEmpty(muacModerateAsAgeInMonths)))))) ? _Utils_Tuple3(accumValue.row1, accumValue.row2 + 1, accumValue.row3) : ((existedDuringExaminationMonth && ((!$elm$core$List$isEmpty(stuntingNormalAsAgeInMonths)) || ((!$elm$core$List$isEmpty(underweightNormalAsAgeInMonths)) || ((!$elm$core$List$isEmpty(wastingNormalAsAgeInMonths)) || (!$elm$core$List$isEmpty(muacNormalAsAgeInMonths)))))) ? _Utils_Tuple3(accumValue.row1, accumValue.row2, accumValue.row3 + 1) : _Utils_Tuple3(accumValue.row1, accumValue.row2, accumValue.row3))); + var _v0 = (existedDuringExaminationMonth && (!$elm$core$List$isEmpty(muacSevereAsAgeInMonths))) ? _Utils_Tuple3(accumValue.row1 + 1, accumValue.row2, accumValue.row3) : ((existedDuringExaminationMonth && (!$elm$core$List$isEmpty(muacModerateAsAgeInMonths))) ? _Utils_Tuple3(accumValue.row1, accumValue.row2 + 1, accumValue.row3) : ((existedDuringExaminationMonth && (!$elm$core$List$isEmpty(muacNormalAsAgeInMonths))) ? _Utils_Tuple3(accumValue.row1, accumValue.row2, accumValue.row3 + 1) : _Utils_Tuple3(accumValue.row1, accumValue.row2, accumValue.row3))); var row1 = _v0.a; var row2 = _v0.b; var row3 = _v0.c; diff --git a/server/hedley/modules/custom/hedley_ncda/scripts/generate-data-for-all.php b/server/hedley/modules/custom/hedley_ncda/scripts/generate-data-for-all.php index 91c63e7d7d..7cf77ba459 100644 --- a/server/hedley/modules/custom/hedley_ncda/scripts/generate-data-for-all.php +++ b/server/hedley/modules/custom/hedley_ncda/scripts/generate-data-for-all.php @@ -19,6 +19,9 @@ // Get the number of nodes to be processed. $batch = drush_get_option('batch', 50); +// Flag to generate NCDA data only if it was not generated already. +$exclude_set = drush_get_option('exclude_set', FALSE); + // Get allowed memory limit. $memory_limit = drush_get_option('memory_limit', 240); @@ -30,6 +33,10 @@ ->propertyCondition('status', NODE_PUBLISHED) ->addTag('exclude_deleted'); +if ($exclude_set) { + $base_query->fieldCondition('field_ncda_data', 'value', NULL, 'IS NULL'); +} + $count_query = clone $base_query; $count_query->propertyCondition('nid', $nid, '>'); $count = $count_query->count()->execute(); diff --git a/server/hedley/modules/custom/hedley_person/hedley_person.module b/server/hedley/modules/custom/hedley_person/hedley_person.module index fe3453b7c7..f2fee35791 100644 --- a/server/hedley/modules/custom/hedley_person/hedley_person.module +++ b/server/hedley/modules/custom/hedley_person/hedley_person.module @@ -1173,8 +1173,11 @@ function hedley_person_node_presave($node) { // Set node label. $first_name = $wrapper->field_first_name->value(); $second_name = $wrapper->field_second_name->value(); - $title = empty($first_name) ? $second_name : "$second_name $first_name"; - $wrapper->title->set($title); + + if (!empty($first_name) || !empty($second_name)) { + $title = empty($first_name) ? $second_name : "$second_name $first_name"; + $wrapper->title->set($title); + } // If being deleted, verify person got no measurements. $deleted = $wrapper->field_deleted->value();