Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-35599

Logic in block_glossary_random->get_content() is broken

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Major Major
    • None
    • 2.3.1
    • Blocks, Glossary
    • MOODLE_23_STABLE

      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 = '';
               }

            moodle.com Moodle HQ
            monidu Nicolas Dunand
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:

                Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.