source: moodle/trunk/fuentes/lib/classes/plugininfo/block.php @ 1331

Last change on this file since 1331 was 136, checked in by mabarracus, 3 years ago

Ported code to xenial

File size: 5.6 KB
Line 
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/**
18 * Defines classes used for plugin info.
19 *
20 * @package    core
21 * @copyright  2011 David Mudrak <david@moodle.com>
22 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24namespace core\plugininfo;
25
26use moodle_url, part_of_admin_tree, admin_settingpage, admin_externalpage;
27
28defined('MOODLE_INTERNAL') || die();
29
30/**
31 * Class for page side blocks
32 */
33class block extends base {
34    /**
35     * Finds all enabled plugins, the result may include missing plugins.
36     * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
37     */
38    public static function get_enabled_plugins() {
39        global $DB;
40
41        return $DB->get_records_menu('block', array('visible'=>1), 'name ASC', 'name, name AS val');
42    }
43
44    /**
45     * Magic method getter, redirects to read only values.
46     *
47     * For block plugins pretends the object has 'visible' property for compatibility
48     * with plugins developed for Moodle version below 2.4
49     *
50     * @param string $name
51     * @return mixed
52     */
53    public function __get($name) {
54        if ($name === 'visible') {
55            debugging('This is now an instance of plugininfo_block, please use $block->is_enabled() instead of $block->visible', DEBUG_DEVELOPER);
56            return ($this->is_enabled() !== false);
57        }
58        return parent::__get($name);
59    }
60
61    public function init_display_name() {
62
63        if (get_string_manager()->string_exists('pluginname', 'block_' . $this->name)) {
64            $this->displayname = get_string('pluginname', 'block_' . $this->name);
65
66        } else if (($block = block_instance($this->name)) !== false) {
67            $this->displayname = $block->get_title();
68
69        } else {
70            parent::init_display_name();
71        }
72    }
73
74    public function get_settings_section_name() {
75        return 'blocksetting' . $this->name;
76    }
77
78    public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
79        global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
80        $ADMIN = $adminroot; // May be used in settings.php.
81        $plugininfo = $this; // Also can be used inside settings.php.
82        $block = $this;      // Also can be used inside settings.php.
83
84        if (!$this->is_installed_and_upgraded()) {
85            return;
86        }
87
88        $section = $this->get_settings_section_name();
89
90        if (!$hassiteconfig || (($blockinstance = block_instance($this->name)) === false)) {
91            return;
92        }
93
94        $settings = null;
95        if ($blockinstance->has_config()) {
96            if (file_exists($this->full_path('settings.php'))) {
97                $settings = new admin_settingpage($section, $this->displayname,
98                    'moodle/site:config', $this->is_enabled() === false);
99                include($this->full_path('settings.php')); // This may also set $settings to null.
100            }
101        }
102        if ($settings) {
103            $ADMIN->add($parentnodename, $settings);
104        }
105    }
106
107    public function is_uninstall_allowed() {
108        if ($this->name === 'settings' or $this->name === 'navigation') {
109            return false;
110        }
111        return true;
112    }
113
114    /**
115     * Return URL used for management of plugins of this type.
116     * @return moodle_url
117     */
118    public static function get_manage_url() {
119        return new moodle_url('/admin/blocks.php');
120    }
121
122    /**
123     * Warning with number of block instances.
124     *
125     * @return string
126     */
127    public function get_uninstall_extra_warning() {
128        global $DB;
129
130        if (!$count = $DB->count_records('block_instances', array('blockname'=>$this->name))) {
131            return '';
132        }
133
134        return '<p>'.get_string('uninstallextraconfirmblock', 'core_plugin', array('instances'=>$count)).'</p>';
135    }
136
137    /**
138     * Pre-uninstall hook.
139     *
140     * This is intended for disabling of plugin, some DB table purging, etc.
141     *
142     * NOTE: to be called from uninstall_plugin() only.
143     * @private
144     */
145    public function uninstall_cleanup() {
146        global $DB, $CFG;
147
148        if ($block = $DB->get_record('block', array('name'=>$this->name))) {
149            // Inform block it's about to be deleted.
150            if (file_exists("$CFG->dirroot/blocks/$block->name/block_$block->name.php")) {
151                $blockobject = block_instance($block->name);
152                if ($blockobject) {
153                    $blockobject->before_delete();  // Only if we can create instance, block might have been already removed.
154                }
155            }
156
157            // First delete instances and related contexts.
158            $instances = $DB->get_records('block_instances', array('blockname' => $block->name));
159            foreach ($instances as $instance) {
160                blocks_delete_instance($instance);
161            }
162
163            // Delete block.
164            $DB->delete_records('block', array('id'=>$block->id));
165        }
166
167        parent::uninstall_cleanup();
168    }
169}
Note: See TracBrowser for help on using the repository browser.