You are here

protected function LearningPathAchievementController::module_approved_activities 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::module_approved_activities()

Returns module approved activities.

Parameters

int $parent: Group ID.

int $module: Module ID.

Return value

int Approved activities.

1 call to LearningPathAchievementController::module_approved_activities()
LearningPathAchievementController::build_lp_steps in src/Controller/LearningPathAchievementController.php
Returns LP steps.

File

src/Controller/LearningPathAchievementController.php, line 444

Class

LearningPathAchievementController
Class LearningPathAchievementController.

Namespace

Drupal\opigno_learning_path\Controller

Code

protected function module_approved_activities($parent, $module, $latest_cert_date = NULL) {
  $approved = 0;
  $user = $this
    ->currentUser();
  $parent = Group::load($parent);
  $module = OpignoModule::load($module);
  $moduleHandler = \Drupal::service('module_handler');
  $step = opigno_learning_path_get_module_step($parent
    ->id(), $user
    ->id(), $module, $latest_cert_date);

  /** @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())
      ->condition('o_m_r.max_score', '', '<>')
      ->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);
    return [
      isset($answer) ? 'lp_step_state_passed' : 'lp_step_state_failed',
    ];
  }, $activities);
  foreach ($activities as $activity) {
    if ($activity[0] == 'lp_step_state_passed') {
      $approved++;
    }
  }
  return $approved;
}