You are here

function opigno_learning_path_get_step_progress in Opigno Learning path 3.x

Same name and namespace in other branches
  1. 8 opigno_learning_path.module \opigno_learning_path_get_step_progress()

Returns step progress.

3 calls to opigno_learning_path_get_step_progress()
LearningPathAchievementController::build_course_steps in src/Controller/LearningPathAchievementController.php
Returns course steps renderable array.
LearningPathAchievementController::build_lp_steps in src/Controller/LearningPathAchievementController.php
Returns LP steps.
opigno_learning_path_get_step_status in ./opigno_learning_path.module
Returns step status.

File

./opigno_learning_path.module, line 3002
Contains opigno_learning_path.module.

Code

function opigno_learning_path_get_step_progress($step, $uid, $step_state_counting = FALSE, $latest_cert_date = NULL) {
  if (isset($step['passed']) && $step['passed']) {
    return 1;
  }
  $typology = isset($step['typology']) ? $step['typology'] : NULL;
  $moduleHandler = \Drupal::service('module_handler');
  if (in_array($typology, [
    'Meeting',
    'ILT',
  ])) {
    return isset($step['attempts']) && $step['attempts'] > 0 ? 1 : 0;
  }
  elseif ($typology === 'Module' || $typology === 'Course') {
    if ($typology === 'Module') {
      $activities = opigno_learning_path_get_module_activities($step['id'], $uid, $step_state_counting, $latest_cert_date);
      $module = OpignoModule::load($step['id']);

      // Limit random activities.
      if ($module
        ->getRandomization() == 2) {
        $unanswered_activities = array_filter($activities, function ($activity) {
          return !$activity['answers'];
        });
        $answered_activities = array_filter($activities, function ($activity) {
          return $activity['answers'];
        });
        $activities = array_slice($answered_activities, 0, $module
          ->getRandomActivitiesCount());
        if (count($activities) < $module
          ->getRandomActivitiesCount()) {
          $diff_num = $module
            ->getRandomActivitiesCount() - count($activities);
          $difference = array_slice($unanswered_activities, 0, $diff_num);
          array_merge($activities, $difference);
        }
      }
    }
    elseif ($typology === 'Course') {
      $activities = opigno_learning_path_get_activities($step['id'], $uid, $latest_cert_date);
    }
    else {
      $activities = [];
    }
    $total = count($activities);
    $attempted = count(array_filter($activities, function ($activity) {
      return $activity['answers'] > 0;
    }));

    // Add cheat for skills modules to jump to the next module if the user already has needed skills.
    if ($attempted == 0 && $typology == 'Module' && $step['current attempt score'] >= $step['required score']) {
      $module = \Drupal::entityTypeManager()
        ->getStorage('opigno_module')
        ->load($step['id']);
      if ($moduleHandler
        ->moduleExists('opigno_skills_system') && $module
        ->getSkillsActive() && isset($step['best_attempt'])) {
        $attempt = \Drupal::entityTypeManager()
          ->getStorage('user_module_status')
          ->load($step['best_attempt']);
        if (!empty($attempt) && $attempt
          ->isFinished()) {
          return 1;
        }
      }
    }
    return $total > 0 ? $attempted / $total : 0;
  }
  return 0;
}