You are here

protected function LearningPathAchievementController::build_module_panel in Opigno Learning path 3.x

Same name and namespace in other branches
  1. 8 src/Controller/LearningPathAchievementController.php \Drupal\opigno_learning_path\Controller\LearningPathAchievementController::build_module_panel()

Returns module panel renderable array.

Parameters

\Drupal\group\Entity\GroupInterface $training: Group.

null|\Drupal\group\Entity\GroupInterface $course: Group.

\Drupal\opigno_module\Entity\OpignoModule $module: Module.

Return value

array Module panel renderable array.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

4 calls to LearningPathAchievementController::build_module_panel()
LearningPathAchievementController::build_course_steps in src/Controller/LearningPathAchievementController.php
Returns course steps renderable array.
LearningPathAchievementController::course_module_panel_ajax in src/Controller/LearningPathAchievementController.php
Loads module panel with a AJAX.
LearningPathAchievementController::trainingStepModuleBuild in src/Controller/LearningPathAchievementController.php
If step is module prepares a render array of content.
LearningPathAchievementController::training_module_panel_ajax in src/Controller/LearningPathAchievementController.php
Loads module panel with a AJAX.

File

src/Controller/LearningPathAchievementController.php, line 232

Class

LearningPathAchievementController
Class LearningPathAchievementController.

Namespace

Drupal\opigno_learning_path\Controller

Code

