[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/admin/ -> blocks.php (source)

   1  <?PHP // $Id: blocks.php,v 1.40.4.5 2008/01/03 15:02:59 skodak Exp $
   2  
   3      // Allows the admin to configure blocks (hide/show, delete 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      require_once($CFG->libdir.'/ddllib.php');
  10  
  11      admin_externalpage_setup('manageblocks');
  12  
  13      $confirm  = optional_param('confirm', 0, PARAM_BOOL);
  14      $hide     = optional_param('hide', 0, PARAM_INT);
  15      $show     = optional_param('show', 0, PARAM_INT);
  16      $delete   = optional_param('delete', 0, PARAM_INT);
  17      $multiple = optional_param('multiple', 0, PARAM_INT);
  18  
  19  /// Print headings
  20  
  21      $strmanageblocks = get_string('manageblocks');
  22      $strdelete = get_string('delete');
  23      $strversion = get_string('version');
  24      $strhide = get_string('hide');
  25      $strshow = get_string('show');
  26      $strsettings = get_string('settings');
  27      $strcourses = get_string('blockinstances', 'admin');
  28      $strname = get_string('name');
  29      $strmultiple = get_string('blockmultiple', 'admin');
  30      $strshowblockcourse = get_string('showblockcourse');
  31  
  32  /// If data submitted, then process and store.
  33  
  34      if (!empty($hide) && confirm_sesskey()) {
  35          if (!$block = get_record('block', 'id', $hide)) {
  36              error("Block doesn't exist!");
  37          }
  38          set_field('block', 'visible', '0', 'id', $block->id);      // Hide block
  39          admin_get_root(true, false);  // settings not required - only pages
  40      }
  41  
  42      if (!empty($show) && confirm_sesskey() ) {
  43          if (!$block = get_record('block', 'id', $show)) {
  44              error("Block doesn't exist!");
  45          }
  46          set_field('block', 'visible', '1', 'id', $block->id);      // Show block
  47          admin_get_root(true, false);  // settings not required - only pages
  48      }
  49  
  50      if (!empty($multiple) && confirm_sesskey()) {
  51          if (!$block = blocks_get_record($multiple)) {
  52              error("Block doesn't exist!");
  53          }
  54          $block->multiple = !$block->multiple;
  55          update_record('block', $block);
  56          admin_get_root(true, false);  // settings not required - only pages
  57      }
  58  
  59      if (!empty($delete) && confirm_sesskey()) {
  60          admin_externalpage_print_header();
  61          print_heading($strmanageblocks);
  62  
  63          if (!$block = blocks_get_record($delete)) {
  64              error("Block doesn't exist!");
  65          }
  66  
  67          if (!block_is_compatible($block->name)) {
  68              $strblockname = $block->name;
  69          }
  70          else {
  71              $blockobject = block_instance($block->name);
  72              $strblockname = $blockobject->get_title();
  73          }
  74  
  75          if (!$confirm) {
  76              notice_yesno(get_string('blockdeleteconfirm', '', $strblockname),
  77                           'blocks.php?delete='.$block->id.'&amp;confirm=1&amp;sesskey='.$USER->sesskey,
  78                           'blocks.php');
  79              admin_externalpage_print_footer();
  80              exit;
  81  
  82          } else {
  83              // Inform block it's about to be deleted
  84              $blockobject = block_instance($block->name);
  85              if ($blockobject) {
  86                  $blockobject->before_delete();  //only if we can create instance, block might have been already removed
  87              }
  88  
  89              // First delete instances and then block
  90              $instances = get_records('block_instance', 'blockid', $block->id);
  91              if(!empty($instances)) {
  92                  foreach($instances as $instance) {
  93                      blocks_delete_instance($instance);
  94                      blocks_delete_instance($instance, true);
  95                  }
  96              }
  97  
  98              // Delete block
  99              if (!delete_records('block', 'id', $block->id)) {
 100                  notify("Error occurred while deleting the $strblockname record from blocks table");
 101              }
 102  
 103              drop_plugin_tables($block->name, "$CFG->dirroot/blocks/$block->name/db/install.xml", false); // old obsoleted table names
 104              drop_plugin_tables('block_'.$block->name, "$CFG->dirroot/blocks/$block->name/db/install.xml", false);
 105  
 106              // Delete the capabilities that were defined by this block
 107              capabilities_cleanup('block/'.$block->name);
 108  
 109              // remove entent handlers and dequeue pending events
 110              events_uninstall('block/'.$block->name);
 111  
 112              $a->block = $strblockname;
 113              $a->directory = $CFG->dirroot.'/blocks/'.$block->name;
 114              notice(get_string('blockdeletefiles', '', $a), 'blocks.php');
 115          }
 116      }
 117  
 118      admin_externalpage_print_header();
 119      print_heading($strmanageblocks);
 120  
 121  /// Main display starts here
 122  
 123  /// Get and sort the existing blocks
 124  
 125      if (false === ($blocks = get_records('block'))) {
 126          error('No blocks found!');  // Should never happen
 127      }
 128  
 129      $incompatible = array();
 130  
 131      foreach ($blocks as $block) {
 132          if(!block_is_compatible($block->name)) {
 133              notify('Block '. $block->name .' is not compatible with the current version of Moodle and needs to be updated by a programmer.');
 134              $incompatible[] = $block;
 135              continue;
 136          }
 137          if(($blockobject = block_instance($block->name)) === false) {
 138              // Failed to load
 139              continue;
 140          }
 141          $blockbyname[$blockobject->get_title()] = $block->id;
 142          $blockobjects[$block->id] = $blockobject;
 143      }
 144  
 145      if(empty($blockbyname)) {
 146          error('One or more blocks are registered in the database, but they all failed to load!');
 147      }
 148  
 149      ksort($blockbyname);
 150  
 151  /// Print the table of all blocks
 152  
 153      $table = new flexible_table('admin-blocks-compatible');
 154  
 155      $table->define_columns(array('name', 'instances', 'version', 'hideshow', 'multiple', 'delete', 'settings'));
 156      $table->define_headers(array($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strmultiple, $strdelete, $strsettings));
 157      $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
 158      $table->set_attribute('id', 'blocks');
 159      $table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide');
 160      $table->setup();
 161  
 162      foreach ($blockbyname as $blockname => $blockid) {
 163  
 164          $blockobject = $blockobjects[$blockid];
 165          $block       = $blocks[$blockid];
 166  
 167          $delete = '<a href="blocks.php?delete='.$blockid.'&amp;sesskey='.$USER->sesskey.'">'.$strdelete.'</a>';
 168  
 169          $settings = ''; // By default, no configuration
 170          if ($blockobject->has_config()) {
 171              if (file_exists($CFG->dirroot.'/blocks/'.$block->name.'/settings.php')) {
 172                  $settings = '<a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=blocksetting'.$block->name.'">'.$strsettings.'</a>';
 173              } else {
 174                  $settings = '<a href="block.php?block='.$blockid.'">'.$strsettings.'</a>';
 175              }
 176          }
 177  
 178          // MDL-11167, blocks can be placed on mymoodle, or the blogs page
 179          // and it should not show up on course search page
 180  
 181          $totalcount = count_records('block_instance', 'blockid', $blockid);
 182  
 183          $count = count_records_sql('SELECT COUNT(*)
 184                                          FROM '.$CFG->prefix.'block_instance
 185                                          WHERE blockid = '.$blockid.' AND
 186                                          pagetype = \'course-view\'');
 187  
 188          if ($count>0) {
 189              $blocklist = "<a href=\"{$CFG->wwwroot}/course/search.php?blocklist=$blockid&amp;sesskey={$USER->sesskey}\" ";
 190              $blocklist .= "title=\"$strshowblockcourse\" >$totalcount</a>";
 191          }
 192          else {
 193              $blocklist = "$totalcount";
 194          }
 195          $class = ''; // Nothing fancy, by default
 196  
 197          if ($blocks[$blockid]->visible) {
 198              $visible = '<a href="blocks.php?hide='.$blockid.'&amp;sesskey='.$USER->sesskey.'" title="'.$strhide.'">'.
 199                         '<img src="'.$CFG->pixpath.'/i/hide.gif" class="icon" alt="'.$strhide.'" /></a>';
 200          } else {
 201              $visible = '<a href="blocks.php?show='.$blockid.'&amp;sesskey='.$USER->sesskey.'" title="'.$strshow.'">'.
 202                         '<img src="'.$CFG->pixpath.'/i/show.gif" class="icon" alt="'.$strshow.'" /></a>';
 203              $class = ' class="dimmed_text"'; // Leading space required!
 204          }
 205          if ($blockobject->instance_allow_multiple()) {
 206              if($blocks[$blockid]->multiple) {
 207                  $multiple = '<span style="white-space: nowrap;">'.get_string('yes').' (<a href="blocks.php?multiple='.$blockid.'&amp;sesskey='.$USER->sesskey.'">'.get_string('change', 'admin').'</a>)</span>';
 208              }
 209              else {
 210                  $multiple = '<span style="white-space: nowrap;">'.get_string('no').' (<a href="blocks.php?multiple='.$blockid.'&amp;sesskey='.$USER->sesskey.'">'.get_string('change', 'admin').'</a>)</span>';
 211              }
 212          }
 213          else {
 214              $multiple = '';
 215          }
 216  
 217          $table->add_data(array(
 218              '<span'.$class.'>'.$blockobject->get_title().'</span>',
 219              $blocklist,
 220              '<span'.$class.'>'.$blockobject->get_version().'</span>',
 221              $visible,
 222              $multiple,
 223              $delete,
 224              $settings
 225          ));
 226      }
 227  
 228      $table->print_html();
 229  
 230      if(!empty($incompatible)) {
 231          print_heading(get_string('incompatibleblocks', 'admin'));
 232  
 233          $table = new flexible_table('admin-blocks-incompatible');
 234  
 235          $table->define_columns(array('block', 'delete'));
 236          $table->define_headers(array($strname, $strdelete));
 237          $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
 238  
 239          $table->set_attribute('id', 'incompatible');
 240          $table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide');
 241  
 242          $table->setup();
 243  
 244          foreach ($incompatible as $block) {
 245              $table->add_data(array(
 246                  $block->name,
 247                  '<a href="blocks.php?delete='.$block->id.'&amp;sesskey='.$USER->sesskey.'">'.$strdelete.'</a>',
 248              ));
 249          }
 250          $table->print_html();
 251      }
 252  
 253      admin_externalpage_print_footer();
 254  
 255  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7