Thursday, October 27, 2011

Converting M1.9 Plug-ins to M2 - Block Part 2 - See What Happens

This is part three of my series concerning porting Moodle 1.9 code to Moodle 2.

Alright. Time to see what happens with my Twitter Search block in Moodle 2...

For my purposes, I have a Moodle 2.1.2+ (Build: 20111019) site for my development site. Yours may be slightly different, but as long as its a 2.1.x it shouldn't matter. And, make sure you have "Developer" level debugging on, set to display on the screen. It will make for easier troubleshooting. This can be found on the "Site administration / Development / Debugging" page.

So, the first thing I am going to try is to drop the existing block code onto the 2.1.2 development site. Who knows? Maybe we don't need to do anything and I'll end this series early.

I copy the "twitter_search" block directory into my "/blocks/" directory, visit the site "Notifications" screen, and... nothing. No message, no error. Hmmm.
(Actually, if you have debug messaging on, you will see some error messages at the top of the "Plugins overview" page.)

Really, this is not unexpected, since the entire Moodle plug-in system has been completely overhauled from 1.x. Essentially, our block does not have the necessary components to be installed. But, if I go to the "Site administration / Plugins / Plugins overview" page, and scroll down to the "Blocks" section, I can see near the bottom of that section (see image below) that Moodle does recognize that my block is present.

The entry for the Twitter Search block is there, but it has a broken language string, and isn't really installed, since there is no version and no "enabled"
setting. Also note that Moodle knows it is an "extension". That is, its not supplied standard with Moodle.

Now we have no choice but to start changing code...

Moodle has provided some documentation on how best to migrate 1.9 code to 2.x. The best place to start is here. None of this block specific though, so there is a lot of information. At Remote-Learber, we've kept our own wiki page documenting what we've learned and what we do as we've ported code. I'll use the lessons learned there to move forward.

I am just going to worry about the minimum amount of coding to get the block installed for now. So, to start with, I need a new file in my block directory called "version.php". This file will define the version of my block, and the required version of Moodle for the block to function. In 1.9, the version is defined in the block code itself, in the "init" function. For the twitter_search block, currently the version is "2010020201". I'll create a new version, "2011100101" for our 2.1 version. I'll create a new "version.php" file, give the block a new version, "2011100101", and remove the version code from the block "init" function.

My new "version.php" file now looks like this:
// This file is part of Moodle -
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <>.

$plugin->version =  2011100101;
$plugin->requires = 2010112400;
The next thing I need to do, to make the block show up properly on the plugins block screen, is change the language directory from "en_utf8" to "en". Moodle 2 has done away with the "utf8" suffix on the directory names, since all languages must be using utf8 strings.

I also need to make one more language string change - I need to change all occurrences of the string used for the block's "title" variable to "pluginname". In this case, the language string "blocktitle" was used, so I will change that. In Moodle 1.9, the "title" variable defined in the "init" function was used to display the block's name throughout the system. In Moodle 2, the "pluginname" language string defines this, and is a required setting.

If there are other language directories present, I will need to remove the "_utf8" portion from their names, and make the "pluginname" string change as well.

I make the language string change in two places - the language files, and the "init" function.

When I'm done, the "init" function code changes from:
function init() {
    $this->title   = get_string('blocktitle','block_twitter_search');
    $this->version = 2010020201;
function init() {
    $this->title = get_string('pluginname','block_twitter_search');
That should be all I need for my block to install correctly. Revisiting my notification screen, I am prompted with a page that indicates my twitter search block is ready for an upgrade. Clicking "Upgrade" successfully installs it. I am most of the way there!

Now, just for fun, I'm going to add an instance of this block to my site's home page and see what happens. If you do the same, you will see that the block adds correctly and begins to display the twitter search tweets as it did on the 1.9 site (sort of). On my site, it correctly displayed ten tweets, but without the proper display layout. Shortly thereafter, it began to display the error:
Transaction id: 3
HTTP status: 404
Status code message: Not Found
And, when I went to edit the block configuration, none of my block's settings were there.

So, while it sort of works, I have some more work to do. I'll leave that for the next post.

No comments:

Post a Comment