You are here

function course_node_update in Course 7

Same name and namespace in other branches
  1. 7.2 course.module \course_node_update()

Implements hook_node_update().

1 call to course_node_update()
course_node_insert in ./course.module
Implements hook_node_insert().

File

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

Code

function course_node_update($node) {
  if (course_node_is_course($node)) {
    $record = $node->course;
    $record['nid'] = $node->nid;

    // Add configurable dates to the node object for easy retrieval.
    // Support configurable date fields.
    $cck_dates = array(
      'open' => 'course_start_date_' . $node->type,
      'close' => 'course_expiration_date_' . $node->type,
      'live_from_date' => 'course_live_from_date_' . $node->type,
      'live_to_date' => 'course_live_to_date_' . $node->type,
    );

    // Check whether each variable is set and the field exists on the
    // content type. If so, load that field's value to the course object,
    // overriding the coresponding column from the database.
    foreach ($cck_dates as $key => $variable) {
      $settings = @unserialize(variable_get($variable, array()));
      $field_exists = isset($settings['field']);
      if ($field_exists) {
        $items = field_get_items('node', $node, $settings['field'], $node->language);
        if ($items && count($items)) {
          $value = $items[0][$settings['value']];
          if (!empty($value)) {
            $date = new DateTime("{$value} UTC");
            $value = $date
              ->format('U');
          }
          $record[$key] = $value;
        }
      }
    }

    // 'Credits' is a numeric field so we need to ensure that it is a number.
    $record['credits'] = isset($record['credits']) ? floatval($record['credits']) : 0;

    // Default the enrollment type if necessary.
    if (empty($record['enrollment_type'])) {
      $record['enrollment_type'] = variable_get('course_default_enrollment_type');
    }
    $existing = db_query('SELECT 1 FROM {course_node} WHERE nid = :nid', array(
      ':nid' => $node->nid,
    ))
      ->fetchField();
    $update = $existing ? array(
      'nid',
    ) : array();
    drupal_write_record('course_node', $record, $update);
    $node->course = $record;

    // Support cloning.
    course_handle_clone($node);

    // Save the course objects - necessary for programmatic course creation.
    if (isset($node->course['objects'])) {
      $course = course_get_course($node);
      course_save_objects($node->course['objects'], $course);
    }
  }
}