You are here

public function UserStatisticsManager::buildBadgesList in Opigno statistics 3.x

Build the user badges list table.

Parameters

int $uid: The user ID to build the badges list for.

Return value

array The render array to build the user badges list table.

File

src/Services/UserStatisticsManager.php, line 809

Class

UserStatisticsManager
User statistics manager service definition.

Namespace

Drupal\opigno_statistics\Services

Code

public function buildBadgesList(int $uid) : array {
  $rows = $this
    ->getUserBadges($uid, FALSE);
  $table_rows = [];

  // Add the share link if social settings enabled and the user is viewing the
  // own profile.
  $sharing_enabled = $this->isSocialsEnabled && $this->currentUid === $uid;
  $build = [
    '#type' => 'table',
    '#attributes' => [
      'class' => [
        'statistics-table',
        'badges-list',
      ],
    ],
    '#header' => [
      [
        'data' => $this
          ->t('Name'),
        'class' => 'name',
      ],
    ],
  ];

  // Add the share link if social settings enabled.
  if ($sharing_enabled) {
    $build['#header'][] = [
      'data' => $this
        ->t('Share'),
      'class' => 'hidden',
    ];
  }
  if (!$rows) {
    return $build + [
      '#rows' => $table_rows,
    ];
  }

  // Share link options.
  $options = [
    'attributes' => [
      'class' => [
        'btn',
        'btn-rounded',
      ],
      'data-opigno-attachment-type' => 'badge',
    ],
  ];
  $title = Markup::create('<i class="fi fi-rr-redo"></i>' . $this
    ->t('share'));

  // Build table rows.
  foreach ($rows as $row) {
    $name = $row->typology === 'Course' ? $row->badge_name_value : $row->badge_name;
    $table_row = [
      [
        'data' => $name ?: $this
          ->t('Badge'),
        'class' => 'name',
      ],
    ];
    if (!$sharing_enabled) {
      $table_rows[] = $table_row;
      continue;
    }

    // Add the share button.
    $options['attributes']['data-opigno-attachment-entity-type'] = $row->typology === 'Course' ? 'group' : 'opigno_module';
    $options['attributes']['data-opigno-post-attachment-id'] = $row->entity_id ?? '';
    $share = Link::createFromRoute($title, '<current>', [], $options)
      ->toRenderable();

    // Prepare the table row.
    $table_row[] = [
      'data' => $share,
      'class' => 'share',
    ];
    $table_rows[] = $table_row;
  }
  return $build + [
    '#rows' => $table_rows,
  ];
}