blocks.php 8.62 KB
Newer Older
1
<?php
2
3
4
5

    // Allows the admin to configure blocks (hide/show, delete and configure)

    require_once('../config.php');
6
    require_once($CFG->libdir.'/adminlib.php');
defacer's avatar
   
defacer committed
7
    require_once($CFG->libdir.'/tablelib.php');
8

9
    admin_externalpage_setup('manageblocks');
10

skodak's avatar
skodak committed
11
    $confirm  = optional_param('confirm', 0, PARAM_BOOL);
12
13
14
    $hide     = optional_param('hide', 0, PARAM_INT);
    $show     = optional_param('show', 0, PARAM_INT);
    $delete   = optional_param('delete', 0, PARAM_INT);
15
16
17
18
19
20
21
22
23

/// Print headings

    $strmanageblocks = get_string('manageblocks');
    $strdelete = get_string('delete');
    $strversion = get_string('version');
    $strhide = get_string('hide');
    $strshow = get_string('show');
    $strsettings = get_string('settings');
24
    $strcourses = get_string('blockinstances', 'admin');
25
    $strname = get_string('name');
thepurpleblob's avatar
thepurpleblob committed
26
    $strshowblockcourse = get_string('showblockcourse');
27
28
29

/// If data submitted, then process and store.

30
    if (!empty($hide) && confirm_sesskey()) {
skodak's avatar
skodak committed
31
        if (!$block = $DB->get_record('block', array('id'=>$hide))) {
32
            print_error('blockdoesnotexist', 'error');
33
        }
skodak's avatar
skodak committed
34
        $DB->set_field('block', 'visible', '0', array('id'=>$block->id));      // Hide block
35
        admin_get_root(true, false);  // settings not required - only pages
36
37
    }

38
    if (!empty($show) && confirm_sesskey() ) {
skodak's avatar
skodak committed
39
        if (!$block = $DB->get_record('block', array('id'=>$show))) {
40
            print_error('blockdoesnotexist', 'error');
41
        }
skodak's avatar
skodak committed
42
        $DB->set_field('block', 'visible', '1', array('id'=>$block->id));      // Show block
43
        admin_get_root(true, false);  // settings not required - only pages
44
45
    }

46
    if (!empty($delete) && confirm_sesskey()) {
47
        echo $OUTPUT->header();
48
        echo $OUTPUT->heading($strmanageblocks);
49
50

        if (!$block = blocks_get_record($delete)) {
51
            print_error('blockdoesnotexist', 'error');
52
53
        }

54
        if (get_string_manager()->string_exists('pluginname', "block_$block->name")) {
55
56
57
            $strblockname = get_string('pluginname', "block_$block->name");
        } else {
            $strblockname = $block->name;
defacer's avatar
   
defacer committed
58
        }
59

defacer's avatar
   
defacer committed
60
        if (!$confirm) {
61
            echo $OUTPUT->confirm(get_string('blockdeleteconfirm', '', $strblockname), 'blocks.php?delete='.$block->id.'&confirm=1', 'blocks.php');
62
            echo $OUTPUT->footer();
63
64
65
            exit;

        } else {
66
            // Inform block it's about to be deleted
67
68
69
70
71
            if (file_exists("$CFG->dirroot/blocks/$block->name/block_$block->name.php")) {
                $blockobject = block_instance($block->name);
                if ($blockobject) {
                    $blockobject->before_delete();  //only if we can create instance, block might have been already removed
                }
72
73
            }

74
            // First delete instances and then block
75
            $instances = $DB->get_records('block_instances', array('blockname' => $block->name));
76
77
78
79
80
            if(!empty($instances)) {
                foreach($instances as $instance) {
                    blocks_delete_instance($instance);
                }
            }
81

82
            // Delete block
83
            $DB->delete_records('block', array('id'=>$block->id));
84

85
86
87
            drop_plugin_tables($block->name, "$CFG->dirroot/blocks/$block->name/db/install.xml", false); // old obsoleted table names
            drop_plugin_tables('block_'.$block->name, "$CFG->dirroot/blocks/$block->name/db/install.xml", false);

88
89
            // Delete the capabilities that were defined by this block
            capabilities_cleanup('block/'.$block->name);
90

Martin Dougiamas's avatar
Martin Dougiamas committed
91
            // Remove event handlers and dequeue pending events
92
93
            events_uninstall('block/'.$block->name);

94
95
96
97
98
99
            $a->block = $strblockname;
            $a->directory = $CFG->dirroot.'/blocks/'.$block->name;
            notice(get_string('blockdeletefiles', '', $a), 'blocks.php');
        }
    }

100
    echo $OUTPUT->header();
101
    echo $OUTPUT->heading($strmanageblocks);
102

103
104
105
106
/// Main display starts here

/// Get and sort the existing blocks

107
    if (!$blocks = $DB->get_records('block', array(), 'name ASC')) {
dongsheng's avatar
dongsheng committed
108
        print_error('noblocks', 'error');  // Should never happen
109
110
    }

defacer's avatar
   
defacer committed
111
112
    $incompatible = array();

113
114
/// Print the table of all blocks

defacer's avatar
   
defacer committed
115
116
    $table = new flexible_table('admin-blocks-compatible');

