You are here

function course_get_course_object in Course 7.2

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

CourseObject factory. Get a loaded course object from database or build one from arguments.

Parameters

mixed $module: The module name of this course object, or an array resembling a row in the {course_outline} table.

string $object_type: The object type belonging to the module.

string $instance: The course object instance ID, FROM {course_outline}.instance.

Course $course: The Course to pass to the CourseObject instantiation.

Return value

CourseObject|FALSE

Deprecated

It is not recommended to use this function anymore.

Use entity_create() to create your own course object instantiations.

If module and object_type are provided, this will construct and return an empty course object.

If instance is also provided, this will get an existing object or return FALSE.

If course is also provided, the newly found or constructed course object will be set to that course before being returned. If the found object does not exist in the provided course, this will return FALSE.

25 calls to course_get_course_object()
CourseContextTestCase::testDetermineContext in tests/CourseContextTestCase.test
Testing finding course and objects via parameter search.
CourseObjectBookTestCase::testBookCourseObject in modules/course_book/course_book.test
CourseObjectBookTestCase::testBookCourseObjectContentAccess in modules/course_book/course_book.test
Books have special behavior when it comes to content access. All the sub pages should be protected.
CourseObjectContentTestCase::testContentCourseObjectCreation in modules/course_content/course_content.test
Test course content object creation.
CourseObjectFulfillmentTestCase::testCourseContentObjectFulfillment in tests/CourseObjectFulfillmentTestCase.test
Test fulfillment of CourseObjects with an enrolled/unenrolled user

... See full list

File

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

Code

function course_get_course_object($module, $object_type = NULL, $instance = NULL, Course $course = NULL) {

  // Cache lookups.
  $lookup =& drupal_static(__FUNCTION__, array());
  if (isset($instance) && !is_scalar($instance)) {
    throw new Exception('Course object instance must be scalar.');
  }
  $available = course_get_handlers('object');
  if (is_array($module)) {

    // Cast array passed to an object.
    $module = (object) $module;
  }
  if (is_object($module) && !empty($module->coid)) {

    // Passed options with the course object ID set.
    $coid = $module->coid;
    if (strpos($coid, 'course_object_') === FALSE) {
      return course_get_course_object_by_id($coid, $course);
    }
  }
  if (is_numeric($module)) {
    $coid = $module;
  }
  elseif (is_object($module)) {

    // This is an already loaded (but not saved) course object.
    $outline_entry = $module;
  }
  elseif (!is_null($instance)) {

    // Get the course context.
    if (!$course) {
      if ($courseNode = course_determine_context($module, $object_type, $instance, TRUE, FALSE)) {
        $course = entity_load_single('course', $courseNode->nid);
      }
    }

    // Search for context.
    if (!isset($lookup["{$module}{$object_type}{$instance}"])) {
      $result = db_query("SELECT * FROM {course_outline} WHERE module = :module AND object_type = :object_type AND instance = :instance", array(
        ':module' => $module,
        ':object_type' => $object_type,
        ':instance' => $instance,
      ));
      $outline_entries = $result
        ->fetchAllAssoc('nid');
      $lookup["{$module}{$object_type}{$instance}"] = $outline_entries;
    }
    else {
      $outline_entries = $lookup["{$module}{$object_type}{$instance}"];
    }
    if ($outline_entries) {

      // Found some course objects.
      //
      // Either the active course is in the courses this instance is in, or, the
      // active course wasn't a parent of any course object found, so use the
      // first object found.
      $coid = $courseNode && $outline_entries[$courseNode->nid] ? $outline_entries[$courseNode->nid]->coid : reset($outline_entries)->coid;
      return course_get_course_object_by_id($coid);
    }
    if ($instance && !$outline_entries) {

      // Provided a search instance, but nothing found.
      return FALSE;
    }
  }
  if (!isset($outline_entry)) {

    // Couldn't find context, and not checking for fulfillment. We can safely
    // construct a new CourseObject.
    $outline_entry = new stdClass();
    $outline_entry->module = $module;
    $outline_entry->object_type = $object_type;
    $outline_entry->instance = $instance;
  }
  $ret = $available[$outline_entry->module][$outline_entry->object_type];
  if ($ret['class']) {
    $class = $ret['class'];
  }
  else {
    $class = 'CourseObjectBroken';
  }
  $courseObject = entity_create('course_object', (array) $outline_entry);
  if ($courseObject) {
    if (!empty($course)) {
      $courseObject
        ->setCourse($course);
    }
    return $courseObject;
  }
  else {
    return FALSE;
  }
}