function course_determine_context in Course 3.x
Same name and namespace in other branches
- 8.3 course.module \course_determine_context()
- 8.2 course.module \course_determine_context()
- 6 course.module \course_determine_context()
- 7.2 course.module \course_determine_context()
- 7 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()
- CourseContextTest::testDetermineContext in tests/
src/ Functional/ CourseContextTest.php - Testing finding course and objects via parameter search.
- CourseContextTest::testMultiContext in tests/
src/ Functional/ CourseContextTest.php - Test objects that belong to multiple courses.
- CourseEventSubscriber::onRequest in src/
EventSubscriber/ CourseEventSubscriber.php - Check if the current node will fulfill an object.
- course_commerce_enroll_user_in_ordered_courses in modules/
course_commerce/ course_commerce.module - Loops through items in an Ubercart order and enrolls the user in courses purchased.
- 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.
1 string reference to 'course_determine_context'
- CourseContextTest::testMultiContext in tests/
src/ Functional/ CourseContextTest.php - Test objects that belong to multiple courses.
File
- ./
course.module, line 800 - course.module Core functionality for Courses.
Code
function course_determine_context($object_type = NULL, $instance = NULL, $no_set = FALSE) {
$lookup =& drupal_static('course_determine_context', array());
if (isset($lookup["{$object_type}:{$instance}"])) {
return $lookup["{$object_type}:{$instance}"];
}
$context = NULL;
// Determine the course node based on passed query parameters.
$result = Drupal::database()
->query("SELECT cid FROM {course_object} WHERE instance = :instance AND object_type = :object_type", array(
':instance' => $instance,
':object_type' => $object_type,
));
$cids = array();
while ($course_outline = $result
->fetch()) {
$cids[] = $course_outline->cid;
}
if (count($cids) > 1) {
if (!empty($_SESSION['course']['active']) && in_array($_SESSION['course']['active'], $cids)) {
// The active course in the session is one of the courses this object
// belongs to.
$context = Course::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 = Course::load($cids[0]);
}
}
elseif ($cids) {
// 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 = Course::load($cids[0]);
}
if ($no_set) {
// Callee just wants context.
$lookup["{$object_type}:{$instance}"] = $context;
return $context;
}
elseif ($context) {
// Set the active course and static cache it.
$_SESSION['course']['active'] = $context->cid;
$lookup["{$object_type}:{$instance}"] = $context;
return $context;
}
}