Custom My Moodle blocks by authentication method

We’ve been using the My Moodle portal to provide a simple landing page for tutors and students alike, as it stands though we had a slight issue that when signed in as either student or tutor you’d see blocks that aren’t of any use (Live Services when logged in via LDAP, for instance)

So I started digging and decided to try my hand at coding up a little block to do the staff room link I wanted. Used this very handy guide on Moodle.org

http://docs.moodle.org/en/Development:Blocks

But still couldn’t get the auth method sorted. So off to the forums and was suggested this…

Tried the code but faced with a white screen, just about to view the debugging logs then spotted everyone’s favourite programming issue… a missing semicolon (can you spot it below?)

function get_content() {

global $USER;

if ($USER->auth != ‘ldap’) {
$this->content->text = ”;
$this->content->footer = ”;
return $this->content
}

// rest of get_content method

A few tweaks later and I have this…

<?php
class block_staffroom extends block_base {
function init() {
$this->title   = ‘Virtual Staffroom’;
$this->version = 2009042700;
}
// The PHP tag and the curly bracket for the class definition
// will only be closed after there is another function added in the next section.

function get_content() {

if ($this->content !== NULL) {
return $this->content;
}

/**

Added by RIAE, hide block if user is not logged in via ldap auth

*/

global $USER;
if ($USER->auth !== ‘ldap’) {
$this->content->text = ”;
$this->content->footer = ”;
return $this->content;
}

/**

end of auth check code

*/

$this->content         =  new stdClass;
$this->content->text = ‘test test test’;
$this->content->footer = ‘Footer here…’;

return $this->content;
}
}   // Here’s the closing curly bracket for the class definition
// and here’s the closing PHP tag from the section above.
?>

Tried adding the code into the Live Services block and it seems to do the trick as well, just a matter of giving it a go on the main site now 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: