This block can yield the following error in some cases. Correcting the logic in the get_content() function prevents this.
PHP catchable fatal error Debug: Argument 2 passed to has_capability() must be an instance of context, null given, called in /var/www/moodle/blocks/glossary_random/block_glossary_random.php on line 134
|
Exploring the code, I found that {{ $cm = $modinfo->instances['glossary'][$glossaryid]; }} is computed without any prior check.
Moving some lines around (i.e. checking for the existence of the corresponding array entries before assigning a value to $cm) solves the problem. Below is my diff:
--- a/blocks/glossary_random/block_glossary_random.php
|
+++ b/blocks/glossary_random/block_glossary_random.php
|
@@ -129,11 +129,6 @@ class block_glossary_random extends block_base {
|
$course = $this->page->course;
|
$modinfo = get_fast_modinfo($course);
|
$glossaryid = $this->config->glossary;
|
- $cm = $modinfo->instances['glossary'][$glossaryid];
|
-
|
- if (!has_capability('mod/glossary:view', get_context_instance(CONTEXT_MODULE, $cm->id))) {
|
- return '';
|
- }
|
|
if (!isset($modinfo->instances['glossary'][$glossaryid])) {
|
// we can get here if the glossary has been deleted, so
|
@@ -147,6 +142,12 @@ class block_glossary_random extends block_base {
|
return $this->content;
|
}
|
|
+ $cm = $modinfo->instances['glossary'][$glossaryid];
|
+
|
+ if (!has_capability('mod/glossary:view', get_context_instance(CONTEXT_MODULE, $cm->id))) {
|
+ return '';
|
+ }
|
+
|
if (empty($this->config->cache)) {
|
$this->config->cache = '';
|
}
|
- is duplicated by
-
MDL-35158 Random Glossary block returns an error when you delete the linked glossary
-
- Closed
-