MDL-69751 course: Handle missing guest user context
authorMihail Geshoski <mihail@moodle.com>
Tue, 6 Oct 2020 05:24:05 +0000 (13:24 +0800)
committerMihail Geshoski <mihail@moodle.com>
Tue, 20 Oct 2020 00:39:17 +0000 (08:39 +0800)
Makes sure the guest user context exists when fetching
the recommended modules. If the context does not exist
an exception is being thrown describing the problem.

course/classes/local/service/content_item_service.php

index ca60efb..832a7eb 100644 (file)
@@ -110,7 +110,12 @@ class content_item_service {
             throw new \coding_exception('The guest user does not exist in the database.');
         }
 
-        $favourites = $this->get_content_favourites(self::RECOMMENDATION_PREFIX, \context_user::instance($CFG->siteguest));
+        // Make sure the guest user context exists.
+        if (!$guestusercontext = \context_user::instance($CFG->siteguest, false)) {
+            throw new \coding_exception('The guest user context does not exist.');
+        }
+
+        $favourites = $this->get_content_favourites(self::RECOMMENDATION_PREFIX, $guestusercontext);
 
         $recommendationcache->set($CFG->siteguest, $favourites);
         return $favourites;