Implemented discussion from bug 2547:

* Block authors no longer need to set $this->content_type
* If you want to make a "list" block, just derive from block_list
  instead of block_base
* All switch() statements gone from library code
* Overloaded method is_empty() added to block classes for convenience
This commit is contained in:
defacer 2005-03-02 19:22:26 +00:00
parent 4e5f28b6a1
commit 3ef642d96a
21 changed files with 88 additions and 89 deletions

View File

@ -1,9 +1,8 @@
<?PHP //$Id$
class block_activity_modules extends block_base {
class block_activity_modules extends block_list {
function init() {
$this->title = get_string('activities');
$this->content_type = BLOCK_TYPE_LIST;
$this->version = 2004041000;
}

View File

@ -1,9 +1,8 @@
<?php //$Id$
class block_admin extends block_base {
class block_admin extends block_list {
function init() {
$this->title = get_string('administration');
$this->content_type = BLOCK_TYPE_LIST;
$this->version = 2004081200;
}

View File

@ -3,7 +3,6 @@
class block_calendar_month extends block_base {
function init() {
$this->title = get_string('calendar', 'calendar');
$this->content_type = BLOCK_TYPE_TEXT;
$this->version = 2004081200;
}

View File

@ -3,7 +3,6 @@
class block_calendar_upcoming extends block_base {
function init() {
$this->title = get_string('upcomingevents', 'calendar');
$this->content_type = BLOCK_TYPE_TEXT;
$this->version = 2004052600;
}

View File

@ -1,9 +1,8 @@
<?PHP //$Id$
class block_course_list extends block_base {
class block_course_list extends block_list {
function init() {
$this->title = get_string('courses');
$this->content_type = BLOCK_TYPE_LIST;
$this->version = 2004111600;
}

View File

@ -3,7 +3,6 @@
class block_course_summary extends block_base {
function init() {
$this->title = get_string('pagedescription', 'block_course_summary');
$this->content_type = BLOCK_TYPE_TEXT;
$this->version = 2004052600;
}

View File

@ -4,7 +4,6 @@ class block_html extends block_base {
function init() {
$this->title = get_string('html', 'block_html');
$this->content_type = BLOCK_TYPE_TEXT;
$this->version = 2004123000;
}

View File

@ -3,7 +3,6 @@
class block_login extends block_base {
function init() {
$this->title = get_string('login');
$this->content_type = BLOCK_TYPE_TEXT;
$this->version = 2004081600;
}

View File

@ -3,7 +3,6 @@
class block_messages extends block_base {
function init() {
$this->title = get_string('messages','message');
$this->content_type = BLOCK_TYPE_TEXT;
$this->version = 2004122800;
}

View File

@ -51,7 +51,7 @@ class block_base {
* The type of content that this block creates. Currently support options - BLOCK_TYPE_LIST, BLOCK_TYPE_TEXT, BLOCK_TYPE_NUKE
* @var int $content_type
*/
var $content_type = NULL;
var $content_type = BLOCK_TYPE_TEXT;
/**
* An object to contain the information to be displayed in the block.
@ -170,6 +170,16 @@ class block_base {
return $this->version;
}
/**
* Returns true or false, depending on whether this block has any content to display
*
* @return boolean
*/
function is_empty() {
$this->get_content();
return(empty($this->content->text) && empty($this->content->footer));
}
/**
* First sets the current value of $this->content to NULL
* then calls the block's {@link get_content()} function
@ -194,51 +204,23 @@ class block_base {
$title .= $this->edit_controls;
}
$this->get_content();
if (!isset($this->content->footer)) {
$this->content->footer = '';
}
switch($this->content_type) {
case BLOCK_TYPE_NUKE:
case BLOCK_TYPE_TEXT:
if (empty($this->content->text) && empty($this->content->footer)) {
if (empty($this->edit_controls)) {
// No content, no edit controls, so just shut up
break;
} else {
// No content but editing, so show something at least
$this->_print_shadow();
}
} else {
if ($this->hide_header() && empty($this->edit_controls)) {
// Header wants to hide, no edit controls to show, so no header it is
print_side_block(NULL, $this->content->text, NULL, NULL, $this->content->footer, $this->html_attributes());
} else {
// The full treatment, please
print_side_block($title, $this->content->text, NULL, NULL, $this->content->footer, $this->html_attributes());
}
}
break;
case BLOCK_TYPE_LIST:
if (empty($this->content->items) && empty($this->content->footer)) {
if (empty($this->edit_controls)) {
// No content, no edit controls, so just shut up
break;
} else {
// No content but editing, so show something at least
$this->_print_shadow();
}
} else {
if ($this->hide_header() && empty($this->edit_controls)) {
// Header wants to hide, no edit controls to show, so no header it is
print_side_block(NULL, '', $this->content->items, $this->content->icons, $this->content->footer, $this->html_attributes());
} else {
// The full treatment, please
print_side_block($title, '', $this->content->items, $this->content->icons, $this->content->footer, $this->html_attributes());
}
}
break;
// is_empty() includes a call to get_content()
if ($this->is_empty()) {
if (empty($this->edit_controls)) {
// No content, no edit controls, so just shut up
break;
} else {
// No content but editing, so show something at least
$this->_print_shadow();
}
} else {
if ($this->hide_header() && empty($this->edit_controls)) {
// Header wants to hide, no edit controls to show, so no header it is
print_side_block(NULL, $this->content->text, NULL, NULL, $this->content->footer, $this->html_attributes());
} else {
// The full treatment, please
print_side_block($title, $this->content->text, NULL, NULL, $this->content->footer, $this->html_attributes());
}
}
}
@ -555,12 +537,60 @@ class block_base {
}
/**
* Class for supporting a postnuke style block as a moodle block
* Specialized class for displaying a block with a list of icons/text labels
*
* @author Jon Papaioannou
* @package blocks
*/
class block_list extends block_base {
var $content_type = BLOCK_TYPE_LIST;
function is_empty() {
$this->get_content();
return (empty($this->content->items) && empty($this->content->footer));
}
function _print_block() {
// Wrap the title in a floating DIV, in case we have edit controls to display
// These controls will always be wrapped on a right-floating DIV
$title = '<div style="float: left;">'.$this->title.'</div>';
if ($this->edit_controls !== NULL) {
$title .= $this->edit_controls;
}
// is_empty() includes a call to get_content()
if ($this->is_empty()) {
if (empty($this->edit_controls)) {
// No content, no edit controls, so just shut up
break;
} else {
// No content but editing, so show something at least
$this->_print_shadow();
}
} else {
if ($this->hide_header() && empty($this->edit_controls)) {
// Header wants to hide, no edit controls to show, so no header it is
print_side_block(NULL, '', $this->content->items, $this->content->icons, $this->content->footer, $this->html_attributes());
} else {
// The full treatment, please
print_side_block($title, '', $this->content->items, $this->content->icons, $this->content->footer, $this->html_attributes());
}
}
}
}
/**
* Class for supporting a phpnuke style block as a moodle block
*
* @author Jon Papaioannou
* @package blocks
*/
class block_nuke extends block_base {
var $content_type = BLOCK_TYPE_NUKE;
function get_content() {
if ($this->content !== NULL) {

View File

@ -3,7 +3,6 @@
class block_news_items extends block_base {
function init() {
$this->title = get_string('latestnews');
$this->content_type = BLOCK_TYPE_TEXT;
$this->version = 2004052600;
}

View File

@ -3,7 +3,6 @@
class block_online_users extends block_base {
function init() {
$this->title = get_string('blockname','block_online_users');
$this->content_type = BLOCK_TYPE_TEXT;
$this->version = 2004111600;
}

View File

@ -1,9 +1,8 @@
<?PHP //$Id$
class block_participants extends block_base {
class block_participants extends block_list {
function init() {
$this->title = get_string('people');
$this->content_type = BLOCK_TYPE_LIST;
$this->version = 2004052600;
}

View File

@ -7,7 +7,6 @@ define('GRADE_FORMAT_ABS', 3);
class block_quiz_results extends block_base {
function init() {
$this->title = get_string('formaltitle', 'block_quiz_results');
$this->content_type = BLOCK_TYPE_TEXT;
$this->version = 2005012600;
}

View File

@ -3,7 +3,6 @@
class block_recent_activity extends block_base {
function init() {
$this->title = get_string('recentactivity');
$this->content_type = BLOCK_TYPE_TEXT;
$this->version = 2004042900;
}

View File

@ -8,7 +8,6 @@ class block_rss_client extends block_base {
function init() {
$this->title = get_string('block_rss_feeds_title', 'block_rss_client');
$this->content_type = BLOCK_TYPE_TEXT;
$this->version = 2004112000;
}

View File

@ -3,7 +3,6 @@
class block_search_forums extends block_base {
function init() {
$this->title = get_string('search', 'forum');
$this->content_type = BLOCK_TYPE_TEXT;
$this->version = 2004041000;
}

View File

@ -4,7 +4,6 @@ class block_section_links extends block_base {
function init() {
$this->title = get_string('blockname', 'block_section_links');
$this->content_type = BLOCK_TYPE_TEXT;
$this->version = 2004052800;
}

View File

@ -1,9 +1,8 @@
<?php //$Id$
class block_site_main_menu extends block_base {
class block_site_main_menu extends block_list {
function init() {
$this->title = get_string('mainmenu');
$this->content_type = BLOCK_TYPE_LIST;
$this->version = 2004052700;
}

View File

@ -1,9 +1,8 @@
<?php //$Id$
class block_social_activities extends block_base {
class block_social_activities extends block_list {
function init(){
$this->title = get_string('blockname','block_social_activities');
$this->content_type = BLOCK_TYPE_LIST;
$this->version = 2004041800;
}

View File

@ -171,29 +171,17 @@ function blocks_delete_instance($instance) {
// by reference for speed; the array is actually not modified.
function blocks_have_content(&$pageblocks, $position) {
foreach($pageblocks[$position] as $instance) {
if (!$instance->visible) {
if(!$instance->visible) {
continue;
}
if (!$record = blocks_get_record($instance->blockid)) {
if(!$record = blocks_get_record($instance->blockid)) {
continue;
}
if (!$obj = block_instance($record->name, $instance)) {
if(!$obj = block_instance($record->name, $instance)) {
continue;
}
$content = $obj->get_content();
$type = $obj->get_content_type();
switch($type) {
case BLOCK_TYPE_LIST:
if(!empty($content->items) || !empty($content->footer)) {
return true;
}
break;
case BLOCK_TYPE_TEXT:
case BLOCK_TYPE_NUKE:
if(!empty($content->text) || !empty($content->footer)) {
return true;
}
break;
if(!$obj->is_empty()) {
return true;
}
}