class CourseObjectAccessGrade in Course 3.x
Same name and namespace in other branches
- 8.3 src/Plugin/course/CourseObjectAccess/CourseObjectAccessGrade.php \Drupal\course\Plugin\course\CourseObjectAccess\CourseObjectAccessGrade
- 8.2 src/Plugin/course/CourseObjectAccess/CourseObjectAccessGrade.php \Drupal\course\Plugin\course\CourseObjectAccess\CourseObjectAccessGrade
Plugin annotation
@CourseObjectAccess(
id = "grade",
label = @Translation("Grade"),
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\course\Plugin\CourseObjectAccessPluginBase implements PluginFormInterface, CourseObjectAccessInterface
- class \Drupal\course\Plugin\course\CourseObjectAccess\CourseObjectAccessGrade
- class \Drupal\course\Plugin\CourseObjectAccessPluginBase implements PluginFormInterface, CourseObjectAccessInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of CourseObjectAccessGrade
File
- src/
Plugin/ course/ CourseObjectAccess/ CourseObjectAccessGrade.php, line 15
Namespace
Drupal\course\Plugin\course\CourseObjectAccessView source
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 user filled out at least one.
if (is_numeric($form_state['course_grade_range']['low']) && is_numeric($form_state['course_grade_range']['high'])) {
// User filled out both.
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 {
// User did not fill out both.
form_error($form['course_grade_range'], t('You must enter a numeric value for both the low and high grade range.'));
}
}
}
/**
* {@inheritdoc}
*/
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;
}
}
}
}
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;
}
/**
* Helper method to get possible graded objects.
*/
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CourseObjectAccessGrade:: |
public | function |
Define the form to be used in the object access settings area. Overrides CourseObjectAccessPluginBase:: |
|
CourseObjectAccessGrade:: |
function |
Evaluate the access conditions. Overrides CourseObjectAccessPluginBase:: |
||
CourseObjectAccessGrade:: |
public | function |
Helper method to get possible graded objects. Overrides CourseObjectAccessPluginBase:: |
|
CourseObjectAccessGrade:: |
public | function |
Overrides CourseObjectAccessPluginBase:: |
|
CourseObjectAccessGrade:: |
public | function | ||
CourseObjectAccessPluginBase:: |
private | property | ||
CourseObjectAccessPluginBase:: |
private | property | ||
CourseObjectAccessPluginBase:: |
public | function | Alter a property during display/tracking. Does not affect the stored options. This is called in CourseObject::getReadOnlyOptions(). | |
CourseObjectAccessPluginBase:: |
public | function | ||
CourseObjectAccessPluginBase:: |
public | function | ||
CourseObjectAccessPluginBase:: |
public | function | ||
CourseObjectAccessPluginBase:: |
public | function | Can the user see the object in the outline? | 2 |
CourseObjectAccessPluginBase:: |
public | function | ||
CourseObjectAccessPluginBase:: |
public | function | ||
CourseObjectAccessPluginBase:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
|
CourseObjectAccessPluginBase:: |
public | function | Can the user take the object? | 2 |
CourseObjectAccessPluginBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
|
CourseObjectAccessPluginBase:: |
public | function | Can the user view the object but not interact? | 2 |
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 98 |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |