Tuesday, August 7, 2012

Now you see it, now you don't - making the SpeakPipe icon selectively appear or disappear on your Moodle 1.9/2.x site.

Mashup: a creative combination or mixing of content from different sources. 

SpeakPipe is a cool Web application that allows the readers of your blog, website, Moodle site to send voice messages instead of text feedback.  If you are new to SpeakPipe, Madras Geek has written an excellent article on it here. Dorian Love also has an excellent post here on SpeakPipe.

I think that I have created a mashup of sorts by combining SpeakPipe embed code with Moodle pages. More so because I now know how to make the SpeakPipe icon appear or disappear based on the type of Moodle resource that is currently accessed by the user, or by the URL of a Moodle webpage.

Thanks to Howard Miller, I was introduced to the powerful yet humble me() function.After several hours of testing and Googling stuff, I finally got it working. Here's the code snippet of my Moodle theme's header.html file:


$haystack = me();
$needle = "forum";
$pos = strpos(strtolower($haystack),strtolower($needle));
//echo $pos;
if($pos === false) {
   // string needle NOT found in haystack
   // do nothing!

else {
   // string needle found in haystack
   // so, go ahead and activate the embed code!
   echo "<script type='text/javascript'>";   echo "(function(d){   var app = d.createElement('script'); app.type = 'text/javascript'; app.async = true;   var pt = ('https:' == document.location.protocol ? 'https://' : 'http://');   app.src = pt + ''   var s = d.getElementsByTagName('script')[0]; s.parentNode.insertBefore(app, s);   })(document);";   echo"</script>";
So what does this achieve? To see the results, be my guest and test it here on my production Moodle site:
Notice the icon appearing on the right edge of the website? That's the SpeakPipe icon!

Alternatively, if you access other non-forum parts of the Moodle site, for example,, and the SpeakPipe voice recorder icon stays tucked away nicely.  This is because I programmed it not to appear.

Thanks for pointing me in the right direction when I needed it, Howard. I now know that this is indeed possible.
So what are the possibilities now? 

Imagine with me if you will. You could create several SpeakPipe accounts, each with their own embed code. You could have one SpeakPipe used for whenever the user is accessing a Moodle forum (because you check for the substring "forum").  You could have use yet another SpeakPipe - for the times when the user is on a quiz page (by checking for the substring "quiz").
Wait, there's more! You could check for the specific resource based on whether the id of the resource is detected or not. For example, if you wanted the SpeakPipe to be activated not by all forums, but only by a forum with the specific id of 3885, as in then you could check if the substring "forum" and substring "id=3885" appears in the partial URL returned by the me() PHP function! Ah yes, think of the possibilities that this opens up.

One drawback is that the header.html file content could get cluttered with hardcoded code just checking for resource types or resource ids.
Well that's it for this post. Chalk up another success in Moodle coding and doing what was previously not thought possible (at least by me!). Many thanks to you, Howard! Also many thanks for Dorian Love for inspiring me on this mashup Quest.
In closing, here's a thought provoking question: what other Web2.0 resource like SpeakPipe do you think you can selectively embed inside a Moodle page?
Frankie Kam

P.S., For Moodle 2.x users, you are not forgotten. Hubert Chati gives a tip how you can implement this for your Moodle 2.x site. Read about it here.

If you like this post or site
a small donation would be nice but would last only a day,
otherwise leaving a comment (or a compliment) below will last me a month!

Ratings and Recommendations by outbrain