Default Blocks

Use the block configuration in config.php to determine which blocks should be added automatically, e.g. upon course creation.

If you do not want any blocks to appear, you cannot use an empty string, but you can just fill in a colon. E.g.:

$CFG->defaultblocks_override = ':';

The colon actually is a separator: any blocks listed before the colon will appear in the left column, any after that in the right column.

If you really don't want any blocks at all, “unlock” the Navigation and Settings blocks under Site Administration > Plugins > Blocks > Manage Blocks. Don't do this in a production environment, however, because I had some issues getting these default blocks back after relocking them.

Developing Blocks

In Moodle 2.3, all blocks must have a method get_content. This method must memoize its result, because it's called twice in the same page request. The first time by block_base#formatted_contents and the second time by block_base#is_empty (both functions are called from block_base#get_content_for_output).

Blocks in the Middle Column

If you want to put a block in the “middle” of the screen you can only do that in the “My home” page (formerly “My Moodle”).

But if you want blocks in the middle column inside a course, you need to make a few changes to standard Moodle. Here's how you that:

Inside lib/blocklib.php

define('BLOCK_POS_RIGHT', 'side-post');
// NEW:
define('BLOCK_POS_CENTER', 'center');

Inside theme/yourtheme/layout/general.php and theme/yourtheme/layout/frontpage.php:

Add this code around the start of the files:

$hascenter  = (empty($PAGE->layout_options['noblocks']) && $PAGE->blocks->region_has_content('center', $OUTPUT));

And right after: <div id=“page-content”>

Insert this:

<?php if ($hascenter) { ?>
   <div class="center-region">
      <?php echo $OUTPUT->blocks_for_region('center') ?>
<?php } ?>

Inside theme/yourtheme/config.php:

Find this array: $THEME→layouts = array(

And alter the various entries: include the 'center' everywhere. Example:

'frontpage' => array(
   'file' => 'frontpage.php',
   'regions' => array('side-pre', 'side-post', 'center'),
   'defaultregion' => 'side-pre',

Inside theme/yourtheme/lang/en/theme_yourtheme.php:

Put it in translation:

$string['region-center'] = 'Center';

Inside lib/outputcomponents.php:

Replace the original function with this one:

public function add_class($class) {
    	if (array_key_exists('class', $this->attributes)) {
        	return $this->attributes['class'] .= ' '.$class;
    	$this->attributes['class'] = $class;

Include Javascript in Your Block

To include javascript, use this code:

    public function get_content() {        
        if ($this->content !== null) return $this->content;
        $this->content = new stdClass;

This code assumes that:

  • your block is called 'simplenav'
  • there is a javascript file called 'jquery.collapsible.min.js' sitting in the root of your block folder

Add Blocks to Your Courses (Or All Courses)

INSERT INTO `mdl_block_instances` (`blockname`, `parentcontextid`, `showinsubcontexts`, `pagetypepattern`, `subpagepattern`, `defaultregion`, `defaultweight`, `configdata`) 
SELECT 'activity_modules', id, 0, 'course-view-*', NULL, 'side-post', 0, '' 
FROM mdl_context
WHERE contextlevel = 50
AND instanceid <> 1
AND id NOT IN (SELECT parentcontextid FROM mdl_block_instances WHERE blockname = 'activity_modules') 

(The ids and actual numbers mentioned here are the same in all Moodle installations).

Quiz Suppresses All Blocks by Default for Students

Attention: the quiz activity suppresses all blocks (except the 'fake' quiz navigation block) by default, if you're logged in with a student role.

See Quiz settings > Appearance > Show blocks during quiz attempts (underneath the 'Show more' link).

Change this behavior for all quizzes with:

UPDATE `mdl_quiz` SET `showblocks`= 1;

All Blocks Are Invisible When Not Logged In

Uncheck Site administration ► Security ► Site policies and then Force users to log in.

In other words, if you require all users to login before they can see anything, then all blocks are excluded too.

Personal Tools