You are here

public function CourseReport::track in Course 6

Same name and namespace in other branches
  1. 7.2 includes/CourseReport.inc \CourseReport::track()
  2. 7 includes/CourseReport.inc \CourseReport::track()

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

File

includes/course.core.inc, line 306
course.core.inc File for main Course class.

Class

CourseReport
Holds a user's total progress through a course and functionality to check for completion of required objects.

Code

public function track() {
  $required = 0;
  $required_complete = 0;
  $prev = NULL;
  foreach ($this->course
    ->getObjects() as $courseObject) {
    if (!$courseObject
      ->getOption('enabled')) {
      continue;
    }
    if (!$prev) {
      $this
        ->setOption('section_name', $courseObject
        ->getTitle());
    }

    // Count required objects.
    $required += $courseObject
      ->getOption('required');

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

    // Log last grade.
    if ($courseObject
      ->isGraded() && $courseObject
      ->getOption('grade_include')) {
      $this
        ->setOption('grade_result', $courseObject
        ->getFulfillment()
        ->getOption('grade_result'));
    }
    if (!$courseObject
      ->getFulfillment()
      ->isComplete() && $prev && $prev
      ->getFulfillment()
      ->isComplete()) {
      $this
        ->setOption('section_name', $courseObject
        ->getTitle());
    }
    $prev = clone $courseObject;
  }
  if ($required_complete >= $required) {

    // Course requirements have been met.
    $this
      ->setOption('section', 'complete');
    $this
      ->setOption('section_name', 'Complete');
    $this
      ->setOption('complete', 1);
    if (!$this
      ->getOption('date_completed')) {
      $this
        ->setOption('date_completed', time());
    }
  }
  $this
    ->setOption('nid', $this->course
    ->getNode()->nid);
  $this
    ->setOption('uid', $this->course
    ->getUser()->uid);
  module_invoke_all('course_report_presave', $this);
  $op = $this
    ->getOption($this->primaryKey) ? 'update' : 'insert';
  $this
    ->save();
  module_invoke_all("course_report_{$op}", $this);
}