diff --git a/blocks/html/tests/privacy/provider_test.php b/blocks/html/tests/privacy/provider_test.php
index f3436a4f1d3..99e062e94df 100644
--- a/blocks/html/tests/privacy/provider_test.php
+++ b/blocks/html/tests/privacy/provider_test.php
@@ -297,6 +297,7 @@ final class provider_test extends \core_privacy\tests\provider_testcase {
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$this->setUser($user);
+ $this->getDataGenerator()->enrol_user($user->id, $course->id, 'editingteacher');
$block = $this->create_course_block($course, $title, $content, $format);
$context = \context_block::instance($block->instance->id);
@@ -324,6 +325,7 @@ final class provider_test extends \core_privacy\tests\provider_testcase {
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$this->setUser($user);
+ $this->getDataGenerator()->enrol_user($user->id, $course->id, 'editingteacher');
$block = $this->create_course_block($course, $title, $content, $format);
$context = \context_block::instance($block->instance->id);
@@ -369,6 +371,7 @@ final class provider_test extends \core_privacy\tests\provider_testcase {
$this->setUser($user2);
$course = $this->getDataGenerator()->create_course();
+ $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'editingteacher');
$courseblock = $this->create_course_block($course, $title, $content, $blockformat);
$coursecontext = \context_block::instance($courseblock->instance->id);
diff --git a/blocks/tests/externallib_test.php b/blocks/tests/externallib_test.php
index 59fda684872..32fa4f15a13 100644
--- a/blocks/tests/externallib_test.php
+++ b/blocks/tests/externallib_test.php
@@ -44,6 +44,7 @@ final class externallib_test extends externallib_advanced_testcase {
global $DB, $FULLME;
$this->resetAfterTest(true);
+ $this->setAdminUser();
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
@@ -77,6 +78,7 @@ final class externallib_test extends externallib_advanced_testcase {
global $DB, $FULLME;
$this->resetAfterTest(true);
+ $this->setAdminUser();
$user = $this->getDataGenerator()->create_user();
@@ -141,6 +143,7 @@ final class externallib_test extends externallib_advanced_testcase {
global $DB, $FULLME;
$this->resetAfterTest(true);
+ $this->setAdminUser();
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
@@ -237,6 +240,7 @@ final class externallib_test extends externallib_advanced_testcase {
global $DB, $CFG;
$this->resetAfterTest(true);
+ $this->setAdminUser();
// Enable MathJax filter in content and headings.
$this->configure_filters([
@@ -362,6 +366,7 @@ final class externallib_test extends externallib_advanced_testcase {
public function test_get_dashboard_blocks_default_dashboard_including_sticky_block(): void {
global $PAGE, $DB;
$this->resetAfterTest(true);
+ $this->setAdminUser();
$user = $this->getDataGenerator()->create_user();
$PAGE->set_url('/my/index.php'); // Need this because some internal API calls require the $PAGE url to be set.
@@ -410,6 +415,7 @@ final class externallib_test extends externallib_advanced_testcase {
public function test_get_dashboard_blocks_custom_user_dashboard(): void {
global $PAGE, $DB;
$this->resetAfterTest(true);
+ $this->setAdminUser();
$user = $this->getDataGenerator()->create_user();
$PAGE->set_url('/my/index.php'); // Need this because some internal API calls require the $PAGE url to be set.
diff --git a/lang/en/error.php b/lang/en/error.php
index 63fdb30f78f..590e2310a48 100644
--- a/lang/en/error.php
+++ b/lang/en/error.php
@@ -31,6 +31,7 @@ $string['agelocationverificationdisabled'] = 'Age and location verification disa
$string['authnotexisting'] = 'The autorization plugin doesn\'t exist';
$string['backupcontainexternal'] = 'This backup file contains external Moodle Network Hosts that are not configured locally';
$string['backuptablefail'] = 'Backup tables could NOT be set up successfully!';
+$string['blockcannotadd'] = 'Cannot add block';
$string['blockcannotconfig'] = 'This block does not support global configuration';
$string['blockcannotinistantiate'] = 'Problem in instantiating block object';
$string['blockcannotread'] = 'Could not read data for blockid= {$a}';
diff --git a/lib/blocklib.php b/lib/blocklib.php
index 865efe68f05..7dc4ef80a3c 100644
--- a/lib/blocklib.php
+++ b/lib/blocklib.php
@@ -245,18 +245,18 @@ class block_manager {
* @return boolean - is there one of these blocks in the current page?
*/
public function is_block_present($blockname) {
- if (empty($this->blockinstances)) {
+ if (empty($this->birecordsbyregion)) {
return false;
}
$requiredbythemeblocks = $this->get_required_by_theme_block_types();
- foreach ($this->blockinstances as $region) {
+ foreach ($this->birecordsbyregion as $region) {
foreach ($region as $instance) {
- if (empty($instance->instance->blockname)) {
+ if (empty($instance->blockname)) {
continue;
}
- if ($instance->instance->blockname == $blockname) {
- if ($instance->instance->requiredbytheme) {
+ if ($instance->blockname == $blockname) {
+ if ($instance->requiredbytheme) {
if (!in_array($blockname, $requiredbythemeblocks)) {
continue;
}
@@ -834,6 +834,14 @@ class block_manager {
$pagetypepattern = $this->page->pagetype;
}
+ if (!empty($this->birecordsbyregion)) {
+ $addableblocks = $this->get_addable_blocks();
+
+ if (!array_key_exists($blockname, $addableblocks)) {
+ throw new moodle_exception('blockcannotadd');
+ }
+ }
+
$blockinstance = new stdClass;
$blockinstance->blockname = $blockname;
$blockinstance->parentcontextid = $this->page->context->id;