You are here

public function StepsBlock::build in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x src/Plugin/Block/StepsBlock.php \Drupal\opigno_learning_path\Plugin\Block\StepsBlock::build()

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/StepsBlock.php, line 126

Class

StepsBlock
Provides a 'article' block.

Namespace

Drupal\opigno_learning_path\Plugin\Block

Code

public function build() {
  $uid = $this->account
    ->id();
  $route_name = $this->routeMatch
    ->getRouteName();
  if ($route_name == 'opigno_module.group.answer_form') {
    $group = $this->routeMatch
      ->getParameter('group');
    $gid = $group
      ->id();
  }
  else {
    $gid = OpignoGroupContext::getCurrentGroupId();
    $group = Group::load($gid);
  }
  if (empty($group)) {
    return [];
  }
  $title = $group
    ->label();

  // Get training guided navigation option.
  $freeNavigation = !OpignoGroupManagerController::getGuidedNavigation($group);
  if ($freeNavigation) {

    // Get all steps for LP.
    $steps = LearningPathContent::getAllStepsOnlyModules($gid, $uid, TRUE);
  }
  else {

    // Get guided steps.
    $steps = LearningPathContent::getAllStepsOnlyModules($gid, $uid);
  }
  $user = $this->account;
  $steps = array_filter($steps, function ($step) use ($user) {
    if ($step['typology'] === 'Meeting') {

      // If the user have not the collaborative features role.
      if (!$user
        ->hasPermission('view meeting entities')) {
        return FALSE;
      }

      // If the user is not a member of the meeting.

      /** @var MeetingInterface $meeting */
      $meeting = $this->entityTypeManager
        ->getStorage('opigno_moxtra_meeting')
        ->load($step['id']);
      if (!$meeting
        ->isMember($user
        ->id())) {
        return FALSE;
      }
    }
    elseif ($step['typology'] === 'ILT') {

      // If the user is not a member of the ILT.

      /** @var ILTInterface $ilt */
      $ilt = $this->entityTypeManager
        ->getStorage('opigno_ilt')
        ->load($step['id']);
      if (!$ilt
        ->isMember($user
        ->id())) {
        return FALSE;
      }
    }
    return TRUE;
  });

  // Get user training expiration flag.
  $expired = LPStatus::isCertificateExpired($group, $uid);
  $score = opigno_learning_path_get_score($gid, $uid);
  $progress = $this->progress
    ->getProgressRound($gid, $uid);
  $is_passed = opigno_learning_path_is_passed($group, $uid, $expired);
  if ($is_passed) {
    $state_class = 'lp_steps_block_summary_state_passed';
    $state_title = $this
      ->t('Passed');
  }
  else {
    $state_class = 'lp_steps_block_summary_state_pending';
    $state_title = $this
      ->t('In progress');
  }

  // Get group context.
  $cid = OpignoGroupContext::getCurrentGroupContentId();
  if (!$cid) {
    return [];
  }
  $gid = OpignoGroupContext::getCurrentGroupId();
  $step_info = [];

  // Reindex steps array.
  $steps = array_values($steps);
  for ($i = 0; $i < count($steps); $i++) {

    // Build link for first step.
    if ($i == 0) {

      // Load first step entity.
      $first_step = OpignoGroupManagedContent::load($steps[$i]['cid']);
      if ($first_step) {

        /** @var ContentTypeBase $content_type */
        $content_type = $this->opignoGroupContentTypesManager
          ->createInstance($first_step
          ->getGroupContentTypeId());
        $step_url = $content_type
          ->getStartContentUrl($first_step
          ->getEntityId(), $gid);
        $link = Link::createFromRoute($steps[$i]['name'], $step_url
          ->getRouteName(), $step_url
          ->getRouteParameters())
          ->toString();
      }
      else {
        $link = '-';
      }
    }
    else {

      // Get link to module.
      $parent_content_id = $steps[$i - 1]['cid'];
      $link = Link::createFromRoute($steps[$i]['name'], 'opigno_learning_path.steps.next', [
        'group' => $gid,
        'parent_content' => $parent_content_id,
      ])
        ->toString();
    }
    array_push($step_info, [
      'name' => $link,
      'score' => $this
        ->buildScore($steps[$i]),
      'state' => $this
        ->buildState($steps[$i]),
    ]);
  }
  $state_summary = [
    'class' => $state_class,
    'title' => $state_title,
    'score' => $this
      ->t('Average score : @score%', [
      '@score' => $score,
    ]),
    'progress' => $this
      ->t('Progress : @progress%', [
      '@progress' => $progress,
    ]),
  ];
  $table_summary = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Name'),
      $this
        ->t('Score'),
      $this
        ->t('State'),
    ],
    '#rows' => $step_info,
    '#attributes' => [
      'class' => [
        'lp_steps_block_table',
      ],
    ],
  ];
  $build = [
    '#theme' => 'opigno_learning_path_step_block',
    '#attributes' => [
      'class' => [
        'lp_steps_block',
      ],
    ],
    '#attached' => [
      'library' => [
        'opigno_learning_path/steps_block',
      ],
    ],
    '#title' => $title,
    '#state_summary' => $state_summary,
    '#table_summary' => $table_summary,
  ];
  return $build;
}