You are here

public function CourseObject::optionsForm in Course 3.x

Same name and namespace in other branches
  1. 8.3 src/Entity/CourseObject.php \Drupal\course\Entity\CourseObject::optionsForm()
  2. 8.2 src/Entity/CourseObject.php \Drupal\course\Entity\CourseObject::optionsForm()

Default options form for all course objects.

Overrides CourseHandler::optionsForm

8 calls to CourseObject::optionsForm()
CourseObjectAttendance::optionsForm in modules/course_attendance/src/Plugin/course/CourseObject/CourseObjectAttendance.php
Default options form for all course objects.
CourseObjectCertificate::optionsForm in modules/course_certificate/src/Plugin/course/CourseObject/CourseObjectCertificate.php
Default options form for all course objects.
CourseObjectCommerce::optionsForm in modules/course_commerce/src/Plugin/course/CourseObject/CourseObjectCommerce.php
Default options form for all course objects.
CourseObjectManual::optionsForm in modules/course_object_manual/src/Plugin/course/CourseObject/CourseObjectManual.php
Default options form for all course objects.
CourseObjectNode::optionsForm in modules/course_content/src/Course/Object/CourseObjectNode.php
Default options form for all course objects.

... See full list

8 methods override CourseObject::optionsForm()
CourseObjectAttendance::optionsForm in modules/course_attendance/src/Plugin/course/CourseObject/CourseObjectAttendance.php
Default options form for all course objects.
CourseObjectCertificate::optionsForm in modules/course_certificate/src/Plugin/course/CourseObject/CourseObjectCertificate.php
Default options form for all course objects.
CourseObjectCommerce::optionsForm in modules/course_commerce/src/Plugin/course/CourseObject/CourseObjectCommerce.php
Default options form for all course objects.
CourseObjectManual::optionsForm in modules/course_object_manual/src/Plugin/course/CourseObject/CourseObjectManual.php
Default options form for all course objects.
CourseObjectNode::optionsForm in modules/course_content/src/Course/Object/CourseObjectNode.php
Default options form for all course objects.

... See full list

File

src/Entity/CourseObject.php, line 151

Class

CourseObject
Parent abstract base class of all course objects.

Namespace

Drupal\course\Entity

Code

