Thursday, October 27, 2011

Converting M1.9 Plug-ins to M2 - Block Part 1 - Know the 1.9 Code

Okay. So let's start my Twitter Search block code port.

Step one is straightforward. Get the block code for 1.9, install it on my 1.9 development site and make sure it works okay there.

Getting the code is easy. The developers have set it up on github for me. I go there, use one of the "download" buttons and unzip the "twitter_search" directory into my development site's "/blocks" directory. I know I've done it right because I see a "twitter_search" subdirectory containing files like "block_twitter_search.php" in my site's "/blocks" directory.

Next step is to complete the install by visiting the administration "Notifications" page. Doing this, I see this message:


twitter_search


Twitter Search tables have been set up correctly

The block is now available to add to my site and course pages.

Adding the block to my main page and clicking the edit icon in the block header, shows what it does. I can select a Twitter search string (defaults to "#moodle"), choose how many tweets I want to see at one time (defaults to "10") and select the refresh rate for the tweets (defaults to 30000 ms or 30 seconds). I changed my settings to display 5 tweets and left the rest alone.

Now that I have a good understanding of what the block does, I'll do a quick review of what's in the code.

A quick look at the directory, and I see that it has a "lang" directory and five main code files. There is no "db" directory so it isn't creating a data table or modifying the Moodle database in anyway. That'll make it easier to port, but I won't get to play with the M2 new database API's either. Oh well, next time.

Next, I want to do a look through of the code, for a number of reasons. I want to see if I understand it right away, I want to get a feel for how its been structured, I want to look for any obvious problems (security, performance, etc.) and I want to see if it has adhered to the Moodle development guidelines.

On my pass through the code, I see no obvious violations of the security guidelines, but there are some minor breaches of the Moodle coding style, primarily around line length and indent spacing. These are minor and I can fix them when I port to M2.

I also notice that this code uses two API's that I'm not very familiar with: CURL and DOM. I may need to familiarize myself with those as I migrate, particularly if there are changes in the way they work with Moodle 2.

So, looking at the code, what I believe is happening is this:
  1. Twitter's search URL is constructed with the search string we configured with the block.
  2. That URL is connected to using CURL, and the XML data returned is stored.
  3. The XML data is loaded into a DOM object for easier XML processing.
  4. The tweets we asked for are pulled from the DOM object using a known XML tag ('entry').
  5. The tweets are placed in the block output, one by one, again using known XML structure in the DOM object.
  6. The output is written to the block.
There is also a bunch of javascript that is placed in the output. Some of that it is in the main block script, and some is contained in a separate file (javascript.js). The javascript seems to be there to handle the automatic updating of the tweets in the block. And one other file called 'ajax_update.php' is used by the javascript to update those tweets in an "ajaxy" way.

Lastly, there is a CSS file ('styles.php') and an instance configuration HTML file ('config_instance.html').

I believe I am now comfortable enough with what is happening in this block to start the M2 port. In my next part, I will start the Moodle 2.1 migration of this code.

No comments:

Post a Comment