You are here

function course_determine_context in Course 6

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

Get the course node automatically, or from optional query parameters.

Parameters

string $module: The implementing course object provider module name.

string $object_type: The course object key as defined by hook_course_handlers().

string $instance: A key used internally by the implementing course object provider module, to identify an instance of *something* used by this course object type.

bool $no_set: Do not set the context (active course), just return it.

bool $flush: Flush the static cache. By default, course_determine_context will stop processing once a course is found, and continue to return it.

Return value

mixed A course node or NULL if course context not found.

6 calls to course_determine_context()
CourseContextTestCase::testDetermineContext in tests/CourseContextTestCase.test
Testing finding course and objects via parameter search.
CourseContextTestCase::testMultiContext in tests/CourseContextTestCase.test
Test objects that belong to multiple courses.
course_get_course_object in ./course.module
CourseObject factory. Get a loaded course object from database or build one from arguments.
course_init in ./course.module
Implements hook_init().
course_scorm_exit in modules/course_scorm/course_scorm.module
Implements hook_exit().

... See full list

File

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

Code

function course_determine_context($module = NULL, $object_type = NULL, $instance = NULL, $no_set = FALSE, $flush = FALSE) {
  static $cache = NULL;
  $context = NULL;
  if (!$context || $flush || $no_set) {

    // Determine the course node based on passed query parameters.
    $result = db_query("SELECT nid FROM {course_outline} WHERE instance = '%s' AND module = '%s' AND object_type = '%s'", $instance, $module, $object_type);
    $nids = array();
    while ($course_outline = db_fetch_object($result)) {
      $nids[] = $course_outline->nid;
    }
    if (count($nids) > 1) {
      if (in_array($_SESSION['course']['active'], $nids)) {

        // The active course in the session is one of the courses this object
        // belongs to.
        $context = node_load($_SESSION['course']['active']);
      }
      else {

        // No active course, or no match. We have to guess since we're accessing
        // this course material outside of the course.
        $context = node_load($nids[0]);
      }
    }
    elseif ($nids) {

      // We don't have an active session (or, the course in the active session
      // didn't contain this course object). So we just guess the first one.
      $context = node_load($nids[0]);
    }
    if ($no_set) {

      // Callee just wants context.
      return $context;
    }
    elseif ($context) {

      // Set the active course and static cache it.
      $_SESSION['course']['active'] = $context->nid;
      $cache = $context;
    }
  }
  return $cache;
}