117
118
    $table->define_columns(array('name', 'instances', 'version', 'hideshow', 'delete', 'settings'));
    $table->define_headers(array($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strdelete, $strsettings));
119
    $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
120
    $table->set_attribute('class', 'compatibleblockstable blockstable generaltable');
defacer's avatar
   
defacer committed
121
    $table->setup();
122

123
124
    foreach ($blocks as $blockid=>$block) {
        $blockname = $block->name;
125

126
127
128
        if (!file_exists("$CFG->dirroot/blocks/$blockname/block_$blockname.php")) {
            $blockobject  = false;
            $strblockname = '<span class="notifyproblem">'.$blockname.' ('.get_string('missingfromdisk').')</span>';
129
130
131
            $plugin = new object();
            $plugin->version = $block->version;

132
        } else {
133
134
135
136
137
138
            $plugin = new object();
            $plugin->version = '???';
            if (file_exists("$CFG->dirroot/blocks/$blockname/version.php")) {
                include("$CFG->dirroot/blocks/$blockname/version.php");
            }

139
140
141
142
143
144
            if (!$blockobject  = block_instance($block->name)) {
                $incompatible[] = $block;
                continue;
            }
            $strblockname = get_string('pluginname', 'block_'.$blockname);
        }
145

skodak's avatar
skodak committed
146
        $delete = '<a href="blocks.php?delete='.$blockid.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>';
147
148

        $settings = ''; // By default, no configuration
149
        if ($blockobject and $blockobject->has_config()) {
150
151
152
153
154
            if (file_exists($CFG->dirroot.'/blocks/'.$block->name.'/settings.php')) {
                $settings = '<a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=blocksetting'.$block->name.'">'.$strsettings.'</a>';
            } else {
                $settings = '<a href="block.php?block='.$blockid.'">'.$strsettings.'</a>';
            }
155
156
        }

157
158
        // MDL-11167, blocks can be placed on mymoodle, or the blogs page
        // and it should not show up on course search page
159

160
161
        $totalcount = $DB->count_records('block_instances', array('blockname'=>$blockname));
        $count = $DB->count_records('block_instances', array('blockname'=>$blockname, 'pagetypepattern'=>'course-view-*'));
162

thepurpleblob's avatar
thepurpleblob committed
163
        if ($count>0) {
skodak's avatar
skodak committed
164
            $blocklist = "<a href=\"{$CFG->wwwroot}/course/search.php?blocklist=$blockid&amp;sesskey=".sesskey()."\" ";
165
            $blocklist .= "title=\"$strshowblockcourse\" >$totalcount</a>";
thepurpleblob's avatar
thepurpleblob committed
166
167
        }
        else {
168
            $blocklist = "$totalcount";
thepurpleblob's avatar
thepurpleblob committed
169
        }
170
171
        $class = ''; // Nothing fancy, by default

172
173
174
175
        if (!$blockobject) {
            // ignore
            $visible = '';
        } else if ($blocks[$blockid]->visible) {
skodak's avatar
skodak committed
176
            $visible = '<a href="blocks.php?hide='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strhide.'">'.
177
                       '<img src="'.$OUTPUT->pix_url('i/hide') . '" class="icon" alt="'.$strhide.'" /></a>';
178
        } else {
skodak's avatar
skodak committed
179
            $visible = '<a href="blocks.php?show='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strshow.'">'.
180
                       '<img src="'.$OUTPUT->pix_url('i/show') . '" class="icon" alt="'.$strshow.'" /></a>';
181
182
183
            $class = ' class="dimmed_text"'; // Leading space required!
        }

184
185
186
187
        if ($block->version == $plugin->version) {
            $version = $block->version;
        } else {
            $version = "$block->version ($plugin->version)";
188
        }
189
190


defacer's avatar
   
defacer committed
191
        $table->add_data(array(
192
            '<span'.$class.'>'.$strblockname.'</span>',
thepurpleblob's avatar
thepurpleblob committed
193
            $blocklist,
194
            '<span'.$class.'>'.$version.'</span>',
195
196
197
            $visible,
            $delete,
            $settings
defacer's avatar
   
defacer committed
198
        ));
199
    }
moodler's avatar
moodler committed
200

defacer's avatar
   
defacer committed
201
202
    $table->print_html();

203
    if (!empty($incompatible)) {
204
        echo $OUTPUT->heading(get_string('incompatibleblocks', 'blockstable', 'admin'));
defacer's avatar
   
defacer committed
205
206

        $table = new flexible_table('admin-blocks-incompatible');
207

defacer's avatar
   
defacer committed
208
209
        $table->define_columns(array('block', 'delete'));
        $table->define_headers(array($strname, $strdelete));
210
        $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
211

212
        $table->set_attribute('class', 'incompatibleblockstable generaltable');
213

defacer's avatar
   
defacer committed
214
215
216
217
218
        $table->setup();

        foreach ($incompatible as $block) {
            $table->add_data(array(
                $block->name,
skodak's avatar
skodak committed
219
                '<a href="blocks.php?delete='.$block->id.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>',
defacer's avatar
   
defacer committed
220
221
222
223
            ));
        }
        $table->print_html();
    }
moodler's avatar
moodler committed
224

225
    echo $OUTPUT->footer();
226

227