You are here

public function CourseEnrollmentEditAction::execute in Course 8.3

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

File

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

Class

CourseEnrollmentEditAction
Action description.

Namespace

Drupal\course\Plugin\Action

Code

public function execute($entity = NULL) {
  $node = \Drupal\node\Entity\Node::load($enrollment->cid);
  $account = Drupal\user\Entity\User::load($enrollment->uid);
  if (!($course_enrollment = course_enrollment_load($node, $account))) {
    $course_enrollment->cid = $node
      ->id();
    $course_enrollment->uid = $account->uid;
  }

  // Update enrollment status.
  if ($context['status'] != '') {
    $enrollment->status = $context['status'];
  }

  // Update enrollment duration.
  if ($context['enroll_end']) {

    // Parse date from popup/plain text.
    if ($unixtime = strtotime($context['enroll_end'])) {
      $enrollment->enroll_end = $unixtime;
    }
  }

  // Update completion.
  if ($context['complete'] != '') {
    $course_enrollment->complete = $context['complete'];
  }

  // Update date completed.
  if ($context['date_completed'] != '') {
    if ($unixtime = strtotime($context['date_completed'])) {
      $course_enrollment->date_completed = $unixtime;
    }
  }

  // Update start date
  if ($context['timestamp']) {
    if ($unixtime = strtotime($context['timestamp'])) {
      $enrollment->timestamp = $unixtime;
    }
  }
  $course = course_get_course($node);
  foreach ($course
    ->getObjects() as $key => $courseObject) {
    $coid = $courseObject
      ->getId();
    $fulfillment = $courseObject
      ->getFulfillment($account);
    if ($context['course_objects'][$coid] !== '') {

      // There was a change
      if ($context['course_objects'][$coid] == 1) {

        // Completed
        $fulfillment
          ->setOption('message', "Fulfillment completed via bulk action.");
        $fulfillment
          ->setComplete($context['course_objects'][$coid]);
      }
      if ($context['course_objects'][$coid] == -1) {

        // Delete attempt
        $fulfillment
          ->delete();
      }
      if ($context['course_objects'][$coid] == 0) {

        // Fail user
        $fulfillment
          ->setOption('message', "Fulfillment failed via bulk action.");
        $fulfillment
          ->setComplete(FALSE);
        $fulfillment
          ->setGrade(0);
      }
      $fulfillment
        ->save();
    }
  }
  course_enrollment_save($enrollment);
  \Drupal::messenger()
    ->addStatus(t('Updated enrollment for %user', array(
    '%user' => $account->name,
  )));
}