You are here

class CourseObjectAccessGrade in Course 3.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/course/CourseObjectAccess/CourseObjectAccessGrade.php \Drupal\course\Plugin\course\CourseObjectAccess\CourseObjectAccessGrade
  2. 8.2 src/Plugin/course/CourseObjectAccess/CourseObjectAccessGrade.php \Drupal\course\Plugin\course\CourseObjectAccess\CourseObjectAccessGrade

Plugin annotation


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

Hierarchy

Expanded class hierarchy of CourseObjectAccessGrade

File

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

Namespace

Drupal\course\Plugin\course\CourseObjectAccess
View 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

Namesort descending Modifiers Type Description Overrides
CourseObjectAccessGrade::buildConfigurationForm public function Define the form to be used in the object access settings area. Overrides CourseObjectAccessPluginBase::buildConfigurationForm
CourseObjectAccessGrade::evaluate function Evaluate the access conditions. Overrides CourseObjectAccessPluginBase::evaluate
CourseObjectAccessGrade::getObjectOptions public function Helper method to get possible graded objects. Overrides CourseObjectAccessPluginBase::getObjectOptions
CourseObjectAccessGrade::optionsDefinition public function Overrides CourseObjectAccessPluginBase::optionsDefinition
CourseObjectAccessGrade::optionsValidate public function
CourseObjectAccessPluginBase::$courseObject private property
CourseObjectAccessPluginBase::$type private property
CourseObjectAccessPluginBase::alterOptions public function Alter a property during display/tracking. Does not affect the stored options. This is called in CourseObject::getReadOnlyOptions().
CourseObjectAccessPluginBase::getCourseObject public function
CourseObjectAccessPluginBase::getOption public function
CourseObjectAccessPluginBase::getOptions public function
CourseObjectAccessPluginBase::see public function Can the user see the object in the outline? 2
CourseObjectAccessPluginBase::setCourseObject public function
CourseObjectAccessPluginBase::setType public function
CourseObjectAccessPluginBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
CourseObjectAccessPluginBase::take public function Can the user take the object? 2
CourseObjectAccessPluginBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
CourseObjectAccessPluginBase::view public function Can the user view the object but not interact? 2
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 98
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.