You are here

class CourseBookEventSubscriber in Course 8.2

Same name and namespace in other branches
  1. 8.3 modules/course_book/src/EventSubscriber/CourseBookEventSubscriber.php \Drupal\course_book\EventSubscriber\CourseBookEventSubscriber
  2. 3.x modules/course_book/src/EventSubscriber/CourseBookEventSubscriber.php \Drupal\course_book\EventSubscriber\CourseBookEventSubscriber

Hierarchy

  • class \Drupal\course_book\EventSubscriber\CourseBookEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of CourseBookEventSubscriber

1 string reference to 'CourseBookEventSubscriber'
course_book.services.yml in modules/course_book/course_book.services.yml
modules/course_book/course_book.services.yml
1 service uses CourseBookEventSubscriber
course_book_event_subscriber in modules/course_book/course_book.services.yml
\Drupal\course_book\EventSubscriber\CourseBookEventSubscriber

File

modules/course_book/src/EventSubscriber/CourseBookEventSubscriber.php, line 12

Namespace

Drupal\course_book\EventSubscriber
View source
class CourseBookEventSubscriber implements EventSubscriberInterface {
  public static function getSubscribedEvents() {
    return [
      KernelEvents::REQUEST => [
        'onRequest',
        28,
      ],
    ];
  }

  /**
   * If the current node is a course object, fulfill it for the current user.
   *
   * @param GetResponseEvent $event
   */
  public function onRequest(GetResponseEvent $event) {
    $route_match = Drupal::routeMatch();
    if ($route_match
      ->getRouteName() == 'entity.node.canonical') {
      $node = $route_match
        ->getParameter('node');
      $account = Drupal::currentUser();
      $type = $node
        ->bundle();
      if (book_type_is_allowed($type)) {
        $search = !empty($node->book['bid']) ? $node->book['bid'] : $node
          ->id();
        if ($courseObject = course_get_course_object('book', $search)) {
          $options = array();

          // Mark this node as fulfillment in course_book's fulfillment tracking.
          if (!empty($node->book['nid'])) {
            $options['book_fulfillment'][$node->book['nid']] = TRUE;
            $courseObject
              ->getFulfillment($account)
              ->addOptions($options)
              ->save();
          }

          // "Grade" the book based on previous book page views.
          $courseObject
            ->grade($account);
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CourseBookEventSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
CourseBookEventSubscriber::onRequest public function If the current node is a course object, fulfill it for the current user.