You are here

function course_edit_enrollment_action in Course 6

Same name and namespace in other branches
  1. 7.2 course.module \course_edit_enrollment_action()
  2. 7 course.module \course_edit_enrollment_action()

Edit enrolment action

Parameters

object $object: An object containing nid and uid properties.

array $context: Values from user input.

File

./course.module, line 1874
course.module Core functionality for Courses.

Code

function course_edit_enrollment_action($object, $context) {
  $enrollment = course_enrolment_load($object->nid, $object->uid);
  $node = node_load($enrollment->nid);
  $account = user_load($enrollment->uid);
  if (!($course_report = course_report_load($node, $account))) {
    $course_report->nid = $node->nid;
    $course_report->uid = $account->uid;
  }

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

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

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

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

  // Update date completed.
  if ($context['date_completed'] != '') {
    if ($unixtime = strtotime($context['date_completed'])) {
      $course_report->date_completed = $unixtime;
    }
  }
  course_enrolment_save($enrollment);
  course_report_save($course_report);
  $course = course_get_course($node, $account);
  foreach ($course
    ->getObjects() as $key => $courseObject) {
    $coid = $courseObject
      ->getId();
    $fulfillment = $courseObject
      ->getFulfillment();
    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
          ->setOption('failed', TRUE);
      }
      $fulfillment
        ->save();
    }
  }
  drupal_set_message(t('Updated enrollment for %user', array(
    '%user' => $account->name,
  )));
}