1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 namespace core_admin\table; 18 19 use html_writer; 20 use moodle_url; 21 use stdClass; 22 23 /** 24 * Tiny admin settings. 25 * 26 * @package core_admin 27 * @copyright 2023 Andrew Lyons <andrew@nicols.co.uk> 28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 29 */ 30 class block_management_table extends \core_admin\table\plugin_management_table { 31 32 /** @var plugininfo[] A list of blocks which cannot be deleted */ 33 protected array $undeletableblocktypes; 34 35 /** @var stdClass[] A list of basic block data */ 36 protected array $blockdata; 37 38 /** @var array<string,int> A list of course counts */ 39 protected array $courseblocks; 40 41 public function __construct() { 42 global $DB; 43 parent::__construct(); 44 $this->undeletableblocktypes = \block_manager::get_undeletable_block_types(); 45 46 $sql = 'SELECT b.name, 47 b.id, 48 COUNT(DISTINCT binst.id) as totalcount 49 FROM {block} b 50 LEFT JOIN {block_instances} binst ON binst.blockname = b.name 51 GROUP BY b.id, 52 b.name 53 ORDER BY b.name ASC'; 54 $this->blockdata = $DB->get_records_sql($sql); 55 56 $sql = "SELECT blockname 57 FROM {block_instances} 58 WHERE pagetypepattern = 'course-view-*' 59 GROUP BY blockname"; 60 $this->courseblocks = $DB->get_records_sql($sql); 61 } 62 63 protected function get_plugintype(): string { 64 return 'block'; 65 } 66 67 public function guess_base_url(): void { 68 $this->define_baseurl( 69 new moodle_url('/admin/blocks.php') 70 ); 71 } 72 73 protected function get_action_url(array $params = []): moodle_url { 74 return new moodle_url('/admin/blocks.php', $params); 75 } 76 77 78 protected function get_table_js_module(): string { 79 return 'core_admin/block_management_table'; 80 } 81 82 protected function get_column_list(): array { 83 $columns = parent::get_column_list(); 84 return array_merge( 85 array_slice($columns, 0, 1, true), 86 ['instances' => get_string('blockinstances', 'admin')], 87 array_slice($columns, 1, 2, true), 88 ['protect' => get_string('blockprotect', 'admin')], 89 array_slice($columns, 3, null, true), 90 ); 91 } 92 93 protected function get_columns_with_help(): array { 94 return [ 95 'protect' => new \help_icon('blockprotect', 'admin'), 96 ]; 97 } 98 99 /** 100 * Render the instances column 101 * @param stdClass $row 102 * @return string 103 */ 104 protected function col_instances(stdClass $row): string { 105 $blockdata = $this->blockdata[$row->plugininfo->name]; 106 if (array_key_exists($blockdata->name, $this->courseblocks)) { 107 return html_writer::link( 108 new moodle_url('/course/search.php', [ 109 'blocklist' => $blockdata->id, 110 ]), 111 $blockdata->totalcount, 112 ); 113 } 114 115 return $blockdata->totalcount; 116 } 117 118 /** 119 * Render the protect column. 120 * 121 * @param stdClass $row 122 * @return string 123 */ 124 protected function col_protect(stdClass $row): string { 125 global $OUTPUT; 126 127 $params = [ 128 'sesskey' => sesskey(), 129 ]; 130 131 $protected = in_array($row->plugininfo->name, $this->undeletableblocktypes); 132 133 $pluginname = $row->plugininfo->displayname; 134 if ($protected) { 135 $params['unprotect'] = $row->plugininfo->name; 136 $icon = $OUTPUT->pix_icon('t/unlock', get_string('blockunprotectblock', 'admin', $pluginname)); 137 } else { 138 $params['protect'] = $row->plugininfo->name; 139 $icon = $OUTPUT->pix_icon('t/lock', get_string('blockprotectblock', 'admin', $pluginname)); 140 } 141 142 return html_writer::link( 143 $this->get_action_url($params), 144 $icon, 145 [ 146 'data-action' => 'toggleprotectstate', 147 'data-plugin' => $row->plugin, 148 'data-target-state' => $protected ? 0 : 1, 149 ], 150 ); 151 return ''; 152 } 153 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body