You are here

public function LpSteps::getCourseStep in Opigno Learning path 8

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

Builds up a training course step.

Parameters

int $group_id: Training group ID.

int $uid: User ID.

\Drupal\group\Entity\GroupInterface $course: Group entity of the course.

Return value

array Data array about step in a group for a user.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

src/LpSteps.php, line 133

Class

LpSteps

Namespace

Drupal\opigno_learning_path

Code

public function getCourseStep($group_id, $uid, GroupInterface $course, $latest_cert_date = NULL) {
  $id = $course
    ->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' => 'ContentTypeCourse',
      'entity_id' => $id,
    ]));
    $course_steps = opigno_learning_path_get_steps($id, $uid, NULL, $latest_cert_date);

    // Get best score as an average modules best score.
    if (!empty($course_steps)) {
      $step_count = count($course_steps);
      $best_score = round(array_sum(array_map(function ($step) {
        return $step['best score'];
      }, $course_steps)) / $step_count);
      $score = $this
        ->courseGetScore($course_steps);
    }
    else {
      $best_score = 0;
      $score = 0;
    }

    // Sum of steps attempts.
    $attempts = array_sum(array_map(function ($step) {
      return (int) $step['attempts'];
    }, $course_steps));

    // Sum of steps time spent.
    $time_spent = array_sum(array_map(function ($step) {
      return (int) $step['time spent'];
    }, $course_steps));

    // Sum of steps time spent in the current attempt.
    $time = array_sum(array_map(function ($step) {
      return (int) $step['current attempt time'];
    }, $course_steps));

    // Get completed steps.
    $completed_steps = array_filter($course_steps, function ($step) {
      return $step['completed on'] > 0;
    });

    // If all steps completed.
    if ($course_steps && count($course_steps) === count($completed_steps)) {

      // Get the last completion time.
      $completed_on = max(array_map(function ($step) {
        return $step['completed on'];
      }, $course_steps));
    }
    else {
      $completed_on = 0;
    }
    $description = $course->field_course_description
      ->view();
    $results[$key] = [
      // OpignoGroupManagedContent id.
      'cid' => $content
        ->id(),
      // Entity id.
      'id' => $id,
      'name' => $course
        ->label(),
      'description' => $description,
      'typology' => 'Course',
      'best score' => $best_score,
      'current attempt score' => $score,
      'required score' => (int) $content
        ->getSuccessScoreMin(),
      'attempts' => $attempts,
      'activities' => 0,
      'time spent' => $time_spent,
      'current attempt time' => $time,
      'completed on' => $completed_on,
      'mandatory' => (int) $content
        ->isMandatory(),
    ];
  }
  return $results[$key];
}