You are here

function course_get_course_object in Course 7

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

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 1523
course.module Core functionality for Courses.

Code

function course_get_course_object($module, $object_type = NULL, $instance = NULL, stdClass $account = NULL, Course $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 = entity_load_single('course', $courseNode->nid);
      }
    }

    // Search for context.
    $outline_entries = array();
    $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,
    ));
    while ($row = $result
      ->fetch()) {
      $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 {
    $class = 'CourseObjectBroken';
  }
  $courseObject = entity_create('course_object', (array) $outline_entry);
  if ($courseObject) {
    return $courseObject;
  }
  else {
    return FALSE;
  }
}