You are here

function CourseObjectAccessGrade::evaluate in Course 3.x

Evaluate the access conditions.

Parameters

AccountInterface $account:

Return value

bool

Overrides CourseObjectAccessPluginBase::evaluate

File

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

Class

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

Namespace

Drupal\course\Plugin\course\CourseObjectAccess

Code

function evaluate(\Drupal\Core\Session\AccountInterface $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()
        ->getEnrollment($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) {

      // In range, criteria met.
      return TRUE;
    }
    else {

      // @fixme @todo D8 messages don't make sense with new access controls
      if (isset($conditional_object)) {
        $this
          ->getCourseObject()
          ->setAccessMessage('grade', t('%title requires a grade between @grade_low% and @grade_high% to continue.', [
          '@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.', [
          '@grade_low' => $config['course_grade_range']['low'],
          '@grade_high' => $config['course_grade_range']['high'],
        ]));
        return FALSE;
      }
    }
  }
}