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

blocks_for_region() is inefficient

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Waiting for peer review
    • Priority: Minor
    • Resolution: Unresolved
    • Affects Version/s: 3.7.2, Future Dev
    • Fix Version/s: None
    • Component/s: Blocks, Performance, Themes
    • Labels:

      Description

      In 'lib/outputrenderers.php' within the 'core_renderer' class there is the method 'blocks_for_region'.  It currently has the code of:

          public function blocks_for_region($region) {
              $blockcontents = $this->page->blocks->get_content_for_region($region, $this);
              $blocks = $this->page->blocks->get_blocks_for_region($region);
              $lastblock = null;
              $zones = array();
              foreach ($blocks as $block) {
                  $zones[] = $block->title;
              }
              $output = '';
              foreach ($blockcontents as $bc) {
                  if ($bc instanceof block_contents) {
                      $output .= $this->block($bc, $region);
                      $lastblock = $bc->title;
                  } else if ($bc instanceof block_move_target) {
                      $output .= $this->block_move_target($bc, $zones, $lastblock, $region);
                  } else {
                      throw new coding_exception('Unexpected type of thing (' . get_class($bc) . ') found in list of block contents.');
                  }
              }
              return $output;
          } 

      where the '$zones' array is the only thing that needs the contents of the humongous '$blocks' array of objects just for the block title, which as it happens is identical to the 'title' attribute contained within the 'block_contents' instances returned by the 'get_content_for_region' call to produce '$blockcontents'.  Thus, changing the code to:

          public function blocks_for_region($region) {
              $blockcontents = $this->page->blocks->get_content_for_region($region, $this);
              $lastblock = null;
              $zones = array();
              foreach ($blockcontents as $bc) {
                  $zones[] = $bc->title;
              }
              $output = '';
              foreach ($blockcontents as $bc) {
                  if ($bc instanceof block_contents) {
                      $output .= $this->block($bc, $region);
                      $lastblock = $bc->title;
                  } else if ($bc instanceof block_move_target) {
                      $output .= $this->block_move_target($bc, $zones, $lastblock, $region);
                  } else {
                      throw new coding_exception('Unexpected type of thing (' . get_class($bc) . ') found in list of block contents.');
                  }
              }
              return $output;
          } 

      will result in '$zones' having exactly the same data!  But without the need to call 'get_blocks_for_region' and return a large data structure.

       

        Attachments

          Activity

            People

            • Assignee:
              gb2048 Gareth J Barnard
              Reporter:
              gb2048 Gareth J Barnard
              Participants:
              Component watchers:
              Adrian Greeve, Mihail Geshoski, Peter Dias, Matteo Scaramuccia, Jake Dallimore, Jun Pataleta, Ryan Wyllie, Amaia Anabitarte, Bas Brands, Carlos Escobedo, Sara Arjona (@sarjona), Víctor Déniz Falcón
            • Votes:
              1 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

              • Created:
                Updated: