diff --git a/locallib.php b/locallib.php index 6c3c8f9..09d76af 100644 --- a/locallib.php +++ b/locallib.php @@ -456,6 +456,38 @@ function lti_map_keyname($key) { return $newkey; } +function get_user_roles_with_archetypes(context $context, $userid = 0, $checkparentcontexts = true, $order = 'c.contextlevel DESC, r.sortorder ASC') { + global $USER, $DB; + + if (empty($userid)) { + if (empty($USER->id)) { + return array(); + } + $userid = $USER->id; + } + + if ($checkparentcontexts) { + $contextids = $context->get_parent_context_ids(); + } else { + $contextids = array(); + } + $contextids[] = $context->id; + + list($contextids, $params) = $DB->get_in_or_equal($contextids, SQL_PARAMS_QM); + + array_unshift($params, $userid); + + $sql = "SELECT ra.*, r.name, r.shortname, r.archetype + FROM {role_assignments} ra, {role} r, {context} c + WHERE ra.userid = ? + AND ra.roleid = r.id + AND ra.contextid = c.id + AND ra.contextid $contextids + ORDER BY $order"; + + return $DB->get_records_sql($sql, $params); +} + /** * Gets the IMS role string for the specified user and LTI course module. * @@ -465,33 +497,42 @@ function lti_map_keyname($key) { */ function lti_get_ims_role($user, $cmid, $courseid) { $roles = array(); + $archetypes_roles = Array('student' => 'Learner', 'teacher' => 'Instructor', 'editingteacher' => 'Instructor', 'manager' => 'Administrator'); if (empty($cmid)) { //If no cmid is passed, check if the user is a teacher in the course //This allows other modules to programmatically "fake" a launch without //a real LTI instance - $coursecontext = context_course::instance($courseid); - - if (has_capability('moodle/course:manageactivities', $coursecontext)) { - array_push($roles, 'Instructor'); - } else { - array_push($roles, 'Learner'); + $coursecontext = context_course::instance($courseid); + $user_roles = get_user_roles_with_archetypes($coursecontext, $user->id); + foreach ($user_roles as $role) { + array_push($roles, "urn:moodle:role/{$role->shortname}"); + if (!empty($role->archetype) && !empty($archetypes_roles[$role->archetype])) { + array_push($roles, "urn:lti:role:ims/lis/{$archetypes_roles[$role->archetype]}"); + } } + } else { $context = context_module::instance($cmid); + $user_roles = get_user_roles_with_archetypes($context, $user->id); - if (has_capability('mod/lti:manage', $context)) { - array_push($roles, 'Instructor'); - } else { - array_push($roles, 'Learner'); + foreach ($user_roles as $role) { + array_push($roles, "urn:moodle:role/{$role->shortname}"); + if (!empty($role->archetype) && !empty($archetypes_roles[$role->archetype])) { + array_push($roles, "urn:lti:role:ims/lis/{$archetypes_roles[$role->archetype]}"); + } } + } if (is_siteadmin($user)) { array_push($roles, 'urn:lti:sysrole:ims/lis/Administrator'); } - return join(',', $roles); }