MDL-75667 admin: Avoid DB calls within loops on admin/blocks.php

This commit is contained in:
Farhan Karmali
2022-12-16 18:53:58 +05:30
parent 5019abfdd6
commit b4e5e47a0f
4 changed files with 27 additions and 8 deletions
+11 -6
View File
@@ -74,9 +74,15 @@
/// Get and sort the existing blocks
if (!$blocks = $DB->get_records('block', array(), 'name ASC')) {
throw new \moodle_exception('noblocks', 'error'); // Should never happen.
}
$sql = "SELECT b.* , COUNT(DISTINCT binst.id) as totalcount, COUNT(DISTINCT bcinst.id) as courseviewcount
FROM {block} b
LEFT JOIN {block_instances} binst ON binst.blockname = b.name
LEFT JOIN {block_instances} bcinst ON bcinst.blockname = b.name AND bcinst.pagetypepattern = 'course-view-*'
GROUP BY b.id, binst.blockname, bcinst.blockname
ORDER BY b.name ASC";
if (!$blocks = $DB->get_records_sql($sql)) {
throw new \moodle_exception('noblocks', 'error'); // Should never happen.
}
$incompatible = array();
@@ -154,9 +160,8 @@
// MDL-11167, blocks can be placed on mymoodle, or the blogs page
// and it should not show up on course search page
$totalcount = $DB->count_records('block_instances', array('blockname'=>$blockname));
$count = $DB->count_records('block_instances', array('blockname'=>$blockname, 'pagetypepattern'=>'course-view-*'));
$totalcount = $blocks[$blockid]->totalcount;
$count = $blocks[$blockid]->courseviewcount;
if ($count>0) {
$blocklist = "<a href=\"{$CFG->wwwroot}/course/search.php?blocklist=$blockid&amp;sesskey=".sesskey()."\" ";
$blocklist .= "title=\"$strshowblockcourse\" >$totalcount</a>";