University

Stuff about university

Of Moodle and First Class Honours

Well, time for a nice hefty blog post I think, as I haven't done one in a while.
I got my final results from university this week. I got a first. Everyone I know has been congratulating me, which is quite overwhelming. I'm happy, but I don't seem to be as happy for myself as everyone else is for me! It's probably becuase I was worried that I wouldn't get one, that when I did it was more a cause for relief then celebration.
Nonetheless, it's the weekend now, so party time tonight. Hells Yeah.

I've also moved into a new flat with my lovely girlfriend, and started a new job. I'm working at Taunton's College in Southampton as their in-house web developer. This involves working primarily with Moodle, the open-source Course Management System/Virtual Learning Environment. This is awesome, for 2 reasons. 1 - I get paid to code PHP, which is what I do for fun anyway. 2 - I get paid to contribute to an open source project, which is a position I've always wanted to be in. And it pays well enough for my nice new flat. And I get a local government pension. And I get to help people teach. Winner. Dream first job? I think so.

Moodle in itself is a pretty cool system, although it's suffered a bit from it's evolutionary development. The main problems are that when new and better solutions get introduced, the old ones remain. This is mainly a backwards-compatibility thing which means a lot of it is being culled for version 2 (the upcoming major release), but it means at the moment there are 3 different ways of keeping track of which javacscript files a page needs, a really flexible permission system which relies on an older "roles" system for assigning the permissions, and lang files for older components all over the place.

That said, the current "best practice" provides some really nifty plug-in APIs,and the database abstraction layer makes interacting with the database a breeze. Hopefully once version 2 hits the mirrors, the cruft will have been cut back, and the new plug-in points will make it an even more versatile platform than it already is (come on, gradebook plugins!).

The Moodle community's also brilliant, as are my Taunton's colleagues. I look forward to working with them all to make Moodle better!

Calling PUT and DELETE on RESTful PHP services with Prototype.js

So I was asked to create a RESTful web service in PHP. No problem. I was asked to create a PHP client that connects to it through cURL. No problem. I was asked to create an AJAX interface to administer it. Problem.

The problem wasn't the same origin policy, as the AJAX interface was to run on the same server as the service. The problem was implementing the HTTP methods.
I use Prototype.js for all of my Javascript coding. I'd recommend it to anyone, especially for AJAX as it makes your life a doddle. The basic syntax of an AJAX request using Prototype looks like this:

ajax = new Ajax.Request('test.php',{
      method: get,
      onSuccess: function(xmlHTTP) {
	$('response').update(xmlHTTP.responseText);
      }
     });

There's a host of options for making various types of request, but that's the gist of it. The problem with this, however, is that not all browsers support the PUT and DELETE methods, which in REST are used to update and delete records, respectively. As such, Prototype's Ajax objects don't try and send an XmlHttpRequest object using PUT or DELETE.
It turns out that these two methods are implemented using POST as a proxy. It then tells the web service the method you really wanted in $_POST['_method']. This means that to implement calls through AJAX, where your code would have looked something like this:

if($_SERVER["REQUEST_METHOD"] == "GET") {

  echo("This looks like a GET request to me!");

} else if ($_SERVER["REQUEST_METHOD"] == "POST") {

  echo("This looks like a POST request to me!");

} else if ($_SERVER["REQUEST_METHOD"] == "PUT") {

  echo("This looks like a PUT request to me!");

} else if ($_SERVER["REQUEST_METHOD"] == "DELETE") {

  echo("This looks like a DELETE request to me!");

}

It would now need to look like this:

if($_SERVER["REQUEST_METHOD"] == "GET") { 

  echo("This looks like a GET request to me!");

} else if ($_SERVER["REQUEST_METHOD"] == "POST" && !isset($_POST['_method'])) {

  echo("This looks like a POST request to me!");

} else if ($_SERVER["REQUEST_METHOD"] == "PUT" || $_POST['_method'] == 'put') {

  echo("This looks like a PUT request to me!");

} else if ($_SERVER["REQUEST_METHOD"] == "DELETE" || $_POST['_method'] == 'delete') {

  echo("This looks like a DELETE request to me!");

}

I'm guessing that any data you're trying to send to PUT that would normally be read in from php://input would have to by hidden in the _POST array somewhere. More experimentation required methinks!

Syndicate content