Skip to content

Commit

Permalink
New naming conventions for class names to bring them in line with the
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
defacer committed Nov 23, 2004
1 parent 905e80d commit e89d741
Show file tree
Hide file tree
Showing 18 changed files with 51 additions and 33 deletions.
28 changes: 23 additions & 5 deletions blocks/HOWTO.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ <h3>Ready, Set, Go!</h3>

<p>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 <span class="filename">/blocks/simplehtml/</span> and creating a file named <span class="filename">/blocks/simplehtml/block_simplehtml.php</span> which will hold our code. We then begin coding the block:</p>

<pre class="code">class CourseBlock_simplehtml extends MoodleBlock {
<pre class="code">class block_simplehtml extends block_base {
function init() {
$this->title = get_string('simplehtml', 'block_simplehtml');
$this->content_type = BLOCK_TYPE_TEXT;
Expand All @@ -118,9 +118,9 @@ <h3>Ready, Set, Go!</h3>

<p>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.</p>

<p>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.</p>
<p>Our class is then given a small method: <a class="function_title" href="#method_init">init</a>. 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.</p>

<p>$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 <a href=#">Part 9</a> how to disable the title's display.</p>
<p>$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 <a href="#section_eye_candy">how to disable the title's display</a>.</p>

<p>$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.</p>

Expand Down Expand Up @@ -413,7 +413,7 @@ <h3>The Effects of Globalization</h3>

</li>

<li>
<li id="section_eye_candy">

<h3>Eye Candy</h3>

Expand Down Expand Up @@ -525,7 +525,7 @@ <h3>Lists and Icons</h3>

<h2 id="appendix_a">Appendix A: Reference</h2>

<p>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.</p>
<p>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.</p>

<p>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.</p>

Expand Down Expand Up @@ -846,6 +846,24 @@ <h2 id="appendix_b">Appendix B: Differences in the Blocks API for Moodle version

<ol>

<li>

<h3>Class naming conventions changed</h3>

<p>In Moodle 1.4, all block classes were required to have a name like <strong>CourseBlock_something</strong> and the base class from which the derived was <strong>MoodleBlock</strong>. 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 <strong>block_something</strong> and derive from <strong>block_base</strong>. This means that in order to make a block compatible with Moodle 1.5, you need to change the class definition

<pre class="code">
class CourseBlock_online_users extends MoodleBlock { ... }
</pre>

<p>to</p>

<pre class="code">
class block_online_users extends block_base { ... }
</pre>

</li>

<li>

<h3>Constructor versus init()</h3>
Expand Down
2 changes: 1 addition & 1 deletion blocks/activity_modules/block_activity_modules.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?PHP //$Id$

class CourseBlock_activity_modules extends MoodleBlock {
class block_activity_modules extends block_base {
function init() {
$this->title = get_string('activities');
$this->content_type = BLOCK_TYPE_LIST;
Expand Down
2 changes: 1 addition & 1 deletion blocks/admin/block_admin.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php //$Id$

class CourseBlock_admin extends MoodleBlock {
class block_admin extends block_base {
function init() {
$this->title = get_string('administration');
$this->content_type = BLOCK_TYPE_LIST;
Expand Down
2 changes: 1 addition & 1 deletion blocks/calendar_month/block_calendar_month.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?PHP //$Id$

class CourseBlock_calendar_month extends MoodleBlock {
class block_calendar_month extends block_base {
function init() {
$this->title = get_string('calendar', 'calendar');
$this->content_type = BLOCK_TYPE_TEXT;
Expand Down
2 changes: 1 addition & 1 deletion blocks/calendar_upcoming/block_calendar_upcoming.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?PHP //$Id$

class CourseBlock_calendar_upcoming extends MoodleBlock {
class block_calendar_upcoming extends block_base {
function init() {
$this->title = get_string('upcomingevents', 'calendar');
$this->content_type = BLOCK_TYPE_TEXT;
Expand Down
2 changes: 1 addition & 1 deletion blocks/course_list/block_course_list.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?PHP //$Id$

class CourseBlock_course_list extends MoodleBlock {
class block_course_list extends block_base {
function init() {
$this->title = get_string('courses');
$this->content_type = BLOCK_TYPE_LIST;
Expand Down
2 changes: 1 addition & 1 deletion blocks/course_summary/block_course_summary.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?PHP //$Id$

class CourseBlock_course_summary extends MoodleBlock {
class block_course_summary extends block_base {
function init() {
$this->title = get_string('pagedescription', 'block_course_summary');
$this->content_type = BLOCK_TYPE_TEXT;
Expand Down
2 changes: 1 addition & 1 deletion blocks/login/block_login.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?PHP //$Id$

class CourseBlock_login extends MoodleBlock {
class block_login extends block_base {
function init() {
$this->title = get_string('login');
$this->content_type = BLOCK_TYPE_TEXT;
Expand Down
8 changes: 4 additions & 4 deletions blocks/moodleblock.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* @author Jon Papaioannou
* @package blocks
*/
class MoodleBlock {
class block_base {

/**
* Internal var for storing/caching translated strings
Expand Down Expand Up @@ -90,7 +90,7 @@ class MoodleBlock {
* The class constructor
*
*/
function MoodleBlock() {
function block_base() {
$this->init();
}

Expand All @@ -99,7 +99,7 @@ function MoodleBlock() {
*
*/
function __construct() {
$this->MoodleBlock();
$this->block_base();
}

/**
Expand Down Expand Up @@ -558,7 +558,7 @@ function instance_config_save($data) {
* @author Jon Papaioannou
* @package blocks
*/
class MoodleBlock_Nuke extends MoodleBlock {
class block_nuke extends block_base {
function get_content() {

if ($this->content !== NULL) {
Expand Down
2 changes: 1 addition & 1 deletion blocks/news_items/block_news_items.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?PHP //$Id$

class CourseBlock_news_items extends MoodleBlock {
class block_news_items extends block_base {
function init() {
$this->title = get_string('latestnews');
$this->content_type = BLOCK_TYPE_TEXT;
Expand Down
2 changes: 1 addition & 1 deletion blocks/online_users/block_online_users.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?PHP //$Id$

class CourseBlock_online_users extends MoodleBlock {
class block_online_users extends block_base {
function init() {
$this->title = get_string('blockname','block_online_users');
$this->content_type = BLOCK_TYPE_TEXT;
Expand Down
2 changes: 1 addition & 1 deletion blocks/participants/block_participants.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?PHP //$Id$

class CourseBlock_participants extends MoodleBlock {
class block_participants extends block_base {
function init() {
$this->title = get_string('people');
$this->content_type = BLOCK_TYPE_LIST;
Expand Down
2 changes: 1 addition & 1 deletion blocks/recent_activity/block_recent_activity.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?PHP //$Id$

class CourseBlock_recent_activity extends MoodleBlock {
class block_recent_activity extends block_base {
function init() {
$this->title = get_string('recentactivity');
$this->content_type = BLOCK_TYPE_TEXT;
Expand Down
2 changes: 1 addition & 1 deletion blocks/search_forums/block_search_forums.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?PHP //$Id$

class CourseBlock_search_forums extends MoodleBlock {
class block_search_forums extends block_base {
function init() {
$this->title = get_string('search', 'forum');
$this->content_type = BLOCK_TYPE_TEXT;
Expand Down
2 changes: 1 addition & 1 deletion blocks/section_links/block_section_links.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?PHP //$Id$

class CourseBlock_section_links extends MoodleBlock {
class block_section_links extends block_base {

function init() {
$this->title = get_string('blockname', 'block_section_links');
Expand Down
2 changes: 1 addition & 1 deletion blocks/site_main_menu/block_site_main_menu.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php //$Id$

class CourseBlock_site_main_menu extends MoodleBlock {
class block_site_main_menu extends block_base {
function init() {
$this->title = get_string('mainmenu');
$this->content_type = BLOCK_TYPE_LIST;
Expand Down
2 changes: 1 addition & 1 deletion blocks/social_activities/block_social_activities.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php //$Id$

class CourseBlock_social_activities extends MoodleBlock {
class block_social_activities extends block_base {
function init(){
$this->title = get_string('blockname','block_social_activities');
$this->content_type = BLOCK_TYPE_LIST;
Expand Down
18 changes: 9 additions & 9 deletions lib/blocklib.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ 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
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);
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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;
}

Expand Down

0 comments on commit e89d741

Please sign in to comment.