You are here

function course_edit_enrollment_action in Course 7

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

Edit enrollment action

Parameters

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

array $context: Values from user input.

File

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

Code

function course_edit_enrollment_action($enrollment, $context) {
  $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['enroll_end']) {

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

  // 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;
    }
  }

  // Update start date
  if ($context['timestamp']) {
    if ($unixtime = strtotime($context['timestamp'])) {
      $enrollment->timestamp = $unixtime;
    }
  }
  $course = course_get_course($node, $account);
  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);
  course_report_save($course_report);
  drupal_set_message(t('Updated enrollment for %user', array(
    '%user' => $account->name,
  )));
}