You are here

function course_save_objects in Course 3.x

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

Saves course objects.

Parameters

CourseObject[] $objects: An array of course object definitions.

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

File

./course.module, line 186
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->cid != $course
        ->getCourse()
        ->id()) {

        // 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',
          'cid',
          'uuid',
          'uniqid',
        );
        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->cid = $course
          ->getCourse()
          ->id();
      }
    }
    $object
      ->save();
  }
}