Thursday, November 3, 2011

Converting M1.9 Plug-ins to M2 - Block Part 5 - Cleaning Up

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

The next problem that I know is there, even though it doesn't seem to be causing any problems, is that the code is trying to include Javascript files that don't exist in the location specified.

In the get_content function of the block, the file "/lib/yui/yahoo/yahoo-min.js" and "/lib/yui/dom/dom-min.js" are specified for inclusion in our HTML. These files are now at "/lib/yui/2.9.0/build/yahoo/yahoo-min.js" and "/lib/yui/2.9.0/build/dom-min.js". There actually is evidence of this problem on the page that is displayed; but its hidden in the HTML code. If I look at the code included in those statements, using something like the Firebug plug-in for Firefox, I can see a "404 Not Found" error embedded in the page.

The easy fix would be to correct the URL's. But since this doesn't seem to be affecting the function, I think I should look and see if I can remove them completely. There are two documents I am looking at: "JavaScript guidelines" and "Migrating your code to the 2.0 rendering API". The second one I am using cautiously as much of it is obsolete.

After reading through these, and then looking at existing code in Moodle 2, I believe I can replace these two HTML lines with a call to $PAGE->requires->yui2_lib(). This should add the necessary YUI code I need to my page if it is not already presently loaded. I think the line I need is:
$PAGE->requires->yui2_lib('yahoo', 'dom');
Unfortunately, making this change doesn't prove that I have fixed it, since there is no problem to verify. But I believe this is correct and will leave it as such. (If anyone can confirm this or knows what is correct, comment here and I'll fix this.)

I also read that Javascript files should be included using the $PAGE->requires->js() function. This helps control when and where the Javascript file gets loaded, and helps to make sure that the same file isn't loaded more than once. An optional boolean argument, set to "true", will load the script in the header of the page. We will need this as we need the functions in there when the block is displayed. I change the line that includes the block's Javascript file in HTML to:
$PAGE->requires->js('/blocks/twitter_search/javascript.js', true);
Note: when I did this initially, I used the $CFG->wwwroot as in the older code as a prefix to the file name. This gave me an error which recoding as above fixed.
The other Javascript I have in the block are the lines that set the callback function and the update link, for the refreshing of the tweets. There may be ways to do this differently, but I'm going to leave them for now. If anyone wishes to submit a better solution, I'll include it.

I'm going to do one more thing. Currently, the block has defaults for the search string, update rate and the number of tweets to display. These are hardcoded into the block as "#moodle", "10" and "30000" (30 seconds). It would be nicer if the site could specify their own defaults without having to change the code. To do that, I am going to add a global configuration page. I'll leave that for the next post.