public function optionsForm(&$form, FormStateInterface $form_state) {
  parent::optionsForm($form, $form_state);
  $config = $this
    ->getOptions();
  $form['header']['#markup'] = t("<h2>Settings for %t</h2>", array(
    '%t' => $this
      ->getTitle(),
  ));
  $form['uniqid'] = array(
    '#type' => 'value',
    '#value' => $this
      ->id(),
  );
  $form['course_tabs']['#type'] = 'vertical_tabs';
  $form['course_tabs']['#default_tab'] = 'edit-title';
  $form['title'] = array(
    '#title' => t('Title & description'),
    '#type' => 'details',
    '#group' => 'course_tabs',
    '#weight' => 0,
  );
  $form['outline'] = array(
    '#type' => 'details',
    '#title' => t('Settings'),
    '#group' => 'course_tabs',
    '#weight' => 1,
  );
  $form['plugins']['access'] = array(
    '#type' => 'details',
    '#title' => 'Access',
    '#group' => 'course_tabs',
    '#weight' => 4,
  );
  $form['delete'] = array(
    '#type' => 'details',
    '#title' => 'Delete',
    '#group' => 'course_tabs',
    '#weight' => 5,
  );
  $form['title']['title'] = array(
    '#title' => t('Title'),
    '#type' => 'textfield',
    '#description' => t('The title of this course object as it will appear to users in the course outline.'),
    '#size' => 100,
    '#default_value' => $config['title'],
    '#group' => 'description',
    '#required' => TRUE,
  );
  $form['outline']['enabled'] = array(
    '#title' => t('Enabled'),
    '#type' => 'checkbox',
    '#description' => t('Enabled course objects will become part of the course. Uncheck this box if you are not using this course object.'),
    '#default_value' => $config['enabled'],
  );
  $form['outline']['hidden'] = array(
    '#title' => t('Visible in outline'),
    '#type' => 'checkbox',
    '#description' => t('Objects that are not visible will not be seen by the learner. Uncheck this box for course objects that you do not want the learner to see.'),
    '#default_value' => !$config['hidden'],
    '#group' => 'course',
  );
  $form['outline']['required'] = array(
    '#title' => t('Completion required'),
    '#type' => 'checkbox',
    '#description' => t('Users must complete required objects. Uncheck this box if this is an optional course object.'),
    '#default_value' => $config['required'],
  );
  $form['outline']['skippable'] = array(
    '#title' => t('Skippable'),
    '#type' => 'checkbox',
    '#default_value' => $config['skippable'],
    '#states' => array(
      'visible' => array(
        '#edit-required' => array(
          'checked' => TRUE,
        ),
      ),
    ),
    '#description' => t('Users can proceed past this object but it will still be required for course completion.'),
  );

  // Delete object
  $form['delete']['delete_button'] = array(
    '#value' => t('Delete'),
    '#weight' => 999,
    '#type' => 'submit',
    '#submit' => array(
      array(
        $this,
        'setDelete',
      ),
      array(
        $this,
        'optionsSubmit',
      ),
    ),
    '#limit_validation_errors' => array(),
  );

  // Only allow deletion of existing instances.
  if (!empty($config['instance'])) {
    $form['delete']['delete_instance'] = array(
      '#title' => t('Also delete the referenced content.'),
      '#type' => 'checkbox',
      '#default_value' => $config['delete_instance'],
      '#stats' => array(
        'visible' => array(
          '#edit-delete' => array(
            'checked' => TRUE,
          ),
        ),
      ),
      '#group' => 'delete',
    );

    // Check for multiple instances.
    if (Drupal::database()
      ->query("SELECT count(coid) FROM {course_object} WHERE object_type = :object_type AND instance = :instance", array(
      ':object_type' => $config['object_type'],
      ':instance' => $config['instance'],
    ))
      ->fetchField() > 1) {
      $form['delete']['delete_instance']['#description'] = t('<span class="error"><strong>WARNING</strong></span>: multiple course objects link to this instance. Deleting the instance might break the other course objects that use it.');
    }
  }
  if ($this
    ->isGraded()) {
    $form['grading'] = array(
      '#title' => t('Grading'),
      '#type' => 'details',
      '#description' => t('Settings for graded objects.'),
      '#group' => 'course_tabs',
      '#weight' => 2,
    );
    $form['grading']['grade_include'] = array(
      '#title' => t('Include in final course grade'),
      '#description' => t('Include this grade result for calculation of the final course grade.<br/>Currently, only the last grade result per Course will be used.'),
      '#default_value' => $config['grade_include'],
      '#type' => 'checkbox',
    );
  }

  // Bring in access plugin configuration.
  $form['plugins']['#tree'] = TRUE;
  $form['plugins']['access']['#title'] = t('Access');
  $form['plugins']['access']['#description'] = t('By default, all required objects appearing before this object in the course outline must be completed before the user may access this object. Conditional access allows for additional conditions to be applied.');
  $form['plugins']['access']['#type'] = 'details';
  $pluginManager = Drupal::service('plugin.manager.course.object.access');
  $plugins = $pluginManager
    ->getDefinitions();
  foreach ($plugins as $key => $plugin) {
    $form['plugins']['access']['#tree'] = TRUE;
    $form['plugins']['access'][$key] = array(
      '#title' => $plugin['label'],
      '#type' => 'details',
      '#tree' => TRUE,
      '#open' => FALSE,
    );

    // Initialize access class.

    /* @var $courseAccess CourseObjectAccessPluginBase */
    $courseAccess = $pluginManager
      ->createInstance($key);
    $courseAccess
      ->setCourseObject($this);
    $courseAccess
      ->setType($key);

    // Add access plugin form to our form.
    $access_form = [];
    $access_form_state = new FormState();
    $form['plugins']['access'][$key] += $courseAccess
      ->buildConfigurationForm($access_form, $access_form_state);
  }

  // Update settings
  $form['actions']['update'] = array(
    '#value' => t('Update'),
    '#weight' => 999,
    '#type' => 'submit',
    '#validate' => array(
      array(
        $this,
        'optionsValidate',
      ),
    ),
    '#submit' => array(
      array(
        $this,
        'optionsSubmit',
      ),
    ),
  );
}