You are here

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

Build the user certificates list table.

Parameters

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

Return value

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

File

src/Services/UserStatisticsManager.php, line 720

Class

UserStatisticsManager
User statistics manager service definition.

Namespace

Drupal\opigno_statistics\Services

Code

public function buildCertificatesList(int $uid) : array {
  $rows = $this
    ->getUserCertificates(0, $uid, FALSE);

  // Add the share link if social settings enabled and the user is viewing the
  // own profile.
  $sharing_enabled = $this->isSocialsEnabled && $this->currentUid === $uid;
  $table_rows = [];
  $build = [
    '#type' => 'table',
    '#attributes' => [
      'class' => [
        'statistics-table',
        'certificates-list',
      ],
    ],
    '#header' => [
      [
        'data' => $this
          ->t('Name'),
        'class' => 'name',
      ],
      [
        'data' => $this
          ->t('Date'),
        'class' => 'date',
      ],
      [
        'data' => $this
          ->t('Download'),
        'class' => 'hidden',
      ],
    ],
  ];
  if (!$rows) {
    return $build + [
      '#rows' => $table_rows,
    ];
  }

  // Add the column to the table if sharing is enabled.
  if ($sharing_enabled) {
    $share = [
      [
        'data' => $this
          ->t('Share'),
        'class' => 'hidden',
      ],
    ];
    array_splice($build['#header'], 2, 0, $share);
  }

  // Link options.
  $download_options = [
    'attributes' => [
      'class' => [
        'btn',
        'btn-rounded',
      ],
    ],
  ];
  $download_title = Markup::create('<i class="fi fi-rr-download"></i>' . $this
    ->t('download'));
  if ($sharing_enabled) {
    $share_options = $download_options;
    $share_options['attributes']['data-opigno-attachment-type'] = 'certificate';
    $share_options['attributes']['data-opigno-attachment-entity-type'] = 'group';
    $share_title = Markup::create('<i class="fi fi-rr-redo"></i>' . $this
      ->t('share'));
  }

  // Build table rows.
  foreach ($rows as $row) {
    $gid = $row->gid ?? 0;
    $name = $this
      ->t('Certificate for @training', [
      '@training' => $row->name ?? '',
    ]);
    $timestamp = strtotime($row->completed ?? 'now');
    $date = $this->dateFormatter
      ->format($timestamp, 'day_month_year');

    // Generate the certificate download link.
    $params = [
      'entity_type' => 'group',
      'entity_id' => $gid,
    ];
    $download = Url::fromRoute('certificate.entity.pdf', $params, $download_options);
    $download = $download
      ->access() ? Link::fromTextAndUrl($download_title, $download)
      ->toRenderable() : [];
    $table_row = [
      [
        'data' => $name,
        'class' => 'name',
      ],
      [
        'data' => $date,
        'class' => 'date',
      ],
      [
        'data' => $download,
        'class' => 'download',
      ],
    ];
    if (!$sharing_enabled) {
      $table_rows[] = $table_row;
      continue;
    }

    // Add the share link if the sharing enabled.
    $share_options['attributes']['data-opigno-post-attachment-id'] = $gid ?? '';
    $share = Link::createFromRoute($share_title, '<current>', [], $share_options)
      ->toRenderable();
    $share = [
      [
        'data' => $share,
        'class' => 'share',
      ],
    ];
    array_splice($table_row, 2, 0, $share);

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