Skip to content

Commit 5272ff3

Browse files
committed
Improve algorithm for getting current course.
1 parent ef38f5a commit 5272ff3

File tree

1 file changed

+2
-7
lines changed

1 file changed

+2
-7
lines changed

classes/jobesandbox.php

+2-7
Original file line numberDiff line numberDiff line change
@@ -155,21 +155,16 @@ public function get_languages() {
155155

156156
public function execute($sourcecode, $language, $input, $files = null, $params = null, $usecache = true) {
157157
global $CFG;
158-
global $COURSE;
159158
global $PAGE;
160159
// Course ID of 1 seems to be the fall back if it's not a course, eg, when bulktesting all q's.
161160
// So, use 1 if we don't find context from the PAGE or the context is not a course.
162-
$courseid = 1;
163161
// Get the current context.
164162
try {
165163
// Had to use try here as isset($PAGE->context) always seems to fail even if the context has been set.
166164
$context = $PAGE->context;
167-
$contextid = $context->id;
168-
if ($context->contextlevel == CONTEXT_COURSE) {
169-
$courseid = $context->instanceid;
170-
}
165+
$courseid = $context->get_course_context(strict: true)->instanceid;
171166
} catch (Exception $e) {
172-
; // Use default id/context of 1 as no $PAGE context is set, eg, could be a websocket UI run.
167+
$courseid = 1; // Use context of 1 as no $PAGE context is set, eg, could be a websocket UI run.
173168
}
174169

175170
$language = strtolower($language);

0 commit comments

Comments
 (0)