Monday 25 March 2013

Moodle 2.4 Blocks Changes in Code

I was looking for a module block for Moodle  My client wanted a scrolling Testimonials block. Ideally they wanted to have a set of several testimonials that could easily be edited and would display in a random order and maybe scroll across the block. I had a look for such a thing, but couldn't find anything, so I decided to write one myself.
There was a good looking tutorial to get started so I did. The only problem was that it didn't work. If I took their code and put it up it just broke Moodle telling me that the block had errors in it.
I initially decided that it was me and making silly typos. Having got rid of my only mistake the thing still didn't work. so I went back to basics.
This is a very simple block just to display some text. It works with Moodle 2.4.

block_testimonials.php


<?php

// This file is part of Moodle - http://moodle.org/

/**
 * Form for editing banners block instances.
 *
 * @package   testimonials Philip M Russell Ltd
 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
 * @license   http://www.gnu.org/copyleft/gpl.banners GNU GPL v3 or later
 */

class block_testimonials extends block_base {

    public function init() {
        $this->title = get_string('pluginname', 'block_testimonials');        
    }
public function get_content() {
    if ($this->content !== null) {
      return $this->content;
    }

    $this->content         =  new stdClass;
    $this->content->text   = 'BRE Training is a great place to learn how to do things properly.';
    $this->content->footer = 'BRE Training';

    return $this->content;
  }
}   // Here's the closing bracket for the class definition


Version.php


<?php
// This file is part of Moodle - http://moodle.org/

/**
 * Version details
 *
 * @package    testimonials
 * @subpackage html
 * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

defined('MOODLE_INTERNAL') || die();

$plugin->version   = 2012112901;        // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires  = 2012062500;        // Requires this Moodle version
$plugin->component = 'block_testimonials';      // Full name of the plugin (used for diagnostics)
$plugin->cron      = 0;
$plugin->maturity  = MATURITY_BETA;
$plugin->release   = '2.3 (Build: 2012112900)'; 

db/access.php


<?php

// This file is part of Moodle - http://moodle.org/
/*
* @copyright 2013 Philip M Russell Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

$capabilities = array(

'block/testimonials:myaddinstance' => array(
'riskbitmask' => RISK_SPAM,
'captype' => 'write',
'contextlevel' => CONTEXT_BLOCK,
'archetypes' => array(
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
)
)

);

lang/en/block_testimonials.php


<?php

// This file is part of Moodle - http://moodle.org/
/**
 * @package   testimonials
 * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
 * @license   http://www.gnu.org/copyleft/gpl.banners GNU GPL v3 or later
 */


$string['pluginname'] = 'Testimonials block';
$string['testimonials'] = 'Simple HTML';
$string['testimonials:addinstance'] = 'Add a new simple HTML block';
$string['testimonials:myaddinstance'] = 'Add a new simple HTML block to the My Moodle page';

edit_form.php


<?php

class block_testimonials_edit_form extends block_edit_form {

    protected function specific_definition($mform) {

        // Section header title according to language file.
        $mform->addElement('header', 'configheader', get_string('blocksettings', 'block'));

        // A sample string variable with a default value.
        $mform->addElement('text', 'config_text', get_string('blockstring', 'block_testimonials'));
        $mform->setDefault('config_text', 'default value');
        $mform->setType('config_text', PARAM_MULTILANG);        

    }
}

Each of these php programs is saved into a directory called testimonials and was copped by into moodle/blocks.

On loading Moodle the block was detected and I was asked to update the Moodle database. If there are any errors then this shows up straight away. Then it is necessary to remove the block and recode.



2 comments:

  1. Philip

    Thank you for this, most helpful, and as you say the moodledocs tutorial doesn't seem to work. You saved me from terminal depression.

    Phil
    Cornwall UK

    ReplyDelete
  2. i really like your post .. i would like to bookmark your site for my future needs :)

    web consultancy

    ReplyDelete