You are here

function course_save_objects in Course 6

Same name and namespace in other branches
  1. 8.3 course.module \course_save_objects()
  2. 8.2 course.module \course_save_objects()
  3. 7.2 course.module \course_save_objects()
  4. 7 course.module \course_save_objects()
  5. 3.x course.module \course_save_objects()

Saves course objects.

Parameters

array $objects: An array of course object definitions.

Course $course: (optional) An instantiated Course, from course_get_course().

1 call to course_save_objects()
course_nodeapi in ./course.module
Implements hook_nodeapi().

File

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

Code

function course_save_objects(array $objects, Course $course = NULL) {
  foreach ($objects as $object) {

    // Check if this course object already exists in the database.
    if (isset($object->coid)) {

      // Check if this object does not belong to the current node.
      if ($object->nid != $course
        ->getNode()->nid) {

        // We are importing or cloning. Ensure the necessary keys are empty,
        // in order to prepare a new object using this object's definitions.
        $unset = array(
          'coid',
          'nid',
          'uuid',
        );
        foreach ($unset as $key) {
          if (isset($object->{$key})) {
            unset($object->{$key});
          }
        }

        // Replace the nid key, to properly associate the current course node
        // with this course object.
        $object->nid = $course
          ->getNode()->nid;

        // Clean out serialized data field.
        $unset_data_keys = array(
          'uniqid',
          'uuid',
        );
        if (isset($object->data) && ($data = unserialize($object->data))) {
          foreach ($unset_data_keys as $key) {
            if (isset($data[$key])) {
              unset($data[$key]);
            }
          }
          $object->data = serialize($data);
        }
      }
    }

    // Set options for this object.
    if ($prepareObject = course_get_course_object($object, NULL, NULL, NULL, $course)) {
      $available_options = $prepareObject
        ->getOptions();
      $options = array();
      foreach ($object as $key => $value) {

        // Check if this key is a valid option.
        if (isset($available_options[$key])) {
          $options[$key] = $value;
        }
      }

      // Set the options.
      $prepareObject
        ->setOptions($options);

      // Save the object, creating new instances, if applicable.
      $prepareObject
        ->save();
    }
  }
}