[HELP] Moodle PHP script for user capabilities

[HELP] Moodle PHP script for user capabilities

by Andrew Leonheart -
Number of replies: 11
Hey... I'm looking for help from anyone that understand Moodle PHP script...
I want to ask how can i retrieve user role in moodle...

In moodle PHP script, i wrote down print_r($USER);
The result is:

stdClass Object ( [id] => 8 [auth] => manual [confirmed] => 1 [policyagreed] => 0 [deleted] => 0 [mnethostid] => 1 [username] => dosen01 [password] => 81dc9bdb52d04dc20036dbd8313ed055 [idnumber] => [firstname] => Teacher 01 [lastname] => User [email] => teacher01@peter.petra.ac.id [emailstop] => 0 [icq] => [skype] => [yahoo] => [aim] => [msn] => [phone1] => [phone2] => [institution] => [department] => [address] => [city] => Surabaya [country] => ID [lang] => en_utf8 [theme] => [timezone] => 7.0 [firstaccess] => 0 [lastaccess] => 1242562225 [lastlogin] => 1241784680 [currentlogin] => 1242560241 [lastip] => 127.0.0.1 [secret] => [picture] => 0 [url] => [description] => [mailformat] => 1 [maildigest] => 0 [maildisplay] => 2 [htmleditor] => 1 [ajax] => 0 [autosubscribe] => 1 [trackforums] => 0 [timemodified] => 1240636474 [trustbitmask] => 0 [imagealt] => [screenreader] => 0 [display] => Array ( [2] => 0 ) [preference] => Array ( [auth_forcepasswordchange] => 0 [email_bounce_count] => 1 [email_send_count] => 1 ) [lastcourseaccess] => Array ( [2] => 1241786038 ) [currentcourseaccess] => Array egg [groupmember] => Array egg [sesskey] => ciN2edi7zhgs6 [sessionIP] => f528764d624db129b32c21fbca0cb8d6 [access] => Array ( [ra] => Array ( [/1/23] => Array ( [0] => 3 ) [/1/12/13] => Array ( [0] => 3 ) [/1] => Array ( [0] => 7 ) ) [rdef] => Array ( [/1:3] => Array ( [moodle/course:manageactivities] => 1 [mod/data:writeentry] => 1 [mod/forum:startdiscussion] => 1 [moodle/course:viewhiddensections] => 1 [block/online_users:viewlist] => 1 [moodle/grade:unlock] => 1 [mod/glossary:manageentries] => 1 [gradeexport/xml:view] => 1 [moodle/question:usemine] => 1 [mod/quiz:manage] => 1 [mod/wiki:participate] => 1 [mod/choice:deleteresponses] => 1 [moodle/site:readallmessages] => 1 [moodle/course:managefiles] => 1 [mod/forum:addnews] => 1 [mod/forum:viewanyrating] => 1 [moodle/course:viewhiddenuserfields] => 1 [block/rss_client:createprivatefeeds] => 1 [moodle/grade:viewall] => 1 [mod/glossary:rate] => 1 [gradeimport/csv:view] => 1 [moodle/question:viewall] => 1 [mod/quiz:preview] => 1 [mod/workshop:manage] => 1 [mod/choice:downloadresponses] => 1 [moodle/site:restore] => 1 [moodle/course:managegroups] => 1 [mod/forum:createattachment] => 1 [mod/forum:viewdiscussion] => 1 [moodle/course:viewparticipants] => 1 [block/rss_client:createsharedfeeds] => 1 [moodle/grade:viewhidden] => 1 [mod/glossary:viewrating] => 1 [gradeimport/xml:view] => 1 [moodle/question:viewmine] => 1 [mod/quiz:view] => 1 [moodle/block:view] => 1 [mod/choice:readresponses] => 1 [moodle/site:trustcontent] => 1 [moodle/course:managemetacourse] => 1 [mod/forum:deleteanypost] => 1 [mod/forum:viewhiddentimedposts] => 1 [moodle/course:viewscales] => 1 [block/rss_client:manageownfeeds] => 1 [moodle/legacy:editingteacher] => 1 [mod/glossary:write] => 1 [gradereport/grader:view] => 1 [moodle/role:assign] => 1 [mod/quiz:viewreports] => 1 [moodle/blog:manageentries] => 1 [mod/data:approve] => 1 [moodle/site:viewfullnames] => 1 [moodle/course:managescales] => 1 [mod/forum:deleteownpost] => 1 [mod/forum:viewqandawithoutposting] => 1 [moodle/course:visibility] => 1 [coursereport/log:view] => 1 [moodle/notes:manage] => 1 [mod/hotpot:attempt] => 1 [gradereport/outcomes:view] => 1 [moodle/role:safeoverride] => 1 [mod/scorm:deleteresponses] => 1 [moodle/blog:view] => 1 [mod/data:comment] => 1 [moodle/site:viewreports] => 1 [mod/forum:editanypost] => 1 [moodle/course:reset] => 1 [mod/forum:viewrating] => 1 [moodle/grade:edit] => 1 [coursereport/log:viewlive] => 1 [moodle/notes:view] => 1 [mod/hotpot:deleteattempt] => 1 [gradereport/overview:view] => 1 [moodle/role:switchroles] => 1 [mod/scorm:savetrack] => 1 [moodle/calendar:manageentries] => 1 [mod/data:managecomments] => 1 [moodle/tag:editblocks] => 1 [mod/forum:initialsubscriptions] => 1 [moodle/course:sectionvisibility] => 1 [mod/forum:viewsubscribers] => 1 [moodle/grade:export] => 1 [coursereport/log:viewtoday] => 1 [moodle/question:add] => 1 [mod/hotpot:grade] => 1 [gradereport/user:view] => 1 [moodle/role:unassignself] => 1 [mod/scorm:viewreport] => 1 [moodle/calendar:managegroupentries] => 1 [mod/data:manageentries] => 1 [moodle/tag:manage] => 1 [mod/forum:managesubscriptions] => 1 [moodle/course:setcurrentsection] => 1 [mod/glossary:approve] => 1 [moodle/grade:hide] => 1 [coursereport/outline:view] => 1 [moodle/question:editall] => 1 [mod/hotpot:viewreport] => 1 [mod/assignment:grade] => 1 [moodle/role:viewhiddenassigns] => 1 [mod/scorm:viewscores] => 1 [moodle/course:activityvisibility] => 1 [mod/data:managetemplates] => 1 [moodle/user:readuserblogs] => 1 [mod/forum:movediscussions] => 1 [moodle/course:update] => 1 [mod/glossary:comment] => 1 [moodle/grade:import] => 1 [coursereport/participation:view] => 1 [moodle/question:editmine] => 1 [mod/lams:manage] => 1 [mod/survey:download] => 1 [mod/assignment:view] => 1 [moodle/site:accessallgroups] => 1 [moodle/course:bulkmessaging] => 1 [mod/data:rate] => 1 [moodle/user:readuserposts] => 1 [mod/forum:rate] => 1 [moodle/course:useremail] => 1 [mod/glossary:export] => 1 [moodle/grade:lock] => 1 [coursereport/stats:view] => 1 [moodle/question:managecategory] => 1 [mod/lesson:edit] => 1 [mod/survey:participate] => 1 [mod/chat:chat] => 1 [moodle/site:backup] => 1 [moodle/course:changefullname] => 1 [mod/data:viewalluserpresets] => 1 [moodle/user:viewdetails] => 1 [mod/forum:replynews] => 1 [moodle/course:view] => 1 [mod/glossary:import] => 1 [moodle/grade:manage] => 1 [gradeexport/ods:view] => 1 [moodle/question:moveall] => 1 [mod/lesson:manage] => 1 [mod/survey:readresponses] => 1 [mod/chat:deletelog] => 1 [moodle/site:doclinks] => 1 [moodle/course:changeidnumber] => 1 [mod/data:viewentry] => 1 [moodle/user:viewhiddendetails] => 1 [mod/forum:replypost] => 1 [moodle/course:viewhiddenactivities] => 1 [mod/glossary:managecategories] => 1 [moodle/grade:manageletters] => 1 [gradeexport/txt:view] => 1 [moodle/question:movemine] => 1 [mod/quiz:deleteattempts] => 1 [mod/wiki:manage] => 1 [mod/chat:readlog] => 1 [moodle/site:import] => 1 [moodle/course:changeshortname] => 1 [mod/data:viewrating] => 1 [mod/forum:splitdiscussions] => 1 [moodle/course:viewhiddencourses] => 1 [moodle/grade:manageoutcomes] => 1 [mod/glossary:managecomments] => 1 [gradeexport/xls:view] => 1 [moodle/question:useall] => 1 [mod/quiz:grade] => 1 [report/courseoverview:view] => 1 [mod/wiki:overridelock] => 1 [mod/choice:choose] => 1 [moodle/site:manageblocks] => 1 ) [/1:7] => Array ( [moodle/legacy:user] => 1 [moodle/site:sendmessage] => 1 [moodle/blog:view] => 1 [moodle/blog:create] => 1 [moodle/calendar:manageownentries] => 1 [moodle/user:editownprofile] => 1 [moodle/user:changeownpassword] => 1 [moodle/my:manageblocks] => 1 [moodle/tag:create] => 1 [moodle/tag:edit] => 1 [moodle/block:view] => 1 [block/online_users:viewlist] => 1 ) ) [loaded] => Array ( [0] => /1/2 ) [dr] => 7 [time] => 1242560241 ) [message_lastpopup] => 0 [mycourses] => 2 )


