View source
<?php
namespace Drupal\course\Plugin\course\CourseObjectAccess;
use Drupal\Core\Form\FormStateInterface;
use Drupal\course\Entity\CourseObject;
use Drupal\course\Plugin\CourseObjectAccessPluginBase;
class CourseObjectAccessGrade extends CourseObjectAccessPluginBase {
public function optionsDefinition() {
$defaults = parent::optionsDefinition();
$defaults += [
'course_grade_range' => [
'low' => NULL,
'high' => NULL,
],
'course_grade_hidden' => NULL,
'conditional_object' => NULL,
];
return $defaults;
}
public function optionsValidate($form, &$form_state) {
if (!empty($form_state['course_grade_range']['low']) || !empty($form_state['course_grade_range']['high'])) {
if (is_numeric($form_state['course_grade_range']['low']) && is_numeric($form_state['course_grade_range']['high'])) {
if ($form_state['course_grade_range']['low'] > $form_state['course_grade_range']['high']) {
form_error($form['course_grade_range'], t('High range cannot be lower than the low range.'));
}
}
else {
form_error($form['course_grade_range'], t('You must enter a numeric value for both the low and high grade range.'));
}
}
}
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')) {
$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) {
return TRUE;
}
else {
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;
}
}
}
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$config = $this
->getOptions();
$options = $this
->getObjectOptions();
$options[0] = '(this course)';
$form['conditional_object'] = [
'#title' => t('Grade to check'),
'#type' => 'select',
'#options' => $options,
'#description' => t('This grade will be checked.'),
'#default_value' => $config['conditional_object'],
];
$form['course_grade_range'] = [
'#type' => 'container',
];
$form['course_grade_range']['low'] = [
'#title' => t('Percentage low'),
'#type' => 'textfield',
'#size' => 4,
'#default_value' => isset($config['course_grade_range']['low']) ? $config['course_grade_range']['low'] : '',
];
$form['course_grade_range']['high'] = [
'#title' => t('Percentage high'),
'#type' => 'textfield',
'#size' => 4,
'#default_value' => isset($config['course_grade_range']['high']) ? $config['course_grade_range']['high'] : '',
];
return $form;
}
public function getObjectOptions() {
$options = [
'',
];
foreach ($this
->getCourseObject()
->getCourse()
->getObjects() as $courseObject) {
if ($courseObject
->id() != $this
->getCourseObject()
->id() && $courseObject
->isGraded()) {
$options[$courseObject
->id()] = $courseObject
->getTitle();
}
}
return $options;
}
}