You are here

function course_save_objects in Course 8.3

Same name and namespace in other branches
  1. 8.2 course.module \course_save_objects()
  2. 6 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

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

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

File

./course.module, line 184
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',
          '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->nid = $course
          ->getNode()->nid;
      }
    }
    $object
      ->save();
  }
}