You are here

function CourseObjectAccessGrade::take in Course 8.3

Same name and namespace in other branches
  1. 8.2 src/Plugin/course/CourseObjectAccess/CourseObjectAccessGrade.php \Drupal\course\Plugin\course\CourseObjectAccess\CourseObjectAccessGrade::take()

Overrides CourseObjectAccessPluginBase::take

2 calls to CourseObjectAccessGrade::take()
CourseObjectAccessGrade::see in src/Plugin/course/CourseObjectAccess/CourseObjectAccessGrade.php
CourseObjectAccessGrade::view in src/Plugin/course/CourseObjectAccess/CourseObjectAccessGrade.php

File

src/Plugin/course/CourseObjectAccess/CourseObjectAccessGrade.php, line 44

Class

CourseObjectAccessGrade
Plugin annotation @CourseObjectAccess( id = "grade", label = @Translation("Grade"), )

Namespace

Drupal\course\Plugin\course\CourseObjectAccess

Code

function take($account) {
  $config = $this
    ->getOptions();
  if (!empty($config['course_grade_range']) && is_numeric($config['course_grade_range']['low']) && is_numeric($config['course_grade_range']['high'])) {
    $grade = NULL;
    $conditional_object = NULL;
    if ($this
      ->getOption('conditional_object')) {

      // Check object grade.
      $conditional_object = CourseObject::load($config['conditional_object']);
      $grade = $conditional_object
        ->getFulfillment($account)
        ->get('grade_result')->value;
    }
    else {
      $grade = $this
        ->getCourseObject()
        ->getCourse()
        ->getTracker($account)
        ->get('grade_result')->value;
    }
    $low_range = $grade >= $config['course_grade_range']['low'];
    $high_range = $grade <= $config['course_grade_range']['high'];
    if ($low_range && $high_range) {
      return TRUE;
    }
    else {
      if (isset($conditional_object)) {
        $this
          ->getCourseObject()
          ->setAccessMessage('grade', t('%title requires a grade between @grade_low% and @grade_high% to continue.', array(
          '@grade_low' => $config['course_grade_range']['low'],
          '@grade_high' => $config['course_grade_range']['high'],
          '%title' => $conditional_object
            ->getTitle(),
        )));
        return FALSE;
      }
      else {
        $this
          ->getCourseObject()
          ->setAccessMessage('grade', t('You must have a grade between @grade_low% and @grade_high% to continue.', array(
          '@grade_low' => $config['course_grade_range']['low'],
          '@grade_high' => $config['course_grade_range']['high'],
        )));
        return FALSE;
      }
    }
  }
}