You are here

public function LpSteps::getModuleStep in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x src/LpSteps.php \Drupal\opigno_learning_path\LpSteps::getModuleStep()

Get list of module steps.

File

src/LpSteps.php, line 53

Class

LpSteps

Namespace

Drupal\opigno_learning_path

Code

public function getModuleStep($group_id, $uid, OpignoModule $module, $latest_cert_date = NULL) {
  $id = $module
    ->id();
  $key = "{$group_id}_{$uid}_{$id}_{$latest_cert_date}";
  $results =& drupal_static(__FUNCTION__);
  if (!isset($results[$key])) {

    /** @var \Drupal\opigno_group_manager\Entity\OpignoGroupManagedContent $content */
    $content = current(OpignoGroupManagedContent::loadByProperties([
      'group_id' => $group_id,
      'group_content_type_id' => 'ContentTypeModule',
      'entity_id' => $id,
    ]));
    if (!is_object($content)) {
      return array_shift($results);
    }
    $user = User::load($uid);

    // Get actual score for Module.
    $actual_score = $module
      ->getUserScore($user, $latest_cert_date);

    // Get required score.
    $required_score = (int) $content
      ->getSuccessScoreMin();

    // Get attempts.
    $attempts = $module
      ->getModuleAttempts($user, NULL, $latest_cert_date, TRUE);
    $last_attempt_score = $this
      ->getLastAttemptScore($attempts);

    // Get activities.
    $activities = $module
      ->getModuleActivities(TRUE);
    $options = [
      'required_score' => $required_score,
      'module' => $module,
      'user' => $user,
      'activities' => $activities,
      'latest_cert_date' => $latest_cert_date,
      'actual_score' => $actual_score,
      'attempts' => $attempts,
    ];
    $completed_on = $this
      ->stepIsComplated($options, $last_attempt_score);
    $results[$key] = [
      // OpignoGroupManagedContent id.
      'cid' => $content
        ->id(),
      // Entity id.
      'id' => $id,
      'name' => $module
        ->label(),
      'description' => $module->description
        ->view(),
      'typology' => 'Module',
      'best score' => $actual_score,
      'current attempt score' => $last_attempt_score,
      'required score' => $required_score,
      'attempts' => count($attempts),
      'activities' => count($activities),
      'time spent' => $this
        ->getTimeSpent($attempts),
      'current attempt time' => 0,
      'completed on' => $completed_on,
      'mandatory' => (int) $content
        ->isMandatory(),
    ];
  }
  return $results[$key];
}