blocks.php 10 KB

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