You are here

function course_get_course_object in Course 6

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. 7.2 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.

stdClass $account: The user object. This will instantiate a fulfillment record on the returned CourseObject.

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

Return value

CourseObject|FALSE

18 calls to course_get_course_object()
Course::getObjects in includes/course.core.inc
Get the course objects in this course.
CourseContextTestCase::testDetermineContext in tests/CourseContextTestCase.test
Testing finding course and objects via parameter search.
CourseObjectBookTestCase::testBookCourseObject in modules/course_book/course_book.test
CourseObjectContentTestCase::testContentCourseObjectCreation in modules/course_content/course_content.test
Test course content object creation.
CourseObjectNodeTestCase::testContentAccess in tests/CourseObjectNodeTestCase.test
Test content privacy for node based course objects.

... See full list

File

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

Code

function course_get_course_object($module, $object_type = NULL, $instance = NULL, $account = NULL, $course = NULL) {
  $available = course_get_handlers('object');
  $fulfillment = FALSE;
  if ($account) {

    // Account was passed. We are preparing for fulfillment.
    $fulfillment = TRUE;
  }
  if (!$account) {
    global $user;
    $account = $user;
  }
  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, $account, $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 = new Course($courseNode, $account);
      }
    }

    // Search for context.
    $outline_entries = array();
    $result = db_query("SELECT * FROM {course_outline} WHERE module = '%s' AND object_type = '%s' AND instance = '%s'", $module, $object_type, $instance);
    while ($row = db_fetch_object($result)) {
      $outline_entries[$row->nid] = $row;
    }
    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, $account);
    }
  }
  if (!isset($outline_entry)) {
    if ($fulfillment) {

      // Doing fulfillment, we need a persistent CourseObject.
      return FALSE;
    }
    else {

      // 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 {
    return FALSE;
  }
  $courseObject = new $class($outline_entry, $account, $course);
  if ($courseObject) {
    return $courseObject;
  }
  else {
    return FALSE;
  }
}