You are here

function CourseObjectBook::grade in Course 3.x

Same name and namespace in other branches
  1. 8.3 modules/course_book/src/Plugin/course/CourseObject/CourseObjectBook.php \Drupal\course_book\Plugin\course\CourseObject\CourseObjectBook::grade()
  2. 8.2 modules/course_book/src/Plugin/course/CourseObject/CourseObjectBook.php \Drupal\course_book\Plugin\course\CourseObject\CourseObjectBook::grade()

Grade (track) the book based on the fulfillment data.

File

modules/course_book/src/Plugin/course/CourseObject/CourseObjectBook.php, line 95

Class

CourseObjectBook
Plugin annotation @CourseObject( id = "book", label = "Book", handlers = { "fulfillment" = "\Drupal\course_book\Plugin\course\CourseObject\CourseObjectBookFulfillment" } )

Namespace

Drupal\course_book\Plugin\course\CourseObject

Code

function grade($user) {

  /* @var $book_manager Drupal\book\BookManager */
  $book_manager = \Drupal::service('book.manager');
  $toc = $book_manager
    ->getTableOfContents($this
    ->getInstanceId(), \Drupal::config('course_book.settings')
    ->get('depth', 100));
  if (course_book_count($this
    ->getInstanceId()) == 0) {

    // Book has no pages. Complete object.
    $this
      ->getFulfillment($user)
      ->setComplete(1)
      ->save();
    return;
  }
  if ($this
    ->getOption('book_tracking') == 'all') {
    $nids = array_keys($toc);
    $fulfillment = $this
      ->getFulfillment($user)
      ->getOption('book_fulfillment');
    $viewed = $fulfillment ? array_keys(array_filter($fulfillment)) : array();
    if (!array_diff($nids, $viewed)) {
      $this
        ->getFulfillment($user)
        ->setComplete(1)
        ->save();
    }
  }
  elseif ($this
    ->getOption('book_tracking') == 'one') {
    $this
      ->getFulfillment($user)
      ->setComplete(1)
      ->save();
  }
}