[O.k. — this too is an insanely geeky post. I promise, I’ll write about education theory or EduCon or something like that soon. But for now, I’ve got my geek on.]

This is a very simple block in Moodle — my first custom-designed Moodle block — that makes it very easy to put a link on a Moodle course directly to the related DrupalEd course/group. As with before, this uses the Moodle variable "Shortname" and corresponds that with the "URL Alias" in Drupal. Those have to correspond or this doesn’t work. 

And if you are into learning how to make custom blocks in Moodle, this page of Block Documentation on the Moodle.org site was incredibly helpful and important, and I really just used their template.

In <site>/moodle/blocks, create a directory called drupal_link. Then create a file block_drupal_link.php — here is that code:

<?php
// DrupalEd Linking
// Chris Lehmann — 8.18.08
// This assumes that you have stored the moodle shortname in
// the URL Path settings in DrupalEd.
class block_drupal_link extends block_base {
function init() {
        $this->title = get_string(‘Drupal Link’, ‘block_drupal_link’);
        $this->version = 2008081800;
    }

function get_content() {
    global $CFG, $COURSE;
    if ($this->content !== NULL) {
        return $this->content;
    }

    $this->content = new stdClass;
    $site = $CFG->drupalsite;
    $this->content->text = "<a href=" . $CFG->drupalsite . $COURSE->shortname .
      ">" . $COURSE->fullname . "</a>";
    $this->content->footer = ”;

    return $this->content;
}

function has_config() {
    return true;
}

function config_save($data) {
    // Default behavior: save all variables as $CFG properties
    foreach ($data as $name => $value) {
        set_config($name, $value);
    }
    return true;
}

}
?>

Then, create a file called config_global.html — this is what will allow you to have global settings for the block. The global setting we create here is the root of the drupaled site, so that it’s the same for all courses. (You could make this editable, course by course, but I didn’t want to because we only have one drupal site.) Here’s that code:

 <table cellpadding="9" cellspacing="0">
 <tr valign="top">
     <td align="right">
         <?php print_string(‘Drupal Site Base URL’, ‘block_drupal_link’); ?>:
     </td>
     <td>
         <?php
         if (!empty($CFG->drupalsite)) {
           $drupalsite=$CFG->drupalsite;
         }
         else
         { $drupalsite=""; }
         print_textarea(true, 1, 50, 0, 0, ‘drupalsite’, $drupalsite);
         ?>

     </td>
 </tr>
 <tr>
     <td colspan="2" align="center">
         <input type="submit" value="<?php print_string(‘savechanges’) ?>" />
     </td>
 </tr>
 </table>

Once you do this, you may need to go to main moodle admin page for moodle to recognize the block, but otherwise, you should see the block in the Administration -> Blocks page. Edit the Settings with the root of your DrupalEd install (include the trailing slash), and you should be able to just add the block to any course and have the link show up. It will show up as the name of the course, rather than the URL. I thought that looked prettier.

Also… one silly issue that I’m wondering about. For some reason, the Block name is enclosed in [[ ]] brackets. I don’t know why. Any ideas?

Blogged with the Flock Browser

Tags: moodle, killer app, drupal, killerapp, programming