MDL-82605 contentbank: pre-load contexts for options list.

Prevents additional DB reads per category/course, which can be
potentially expensive on large sites.
This commit is contained in:
Paul Holden 2024-08-01 17:59:52 +01:00
parent e0381615aa
commit 09fae09cd5
No known key found for this signature in database
GPG Key ID: A81A96D6045F6164
2 changed files with 10 additions and 13 deletions

View File

@ -14,14 +14,6 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Content bank class
*
* @package core_contentbank
* @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_contentbank; namespace core_contentbank;
use core_plugin_manager; use core_plugin_manager;
@ -246,8 +238,11 @@ class contentbank {
$courses = $coursescache->get($userid); $courses = $coursescache->get($userid);
if ($categories === false || $courses === false) { if ($categories === false || $courses === false) {
// Required fields for preloading the context record.
$contextfields = 'ctxid, ctxpath, ctxdepth, ctxlevel, ctxinstance, ctxlocked';
list($categories, $courses) = get_user_capability_contexts($capability, true, $userid, true, list($categories, $courses) = get_user_capability_contexts($capability, true, $userid, true,
'fullname, ctxlevel, ctxinstance, ctxid', 'name, ctxlevel, ctxinstance, ctxid', 'fullname', 'name'); "fullname, {$contextfields}", "name, {$contextfields}", 'fullname', 'name');
$categoriescache->set($userid, $categories); $categoriescache->set($userid, $categories);
$coursescache->set($userid, $courses); $coursescache->set($userid, $courses);
} }

View File

@ -16,8 +16,8 @@
namespace core_contentbank\output; namespace core_contentbank\output;
use context_course; use core\context\{course, coursecat};
use context_coursecat; use core\context_helper;
use core_contentbank\content; use core_contentbank\content;
use core_contentbank\contentbank; use core_contentbank\contentbank;
use renderable; use renderable;
@ -133,8 +133,9 @@ class bankcontent implements renderable, templatable {
} }
$options = []; $options = [];
foreach ($this->allowedcategories as $allowedcategory) { foreach ($this->allowedcategories as $allowedcategory) {
context_helper::preload_from_record(clone $allowedcategory);
$options[$allowedcategory->ctxid] = format_string($allowedcategory->name, true, [ $options[$allowedcategory->ctxid] = format_string($allowedcategory->name, true, [
'context' => context_coursecat::instance($allowedcategory->ctxinstance), 'context' => coursecat::instance($allowedcategory->ctxinstance),
]); ]);
} }
if (!empty($options)) { if (!empty($options)) {
@ -144,8 +145,9 @@ class bankcontent implements renderable, templatable {
foreach ($this->allowedcourses as $allowedcourse) { foreach ($this->allowedcourses as $allowedcourse) {
// Don't add the frontpage course to the list. // Don't add the frontpage course to the list.
if ($allowedcourse->id != $SITE->id) { if ($allowedcourse->id != $SITE->id) {
context_helper::preload_from_record(clone $allowedcourse);
$options[$allowedcourse->ctxid] = format_string($allowedcourse->fullname, true, [ $options[$allowedcourse->ctxid] = format_string($allowedcourse->fullname, true, [
'context' => context_course::instance($allowedcourse->ctxinstance), 'context' => course::instance($allowedcourse->ctxinstance),
]); ]);
} }
} }