Skip to content

Commit

Permalink
HPC-9988: Add number of GHO plans to supported global key figures
Browse files Browse the repository at this point in the history
  • Loading branch information
berliner committed Dec 23, 2024
1 parent cd157a1 commit 0a406b0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ public function getData(string $source_key = 'data') {
],
'expectedReach' => 'Expected reach',
];
$caseload_values = $this->getPlanQuery()->getCaseloadTotalValues($types);
$affected_countries = $this->getPlanQuery()->getNumerOfGhoCountries();
$plan_query = $this->getPlanQuery();
$caseload_values = $plan_query->getCaseloadTotalValues($types);
$affected_countries = $plan_query->getNumerOfGhoCountries();
$gho_plans = count($plan_query->getGhoPlans());
return [
'total_funding' => $funding,
'total_requirements' => $requirements,
Expand All @@ -74,6 +76,7 @@ public function getData(string $source_key = 'data') {
'people_reached_percent' => CommonHelper::calculateRatio($caseload_values['reached_custom'], $caseload_values['target_custom']),
'people_expected_reach' => $caseload_values['expectedReach'],
'countries_affected' => $affected_countries,
'plans_inside_gho' => $gho_plans,
];
}

Expand Down Expand Up @@ -262,6 +265,9 @@ public function getAllowedItemTypes() {
'countries_affected' => [
'label' => $this->t('Countries affected'),
],
'plans_inside_gho' => [
'label' => $this->t('Number of plans'),
],
],
],
'label_value' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,12 @@ private function getTypes() {
'allow_sum' => TRUE,
'theme' => 'hpc_amount',
],
'plans_inside_gho' => [
'label' => $this->t('Number of GHO plans'),
'data_type' => 'integer',
'allow_sum' => TRUE,
'theme' => 'hpc_amount',
],
];
$configuration = $this->getPluginConfiguration();
if (array_key_exists('item_types', $configuration)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,28 +101,44 @@ public function getPlans($filter = TRUE) {
}

/**
* Get the caseload total values for the supplied types.
* Get the GHO plans.
*
* @param bool $filter
* Whether the plans should be filtered or not.
*
* @return \Drupal\ghi_plans\ApiObjects\Partials\PlanOverviewPlan[]
* An array of GHO plans.
*/
public function getGhoPlans($filter = FALSE) {
$plans = $this->getPlans($filter);
if (empty($plans)) {
return [];
}
$plans = array_filter($plans, function (PlanOverviewPlan $plan) {
return $plan->isPartOfGho();
});
return $plans;
}

/**
* Get the number of affected countries for the GHO plans.
*
* @return int
* The number of unique countries of all GHO plans.
*/
public function getNumerOfGhoCountries() {
// Get the plans, but make sure they are not filtered for visibility. The
// number of affected countries will appear only in the key figures
// Get the GHO plans, but make sure they are not filtered for visibility.
// The number of affected countries will appear only in the key figures
// element, where we want the number of countries for all GHO plans
// independently of whether specific plans are hidden from global pages or
// not.
$plans = $this->getPlans(FALSE);
$plans = $this->getGhoPlans(FALSE);
if (empty($plans)) {
return 0;
}

$countries = [];
foreach ($plans as $plan) {
// Only include plans with isPartOfGho=true.
if (!$plan->isPartOfGho()) {
continue;
}
$plan_countries = $plan->getCountries();
if (empty($plan_countries)) {
continue;
Expand All @@ -149,11 +165,11 @@ public function getNumerOfGhoCountries() {
* An array keyed by the type and valued by the total sum of that type
*/
public function getCaseloadTotalValues(array $types) {
// Get the plans, but make sure they are not filtered for visibility. The
// caseload totals will appear only in the key figures element, where we
// want the full GHO figures independently of whether specific plans are
// Get the GHO plans, but make sure they are not filtered for visibility.
// The caseload totals will appear only in the key figures element, where
// we want the full GHO figures independently of whether specific plans are
// hidden from global pages or not.
$plans = $this->getPlans(FALSE);
$plans = $this->getGhoPlans(FALSE);

// Setting up the array keyed by the types and values as 0.
$caseload_totals = array_fill_keys(array_keys($types), 0);
Expand All @@ -169,29 +185,23 @@ public function getCaseloadTotalValues(array $types) {
// summing these plans caseload values for all plans that have
// isPartOfGho = true from this endpoint:
// https://api.hpc.tools/v2/plan/overview/{year}
if (!empty($plans)) {
foreach ($plans as $plan) {
// Include plans with isPartOfGho=true.
if (!$plan->isPartOfGho()) {
continue;
}
foreach ($plans as $plan) {

// Check caseLoads and respective totals property has value.
$caseload_items = $plan->getPlanCaseloadFields($attachment_overrides[$plan->id()] ?? NULL);
if (empty($caseload_items)) {
continue;
}
// Check caseLoads and respective totals property has value.
$caseload_items = $plan->getPlanCaseloadFields($attachment_overrides[$plan->id()] ?? NULL);
if (empty($caseload_items)) {
continue;
}

$plan_caseloads[$plan->id()] = [];
$plan_caseloads[$plan->id()] = [];

foreach ($types as $type_key => $type) {
$label = is_scalar($type) ? $type : ($type['label'] ?? NULL);
$key = is_array($type) && !empty($type['type']) ? $type['type'] : $type_key;
$fallback = is_array($type) && !empty($type['fallback']) ? $type['fallback'] : NULL;
$value = $plan->getCaseloadValue($key, $label, $fallback);
$caseload_totals[$type_key] += $value ?? 0;
$plan_caseloads[$plan->id()][$type_key] = $value;
}
foreach ($types as $type_key => $type) {
$label = is_scalar($type) ? $type : ($type['label'] ?? NULL);
$key = is_array($type) && !empty($type['type']) ? $type['type'] : $type_key;
$fallback = is_array($type) && !empty($type['fallback']) ? $type['fallback'] : NULL;
$value = $plan->getCaseloadValue($key, $label, $fallback);
$caseload_totals[$type_key] += $value ?? 0;
$plan_caseloads[$plan->id()][$type_key] = $value;
}
}

Expand Down

0 comments on commit 0a406b0

Please sign in to comment.