+ Post New Thread
Results 1 to 3 of 3
EduGeek Moodle Thread, Moodle Site Administrators IF statement in Projects:; i need help and was wondering if anyone has any ideas on this one i need to create an if ...
  1. #1

    Join Date
    Jul 2012
    Location
    lincolnshire
    Posts
    23
    Thank Post
    7
    Thanked 3 Times in 3 Posts
    Rep Power
    5

    Moodle Site Administrators IF statement

    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?
    Last edited by cyberrant; 20th June 2013 at 10:49 PM.

  2. #2

    Join Date
    Jul 2012
    Location
    lincolnshire
    Posts
    23
    Thank Post
    7
    Thanked 3 Times in 3 Posts
    Rep Power
    5
    answer found

    if (is_siteadmin()) {
    // do something for site admins
    } else {
    // do something else
    }

  3. #3
    Marci's Avatar
    Join Date
    Jun 2008
    Location
    Wakefield, West Yorkshire
    Posts
    895
    Thank Post
    84
    Thanked 235 Times in 194 Posts
    Rep Power
    82
    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...
    Code:
    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...

    Code:
    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
    Last edited by Marci; 21st June 2013 at 08:57 AM.

  4. Thanks to Marci from:

    demsley (11th July 2013)

SHARE:
+ Post New Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •