You are here

function views_aggregator_count_unique in Views Aggregator Plus 8

Aggregates a field group as a count of the unique number of group members.

Parameters

array $groups: An array of groups of rows, each group indexed by group value.

object $field_handler: The handler for the view column to count groups members in.

string $group_regexp: An optional regexp to count, if omitted all non-empty group values count.

string $column_regexp: An optional regexp to count, if omitted all non-empty group values count.

Return value

array An array of values, one for each group and one for the column.

2 string references to 'views_aggregator_count_unique'
ViewsAggregatorResultsTest::testColumnResultFunctions in tests/src/Functional/Plugin/ViewsAggregatorResultsTest.php
Test the column functions.
ViewsAggregatorResultsTest::testGroupResultFunctions in tests/src/Functional/Plugin/ViewsAggregatorResultsTest.php
Test the group functions.

File

./views_aggregator_functions.inc, line 275
views_aggregator_functions.inc

Code

function views_aggregator_count_unique(array $groups, $field_handler, $group_regexp = NULL, $column_regexp = NULL) {
  $values = [];
  $count_column = 0;
  $regexp = isset($group_regexp) ? $group_regexp : $column_regexp;
  if (preg_match('/[a-zA-Z0-9_]+/', $regexp)) {

    // Interpret omitted brace chars in the regexp as a verbatim text match.
    $regexp = "/{$regexp}/";
  }
  foreach ($groups as $group => $rows) {
    $count = 0;
    $cell_values = [];
    foreach ($rows as $num => $row) {
      $cell = views_aggregator_get_cell($field_handler, $num, TRUE);
      if (isset($cell) && $cell != '' && (empty($regexp) || preg_match($regexp, $cell)) && !in_array($cell, $cell_values)) {
        $cell_values[] = $cell;
        $count++;
      }
    }
    $values[$group] = $count;
    $count_column += $count;
  }
  $values['column'] = $count_column;
  return $values;
}