You are here

function CourseObjectAccessConditional::take in Course 7.2

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

Can the user take the object?

Parameters

type $account:

Return value

boolean

Overrides CourseObjectAccess::take

1 call to CourseObjectAccessConditional::take()
CourseObjectAccessConditional::view in plugins/course_object_access/conditional.inc
Can the user view the object but not interact?

File

plugins/course_object_access/conditional.inc, line 23

Class

CourseObjectAccessConditional

Code

function take($account) {
  $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($account)
              ->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($account)
              ->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;
}