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

blocks_for_region() is inefficient

XMLWordPrintable

      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.

       

            gb2048 Gareth J Barnard
            gb2048 Gareth J Barnard
            Mathew May Mathew May
            Andrew Lyons Andrew Lyons
            Janelle Barcega Janelle Barcega
            Votes:
            1 Vote for this issue
            Watchers:
            9 Start watching this issue

              Created:
              Updated:
              Resolved:

                Estimated:
                Original Estimate - 0 minutes
                0m
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 1 day, 1 hour, 55 minutes
                1d 1h 55m

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