[Be aware — this is by FAR the geekiest post I’ve written in a long, long time.]

Thought I’d share this for anyone who is trying to use both Moodle and Drupal. We just figured out a quick way to create a link on a DrupalEd Group page to a corresponding Moodle course.

Here’s how:

This assumes that a) you have CCK and Computed Field installed

First, give every DrupalEd course an automatic alias that is the same as your Moodle short-course name. (Yes, right now, we have to do that by hand. That needs to change eventually.)

Then, in Content Management -> Content Types -> Course — create a new field called field_moodle_link (or something like that) and select Field Type — Computed and create the field.

In the next page that pops up, fill in the Label with whatever you want the label to be on the Drupal Group page. Then I chose "Required" under data settings, but I’m not 100% sure that’s necessary. And under Computed Code, enter this:

$db = mysql_connect("<machine>", "<moodle_username>", "<moodle_password>");
 mysql_select_db("<moodle_db>",$db);

 #Enter base moodle website here
 $website = "http://www.yourwebsitehere.org/moodle";

 $nodepath = "node/";
 $nodepath .= arg(1);
 $shortname = drupal_get_path_alias($nodepath);

 $query = "SELECT id,fullname from mdl_course where shortname=’$shortname’";

# Standard debug test
# print("<br>$query");

 $idquery = mysql_query($query);
 if ($idarray = mysql_fetch_array($idquery))
 {
   $id = $idarray["id"];
   $fullname = $idarray["fullname"];
   $node_field[0][‘value’] = "<br><a href=$website/course/view.php?id=$id>$fullname</a>";
 }
 else
 {
  $node_field[0][‘value’] =  "No Moodle Course w/ shortname: $shortname";
 }
?>

Make sure "Display this field" is checked, and I use this as my display format:

$display =  $node_field_item[‘value’] . "<br><br>";

And then save it.

Once it’s saved, click "Manage Fields" and make sure that your new field has a lower numerical value than the Highlighted Content Field, so that it’s at the top of the Drupal page.

What I’d like to do eventually, is figure out how to make that link appear in the Group Details block, but I haven’t figured out how to edit that. Anyone who knows, I’d love to know.

In the meantime, drop me a note if you find this useful… or make it better.

(And now, off to figure out Moodle blocks. And yes, I’m still a principal, why do you ask?)

Blogged with the Flock Browser

Tags: killerapp, drupal, programming