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

Logic in block_glossary_random->get_content() is broken

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Duplicate
    • Affects Version/s: 2.3.1
    • Fix Version/s: None
    • Component/s: Blocks, Glossary
    • Labels:
    • Affected Branches:
      MOODLE_23_STABLE

      Description

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

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              moodle.com moodle.com
              Reporter:
              monidu Nicolas Dunand
              Participants:
              Component watchers:
              Adrian Greeve, Jake Dallimore, Mathew May, Mihail Geshoski, Peter Dias, Adrian Greeve, Jake Dallimore, Mathew May, Mihail Geshoski, Peter Dias
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: