You are here

function CertificateTabController::renderCertificateTab in Course 8.3

Same name and namespace in other branches
  1. 8.2 modules/course_certificate/src/Controller/CertificateTabController.php \Drupal\course_certificate\Controller\CertificateTabController::renderCertificateTab()

Full tab

Parameters

Course $course:

AccountInterface $account:

Return value

type

1 call to CertificateTabController::renderCertificateTab()
CertificateTabController::renderDefaultCertificateTab in modules/course_certificate/src/Controller/CertificateTabController.php
Helper to plugin current user when not provided in path
1 string reference to 'CertificateTabController::renderCertificateTab'
course_certificate.routing.yml in modules/course_certificate/course_certificate.routing.yml
modules/course_certificate/course_certificate.routing.yml

File

modules/course_certificate/src/Controller/CertificateTabController.php, line 95

Class

CertificateTabController
An example controller.

Namespace

Drupal\course_certificate\Controller

Code

function renderCertificateTab(Course $course, AccountInterface $account) {

  // Get all templates for this entity combo
  $render = [];
  $valid_certs = [];
  $global_certs = CertificateMapping::getGlobalCertificateMappings();
  $certificate_mappers = Drupal::service('plugin.manager.certificate_mapper');
  $map_defs = $certificate_mappers
    ->getDefinitions();
  $certs = $course
    ->get('certificate')
    ->referencedEntities();

  //Default to load a page
  $render['info']['#markup'] = '';
  foreach ($map_defs as $map_key => $maps) {
    $plugin = $certificate_mappers
      ->createInstance($map_key, [
      'of' => 'configuration values',
    ]);
    $matches = $plugin
      ->processMapping($course, $account) ?? [];
    foreach ($matches as $match) {
      foreach ($certs as $local) {
        if ($local
          ->isMatch($map_key, $match)) {
          $valid_certs["{$map_key}.{$match}"] = $local
            ->get('cid')->value;
        }
      }

      // If local is not set, check the global mappings
      if (!isset($valid_certs["{$map_key}.{$match}"])) {
        $render['table'] = [
          '#type' => 'table',
          '#header' => [
            $this
              ->t('Type'),
            $this
              ->t('Download'),
          ],
        ];
        foreach ($global_certs as $global) {
          if ($global
            ->isMatch($map_key, $match) && $global
            ->get('cid')->value !== '-1') {
            $valid_certs["{$map_key}.{$match}"] = $global
              ->get('cid')->value;
          }
        }
      }
      elseif ($valid_certs["{$map_key}.{$match}"] == '-1') {
        unset($valid_certs["{$map_key}.{$match}"]);
      }
    }
  }

  // Return markup if we need to present messages
  if (count($valid_certs) > 1) {
    $render['info']['#markup'] = ' You are eligible for multiple certificates.';
  }
  if (empty($valid_certs)) {
    $render['info']['#markup'] = 'You are not eligible for a certificate';
  }
  foreach ($valid_certs as $cert_name => $val) {
    $opts = [
      'course' => $course
        ->id(),
      'account' => $account
        ->id(),
      'template' => $val,
    ];
    $render['table'][$val] = [
      'type' => [
        '#markup' => $cert_name,
      ],
      'download' => Link::createFromRoute(t('Download certificate'), 'certificate.course.pdf', $opts)
        ->toRenderable(),
    ];
  }
  return $render;
}