You are here

public function Progress::getProgressBuildGroupPage in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x src/Progress.php \Drupal\opigno_learning_path\Progress::getProgressBuildGroupPage()

Get get progress for group page.

Parameters

int $group_id: Group ID.

int $uid: User ID.

int $latest_cert_date: Latest certification date.

Return value

array Renderable array.

1 call to Progress::getProgressBuildGroupPage()
Progress::getProgressBuild in src/Progress.php
Get get progress bar it self.

File

src/Progress.php, line 239

Class

Progress
Class JoinService.

Namespace

Drupal\opigno_learning_path

Code

public function getProgressBuildGroupPage($group_id, $account_id, $latest_cert_date) {

  /** @var \Drupal\group\Entity\GroupInterface $group */
  $group = Group::load($group_id);
  $account = User::load($account_id);
  $date_formatter = \Drupal::service('date.formatter');
  $expiration_message = '';
  $expiration_set = LPStatus::isCertificateExpireSet($group);
  if ($expiration_set) {
    if ($expiration_message = LPStatus::getCertificateExpireTimestamp($group
      ->id(), $account_id)) {
      $expiration_message = ' ' . $date_formatter
        ->format($expiration_message, 'custom', 'F d, Y');
    }
  }

  // Get data from achievements.
  $query = $this->database
    ->select('opigno_learning_path_achievements', 'a')
    ->fields('a', [
    'score',
    'progress',
    'time',
    'completed',
  ])
    ->condition('a.gid', $group_id)
    ->condition('a.uid', $account_id)
    ->condition('a.status', 'completed');
  $achievements_data = $query
    ->execute()
    ->fetchAssoc();
  if ($achievements_data) {
    $score = $achievements_data['score'];
    $completed = $achievements_data['completed'];
    if ($achievements_data['completed']) {
      $format = 'Y-m-d H:i:s';
      $completed = DrupalDateTime::createFromFormat($format, $achievements_data['completed']);
      $completed = $completed
        ->format('F d, Y');
    }
    $state = $this
      ->t('Passed');
    $progress = $achievements_data['progress'];
    $is_passed = TRUE;
  }
  else {
    $score = opigno_learning_path_get_score($group_id, $account_id);
    $progress = $this
      ->getProgressRound($group_id, $account_id, $latest_cert_date);
    $is_passed = opigno_learning_path_is_passed($group, $account_id);
    $completed = opigno_learning_path_completed_on($group_id, $account_id);
    $completed = $completed > 0 ? $date_formatter
      ->format($completed, 'custom', 'F d, Y') : '';
    $state = $is_passed ? $this
      ->t('Passed') : $this
      ->t('Failed');
  }
  if ($is_passed || $progress == 100) {

    // Expire message if necessary.
    if ($expiration_set) {

      // Expiration set, create expiration message.
      if ($expiration_message) {
        $expiration_message = ' - ' . $this
          ->t('Valid until') . $expiration_message;
      }
    }
    $summary = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'lp_progress_summary',
        ],
      ],
      // H2 Need for correct structure.
      [
        '#type' => 'html_tag',
        '#tag' => 'h2',
        '#value' => $this
          ->t('Progress status'),
        '#attributes' => [
          'class' => [
            'sr-only',
          ],
        ],
      ],
      [
        '#type' => 'html_tag',
        '#tag' => 'p',
        '#attributes' => [
          'class' => $is_passed ? [
            'lp_progress_summary_passed',
          ] : [
            'lp_progress_summary_failed',
          ],
        ],
        '#value' => '',
      ],
      [
        '#type' => 'html_tag',
        '#tag' => 'h3',
        '#attributes' => [
          'class' => [
            'lp_progress_summary_title',
          ],
        ],
        '#value' => $state . $expiration_message,
      ],
      [
        '#type' => 'html_tag',
        '#tag' => 'p',
        '#attributes' => [
          'class' => [
            'lp_progress_summary_score',
          ],
        ],
        '#value' => $this
          ->t('Average score : @score%', [
          '@score' => $score,
        ]),
      ],
      !empty($completed) ? [
        '#type' => 'html_tag',
        '#tag' => 'p',
        '#attributes' => [
          'class' => [
            'lp_progress_summary_date',
          ],
        ],
        '#value' => $this
          ->t('Completed on @date', [
          '@date' => $completed,
        ]),
      ] : [],
    ];
  }
  elseif ($expiration_set && LPStatus::isCertificateExpired($group, $account_id)) {
    $summary = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'lp_progress_summary',
        ],
      ],
      // H2 Need for correct structure.
      [
        '#type' => 'html_tag',
        '#tag' => 'h2',
        '#value' => $this
          ->t('Progress status'),
        '#attributes' => [
          'class' => [
            'sr-only',
          ],
        ],
      ],
      [
        '#type' => 'html_tag',
        '#tag' => 'p',
        '#attributes' => [
          'class' => [
            'lp_progress_summary_expired',
          ],
        ],
        '#value' => '',
      ],
      [
        '#type' => 'html_tag',
        '#tag' => 'h3',
        '#attributes' => [
          'class' => [
            'lp_progress_summary_title',
          ],
        ],
        '#value' => $this
          ->t('Expired on') . ' ' . $expiration_message,
      ],
      [
        '#type' => 'html_tag',
        '#tag' => 'p',
        '#attributes' => [
          'class' => [
            'lp_progress_summary_score',
          ],
        ],
        '#value' => $this
          ->t('Please start this training again to get new certification'),
      ],
    ];
  }
  $content = [];
  $content[] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'col-sm-9',
        'mb-3',
      ],
    ],
    [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'lp_progress',
        ],
      ],
      [
        '#type' => 'html_tag',
        '#tag' => 'p',
        '#attributes' => [
          'class' => [
            'lp_progress_label',
          ],
        ],
        '#value' => $this
          ->t('Global Training Progress'),
      ],
      [
        '#type' => 'html_tag',
        '#tag' => 'p',
        '#attributes' => [
          'class' => [
            'lp_progress_value',
          ],
        ],
        '#value' => $progress . '%',
      ],
      [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'lp_progress_bar',
          ],
        ],
        [
          '#type' => 'html_tag',
          '#tag' => 'div',
          '#attributes' => [
            'class' => [
              'lp_progress_bar_completed',
            ],
            'style' => "width: {$progress}%",
          ],
          '#value' => '',
        ],
      ],
    ],
    isset($summary) ? $summary : [],
    '#attached' => [
      'library' => [
        'opigno_learning_path/training_content',
        'core/drupal.dialog.ajax',
      ],
    ],
  ];
  $continue_route = 'opigno_learning_path.steps.start';
  $edit_route = 'entity.group.edit_form';
  $members_route = 'opigno_learning_path.membership.overview';
  $route_args = [
    'group' => $group
      ->id(),
  ];
  $continue_url = Url::fromRoute($continue_route, $route_args);
  $edit_url = Url::fromRoute($edit_route, $route_args);
  $members_url = Url::fromRoute($members_route, $route_args);
  $admin_continue_button = Link::fromTextAndUrl(Markup::create('<i class="icon-chevron-right1"></i><span class="sr-only">' . $this
    ->t('Continue training') . '</span>'), $continue_url)
    ->toRenderable();
  $admin_continue_button['#attributes']['class'][] = 'lp_progress_admin_continue';
  $admin_continue_button['#attributes']['class'][] = 'use-ajax';
  $admin_continue_button['#attributes']['class'][] = 'lp_progress_control';
  $edit_button = Link::fromTextAndUrl(Markup::create('<i class="icon-pencil"></i><span class="sr-only">' . $this
    ->t('Edit training') . '</span>'), $edit_url)
    ->toRenderable();
  $edit_button['#attributes']['class'][] = 'lp_progress_admin_edit';
  $edit_button['#attributes']['class'][] = 'lp_progress_control';
  $members_button = Link::fromTextAndUrl(Markup::create('<i class="icon-pencil"></i><span class="sr-only">' . $this
    ->t('Member of training') . '</span>'), $members_url)
    ->toRenderable();
  $members_button['#attributes']['class'][] = 'lp_progress_admin_edit';
  $members_button['#attributes']['class'][] = 'lp_progress_control';

  // Set button text depending on if the learning path is started or ongoing.
  $continue_button_text = opigno_learning_path_started($group, \Drupal::currentUser()) ? $this
    ->t('Continue Training') : $this
    ->t('Start');

  // If learning path is passed, set button text accordingly.
  if (opigno_learning_path_is_passed($group, $account_id)) {
    $continue_button_text = $this
      ->t('Passed');
  }
  $continue_button = Link::fromTextAndUrl($continue_button_text, $continue_url)
    ->toRenderable();
  $continue_button['#attributes']['class'][] = 'lp_progress_continue';
  $continue_button['#attributes']['class'][] = 'use-ajax';
  $buttons = [];
  if ($group
    ->access('update', $account)) {
    $buttons[] = $admin_continue_button;
    $buttons[] = $edit_button;
  }
  elseif ($group
    ->access('administer members', $account)) {
    $buttons[] = $admin_continue_button;
    $buttons[] = $members_button;
  }
  else {
    $buttons[] = $continue_button;
  }
  $content[] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'col-sm-3',
        'mb-3',
        'd-flex',
      ],
    ],
    $buttons,
  ];
  return $content;
}