You are here

public function CourseObjectAccessConditional::buildConfigurationForm in Course 3.x

Define the form to be used in the object access settings area.

Overrides CourseObjectAccessPluginBase::buildConfigurationForm

File

src/Plugin/course/CourseObjectAccess/CourseObjectAccessConditional.php, line 91

Class

CourseObjectAccessConditional
Plugin annotation @CourseObjectAccess( id = "conditional", label = @Translation("Conditional"), )

Namespace

Drupal\course\Plugin\course\CourseObjectAccess

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildConfigurationForm($form, $form_state);
  $config = $this
    ->getOptions();
  $form['conditional_type'] = array(
    '#title' => t('Event'),
    '#type' => 'select',
    '#options' => array(
      '',
      'started' => t('User started course object'),
      'completed' => t('User completed course object'),
    ),
    '#description' => t('This sets the conditional behavior.'),
  );
  if (Drupal::moduleHandler()
    ->moduleExists('timeperiod')) {
    $form['conditional_time'] = array(
      '#title' => t('Time'),
      '#type' => 'timeperiod_select',
      '#units' => array(
        '86400' => array(
          'max' => 30,
          'step size' => 1,
        ),
        '3600' => array(
          'max' => 24,
          'step size' => 1,
        ),
        '60' => array(
          'max' => 60,
          'step size' => 1,
        ),
      ),
      '#description' => t('Length of time after the event happens when this course object should be accessible.'),
      '#default_value' => $config['conditional_time'],
    );
  }
  else {
    $form['conditional_time'] = array(
      '#title' => t('Time'),
      '#type' => 'textfield',
      '#size' => 8,
      '#description' => t('Length of time in seconds after the event happens when this course object should be accessible.'),
      '#default_value' => $config['conditional_time'],
    );
  }
  $form['conditional_object'] = array(
    '#title' => t('Course object'),
    '#type' => 'select',
    '#options' => $this
      ->getObjectOptions(),
    '#description' => t('The course object to check for the type and time of conditional display.'),
  );
  $form['conditional_hidden'] = array(
    '#title' => t('Hide object until ready'),
    '#type' => 'checkbox',
    '#description' => t('This will prevent the course object from appearing in the course outline until it is ready.'),
  );
  $conditional_dependent = array(
    '#states' => array(
      'visible' => array(
        array(
          '#edit-plugins-access-conditional-conditional-type' => array(
            'value' => 'started',
          ),
        ),
        array(
          '#edit-plugins-access-conditional-conditional-type' => array(
            'value' => 'completed',
          ),
        ),
      ),
    ),
  );
  foreach (Element::children($form) as $key) {
    if ($key != 'conditional_time' && isset($config[$key])) {
      $form[$key]['#default_value'] = $config[$key];
    }
    if ($key != 'conditional_type' && $key != 'conditional_time') {
      $form[$key] += $conditional_dependent;
    }
  }
  return $form;
}