Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
/admin/ -> blocks.php (source)

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