protected function build_module_panel(GroupInterface $training, GroupInterface $course = NULL, OpignoModule $module, AccountInterface $account = NULL) {

  /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
  $date_formatter = \Drupal::service('date.formatter');
  $user = $this
    ->currentUser($account);
  $moduleHandler = \Drupal::service('module_handler');

  // Get training latest certification timestamp.
  $latest_cert_date = LPStatus::getTrainingStartDate($training, $user
    ->id());
  $parent = isset($course) ? $course : $training;
  $step = opigno_learning_path_get_module_step($parent
    ->id(), $user
    ->id(), $module, $latest_cert_date);
  $completed_on = $step['completed on'];
  $completed_on = $completed_on > 0 ? $date_formatter
    ->format($completed_on, 'custom', 'F d, Y') : '';

  /** @var \Drupal\opigno_module\Entity\OpignoModule $module */
  $module = OpignoModule::load($step['id']);

  /** @var \Drupal\opigno_module\Entity\UserModuleStatus[] $attempts */
  $attempts = $module
    ->getModuleAttempts($user, NULL, $latest_cert_date);
  if ($moduleHandler
    ->moduleExists('opigno_skills_system') && $module
    ->getSkillsActive() && $module
    ->getModuleSkillsGlobal() && !empty($attempts)) {
    $activities_from_module = $module
      ->getModuleActivities();
    $activity_ids = array_keys($activities_from_module);
    $attempt = $this
      ->getTargetAttempt($attempts, $module);
    $db_connection = \Drupal::service('database');
    $query = $db_connection
      ->select('opigno_answer_field_data', 'o_a_f_d');
    $query
      ->leftJoin('opigno_module_relationship', 'o_m_r', 'o_a_f_d.activity = o_m_r.child_id');
    $query
      ->addExpression('MAX(o_a_f_d.activity)', 'id');
    $query
      ->condition('o_a_f_d.user_id', $user
      ->id())
      ->condition('o_a_f_d.module', $module
      ->id());
    if (!$module
      ->getModuleSkillsGlobal()) {
      $query
        ->condition('o_a_f_d.activity', $activity_ids, 'IN');
    }
    $query
      ->condition('o_a_f_d.user_module_status', $attempt
      ->id())
      ->groupBy('o_a_f_d.activity');
    $activities = $query
      ->execute()
      ->fetchAllAssoc('id');
  }
  else {
    $activities = $module
      ->getModuleActivities();
  }

  /** @var \Drupal\opigno_module\Entity\OpignoActivity[] $activities */
  $activities = array_map(function ($activity) {

    /** @var \Drupal\opigno_module\Entity\OpignoActivity $activity */
    return OpignoActivity::load($activity->id);
  }, $activities);
  if (!empty($attempts)) {

    // If "newest" score - get the last attempt,
    // else - get the best attempt.
    $attempt = $this
      ->getTargetAttempt($attempts, $module);
    $max_score = $attempt
      ->calculateMaxScore();
    $score_percent = $attempt
      ->getAttemptScore();
    $score = round($score_percent * $max_score / 100);
  }
  else {
    $attempt = NULL;
    $max_score = !empty($activities) ? array_sum(array_map(function ($activity) use ($module) {
      return (int) $this
        ->get_activity_max_score($module, $activity);
    }, $activities)) : 0;
    $score_percent = 0;
    $score = 0;
  }
  $activities_done = 0;
  $activities = array_map(function ($activity) use ($user, $module, $attempt, &$activities_done) {

    /** @var \Drupal\opigno_module\Entity\OpignoActivity $activity */

    /** @var \Drupal\opigno_module\Entity\OpignoAnswer $answer */
    $answer = isset($attempt) ? $activity
      ->getUserAnswer($module, $attempt, $user) : NULL;
    $score = isset($answer) ? $answer
      ->getScore() : 0;
    $max_score = (int) $this
      ->get_activity_max_score($module, $activity);
    if ($max_score == 0 && $activity
      ->get('auto_skills')
      ->getValue()[0]['value'] == 1) {
      $max_score = 10;
    }
    if ($answer && $activity
      ->hasField('opigno_evaluation_method') && $activity
      ->get('opigno_evaluation_method')->value && !$answer
      ->isEvaluated()) {
      $state_class = 'pending';
    }
    else {
      $state_class = isset($answer) ? $answer
        ->isEvaluated() ? 'passed' : 'failed' : 'pending';
    }
    if ($state_class == 'passed') {
      $activities_done++;
    }
    return [
      [
        'class' => 'name',
        'data' => $activity
          ->getName(),
      ],
      [
        'class' => 'progress',
        'data' => [
          '#markup' => $score . '/' . $max_score,
        ],
      ],
      [
        'class' => 'status',
        'data' => [
          '#type' => 'inline_template',
          '#template' => '<div class="status-wrapper"><span class="led {{state_class}}">{{state_label}}</span></div>',
          '#context' => [
            'state_class' => $state_class,
            'state_label' => [
              'passed' => $this
                ->t('Done'),
              'pending' => $this
                ->t('Pending'),
              'failed' => $this
                ->t('Failed'),
            ][$state_class],
          ],
        ],
      ],
    ];
  }, $activities);
  $activities = [
    '#type' => 'table',
    '#attributes' => [
      'class' => [
        'statistics-table',
      ],
    ],
    '#header' => [
      $this
        ->t('Activity'),
      $this
        ->t('Score'),
      $this
        ->t('Status'),
    ],
    '#rows' => $activities,
  ];
  $training_id = $training
    ->id();
  $module_id = $module
    ->id();
  if (isset($course)) {
    $course_id = $course
      ->id();
    $id = "module_panel_{$training_id}_{$course_id}_{$module_id}";
  }
  else {
    $id = "module_panel_{$training_id}_{$module_id}";
  }
  $info_items = [
    [
      '#type' => 'inline_template',
      '#template' => '<div class="activity-info__item"><div class="label">{{"Activities Done"|t}}</div><div class="value"><span>{{activities_done}}/{{activities}}</span></div></div>',
      '#context' => [
        'activities_done' => (int) $activities_done,
        'activities' => (int) $step["activities"],
      ],
    ],
    [
      '#type' => 'inline_template',
      '#template' => '<div class="activity-info__item"><div class="label">{{"Score"|t}}</div><div class="value"><span>{{score}}/{{max_score}}</span></div></div>',
      '#context' => [
        'score' => (int) $score,
        'max_score' => (int) $max_score,
      ],
    ],
  ];
  if ($module && $attempt) {
    $see_activity = Link::createFromRoute($this
      ->t('See activity results'), 'opigno_module.module_result', [
      'opigno_module' => $module
        ->id(),
      'user_module_status' => $attempt
        ->id(),
    ], [
      'query' => [
        'skip-links' => TRUE,
      ],
    ])
      ->toRenderable();
    $see_activity['#attributes'] = [
      'class' => 'btn btn-rounded btn-small',
    ];
  }
  return [
    '#type' => 'container',
    'activities' => $activities,
    'info_items' => $info_items,
    'link' => $see_activity ?? [],
  ];
}