diff --git a/mod/lti/locallib.php b/mod/lti/locallib.php index 103ebf2..4d15d39 100644 --- a/mod/lti/locallib.php +++ b/mod/lti/locallib.php @@ -467,6 +467,46 @@ function lti_map_keyname($key) { } /** + * Gets the user roles and archetypes to be used as LTI launch data + * @param context $context + * @param int $userid + * @param bool $checkparentcontexts + * @param string $order + * @return array + */ +function lti_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. * * @param mixed $user User object or user id @@ -475,25 +515,24 @@ 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'); - } + $context = context_course::instance($courseid); } else { $context = context_module::instance($cmid); + } - if (has_capability('mod/lti:manage', $context)) { - array_push($roles, 'Instructor'); - } else { - array_push($roles, 'Learner'); + $user_roles = lti_get_user_roles_with_archetypes($context, $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]}"); } }