From fd85ceac57e2d3cee92af5c29d96ea14d4e91449 Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Sun, 20 Mar 2011 12:29:22 +0100 Subject: [PATCH] MDL-26859 improve coding style in blog related blocks --- blocks/blog_menu/block_blog_menu.php | 49 +++++++------- blocks/blog_recent/block_blog_recent.php | 41 +++++++----- blocks/blog_tags/block_blog_tags.php | 83 +++++++++++++++++------- 3 files changed, 113 insertions(+), 60 deletions(-) diff --git a/blocks/blog_menu/block_blog_menu.php b/blocks/blog_menu/block_blog_menu.php index b431a39e702..9b237ecb3ef 100644 --- a/blocks/blog_menu/block_blog_menu.php +++ b/blocks/blog_menu/block_blog_menu.php @@ -1,5 +1,4 @@ . - /** * Blog Menu Block page. * - * @package moodlecore - * @subpackage blog + * @package block + * @subpackage blog_menu * @copyright 2009 Nicolas Connault * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -/** - * Require the blog lib file, several useful functions in there - */ -require_once($CFG->dirroot .'/blog/lib.php'); +defined('MOODLE_INTERNAL') || die(); /** * The blog menu block class @@ -56,27 +51,37 @@ class block_blog_menu extends block_base { } function get_content() { + global $CFG; - // Check if we've already generated content - if (!empty($this->content)) { + // detect if blog enabled + if ($this->content !== NULL) { return $this->content; } - // Prep the content - $this->content = new stdClass; - - /** - * Prepare the content for this block - */ - $options = blog_get_all_options($this->page); - if (count($options)==0) { - // Don't display menu block if block is set at site level, and user is not logged in + if (empty($CFG->bloglevel)) { + $this->content = new stdClass(); $this->content->text = ''; if ($this->page->user_is_editing()) { - // If editing is enabled show an informative message $this->content->text = get_string('blogdisable', 'blog'); } return $this->content; + + } else if ($CFG->bloglevel < BLOG_GLOBAL_LEVEL and (!isloggedin() or isguestuser())) { + $this->content = new stdClass(); + $this->content->text = ''; + return $this->content; + } + + // require necessary libs and get content + require_once($CFG->dirroot .'/blog/lib.php'); + + // Prep the content + $this->content = new stdClass(); + + $options = blog_get_all_options($this->page); + if (count($options) == 0) { + $this->content->text = ''; + return $this->content; } // Iterate the option types @@ -92,9 +97,7 @@ class block_blog_menu extends block_base { // Display the content as a list $this->content->text = html_writer::alist($menulist, array('class'=>'list')); - /** - * Prepare the footer for this block - */ + // Prepare the footer for this block if (has_capability('moodle/blog:search', get_context_instance(CONTEXT_SYSTEM))) { // Full-text search field $form = html_writer::tag('label', get_string('search', 'admin'), array('for'=>'blogsearchquery', 'class'=>'accesshide')); diff --git a/blocks/blog_recent/block_blog_recent.php b/blocks/blog_recent/block_blog_recent.php index 478b4585437..20313b626a0 100644 --- a/blocks/blog_recent/block_blog_recent.php +++ b/blocks/blog_recent/block_blog_recent.php @@ -1,5 +1,4 @@ . - /** * Recent Blog Entries Block page. * - * @package moodlecore - * @subpackage blog + * @package block + * @subpackage blog_recent * @copyright 2009 Nicolas Connault * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -require_once($CFG->dirroot .'/blog/lib.php'); -require_once($CFG->dirroot .'/blog/locallib.php'); +defined('MOODLE_INTERNAL') || die(); /** * This block simply outputs a list of links to recent blog entries, depending on @@ -48,7 +45,29 @@ class block_blog_recent extends block_base { } function get_content() { - global $CFG, $USER, $PAGE, $DB, $OUTPUT; + global $CFG; + + if ($this->content !== NULL) { + return $this->content; + } + + // verify blog is enabled + if (empty($CFG->bloglevel)) { + $this->content = new stdClass(); + $this->content->text = ''; + if ($this->page->user_is_editing()) { + $this->content->text = get_string('blogdisable', 'blog'); + } + return $this->content; + + } else if ($CFG->bloglevel < BLOG_GLOBAL_LEVEL and (!isloggedin() or isguestuser())) { + $this->content = new stdClass(); + $this->content->text = ''; + return $this->content; + } + + require_once($CFG->dirroot .'/blog/lib.php'); + require_once($CFG->dirroot .'/blog/locallib.php'); if (empty($this->config->recentbloginterval)) { $this->config->recentbloginterval = 8400; @@ -58,14 +77,6 @@ class block_blog_recent extends block_base { $this->config->numberofrecentblogentries = 4; } - if (empty($CFG->bloglevel) || ($CFG->bloglevel < BLOG_GLOBAL_LEVEL && !(isloggedin() && !isguestuser()))) { - $this->content->text = ''; - if ($this->page->user_is_editing()) { - $this->content->text = get_string('blogdisable', 'blog'); - } - return $this->content; - } - $this->content = new stdClass(); $this->content->footer = ''; diff --git a/blocks/blog_tags/block_blog_tags.php b/blocks/blog_tags/block_blog_tags.php index a5d82507549..c3fa8d8f2d7 100644 --- a/blocks/blog_tags/block_blog_tags.php +++ b/blocks/blog_tags/block_blog_tags.php @@ -1,12 +1,33 @@ . -//TODO: fix these sloppy constant names or move them elsewhere! +/** + * Blog tags block. + * + * @package block + * @subpackage blog_tags + * @copyright 2006 Shane Elliott + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ -define('BLOGDEFAULTTIMEWITHIN', 90); -define('BLOGDEFAULTNUMBEROFTAGS', 20); -define('BLOGDEFAULTSORT', 'name'); +defined('MOODLE_INTERNAL') || die(); -require_once($CFG->dirroot .'/blog/lib.php'); +define('BLOCK_BLOG_TAGS_DEFAULTTIMEWITHIN', 90); +define('BLOCK_BLOG_TAGS_DEFAULTNUMBEROFTAGS', 20); +define('BLOCK_BLOG_TAGS_DEFAULTSORT', 'name'); class block_blog_tags extends block_base { function init() { @@ -42,29 +63,47 @@ class block_blog_tags extends block_base { function get_content() { global $CFG, $SITE, $USER, $DB, $OUTPUT; - if (empty($CFG->usetags) || empty($CFG->bloglevel)) { + if ($this->content !== NULL) { + return $this->content; + } + + // make sure blog and tags are actually enabled + if (empty($CFG->bloglevel)) { + $this->content = new stdClass(); + $this->content->text = ''; + if ($this->page->user_is_editing()) { + $this->content->text = get_string('blogdisable', 'blog'); + } + return $this->content; + + } else if (empty($CFG->usetags)) { + $this->content = new stdClass(); $this->content->text = ''; if ($this->page->user_is_editing()) { $this->content->text = get_string('tagsaredisabled', 'tag'); } return $this->content; - } - if (empty($this->config->timewithin)) { - $this->config->timewithin = BLOGDEFAULTTIMEWITHIN; - } - if (empty($this->config->numberoftags)) { - $this->config->numberoftags = BLOGDEFAULTNUMBEROFTAGS; - } - if (empty($this->config->sort)) { - $this->config->sort = BLOGDEFAULTSORT; - } - - if ($this->content !== NULL) { + } else if ($CFG->bloglevel < BLOG_GLOBAL_LEVEL and (!isloggedin() or isguestuser())) { + $this->content = new stdClass(); + $this->content->text = ''; return $this->content; } - $this->content = new stdClass; + // require the libs and do the work + require_once($CFG->dirroot .'/blog/lib.php'); + + if (empty($this->config->timewithin)) { + $this->config->timewithin = BLOCK_BLOG_TAGS_DEFAULTTIMEWITHIN; + } + if (empty($this->config->numberoftags)) { + $this->config->numberoftags = BLOCK_BLOG_TAGS_DEFAULTNUMBEROFTAGS; + } + if (empty($this->config->sort)) { + $this->config->sort = BLOCK_BLOG_TAGS_DEFAULTSORT; + } + + $this->content = new stdClass(); $this->content->text = ''; $this->content->footer = ''; @@ -129,7 +168,7 @@ class block_blog_tags extends block_base { /// Now we sort the tag display order $CFG->tagsort = $this->config->sort; - usort($etags, "blog_tags_sort"); + usort($etags, "block_blog_tags_sort"); /// Finally we create the output /// Accessibility: markup as a list. @@ -163,7 +202,7 @@ class block_blog_tags extends block_base { } } -function blog_tags_sort($a, $b) { +function block_blog_tags_sort($a, $b) { global $CFG; if (empty($CFG->tagsort)) { @@ -175,7 +214,7 @@ function blog_tags_sort($a, $b) { if (is_numeric($a->$tagsort)) { return ($a->$tagsort == $b->$tagsort) ? 0 : ($a->$tagsort > $b->$tagsort) ? 1 : -1; } elseif (is_string($a->$tagsort)) { - return strcmp($a->$tagsort, $b->$tagsort); + return strcmp($a->$tagsort, $b->$tagsort); //TODO: this is not compatible with UTF-8!! } else { return 0; }