To get user's firstname, i can use $USER->firstname the result will be "Teacher 01" (see the red part of the code above)
but how can i get user's role / legacy in moodle? somehow i know it's "moodle/legacy:editingteacher" but how can i either the result is 1 or 0??
I tried to use:
$USER->moodle/legacy:editingteacher No result...
$USER->'moodle/legacy:editingteacher' No result...
$USER->[moodle/legacy:editingteacher] Unexpexted '['
$USER->{moodle/legacy:editingteacher} Unexpected :
$USER->{moodle/legacy->editingteacher} No result...
$USER->{'moodle/legacy:editingteacher'} No Result...


Can anyone help me??
Thanks in advance...
Average of ratings: -
In reply to Andrew Leonheart

Re: [HELP] Moodle PHP script for user capabilities

by Mike Worth -
Roles are now stored in separate tables in order to make the capability system a lot more flexible.

get_records_sql('SELECT firstname,lastname,role.name FROM '.$CFG->prefix.'user AS user join '.$CFG->prefix.'role_assignments AS role_assignments ON userid=user.id JOIN '.$CFG->role AS role ON roleid=role.id);

Will give you all a row per role assignment, containing the teachers name and the name of the role. What do you want this for? There is a nice has_capability function if you want to check if the user has permission to do something

Mike
Average of ratings: Useful (1)
In reply to Mike Worth

