You are here

public function StepsBlock::processActivityList in Opigno Learning path 3.x

Converts a step array to a renderable array.

File

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

Class

StepsBlock
Provides a 'article' block.

Namespace

Drupal\opigno_learning_path\Plugin\Block

Code

public function processActivityList($elements) {
  $meta = CacheableMetadata::createFromRenderArray($elements);
  if (in_array($elements["step"]['typology'], [
    'Module',
  ])) {
    [
      $training,
      $course,
      $module,
    ] = $this
      ->getTrainingAndCourse($elements["step"]);
    [
      $activities,
      $attempts,
    ] = $this
      ->getActivities($training, $course, $module);
    $meta
      ->addCacheableDependency($attempts);
    $activity_status = $this
      ->getActivityStatus($activities, $attempts, $module);
    $activity_passed = array_filter($activity_status, static function ($status) {
      return 'passed' === $status;
    });
    $next = FALSE;
    $current_module = $this
      ->isModuleCurrent($elements["step"]);
    foreach ($activities as &$activity) {
      $meta
        ->addCacheableDependency($activity);
      $state = $activity_status[$activity
        ->id()] ?? 'pending';
      $current = $this
        ->getCurrentActivityId() === $activity
        ->id();
      $is_link = in_array($state, [
        'passed',
      ]);
      $activity = [
        '#theme' => 'opigno_lp_step_module_activity',
        '#activity' => $activity,
        "#state" => $state,
        '#current' => $current,
        '#link' => $current_module && ($current || $is_link || $next) ? Url::fromRoute('opigno_module.group.answer_form', [
          'group' => $training
            ->id(),
          'opigno_activity' => $activity
            ->id(),
          'opigno_module' => $module
            ->id(),
        ])
          ->toString() : FALSE,
        '#pre_render' => [
          [
            $this,
            'processActivityStatus',
          ],
        ],
      ];
      $next = $is_link && $current;
    }
    $elements['activity_counter'] = [
      '#markup' => $this
        ->t('%passed/%total activities done', [
        '%passed' => count($activity_passed),
        '%total' => count($activity_status),
      ]),
    ];
    $elements['activities'] = $activities;
  }
  $meta
    ->applyTo($elements);
  return $elements;
}