You are here

public function CourseEnrollment::evaluate in Course 8.3

Same name and namespace in other branches
  1. 8.2 src/Entity/CourseEnrollment.php \Drupal\course\Entity\CourseEnrollment::evaluate()
  2. 3.x src/Entity/CourseEnrollment.php \Drupal\course\Entity\CourseEnrollment::evaluate()

Track the course (scan required objects, update progress, completion, etc).

2 calls to CourseEnrollment::evaluate()
CourseEnrollment::preSave in src/Entity/CourseEnrollment.php
If a duration is set on the course, apply it to this enrollment.
CourseEnrollment::track in src/Entity/CourseEnrollment.php

File

src/Entity/CourseEnrollment.php, line 209

Class

CourseEnrollment
Defines the profile entity class.

Namespace

Drupal\course\Entity

Code

public function evaluate() {
  $required = 0;
  $required_complete = 0;
  $prev = NULL;
  $account = $this
    ->getUser();
  $grades = [];
  foreach ($this
    ->getCourse()
    ->getObjects() as $courseObject) {
    if (!$courseObject
      ->get('enabled')->value) {
      continue;
    }
    if (!$prev) {
      $this
        ->set('section_name', $courseObject
        ->getTitle());
      $this
        ->set('coid', $courseObject
        ->getId());
    }

    // Count required objects.
    $required += (int) $courseObject
      ->get('required')
      ->getString();

    // Count completed required objects.
    $required_complete += $courseObject
      ->get('required')
      ->getString() && $courseObject
      ->getFulfillment($account)
      ->isComplete();

    // Log last grade.
    if ($courseObject
      ->isGraded() && $courseObject
      ->getOption('grade_include')) {
      $grades[$courseObject
        ->id()] = $courseObject
        ->getFulfillment($account)
        ->getOption('grade_result');
    }
    if (!$courseObject
      ->getFulfillment($account)
      ->isComplete() && $prev && $prev
      ->getFulfillment($account)
      ->isComplete()) {
      $this
        ->set('section_name', $courseObject
        ->getTitle());
      $this
        ->set('coid', $courseObject
        ->getId());
    }
    $prev = clone $courseObject;
  }
  if (!empty($grades)) {
    $this
      ->set('grade_result', array_sum($grades) / count($grades));
  }
  if ($required_complete >= $required) {

    // Course requirements have been met.
    $this
      ->set('section', 'complete');
    $this
      ->set('section_name', 'Complete');
    $this
      ->set('complete', 1);
    if ($this
      ->get('date_completed')
      ->isEmpty()) {
      $this
        ->set('date_completed', REQUEST_TIME);
    }
  }
}