You are here

function course_determine_context in Course 7

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

5 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_uc_enroll_user_in_ordered_courses in modules/course_uc/course_uc.module
Loops through items in an Ubercart order and enrolls the user in courses purchased.

File

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

Code

function course_determine_context($module = NULL, $object_type = NULL, $instance = NULL, $no_set = FALSE) {
  $context = NULL;

  // Determine the course node based on passed query parameters.
  $result = db_query("SELECT nid FROM {course_outline} WHERE instance = :instance AND module = :module AND object_type = :object_type", array(
    ':instance' => $instance,
    ':module' => $module,
    ':object_type' => $object_type,
  ));
  $nids = array();
  while ($course_outline = $result
    ->fetch()) {
    $nids[] = $course_outline->nid;
  }
  if (count($nids) > 1) {
    if (!empty($_SESSION['course']['active']) && 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;
    return $context;
  }
}