Share and be happy!

While additional Moodle functionality developing for one of our customers required check the user the presence of his only as a «Student» role for a given course, while if the user has the teacher or manager role, the function should return false. In Moodle there are two similar functions, but they perform a few more unnecessary problems for us, so we wrote a function. Here’s what happened.

First, to build an SQL-query is neccessary to find out what Moodle database tables are participate in a cycle of forming a user to verify the role of a particular rate, in this case five tables:

  • role_assignments — contexts relations table, users and roles
  • user_enrolments — relations table for enrolements and users
  • role — roles table
  • context — contexts table
  • enrol — enrolements table

We need to link these five tables, eventually we’ll get some records of related tables, and the number of records — the number of user roles in a needle course. It should take into account the possibility of checking only a single role, for example, the user can be student and manager of the course, and we need only those users who have only one role, such as «student».

Let’s build the SQL-query

Then we write a «naked» SQL-query, given that we have the default table prefix mdl_, let’s say that we have an internal user with id = 10, course id is 3, and the necessary role to verify is the student.

SELECT * FROM mdl_role_assignments AS ra LEFT JOIN mdl_user_enrolments AS ue ON ra.userid = ue.userid LEFT JOIN mdl_role AS r ON ra.roleid = LEFT JOIN mdl_context AS c ON = ra.contextid LEFT JOIN mdl_enrol AS e ON e.courseid = c.instanceid AND ue.enrolid = WHERE r.shortname = 'student' AND ue.userid = '10' AND e.courseid = '3';

As seen from the query, we can substitute the three variables, which will be sampled data:

  • the name of the role (by default available 6 roles: manager, coursecreator, editingteacher, teacher, student, guest)
  • user id
  • id of the course

Accordingly, we can to write a function with parameters for such a request. Do not forget about the Moodle data API and do properly executed SQL-based queries global object $DB.

function lms_service_is_user_has_role_in_course($user_id, $course_id, $single = true, $role = 'student') {
    global $DB;
    $sql = "SELECT * FROM {role_assignments} AS ra LEFT JOIN {user_enrolments} AS ue ON ra.userid = ue.userid LEFT JOIN {role} AS r ON ra.roleid = LEFT JOIN {context} AS c ON = ra.contextid LEFT JOIN {enrol} AS e ON e.courseid = c.instanceid AND ue.enrolid = WHERE r.shortname = ? AND ue.userid = ? AND e.courseid = ?";

    $result = $DB->get_records_sql($sql, array( $role, $user_id, $course_id ));
    if ( $result ) {
        if ( $single ) {
            if ( count( $result ) == 1 ) {
                return true;
            } else {
                return false;
        return true;
    return false;

Let’s look at parameters

  • $user_id — the inner specific user id
  • $course_id — needle internal course id
  • $single — variable that checks whether the user has provided a role in the course of a single (true/false, default is true)
  • $role — as the name (default is student)

Example of use

//course with id=3, user with id=12, must be only with coursecreator role
if ( lms_service_is_user_has_role_in_course(12, 3, true, 'coursecreator') ) {
    echo 'yes!';

Write a comment

Your email address will not be published. Required fields are marked *

Waiting feedback for mutually beneficial cooperation

Our services are b2b-oriented. We strive for high-quality Moodle services supply as it leads to higher income and satisfaction of our clients. If you have any questions or desires, please contact us.

A pop-up window

Contact us
Programming technologies Moodle: CSS3, AngularJS, Bootstrap

© 2014–2016, «LMS-Service». All rights reserved.