Re: [HELP] Moodle PHP script for user capabilities

by Andrew Leonheart -
Thanks, Mike...
I put few text & link in the site's main menu, and i want some menu just can be viewed and used by teachers, some just for students, and some for both.

how do i use this function? in this case i want to check if a user is an admin, editingteacher, teacher, or student

i tried this from acc with editingteacher role:
has capability('editingteacher') return false
has capability('teacher') return false
has capability('editingteacher',NULL,$USER->id) return false

how should i use this funtion? can you give an example?

Thanks in advance... ^^
In reply to Andrew Leonheart

Re: [HELP] Moodle PHP script for user capabilities

by Mike Worth -
It potentially gets a little difficult, do you have system roles assigned? The reason I ask is that if teachers are not teachers except in the classes they teach then it is a bit difficult to ask 'is this a teacher' on the front page. What I do for things like this is have a system 'staff' role that does this sort of thing (allows access to room booking, shows various links etc.). Then it is a simple case of if(has_capability('[CAPABILITY]',get_context_instance(CONTEXT_SYSTEM)){
should do it.

Mike
Average of ratings: Useful (1)
In reply to Mike Worth

Re: [HELP] Moodle PHP script for user capabilities

by Andrew Leonheart -
What if i didn't assign system role, but instead i assign front page role??
Thanks in advance.. ^^
In reply to Andrew Leonheart

Re: [HELP] Moodle PHP script for user capabilities

by Mike Worth -
I can't say I've tried it but I would assume that the menus etc would show up properly only on the front page. (system roles apply everywhere, while frontpage roles only apply when on the front page)

Mike
In reply to Mike Worth

Re: [HELP] Moodle PHP script for user capabilities

by Andrew Leonheart -
Yeah, but if I assign it to system role, the course became a mess.. For example, i assign teacher 01 and teacher 02 as teacher in system role, an then i assign teacher 01 as teacher in course A...
In front page after a user logged in, Course A will be shown in front page, and there will be 2 Teacher in Course A (Teacher 01 and Teacher 02) instead of just one (Teacher 01).

Oh anyway, do you know how to get to know all course taught by a user and/or all course in which the user is a student? Is there any function in PHP script to know that?

Thanks in advance.
In reply to Andrew Leonheart

Re: [HELP] Moodle PHP script for user capabilities

by Mike Worth -
What you need is the teacher role assigned only in courses, then make a new 'staff' role that has very few capabilities and assign only this in system context.

Is get_my_courses() what you want? (you can go here and find documentation on all the functions, classes etc in moodle)

Mike
In reply to Mike Worth

Re: [HELP] Moodle PHP script for user capabilities

by Peter Wetz -
hi!


i would like to know all available capabilities. which ones are available?
and this "has_capability" function in the above example: how does it know from which user it checks the capability?

generally i want to find out if a user has "administrator" or at least "high" privileges/capabilities via php.

thanks in advance,
Peter
In reply to Peter Wetz

Re: [HELP] Moodle PHP script for user capabilities

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Look on the define roles screen for any role. That list all capabilities.

Alternatively, look at the capabilities table in the DB.

If nothing more specific is available, it sounds like you want moodle/system:config.
In reply to Tim Hunt

Re: [HELP] Moodle PHP script for user capabilities

by ranganath t.m -

hello tim, I am stuck up in a problem where i need help from u desperately.

so here is my prob, i need to identify the role of the user loggedin id like teacher,student or Admin. I have my own php file where i am trying to show time table for different users based on there logins. I am new for moodle so it ll be great pleasure if u help me out for this.

 

thanku

Ranganath.