From 3ef642d96ad2779e1cec54f24e83c3dd5dd8c042 Mon Sep 17 00:00:00 2001 From: defacer Date: Wed, 2 Mar 2005 19:22:26 +0000 Subject: [PATCH] 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 --- .../block_activity_modules.php | 3 +- blocks/admin/block_admin.php | 3 +- .../calendar_month/block_calendar_month.php | 1 - .../block_calendar_upcoming.php | 1 - blocks/course_list/block_course_list.php | 3 +- .../course_summary/block_course_summary.php | 1 - blocks/html/block_html.php | 1 - blocks/login/block_login.php | 1 - blocks/messages/block_messages.php | 1 - blocks/moodleblock.class.php | 124 +++++++++++------- blocks/news_items/block_news_items.php | 1 - blocks/online_users/block_online_users.php | 1 - blocks/participants/block_participants.php | 3 +- blocks/quiz_results/block_quiz_results.php | 1 - .../recent_activity/block_recent_activity.php | 1 - blocks/rss_client/block_rss_client.php | 1 - blocks/search_forums/block_search_forums.php | 1 - blocks/section_links/block_section_links.php | 1 - .../site_main_menu/block_site_main_menu.php | 3 +- .../block_social_activities.php | 3 +- lib/blocklib.php | 22 +--- 21 files changed, 88 insertions(+), 89 deletions(-) diff --git a/blocks/activity_modules/block_activity_modules.php b/blocks/activity_modules/block_activity_modules.php index 1cddbae615b..89baf13f8da 100644 --- a/blocks/activity_modules/block_activity_modules.php +++ b/blocks/activity_modules/block_activity_modules.php @@ -1,9 +1,8 @@ title = get_string('activities'); - $this->content_type = BLOCK_TYPE_LIST; $this->version = 2004041000; } diff --git a/blocks/admin/block_admin.php b/blocks/admin/block_admin.php index eb0a96c9fa5..1b8ce2eaf91 100644 --- a/blocks/admin/block_admin.php +++ b/blocks/admin/block_admin.php @@ -1,9 +1,8 @@ title = get_string('administration'); - $this->content_type = BLOCK_TYPE_LIST; $this->version = 2004081200; } diff --git a/blocks/calendar_month/block_calendar_month.php b/blocks/calendar_month/block_calendar_month.php index 02e38fbdb7a..83f4fcd8ecf 100644 --- a/blocks/calendar_month/block_calendar_month.php +++ b/blocks/calendar_month/block_calendar_month.php @@ -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; } diff --git a/blocks/calendar_upcoming/block_calendar_upcoming.php b/blocks/calendar_upcoming/block_calendar_upcoming.php index e0aee8fe053..a120ccc0f9b 100644 --- a/blocks/calendar_upcoming/block_calendar_upcoming.php +++ b/blocks/calendar_upcoming/block_calendar_upcoming.php @@ -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; } diff --git a/blocks/course_list/block_course_list.php b/blocks/course_list/block_course_list.php index 92e4d667b7e..f94799a6baf 100644 --- a/blocks/course_list/block_course_list.php +++ b/blocks/course_list/block_course_list.php @@ -1,9 +1,8 @@ title = get_string('courses'); - $this->content_type = BLOCK_TYPE_LIST; $this->version = 2004111600; } diff --git a/blocks/course_summary/block_course_summary.php b/blocks/course_summary/block_course_summary.php index fc4ed93f0c0..1ba126e3f6f 100644 --- a/blocks/course_summary/block_course_summary.php +++ b/blocks/course_summary/block_course_summary.php @@ -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; } diff --git a/blocks/html/block_html.php b/blocks/html/block_html.php index 46a676107f5..feb96a9d829 100755 --- a/blocks/html/block_html.php +++ b/blocks/html/block_html.php @@ -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; } diff --git a/blocks/login/block_login.php b/blocks/login/block_login.php index 7fe7381fd69..1f5f5e4014c 100644 --- a/blocks/login/block_login.php +++ b/blocks/login/block_login.php @@ -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; } diff --git a/blocks/messages/block_messages.php b/blocks/messages/block_messages.php index 5293c1d69d5..0f4685afceb 100644 --- a/blocks/messages/block_messages.php +++ b/blocks/messages/block_messages.php @@ -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; } diff --git a/blocks/moodleblock.class.php b/blocks/moodleblock.class.php index 84f813a88bb..2c1e695563a 100644 --- a/blocks/moodleblock.class.php +++ b/blocks/moodleblock.class.php @@ -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 = '
'.$this->title.'
'; + 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) { diff --git a/blocks/news_items/block_news_items.php b/blocks/news_items/block_news_items.php index 1505d413bd5..ce668a95853 100644 --- a/blocks/news_items/block_news_items.php +++ b/blocks/news_items/block_news_items.php @@ -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; } diff --git a/blocks/online_users/block_online_users.php b/blocks/online_users/block_online_users.php index c69bc9f3ce9..d17909ba89c 100644 --- a/blocks/online_users/block_online_users.php +++ b/blocks/online_users/block_online_users.php @@ -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; } diff --git a/blocks/participants/block_participants.php b/blocks/participants/block_participants.php index 476a09f87b3..e0d3d0bb63e 100644 --- a/blocks/participants/block_participants.php +++ b/blocks/participants/block_participants.php @@ -1,9 +1,8 @@ title = get_string('people'); - $this->content_type = BLOCK_TYPE_LIST; $this->version = 2004052600; } diff --git a/blocks/quiz_results/block_quiz_results.php b/blocks/quiz_results/block_quiz_results.php index 61901c4a053..1ee3ce38f03 100644 --- a/blocks/quiz_results/block_quiz_results.php +++ b/blocks/quiz_results/block_quiz_results.php @@ -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; } diff --git a/blocks/recent_activity/block_recent_activity.php b/blocks/recent_activity/block_recent_activity.php index 31305ff5611..0c293cecc7f 100644 --- a/blocks/recent_activity/block_recent_activity.php +++ b/blocks/recent_activity/block_recent_activity.php @@ -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; } diff --git a/blocks/rss_client/block_rss_client.php b/blocks/rss_client/block_rss_client.php index c9233e3011d..a5fb531ca36 100644 --- a/blocks/rss_client/block_rss_client.php +++ b/blocks/rss_client/block_rss_client.php @@ -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; } diff --git a/blocks/search_forums/block_search_forums.php b/blocks/search_forums/block_search_forums.php index a38f374bb91..bf114a2f83b 100644 --- a/blocks/search_forums/block_search_forums.php +++ b/blocks/search_forums/block_search_forums.php @@ -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; } diff --git a/blocks/section_links/block_section_links.php b/blocks/section_links/block_section_links.php index de6de78d4d6..df68d79df7d 100644 --- a/blocks/section_links/block_section_links.php +++ b/blocks/section_links/block_section_links.php @@ -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; } diff --git a/blocks/site_main_menu/block_site_main_menu.php b/blocks/site_main_menu/block_site_main_menu.php index a030e9c0e98..b0786a1e855 100644 --- a/blocks/site_main_menu/block_site_main_menu.php +++ b/blocks/site_main_menu/block_site_main_menu.php @@ -1,9 +1,8 @@ title = get_string('mainmenu'); - $this->content_type = BLOCK_TYPE_LIST; $this->version = 2004052700; } diff --git a/blocks/social_activities/block_social_activities.php b/blocks/social_activities/block_social_activities.php index 706a5a72bb8..913ff92ac2d 100644 --- a/blocks/social_activities/block_social_activities.php +++ b/blocks/social_activities/block_social_activities.php @@ -1,9 +1,8 @@ title = get_string('blockname','block_social_activities'); - $this->content_type = BLOCK_TYPE_LIST; $this->version = 2004041800; } diff --git a/lib/blocklib.php b/lib/blocklib.php index 0215ff46640..a9e00eaa8c5 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -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; } }