blocks.php 8.13 KB
Newer Older
1
2
3
4
5
<?PHP // $Id$

    // 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
        admin_externalpage_print_header();
48
        echo $OUTPUT->heading($strmanageblocks);
49
50

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

defacer's avatar
   
defacer committed
54
55
56
57
        else {
            $blockobject = block_instance($block->name);
            $strblockname = $blockobject->get_title();
        }
58

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

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

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

81
            // Delete block
skodak's avatar
skodak committed
82
            if (!$DB->delete_records('block', array('id'=>$block->id))) {
83
84
85
                notify("Error occurred while deleting the $strblockname record from blocks table");
            }

86
87
88
            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);

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

92
93
94
            // remove entent handlers and dequeue pending events
            events_uninstall('block/'.$block->name);

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

101
    admin_externalpage_print_header();
102
    echo $OUTPUT->heading($strmanageblocks);
103

104
105
106
107
/// Main display starts here

/// Get and sort the existing blocks

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

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

114
    foreach ($blocks as $block) {
skodak's avatar
skodak committed
115
        if (($blockobject = block_instance($block->name)) === false) {
116
117
118
119
120
121
            // Failed to load
            continue;
        }
        $blockbyname[$blockobject->get_title()] = $block->id;
        $blockobjects[$block->id] = $blockobject;
    }
defacer's avatar
defacer committed
122
123

    if(empty($blockbyname)) {
dongsheng's avatar
dongsheng committed
124
        print_error('failtoloadblocks', 'error');
defacer's avatar
defacer committed
125
126
    }

127
128
129
130
    ksort($blockbyname);

/// Print the table of all blocks

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

133
134
    $table->define_columns(array('name', 'instances', 'version', 'hideshow', 'delete', 'settings'));
    $table->define_headers(array($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strdelete, $strsettings));
135
    $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
defacer's avatar
   
defacer committed
136
    $table->set_attribute('id', 'blocks');
137
    $table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide');
defacer's avatar
   
defacer committed
138
    $table->setup();
139
140
141
142

    foreach ($blockbyname as $blockname => $blockid) {

        $blockobject = $blockobjects[$blockid];
143
        $block       = $blocks[$blockid];
144

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

        $settings = ''; // By default, no configuration
148
149
150
151
152
153
        if ($blockobject->has_config()) {
            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>';
            }
154
155
        }

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

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

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

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

defacer's avatar
   
defacer committed
180
        $table->add_data(array(
moodler's avatar
moodler committed
181
            '<span'.$class.'>'.$blockobject->get_title().'</span>',
thepurpleblob's avatar
thepurpleblob committed
182
            $blocklist,
183
            '<span'.$class.'>'.$blockobject->get_version().'</span>',
184
185
186
            $visible,
            $delete,
            $settings
defacer's avatar
   
defacer committed
187
        ));
188
    }
moodler's avatar
moodler committed
189

defacer's avatar
   
defacer committed
190
191
192
    $table->print_html();

    if(!empty($incompatible)) {
193
        echo $OUTPUT->heading(get_string('incompatibleblocks', 'admin'));
defacer's avatar
   
defacer committed
194
195

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

defacer's avatar
   
defacer committed
197
198
        $table->define_columns(array('block', 'delete'));
        $table->define_headers(array($strname, $strdelete));
199
        $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
200

defacer's avatar
   
defacer committed
201
        $table->set_attribute('id', 'incompatible');
202
        $table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide');
203

defacer's avatar
   
defacer committed
204
205
206
207
208
        $table->setup();

        foreach ($incompatible as $block) {
            $table->add_data(array(
                $block->name,
skodak's avatar
skodak committed
209
                '<a href="blocks.php?delete='.$block->id.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>',
defacer's avatar
   
defacer committed
210
211
212
213
            ));
        }
        $table->print_html();
    }
moodler's avatar
moodler committed
214

215
    echo $OUTPUT->footer();
216
217

?>