You are here

public function CourseEnrollmentEditAction::buildConfigurationForm in Course 8.3

Same name and namespace in other branches
  1. 8.2 src/Plugin/Action/CourseEnrollmentEditAction.php \Drupal\course\Plugin\Action\CourseEnrollmentEditAction::buildConfigurationForm()
  2. 3.x src/Plugin/Action/CourseEnrollmentEditAction.php \Drupal\course\Plugin\Action\CourseEnrollmentEditAction::buildConfigurationForm()

File

src/Plugin/Action/CourseEnrollmentEditAction.php, line 124

Class

CourseEnrollmentEditAction
Action description.

Namespace

Drupal\course\Plugin\Action

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form = array();

  // Load the first enrollment.
  $selection = reset($context['selection']);
  $check_enrollment = Drupal\course\Entity\CourseEnrollment::load($selection);
  $node = \Drupal\node\Entity\Node::load($check_enrollment->nid);

  // Check if this action is being performed on a single user, and set the
  // account accordingly.
  $account = NULL;
  $course_enrollment = NULL;
  if (count($context['selection']) === 1) {

    // Only one user and course, so let's prefill values.
    $enrollment = $check_enrollment;
    $account = Drupal\user\Entity\User::load($enrollment->uid);
    $course_enrollment = course_enrollment_load($node, $account);
  }
  $form['timestamp'] = array(
    '#title' => t('Set started date to'),
    '#type' => Drupal::moduleHandler()
      ->moduleExists('date_popup') ? 'date_popup' : 'date_text',
    '#date_format' => 'Y-m-d H:i',
    '#description' => t('The date the user started the course.'),
    '#default_value' => !empty($enrollment->timestamp) ? format_date($enrollment->timestamp, 'custom', 'Y-m-d H:i') : NULL,
  );
  $form['enroll_end'] = array(
    '#title' => t('Extend course enrollment until'),
    '#type' => Drupal::moduleHandler()
      ->moduleExists('date_popup') ? 'date_popup' : 'date_text',
    '#date_format' => 'Y-m-d H:i',
    '#description' => t('The date when the user will not be able to access the course.'),
    '#default_value' => !empty($enrollment->enroll_end) ? format_date($enrollment->enroll_end, 'custom', 'Y-m-d H:i') : NULL,
  );
  $form['status'] = array(
    '#title' => t('Set enrollment status to'),
    '#type' => 'select',
    '#options' => array(
      '' => '',
      1 => 'Active',
      0 => 'Inactive',
    ),
    '#default_value' => !empty($enrollment->status) ? $enrollment->status : NULL,
    '#description' => t('Setting an enrollment to "inactive" will prevent a user from accessing the course.'),
  );
  $form['complete'] = array(
    '#title' => t('Set completion status to'),
    '#type' => 'select',
    '#options' => array(
      '' => '',
      1 => t('Complete'),
      0 => t('Incomplete'),
    ),
    '#description' => t("This will change a user's course completion. Set this to incomplete to re-evaluate all requirements. Courses will never be automatically un-completed once they have been marked completed."),
    '#default_value' => !empty($course_enrollment->complete) ? $course_enrollment->complete : NULL,
  );
  $form['date_completed'] = array(
    '#title' => t('Set completion date to'),
    '#type' => Drupal::moduleHandler()
      ->moduleExists('date_popup') ? 'date_popup' : 'date_text',
    '#date_format' => 'Y-m-d H:i',
    '#description' => t('The date of completion.'),
    '#default_value' => !empty($course_enrollment->date_completed) ? format_date($course_enrollment->date_completed, 'custom', 'Y-m-d H:i') : NULL,
  );
  if (isset($node)) {

    // Get course objects, with or without a single user account information.
    $course = course_get_course($node);
    $objects = $course
      ->getObjects();

    // Build a list of a single user's fulfillments.
    $fulfillments = NULL;
    if ($account) {
      $fulfillments = array();
      foreach ($objects as $courseObject) {
        $fulfillments[$courseObject
          ->getId()] = $courseObject
          ->getFulfillment($account);
      }
    }
    $form['course_objects'] = array(
      '#title' => t('Set completion status'),
      '#description' => t('Set the status of a course object to be applied to selected users.'),
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#tree' => TRUE,
      '#prefix' => '<span id="course-objects-wrapper">',
      '#suffix' => '</span>',
    );
    foreach ($objects as $courseObject) {
      $form['course_objects'][$courseObject
        ->getId()] = array(
        '#type' => 'select',
        '#title' => check_plain($courseObject
          ->getTitle()),
        '#options' => array(
          '' => '- no change - ',
          1 => t('Complete'),
          -1 => t('Incomplete'),
          0 => t('Failed'),
        ),
        '#default_value' => $fulfillments ? $fulfillments[$courseObject
          ->getId()]
          ->isComplete() : NULL,
      );
    }
  }
}