You are here

function opigno_learning_path_resumed_step in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x opigno_learning_path.module \opigno_learning_path_resumed_step()

Function to detect if user has resumed step in LP.

Return step content id or FALSE if there is no resumed step.

1 call to opigno_learning_path_resumed_step()
LearningPathStepsController::start in src/Controller/LearningPathStepsController.php
Start the learning path.

File

./opigno_learning_path.module, line 3832
Contains opigno_learning_path.module.

Code

function opigno_learning_path_resumed_step(array $steps) {
  $account = \Drupal::currentUser();

  // Array with all finished modules.
  $attempts_finished = [];
  foreach ($steps as $step) {
    $opigno_module = OpignoModule::load($step['id']);
    if ($opigno_module != NULL) {
      $attempts = $opigno_module
        ->getModuleAttempts($account, 'last');
      if ($attempts) {

        // Get last attempt.
        $last_attempt = end($attempts);
        if (!$last_attempt
          ->isFinished()) {

          // Means last attempt is unfinished. Return FALSE.
          break;
        }
        $attempts_finished[$opigno_module
          ->id()] = intval($last_attempt
          ->get('finished')->value);
      }
    }
  }
  if (!empty($attempts_finished)) {

    // Get last finished opigno_module id.
    $last_module_id = array_keys($attempts_finished, max($attempts_finished));
    $last_step = end($steps);
    if (reset($last_module_id) == $last_step['id']) {

      // continue.
    }
    else {
      for ($i = 0; $i < count($steps); ++$i) {
        if (intval($steps[$i]['id']) == reset($last_module_id)) {
          $next_step_cid = $steps[$i + 1]['cid'];
          return $next_step_cid;
        }
      }
    }
  }
  return FALSE;
}