You are here

protected function LearningPathAchievementController::build_module_panel in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x 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

2 calls to LearningPathAchievementController::build_module_panel()
LearningPathAchievementController::course_module_panel_ajax in src/Controller/LearningPathAchievementController.php
Loads module panel with a AJAX.
LearningPathAchievementController::training_module_panel_ajax in src/Controller/LearningPathAchievementController.php
Loads module panel with a AJAX.

File

src/Controller/LearningPathAchievementController.php, line 217

Class

LearningPathAchievementController
Class LearningPathAchievementController.

Namespace

Drupal\opigno_learning_path\Controller

Code

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

  /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
  $date_formatter = \Drupal::service('date.formatter');
  $user = $this
    ->currentUser();
  $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 = array_map(function ($activity) use ($user, $module, $attempt) {

    /** @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 = 'lp_step_state_pending';
    }
    else {
      $state_class = isset($answer) ? 'lp_step_state_passed' : ($max_score == 0 ? 'lp_step_state_pending' : 'lp_step_state_failed');
    }
    return [
      [
        'data' => $activity
          ->getName(),
      ],
      [
        'data' => [
          '#markup' => $score . '/' . $max_score,
        ],
      ],
      [
        'data' => [
          '#type' => 'html_tag',
          '#tag' => 'span',
          '#attributes' => [
            'class' => [
              $state_class,
            ],
          ],
          '#value' => '',
        ],
      ],
    ];
  }, $activities);
  $activities = [
    '#type' => 'table',
    '#attributes' => [
      'class' => [
        'lp_module_panel_activities_overview',
      ],
    ],
    '#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}";
  }
  return [
    '#type' => 'container',
    '#attributes' => [
      'id' => $id,
      'class' => [
        'lp_module_panel',
      ],
    ],
    [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'lp_module_panel_header',
        ],
      ],
      [
        '#markup' => '<a href="#" class="lp_module_panel_close">&times;</a>',
      ],
      [
        '#type' => 'html_tag',
        '#tag' => 'h3',
        '#attributes' => [
          'class' => [
            'lp_module_panel_title',
          ],
        ],
        '#value' => $step['name'] . ' ' . (!empty($completed_on) ? t('completed') : ''),
      ],
    ],
    [
      '#type' => 'html_tag',
      '#tag' => 'hr',
      '#value' => '',
    ],
    [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'lp_module_panel_content',
        ],
      ],
      !empty($completed_on) ? [
        '#type' => 'html_tag',
        '#tag' => 'p',
        '#value' => t('@name completed on @date', [
          '@name' => $step['name'],
          '@date' => $completed_on,
        ]),
      ] : [],
      !($max_score == 0) ? [
        '#type' => 'html_tag',
        '#tag' => 'p',
        '#value' => t('User got @score of @max_score possible points.', [
          '@score' => $score,
          '@max_score' => $max_score,
        ]),
      ] : [],
      !($max_score == 0) ? [
        '#type' => 'html_tag',
        '#tag' => 'p',
        '#value' => t('Total score @percent%', [
          '@percent' => $score_percent,
        ]),
      ] : [],
      [
        '#type' => 'html_tag',
        '#tag' => 'h3',
        '#attributes' => [
          'class' => [
            'lp_module_panel_overview_title',
          ],
        ],
        '#value' => t('Activities Overview'),
      ],
      $activities,
      isset($attempt) ? [
        Link::createFromRoute($this
          ->t('Details'), 'opigno_module.module_result', [
          'opigno_module' => $module
            ->id(),
          'user_module_status' => $attempt
            ->id(),
        ], [
          'query' => [
            'skip-links' => TRUE,
          ],
        ])
          ->toRenderable(),
      ] : [],
    ],
  ];
}