You are here

public function PcpService::getCompletePercentageData in Profile Complete Percent 8

Builds array of pcp data.

Parameters

\Drupal\user\UserInterface $user: The user to collect the data for.

Return value

array The collected data.

Overrides PcpServiceInterface::getCompletePercentageData

File

src/PcpService.php, line 48

Class

PcpService
Class PcpService.

Namespace

Drupal\pcp

Code

public function getCompletePercentageData(UserInterface $user) {
  $userFields = $this
    ->getUserFields();

  /** @var array $profileFields */
  $profileFields = array_filter($this->config
    ->get('profile_fields'));

  // Filter out the fields that are deleted. Outdated configuration may still
  // contain non-existing fields.
  $profileFields = array_intersect_key($profileFields, $userFields);
  if (empty($profileFields)) {
    return [
      'current_percent' => 100,
      'next_percent' => 0,
    ];
  }

  // Collect data of empty fields.
  $emptyFields = [];
  foreach ($profileFields as $fieldName) {
    if ($user
      ->get($fieldName)
      ->isEmpty()) {
      $emptyFields[$fieldName] = $userFields[$fieldName]
        ->label();
    }
  }
  $fieldCount = count($profileFields);
  $emptyFieldCount = count($emptyFields);
  $completedFieldCount = $fieldCount - $emptyFieldCount;
  $nextField = $this
    ->getNextField($emptyFields);
  $nextFieldTitle = isset($emptyFields[$nextField]) ? $emptyFields[$nextField] : '';
  return [
    'uid' => $user
      ->id(),
    'total' => $fieldCount,
    'open_link' => $this->config
      ->get('open_link') == 0 ? '_self' : '_target',
    'completed' => $completedFieldCount,
    'incomplete' => $emptyFieldCount,
    'hide_pcp_block' => (bool) $this->config
      ->get('hide_block_on_complete'),
    'nextfield_title' => $nextFieldTitle,
    'current_percent' => $this
      ->calcCurrentPercentage($completedFieldCount, $fieldCount),
    'next_percent' => $this
      ->calcNextPercentage($completedFieldCount, $fieldCount),
    'nextfield_name' => str_replace('_', '-', $nextField),
  ];
}