User roleid in a course check by SQL-Statement

User roleid in a course check by SQL-Statement

by Moritz Grey -
Number of replies: 5

Dear experts,

can I get the role_ID of a user in a course with every SQL Statemant?

I also want to know what is the role of the personal user in the course?



Thank you for your help!

Moritz Gray

Average of ratings: -
In reply to Moritz Grey

Re: User roleid in a course check by SQL-Statement

by Matt T -
What context is this arising in? Are you developing a Moodle plugin? What is the nature of the code that is needs to know the role of the user - in other words - what is it supposed to do?

On my part, this context would be helpful.

Also, it's not clear to me what the difference is between the second and third line of your post. What do you mean by personal user?
In reply to Matt T

Re: User roleid in a course check by SQL-Statement

by Moritz Grey -
Hey, thanks for answer, 

i want to create a Table which the Follows Infosmation

Coursname: (Example) Business English 
Firstname | Lastname | Role 
Example 
Roy | Black | Teacher
Chris | Martin | Student 

etc.... 

Thanks for Help 

MoGr
In reply to Moritz Grey

Re: User roleid in a course check by SQL-Statement

by Matt T -
What I really would like to know is what are you are trying to achieve as an end result. If you can, try and put it in the form of a 'story'. For example:

-  "I want to be able to display a report that shows me 'teachers' and 'students' in a course."
- "I want a block to only be visible to 'teachers' and not to 'students'."
- "I want to allow 'teachers' to add 'students' to a course."

Moodle has roles functionality built-in. 'Teacher' and 'student' are, in fact, predefined. I wouldn't recommend implementing your own table for this. I'm trying to figure out whether there's a reason why you can't just use the standard roles/permissions functionality.

https://docs.moodle.org/310/en/Roles_and_permissions
In reply to Matt T

Re: User roleid in a course check by SQL-Statement

by Moritz Grey -
Thanks for Help again smile

Sorry, i wan't to create with Help from php a Skript, which create me a Table of
all Courses whith the enroled Users.

Now my skript gives me the follow information:

1. Name of Course
2. User-Firstname
3. User-Lastname


But i cannot find out the role form the User. Is the User Teacher, Student, Admin, etc....

How can i find out This Role, special:

I want to know which role hast the user with the id (User-ID) 50 in the course which have the id (Course-ID) 75.

Thanks for Help smile

MoGr
In reply to Moritz Grey

Re: User roleid in a course check by SQL-Statement

by Matt T -
I'd suggest a moderator move this post to the development forum.

You're going to have a fair bit of work to do doing this via hand-coded SQL. See the schema pic here (not sure if it's up to date). You at least need to join on several tables.

https://docs.moodle.org/dev/File:RolesDatabase.png

Part of the difficulty is that roles are set per 'context' - so a student in one course might be a teacher in another course - so you have to account for that.

You might be better off actually using some Moodle code to do this. But that requires a bit of knowledge of Moodle internals, and you shouldn't place a PHP script on a public web server unless you know what you are doing (or you risk data exposure) - eg you may want to place is_siteadmin() || die(); at the top of the script.

Here's an example of code that may help achieving what you're after, although I can't vouch for its correctness:

https://moodle.org/mod/forum/discuss.php?d=257611#p1117390