Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
/admin/ -> blocks.php (source)

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 402] [Versions 401 and 403]

   1  <?php
   2  
   3      // Allows the admin to configure blocks (hide/show, uninstall and configure)
   4  
   5      require_once('../config.php');
   6      require_once($CFG->libdir.'/adminlib.php');
   7      require_once($CFG->libdir.'/blocklib.php');
   8      require_once($CFG->libdir.'/tablelib.php');
   9  
  10      admin_externalpage_setup('manageblocks');
  11  
  12      $confirm  = optional_param('confirm', 0, PARAM_BOOL);
  13      $hide     = optional_param('hide', 0, PARAM_INT);
  14      $show     = optional_param('show', 0, PARAM_INT);
  15      $unprotect = optional_param('unprotect', 0, PARAM_INT);
  16      $protect = optional_param('protect', 0, PARAM_INT);
  17  
  18  /// Print headings
  19  
  20      $strmanageblocks = get_string('manageblocks');
  21      $struninstall = get_string('uninstallplugin', 'core_admin');
  22      $strversion = get_string('version');
  23      $strhide = get_string('hide');
  24      $strshow = get_string('show');
  25      $strsettings = get_string('settings');
  26      $strcourses = get_string('blockinstances', 'admin');
  27      $strname = get_string('name');
  28      $strshowblockcourse = get_string('showblockcourse');
  29      $strprotecthdr = get_string('blockprotect', 'admin'). $OUTPUT->help_icon('blockprotect','admin');
  30      $strprotect = get_string('blockprotect', 'admin');
  31      $strunprotect = get_string('blockunprotect', 'admin');
  32  
  33  
  34      // If data submitted, then process and store.
  35      if (!empty($hide) && confirm_sesskey()) {
  36          if (!$block = $DB->get_record('block', ['id' => $hide])) {
  37              throw new \moodle_exception('blockdoesnotexist', 'error');
  38          }
  39  
  40          $class = \core_plugin_manager::resolve_plugininfo_class('block');
  41          $class::enable_plugin($block->name, false);
  42          // Settings not required - only pages.
  43          admin_get_root(true, false);
  44      }
  45  
  46      if (!empty($show) && confirm_sesskey() ) {
  47          if (!$block = $DB->get_record('block', ['id' => $show])) {
  48              throw new \moodle_exception('blockdoesnotexist', 'error');
  49          }
  50  
  51          $class = \core_plugin_manager::resolve_plugininfo_class('block');
  52          $class::enable_plugin($block->name, true);
  53          // Settings not required - only pages.
  54          admin_get_root(true, false);
  55      }
  56  
  57      if (!empty($protect) && confirm_sesskey()) {
  58          block_manager::protect_block((int)$protect);
  59          admin_get_root(true, false);  // settings not required - only pages
  60      }
  61  
  62      if (!empty($unprotect) && confirm_sesskey()) {
  63          block_manager::unprotect_block((int)$unprotect);
  64          admin_get_root(true, false);  // settings not required - only pages
  65      }
  66  
  67      $undeletableblocktypes = block_manager::get_undeletable_block_types();
  68  
  69      echo $OUTPUT->header();
  70      echo $OUTPUT->heading($strmanageblocks);
  71  
  72      echo $OUTPUT->notification(get_string('noteunneededblocks', 'admin'), 'info');
  73  /// Main display starts here
  74  
  75  /// Get and sort the existing blocks
  76  
  77      if (!$blocks = $DB->get_records('block', array(), 'name ASC')) {
  78          throw new \moodle_exception('noblocks', 'error');  // Should never happen.
  79      }
  80  
  81      $incompatible = array();
  82  
  83  /// Print the table of all blocks
  84  
  85      $table = new flexible_table('admin-blocks-compatible');
  86  
  87      $table->define_columns(array('name', 'instances', 'version', 'hideshow', 'undeletable', 'settings', 'uninstall'));
  88      $table->define_headers(array($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strprotecthdr, $strsettings, $struninstall));
  89      $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
  90      $table->set_attribute('class', 'admintable blockstable generaltable');
  91      $table->set_attribute('id', 'compatibleblockstable');
  92      $table->setup();
  93      $tablerows = array();
  94  
  95      // Sort blocks using current locale.
  96      $blocknames = array();
  97      foreach ($blocks as $blockid=>$block) {
  98          $blockname = $block->name;
  99          if (file_exists("$CFG->dirroot/blocks/$blockname/block_$blockname.php")) {
 100              $blocknames[$blockid] = get_string('pluginname', 'block_'.$blockname);
 101          } else {
 102              $blocknames[$blockid] = $blockname;
 103          }
 104      }
 105      core_collator::asort($blocknames);
 106  
 107      foreach ($blocknames as $blockid=>$strblockname) {
 108          $block = $blocks[$blockid];
 109          $blockname = $block->name;
 110          $dbversion = get_config('block_'.$block->name, 'version');
 111  
 112          if (!file_exists("$CFG->dirroot/blocks/$blockname/block_$blockname.php")) {
 113              $blockobject  = false;
 114              $strblockname = '<span class="notifyproblem">'.$strblockname.' ('.get_string('missingfromdisk').')</span>';
 115              $plugin = new stdClass();
 116              $plugin->version = $dbversion;
 117  
 118          } else {
 119              $plugin = new stdClass();
 120              $plugin->version = '???';
 121              if (file_exists("$CFG->dirroot/blocks/$blockname/version.php")) {
 122                  include("$CFG->dirroot/blocks/$blockname/version.php");
 123              }
 124  
 125              if (!$blockobject  = block_instance($block->name)) {
 126                  $incompatible[] = $block;
 127                  continue;
 128              }
 129          }
 130  
 131          if ($uninstallurl = core_plugin_manager::instance()->get_uninstall_url('block_'.$blockname, 'manage')) {
 132              $uninstall = html_writer::link($uninstallurl, $struninstall);
 133          } else {
 134              $uninstall = '';
 135          }
 136  
 137          $settings = ''; // By default, no configuration
 138          if ($blockobject and $blockobject->has_config()) {
 139              $blocksettings = admin_get_root()->locate('blocksetting' . $block->name);
 140  
 141              if ($blocksettings instanceof admin_externalpage) {
 142                  $settings = '<a href="' . $blocksettings->url .  '">' . get_string('settings') . '</a>';
 143              } else if ($blocksettings instanceof admin_settingpage) {
 144                  $settings = '<a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=blocksetting'.$block->name.'">'.$strsettings.'</a>';
 145              } else if (!file_exists($CFG->dirroot.'/blocks/'.$block->name.'/settings.php')) {
 146                  // If the block's settings node was not found, we check that the block really provides the settings.php file.
 147                  // Note that blocks can inject their settings to other nodes in the admin tree without using the default locations.
 148                  // This can be done by assigning null to $setting in settings.php and it is a valid case.
 149                  debugging('Warning: block_'.$block->name.' returns true in has_config() but does not provide a settings.php file',
 150                      DEBUG_DEVELOPER);
 151              }
 152          }
 153  
 154          // MDL-11167, blocks can be placed on mymoodle, or the blogs page
 155          // and it should not show up on course search page
 156  
 157          $totalcount = $DB->count_records('block_instances', array('blockname'=>$blockname));
 158          $count = $DB->count_records('block_instances', array('blockname'=>$blockname, 'pagetypepattern'=>'course-view-*'));
 159  
 160          if ($count>0) {
 161              $blocklist = "<a href=\"{$CFG->wwwroot}/course/search.php?blocklist=$blockid&amp;sesskey=".sesskey()."\" ";
 162              $blocklist .= "title=\"$strshowblockcourse\" >$totalcount</a>";
 163          }
 164          else {
 165              $blocklist = "$totalcount";
 166          }
 167          $class = ''; // Nothing fancy, by default
 168  
 169          if (!$blockobject) {
 170              // ignore
 171              $visible = '';
 172          } else if ($blocks[$blockid]->visible) {
 173              $visible = '<a href="blocks.php?hide='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strhide.'">'.
 174                         $OUTPUT->pix_icon('t/hide', $strhide) . '</a>';
 175          } else {
 176              $visible = '<a href="blocks.php?show='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strshow.'">'.
 177                         $OUTPUT->pix_icon('t/show', $strshow) . '</a>';
 178              $class = 'dimmed_text';
 179          }
 180  
 181          if ($dbversion == $plugin->version) {
 182              $version = $dbversion;
 183          } else {
 184              $version = "$dbversion ($plugin->version)";
 185          }
 186  
 187          if (!$blockobject) {
 188              // ignore
 189              $undeletable = '';
 190          } else if (in_array($blockname, $undeletableblocktypes)) {
 191              $undeletable = '<a href="blocks.php?unprotect='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strunprotect.'">'.
 192                         $OUTPUT->pix_icon('t/unlock', $strunprotect) . '</a>';
 193          } else {
 194              $undeletable = '<a href="blocks.php?protect='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strprotect.'">'.
 195                         $OUTPUT->pix_icon('t/lock', $strprotect) . '</a>';
 196          }
 197  
 198          $row = array(
 199              $strblockname,
 200              $blocklist,
 201              $version,
 202              $visible,
 203              $undeletable,
 204              $settings,
 205              $uninstall,
 206          );
 207          $table->add_data($row, $class);
 208      }
 209  
 210      $table->print_html();
 211  
 212      if (!empty($incompatible)) {
 213          echo $OUTPUT->heading(get_string('incompatibleblocks', 'blockstable', 'admin'));
 214  
 215          $table = new flexible_table('admin-blocks-incompatible');
 216  
 217          $table->define_columns(array('block', 'uninstall'));
 218          $table->define_headers(array($strname, $struninstall));
 219          $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
 220  
 221          $table->set_attribute('class', 'incompatibleblockstable generaltable');
 222  
 223          $table->setup();
 224  
 225          foreach ($incompatible as $block) {
 226              if ($uninstallurl = core_plugin_manager::instance()->get_uninstall_url('block_'.$block->name, 'manage')) {
 227                  $uninstall = html_writer::link($uninstallurl, $struninstall);
 228              } else {
 229                  $uninstall = '';
 230              }
 231              $table->add_data(array(
 232                  $block->name,
 233                  $uninstall,
 234              ));
 235          }
 236          $table->print_html();
 237      }
 238  
 239      echo $OUTPUT->footer();