Jump to content

Recommended Posts

Posted (edited)

i need help and was wondering if anyone has any ideas on this one

 

i need to create an if statement for moodle that will look at the current user login and if they are a site administrator it will give me back a different result.

 

i know it will need to look like this

 

if ($user->???? == ???) {

WHAT I WANT TO HAPPEN

} else {

WHAT I WANT TO HAPPEN

}

 

does anyone have any ideas on how to fill the blanks?

Edited by cyberrant
Posted (edited)

That function is for use within core or for temporary fudges, and could end up deprecated without warning. The recommended method in a block / plugin etc is to check capabilities:

 

If user is an admin...

global $CFG;
require_once($CFG->dirroot.'/lib/accesslib.php');

if (has_capability('moodle/site:config, get_context_instance(CONTEXT_SYSTEM))){
   echo 'Congrats, you're an admin!';
}

 

And, if user isn't an admin...

 

global $CFG, $OUTPUT;
require_once($CFG->dirroot.'/lib/accesslib.php');

if (!has_capability('moodle/site:config, get_context_instance(CONTEXT_SYSTEM))){
   echo 'You're not an admin - bailing out';
   echo $OUTPUT->footer();
   die();
}

 

Core API Capabilities ref: Roles - MoodleDocs

Access API ref: Access API - MoodleDocs

Edited by Marci
  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...