From ea178a7da43c495a346f6dc411b54096cb9a48f7 Mon Sep 17 00:00:00 2001
From: Chris Sangwin <C.J.Sangwin@ed.ac.uk>
Date: Fri, 26 May 2023 15:19:15 +0100
Subject: [PATCH] Do not set moodle context inside functions (WIP).

---
 adminui/dependencies.php | 10 +++++-----
 deploy.php               |  1 +
 locallib.php             |  9 +--------
 3 files changed, 7 insertions(+), 13 deletions(-)

diff --git a/adminui/dependencies.php b/adminui/dependencies.php
index d596e5c9e..7e2100e5b 100644
--- a/adminui/dependencies.php
+++ b/adminui/dependencies.php
@@ -124,7 +124,7 @@ if (data_submitted() && optional_param('includes', false, PARAM_BOOL)) {
         if (isset($q->compiledcache['includes']) && (
             (isset($q->compiledcache['includes']['keyval']) && count($q->compiledcache['includes']['keyval']) > 0) ||
             (isset($q->compiledcache['includes']['castext']) && count($q->compiledcache['includes']['castext']) > 0))) {
-            list($context, $seed, $urlparams) = qtype_stack_setup_question_test_page($q, false);
+            list($context, $seed, $urlparams) = qtype_stack_setup_question_test_page($q);
             if (stack_determine_moodle_version() < 400) {
                 $qurl = question_preview_url($item->questionid, null, null, null, null, $context);
             } else {
@@ -190,7 +190,7 @@ if (data_submitted() && optional_param('jsxgraphs', false, PARAM_BOOL)) {
         }
         // Confirm that it does have these.
         if ($block || $filter || $other) {
-            list($context, $seed, $urlparams) = qtype_stack_setup_question_test_page($q, false);
+            list($context, $seed, $urlparams) = qtype_stack_setup_question_test_page($q);
             if (stack_determine_moodle_version() < 400) {
                 $qurl = question_preview_url($item->questionid, null, null, null, null, $context);
             } else {
@@ -217,7 +217,7 @@ if (data_submitted() && optional_param('script', false, PARAM_BOOL)) {
     // Load the whole question, simpler to get the contexts correct that way.
     foreach ($qs as $item) {
         $q = question_bank::load_question($item->questionid);
-        list($context, $seed, $urlparams) = qtype_stack_setup_question_test_page($q, false);
+        list($context, $seed, $urlparams) = qtype_stack_setup_question_test_page($q);
         if (stack_determine_moodle_version() < 400) {
             $qurl = question_preview_url($item->questionid, null, null, null, null, $context);
         } else {
@@ -242,7 +242,7 @@ if (data_submitted() && optional_param('PLUGINFILE', false, PARAM_BOOL)) {
     // Load the whole question, simpler to get the contexts correct that way.
     foreach ($qs as $item) {
         $q = question_bank::load_question($item->questionid);
-        list($context, $seed, $urlparams) = qtype_stack_setup_question_test_page($q, false);
+        list($context, $seed, $urlparams) = qtype_stack_setup_question_test_page($q);
         if (stack_determine_moodle_version() < 400) {
             $qurl = question_preview_url($item->questionid, null, null, null, null, $context);
         } else {
@@ -267,7 +267,7 @@ if (data_submitted() && optional_param('langs', false, PARAM_BOOL)) {
     // Load the whole question, simpler to get the contexts correct that way.
     foreach ($qs as $item) {
         $q = question_bank::load_question($item->questionid);
-        list($context, $seed, $urlparams) = qtype_stack_setup_question_test_page($q, false);
+        list($context, $seed, $urlparams) = qtype_stack_setup_question_test_page($q);
         if (stack_determine_moodle_version() < 400) {
             $qurl = question_preview_url($item->questionid, null, null, null, null, $context);
         } else {
diff --git a/deploy.php b/deploy.php
index c736c3b29..fb43b60ac 100644
--- a/deploy.php
+++ b/deploy.php
@@ -36,6 +36,7 @@ $question = question_bank::load_question($questionid);
 
 // Process any other URL parameters, and do require_login.
 list($context, $seed, $urlparams) = qtype_stack_setup_question_test_page($question);
+$PAGE->set_context($context);
 
 // Check permissions.
 question_require_capability_on($questiondata, 'edit');
diff --git a/locallib.php b/locallib.php
index 18272260b..d3081a9e5 100644
--- a/locallib.php
+++ b/locallib.php
@@ -207,9 +207,7 @@ function stack_string_sanitise($str) {
  * that is needed on all of them.
  * @return array page context, selected seed (or null), and URL parameters.
  */
-function qtype_stack_setup_question_test_page($question, $setcontext = true) {
-    global $PAGE;
-
+function qtype_stack_setup_question_test_page($question) {
     $seed = optional_param('seed', null, PARAM_INT);
     $urlparams = array('questionid' => $question->id);
     if (!is_null($seed) && $question->has_random_variants()) {
@@ -238,11 +236,6 @@ function qtype_stack_setup_question_test_page($question, $setcontext = true) {
         } else {
             $urlparams['courseid'] = SITEID;
         }
-        if ($setcontext) {
-            require_login();
-            $PAGE->set_context($context);
-        }
-        // Note that in the other cases, require_login will set the correct page context.
     }
 
     return array($context, $seed, $urlparams);
-- 
GitLab