From: defacer Date: Tue, 23 Nov 2004 18:53:34 +0000 (+0000) Subject: New naming conventions for class names to bring them in line with the X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=e89d741af301795cbff6fbc220d50160c3937a1e;p=moodle.git New naming conventions for class names to bring them in line with the rest of Moodle. The convention for blocks is now: class block_something extends block_base { ... } HOWTO updated accordingly, plus some more minor polishing. More to come. --- diff --git a/blocks/HOWTO.html b/blocks/HOWTO.html index c2dd411468..832a6cfafe 100644 --- a/blocks/HOWTO.html +++ b/blocks/HOWTO.html @@ -108,7 +108,7 @@

To define a "block" in Moodle, in the most basic case we need to provide just one source code file. We start by creating the directory /blocks/simplehtml/ and creating a file named /blocks/simplehtml/block_simplehtml.php which will hold our code. We then begin coding the block:

-
class CourseBlock_simplehtml extends MoodleBlock {
+    
class block_simplehtml extends block_base {
     function init() {
         $this->title = get_string('simplehtml', 'block_simplehtml');
         $this->content_type = BLOCK_TYPE_TEXT;
@@ -118,9 +118,9 @@
 
     

The first line is our block class definition; it must be named exactly in the manner shown. Again, only the "simplehtml" part can (and indeed must) change; everything else is standardized.

-

Our class is then given a small method: init(). This is essential for all blocks, and its function is to set the three class member variables listed inside it. But what do these values actually mean? Here's a more detailed description.

+

Our class is then given a small method: init. This is essential for all blocks, and its function is to set the three class member variables listed inside it. But what do these values actually mean? Here's a more detailed description.

-

$this->title is the title displayed in the header of our block. We can set it to whatever we like; in this case it's set to read the actual title from a language file we are presumably distributing together with the block. I 'll skip ahead a bit here and say that if you want your block to display NO title at all, then you should set this to any descriptive value you want (but NOT make it empty). We will later see in Part 9 how to disable the title's display.

+

$this->title is the title displayed in the header of our block. We can set it to whatever we like; in this case it's set to read the actual title from a language file we are presumably distributing together with the block. I 'll skip ahead a bit here and say that if you want your block to display NO title at all, then you should set this to any descriptive value you want (but NOT make it empty). We will later see how to disable the title's display.

$this->content_type tells Moodle what kind of content to expect from this block. Here we have two simple choices. Either we set content_type to BLOCK_TYPE_TEXT, which tells Moodle to just take our content and display it on screen as-is; or we set it to BLOCK_TYPE_LIST, which tells Moodle that we want our block to display a nicely formatted list of items with optional icons next to each one. We can use BLOCK_TYPE_TEXT to manually create any content we want (do not be fooled by the name; HTML is allowed in the block's content without restriction) or use BLOCK_TYPE_LIST to easily create a simple menu.

@@ -413,7 +413,7 @@ function instance_config_save($data) { -
  • +
  • Eye Candy

    @@ -525,7 +525,7 @@ function get_content() {

    Appendix A: Reference

    -

    This Appendix will discuss the base class MoodleBlock from which all other block classes derive, and present each and every method that can be overridden by block developers in detail. Methods that should NOT be overridden are explicitly referred to as such. After reading this Appendix, you will have a clear understanding of every method which you should or could override to implement functionality for your block.

    +

    This Appendix will discuss the base class block_base from which all other block classes derive, and present each and every method that can be overridden by block developers in detail. Methods that should NOT be overridden are explicitly referred to as such. After reading this Appendix, you will have a clear understanding of every method which you should or could override to implement functionality for your block.

    The methods are divided into three categories: those you may use and override in your block, those that you may NOT override but might want to use, and those that should NEITHER be used NOR overridden. In each category, methods are presented in alphabetical order.

    @@ -846,6 +846,24 @@ function specialization() {
      +
    1. + +

      Class naming conventions changed

      + +

      In Moodle 1.4, all block classes were required to have a name like CourseBlock_something and the base class from which the derived was MoodleBlock. This has changed in Moodle 1.5, to bring the naming conventions in line with other object-oriented aspects of Moodle (for example there are classes enrolment_base, resource_base etc). The new block classes should instead be named like block_something and derive from block_base. This means that in order to make a block compatible with Moodle 1.5, you need to change the class definition + +

      +class CourseBlock_online_users extends MoodleBlock { ... }
      +
      + +

      to

      + +
      +class block_online_users extends block_base { ... }
      +
      + +
    2. +
    3. Constructor versus init()

      diff --git a/blocks/activity_modules/block_activity_modules.php b/blocks/activity_modules/block_activity_modules.php index c8a1b757ae..1cddbae615 100644 --- a/blocks/activity_modules/block_activity_modules.php +++ b/blocks/activity_modules/block_activity_modules.php @@ -1,6 +1,6 @@ title = get_string('activities'); $this->content_type = BLOCK_TYPE_LIST; diff --git a/blocks/admin/block_admin.php b/blocks/admin/block_admin.php index a7a6405c30..71100875f3 100644 --- a/blocks/admin/block_admin.php +++ b/blocks/admin/block_admin.php @@ -1,6 +1,6 @@ title = get_string('administration'); $this->content_type = BLOCK_TYPE_LIST; diff --git a/blocks/calendar_month/block_calendar_month.php b/blocks/calendar_month/block_calendar_month.php index defb8bdc8d..244c1f1611 100644 --- a/blocks/calendar_month/block_calendar_month.php +++ b/blocks/calendar_month/block_calendar_month.php @@ -1,6 +1,6 @@ title = get_string('calendar', 'calendar'); $this->content_type = BLOCK_TYPE_TEXT; diff --git a/blocks/calendar_upcoming/block_calendar_upcoming.php b/blocks/calendar_upcoming/block_calendar_upcoming.php index 15dff0111e..e0aee8fe05 100644 --- a/blocks/calendar_upcoming/block_calendar_upcoming.php +++ b/blocks/calendar_upcoming/block_calendar_upcoming.php @@ -1,6 +1,6 @@ title = get_string('upcomingevents', 'calendar'); $this->content_type = BLOCK_TYPE_TEXT; diff --git a/blocks/course_list/block_course_list.php b/blocks/course_list/block_course_list.php index 88b5c4b45e..d7735dfcfe 100644 --- a/blocks/course_list/block_course_list.php +++ b/blocks/course_list/block_course_list.php @@ -1,6 +1,6 @@ title = get_string('courses'); $this->content_type = BLOCK_TYPE_LIST; diff --git a/blocks/course_summary/block_course_summary.php b/blocks/course_summary/block_course_summary.php index 9639735e38..24b7dbffc4 100644 --- a/blocks/course_summary/block_course_summary.php +++ b/blocks/course_summary/block_course_summary.php @@ -1,6 +1,6 @@ title = get_string('pagedescription', 'block_course_summary'); $this->content_type = BLOCK_TYPE_TEXT; diff --git a/blocks/login/block_login.php b/blocks/login/block_login.php index 7919ad3854..bdc02741e0 100644 --- a/blocks/login/block_login.php +++ b/blocks/login/block_login.php @@ -1,6 +1,6 @@ title = get_string('login'); $this->content_type = BLOCK_TYPE_TEXT; diff --git a/blocks/moodleblock.class.php b/blocks/moodleblock.class.php index 8585057b5a..d7267ba356 100644 --- a/blocks/moodleblock.class.php +++ b/blocks/moodleblock.class.php @@ -33,7 +33,7 @@ define('BLOCK_TYPE_NUKE', 3); * @author Jon Papaioannou * @package blocks */ -class MoodleBlock { +class block_base { /** * Internal var for storing/caching translated strings @@ -90,7 +90,7 @@ class MoodleBlock { * The class constructor * */ - function MoodleBlock() { + function block_base() { $this->init(); } @@ -99,7 +99,7 @@ class MoodleBlock { * */ function __construct() { - $this->MoodleBlock(); + $this->block_base(); } /** @@ -558,7 +558,7 @@ class MoodleBlock { * @author Jon Papaioannou * @package blocks */ -class MoodleBlock_Nuke extends MoodleBlock { +class block_nuke extends block_base { 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 b896f2d750..50e07b677b 100644 --- a/blocks/news_items/block_news_items.php +++ b/blocks/news_items/block_news_items.php @@ -1,6 +1,6 @@ title = get_string('latestnews'); $this->content_type = BLOCK_TYPE_TEXT; diff --git a/blocks/online_users/block_online_users.php b/blocks/online_users/block_online_users.php index 53c80548df..18afee531b 100644 --- a/blocks/online_users/block_online_users.php +++ b/blocks/online_users/block_online_users.php @@ -1,6 +1,6 @@ title = get_string('blockname','block_online_users'); $this->content_type = BLOCK_TYPE_TEXT; diff --git a/blocks/participants/block_participants.php b/blocks/participants/block_participants.php index 02e181bc96..f709e224a6 100644 --- a/blocks/participants/block_participants.php +++ b/blocks/participants/block_participants.php @@ -1,6 +1,6 @@ title = get_string('people'); $this->content_type = BLOCK_TYPE_LIST; diff --git a/blocks/recent_activity/block_recent_activity.php b/blocks/recent_activity/block_recent_activity.php index 4a64ddbe62..31305ff561 100644 --- a/blocks/recent_activity/block_recent_activity.php +++ b/blocks/recent_activity/block_recent_activity.php @@ -1,6 +1,6 @@ title = get_string('recentactivity'); $this->content_type = BLOCK_TYPE_TEXT; diff --git a/blocks/search_forums/block_search_forums.php b/blocks/search_forums/block_search_forums.php index 75c78fdb69..dffe1ff800 100644 --- a/blocks/search_forums/block_search_forums.php +++ b/blocks/search_forums/block_search_forums.php @@ -1,6 +1,6 @@ title = get_string('search', 'forum'); $this->content_type = BLOCK_TYPE_TEXT; diff --git a/blocks/section_links/block_section_links.php b/blocks/section_links/block_section_links.php index ec4dfc8449..e1fda967ea 100644 --- a/blocks/section_links/block_section_links.php +++ b/blocks/section_links/block_section_links.php @@ -1,6 +1,6 @@ title = get_string('blockname', 'block_section_links'); diff --git a/blocks/site_main_menu/block_site_main_menu.php b/blocks/site_main_menu/block_site_main_menu.php index 72bd588cf7..f6acc69402 100644 --- a/blocks/site_main_menu/block_site_main_menu.php +++ b/blocks/site_main_menu/block_site_main_menu.php @@ -1,6 +1,6 @@ title = get_string('mainmenu'); $this->content_type = BLOCK_TYPE_LIST; diff --git a/blocks/social_activities/block_social_activities.php b/blocks/social_activities/block_social_activities.php index bb980e634e..d56a6a2aab 100644 --- a/blocks/social_activities/block_social_activities.php +++ b/blocks/social_activities/block_social_activities.php @@ -1,6 +1,6 @@ title = get_string('blockname','block_social_activities'); $this->content_type = BLOCK_TYPE_LIST; diff --git a/lib/blocklib.php b/lib/blocklib.php index 3bfac65b6e..59af30d340 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -61,7 +61,7 @@ function block_method_result($blockname, $method) { if(!block_load_class($blockname)) { return NULL; } - return eval('return CourseBlock_'.$blockname.'::'.$method.'();'); + return eval('return block_'.$blockname.'::'.$method.'();'); } //This function creates a new object of the specified block class @@ -69,7 +69,7 @@ function block_instance($blockname, $instance = NULL) { if(!block_load_class($blockname)) { return false; } - $classname = 'CourseBlock_'.$blockname; + $classname = 'block_'.$blockname; $retval = new $classname; if($instance !== NULL) { $retval->load_instance($instance); @@ -83,7 +83,7 @@ function block_load_class($blockname) { global $CFG; @include_once($CFG->dirroot.'/blocks/moodleblock.class.php'); - $classname = 'CourseBlock_'.$blockname; + $classname = 'block_'.$blockname; @include_once($CFG->dirroot.'/blocks/'.$blockname.'/block_'.$blockname.'.php'); // After all this, return value indicating success or failure @@ -677,8 +677,8 @@ function upgrade_blocks_plugins($continueto) { } include_once($CFG->dirroot .'/blocks/moodleblock.class.php'); - if(!class_exists('moodleblock')) { - error('Class MoodleBlock is not defined or file not found for /blocks/moodleblock.class.php'); + if(!class_exists('block_base')) { + error('Class block_base is not defined or file not found for /blocks/moodleblock.class.php'); } foreach ($blocks as $blockname) { @@ -705,7 +705,7 @@ function upgrade_blocks_plugins($continueto) { } } - $classname = 'CourseBlock_'.$blockname; + $classname = 'block_'.$blockname; if(!class_exists($classname)) { $notices[] = 'Block '. $blockname .': '. $classname .' not implemented'; continue; @@ -732,9 +732,9 @@ function upgrade_blocks_plugins($continueto) { $block = new stdClass; // This may be used to update the db below $blockobj = new $classname; // This is what we 'll be testing - // Inherits from MoodleBlock? - if(!is_subclass_of($blockobj, 'moodleblock')) { - $notices[] = 'Block '. $blockname .': class does not inherit from MoodleBlock'; + // Inherits from block_base? + if(!is_subclass_of($blockobj, 'block_base')) { + $notices[] = 'Block '. $blockname .': class does not inherit from block_base'; continue; }