You are here

function CourseObjectAccessConditional::take in Course 7

Same name and namespace in other branches
  1. 6 plugins/course/access/conditional.inc \CourseObjectAccessConditional::take()
  2. 7.2 plugins/course_object_access/conditional.inc \CourseObjectAccessConditional::take()

Overrides CourseObjectAccess::take

1 call to CourseObjectAccessConditional::take()
CourseObjectAccessConditional::view in plugins/course_object_access/conditional.inc

File

plugins/course_object_access/conditional.inc, line 23

Class

CourseObjectAccessConditional

Code

function take() {
  $type = $this
    ->getOption('conditional_type');
  $offset = $this
    ->getOption('conditional_time');
  $time = REQUEST_TIME;
  $access = TRUE;
  $title = $this
    ->getCourseObject()
    ->getTitle();
  $msg = t('%title is not yet available', array(
    '%title' => $title,
  ));
  if ($type) {
    foreach ($this
      ->getCourseObject()
      ->getCourse()
      ->getObjects() as $courseObject) {
      if ($courseObject
        ->getId() == $this
        ->getOption('conditional_object')) {
        switch ($type) {
          case 'completed':
            $completed = $courseObject
              ->getFulfillment()
              ->getOption('date_completed');
            if (!$completed) {
              $access = FALSE;
            }
            else {
              $date = $completed + $offset;
              $access = $time >= $date;
              $msg = t('%title will be available on %date', array(
                '%title' => $title,
                '%date' => format_date($date, 'short'),
              ));
            }
            break;
          case 'started':
            $started = $courseObject
              ->getFulfillment()
              ->getOption('date_started');
            if (!$started) {
              $access = FALSE;
            }
            else {
              $date = $started + $offset;
              $access = $time >= $date;
              $msg = t('%title will be available on %date', array(
                '%title' => $title,
                '%date' => format_date($date, 'short'),
              ));
            }
            break;
        }
      }
    }
  }
  if (!$access) {
    $this
      ->getCourseObject()
      ->setAccessMessage('conditional', $msg);
  }
  return $access;
}