Commit 74f6cf80 authored by tjhunt's avatar tjhunt
Browse files

roles / profile: MDL-14937 A the user's roles report from contrib to core.

This is work in progress. I have integrated it into the profile tabs. Now I just need to make it work again.
parent cc89b990
......@@ -34,7 +34,7 @@
require_once($CFG->dirroot . '/' . $CFG->admin . '/roles/lib.php');
$contextid = required_param('contextid',PARAM_INT);
$contextuserid = optional_param('userid', 0, PARAM_INT); // needed for user tabs
$userid = optional_param('userid', 0, PARAM_INT); // needed for user tabs
$courseid = optional_param('courseid', 0, PARAM_INT); // needed for user tabs
if (! $context = get_context_instance_by_id($contextid)) {
......@@ -89,8 +89,8 @@
/// Print the header and tabs
if ($context->contextlevel == CONTEXT_USER) {
$contextuser = $DB->get_record('user', array('id' => $contextuserid));
$fullname = fullname($contextuser, has_capability('moodle/site:viewfullnames', $context));
$user = $DB->get_record('user', array('id' => $userid));
$fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
/// course header
$navlinks = array();
......@@ -98,7 +98,7 @@
if (has_capability('moodle/course:viewparticipants', get_context_instance(CONTEXT_COURSE, $courseid))) {
$navlinks[] = array('name' => get_string('participants'), 'link' => "$CFG->wwwroot/user/index.php?id=$courseid", 'type' => 'misc');
}
$navlinks[] = array('name' => $fullname, 'link' => "$CFG->wwwroot/user/view.php?id=$contextuserid&course=$courseid", 'type' => 'misc');
$navlinks[] = array('name' => $fullname, 'link' => "$CFG->wwwroot/user/view.php?id=$userid&course=$courseid", 'type' => 'misc');
$navlinks[] = array('name' => $straction, 'link' => null, 'type' => 'misc');
$navigation = build_navigation($navlinks);
......@@ -106,7 +106,7 @@
/// site header
} else {
$navlinks[] = array('name' => $fullname, 'link' => "$CFG->wwwroot/user/view.php?id=$contextuserid&course=$courseid", 'type' => 'misc');
$navlinks[] = array('name' => $fullname, 'link' => "$CFG->wwwroot/user/view.php?id=$userid&course=$courseid", 'type' => 'misc');
$navlinks[] = array('name' => $straction, 'link' => null, 'type' => 'misc');
$navigation = build_navigation($navlinks);
print_header($title, $course->fullname, $navigation, "", "", true, " ", navmenu($course));
......@@ -135,12 +135,12 @@
print_heading_with_help($title, 'checkpermissions');
/// If a user has been chosen, show all the permissions for this user.
$user = $userselector->get_selected_user();
if (!is_null($user)) {
$reportuser = $userselector->get_selected_user();
if (!is_null($reportuser)) {
print_box_start('generalbox boxaligncenter boxwidthwide');
print_heading(get_string('permissionsforuser', 'role', fullname($user)), '', 3);
print_heading(get_string('permissionsforuser', 'role', fullname($reportuser)), '', 3);
$table = new explain_capability_table($context, $user, $contextname);
$table = new explain_capability_table($context, $reportuser, $contextname);
$table->display();
print_box_end();
......@@ -155,8 +155,8 @@
/// Hidden fields.
echo '<input type="hidden" name="contextid" value="' . $context->id . '" />';
if (!empty($contextuserid)) {
echo '<input type="hidden" name="userid" value="' . $contextuserid . '" />';
if (!empty($userid)) {
echo '<input type="hidden" name="userid" value="' . $userid . '" />';
}
if ($courseid && $courseid != SITEID) {
echo '<input type="hidden" name="courseid" value="' . $courseid . '" />';
......
......@@ -179,35 +179,27 @@ $activetwo = array();
if ($context->contextlevel != CONTEXT_SYSTEM) { // Print tabs for anything except SYSTEM context
if ($context->contextlevel == CONTEXT_MODULE) { // Only show update button if module
$toprow[] = new tabobject('update', $CFG->wwwroot.'/course/mod.php?update='.
$context->instanceid.'&amp;return=true&amp;sesskey='.sesskey(), get_string('settings'));
}
if (!empty($assignableroles) || $currenttab=='assign') {
$toprow[] = new tabobject('assign',
$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$context->id,
get_string('localroles', 'role'),
'',
true);
$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$context->id,
get_string('localroles', 'role'), '', true);
}
if (!empty($overridableroles)) {
$toprow[] = new tabobject('override',
$CFG->wwwroot.'/'.$CFG->admin.'/roles/override.php?contextid='.$context->id,
get_string('overridepermissions', 'role'),
'',
true);
$CFG->wwwroot.'/'.$CFG->admin.'/roles/override.php?contextid='.$context->id,
get_string('overridepermissions', 'role'), '', true);
}
if (has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride',
'moodle/role:override', 'moodle/role:assign'), $context)) {
$toprow[] = new tabobject('check',
$CFG->wwwroot.'/'.$CFG->admin.'/roles/check.php?contextid='.$context->id,
get_string('checkpermissions', 'role'),
'',
true);
$CFG->wwwroot.'/'.$CFG->admin.'/roles/check.php?contextid='.$context->id,
get_string('checkpermissions', 'role'));
}
}
......
<?php // $Id$
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.org //
// //
// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////
/**
* User roles report list all the users who have been assigned a particular
* role in all contexts.
*
* @copyright &copy; 2007 The Open University and others
* @author t.j.hunt@open.ac.uk and others
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package roles
*//** */
require_once(dirname(__FILE__) . '/../../config.php');
// Get params.
$userid = required_param('userid', PARAM_INT);
$courseid = required_param('courseid', PARAM_INT);
// Validate them and get the corresponding objects.
$user = $DB->get_record('user', array('id' => $userid));
if (!$course = $DB->get_record('course', array('id' => $courseid))) {
print_error('invalidcourse', 'error');
}
$usercontext = get_context_instance(CONTEXT_USER, $user->id);
$coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
$baseurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/usersroles.php?userid='.$userid.'&amp;courseid='.$courseid;
/// Check login and permissions.
require_login($course);
$canview = has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride',
'moodle/role:override', 'moodle/role:manage'), $usercontext);
if (!$canview) {
print_error('nopermissions', 'error', '', get_string('checkpermissions', 'role'));
}
/// These are needed to determine which tabs tabs.php should show.
$assignableroles = get_assignable_roles($usercontext, ROLENAME_BOTH);
$overridableroles = get_overridable_roles($usercontext, ROLENAME_BOTH);
/// Print the header and tabs
$fullname = fullname($user, has_capability('moodle/site:viewfullnames', $coursecontext));
$straction = get_string('thisusersroles', 'role');
$title = get_string('xroleassignments', 'role', $fullname);
/// Course header
$navlinks = array();
if ($courseid != SITEID) {
if (has_capability('moodle/course:viewparticipants', $coursecontext)) {
$navlinks[] = array('name' => get_string('participants'), 'link' => "$CFG->wwwroot/user/index.php?id=$courseid", 'type' => 'misc');
}
$navlinks[] = array('name' => $fullname, 'link' => "$CFG->wwwroot/user/view.php?id=$userid&amp;course=$courseid", 'type' => 'misc');
$navlinks[] = array('name' => $straction, 'link' => null, 'type' => 'misc');
$navigation = build_navigation($navlinks);
print_header($title, $fullname, $navigation, '', '', true, '&nbsp;', navmenu($course));
/// Site header
} else {
$navlinks[] = array('name' => $fullname, 'link' => "$CFG->wwwroot/user/view.php?id=$userid&amp;course=$courseid", 'type' => 'misc');
$navlinks[] = array('name' => $straction, 'link' => null, 'type' => 'misc');
$navigation = build_navigation($navlinks);
print_header($title, $course->fullname, $navigation, '', '', true, '&nbsp;', navmenu($course));
}
$showroles = 1;
$currenttab = 'usersroles';
include_once($CFG->dirroot.'/user/tabs.php');
print_heading($title, '', 3);
echo 'Sorry, not complete yet, but I want to get this checked in before I go home.';
print_footer($course);
die; // TODO
// Standard moodleform if statement.
if ($mform->is_cancelled()) {
// Don't think this will ever happen, but do nothing.
} else if ($fromform = $mform->get_data()){
if (!(isset($fromform->username) && $user = $DB->get_record('user', array('username'=>$fromform->username)))) {
// We got data, but the username was invalid.
if (!isset($fromform->username)) {
$message = get_string('unknownuser', 'report_userroles');
} else {
$message = get_string('unknownusername', 'report_userroles', $fromform->username);
}
print_heading($message, '', 3);
} else {
// We have a valid username, do stuff.
$fullname = $fromform->username . ' (' . fullname($user) . ')';
// Do any role unassignments that were requested.
if ($tounassign = optional_param('unassign', array(), PARAM_SEQUENCE)) {
echo '<form method="post" action="', $CFG->wwwroot, '/admin/report/userroles/index.php">', "\n";
foreach ($tounassign as $assignment) {
list($contextid, $roleid) = explode(',', $assignment);
role_unassign($roleid, $user->id, 0, $contextid);
echo '<input type="hidden" name="assign[]" value="', $assignment, '" />', "\n";
}
notify(get_string('rolesunassigned', 'report_userroles'), 'notifysuccess');
form_fields_to_fool_mform($user->username, $mform);
echo '<input type="submit" value="', get_string('undounassign', 'report_userroles'), '" />', "\n";
echo '</form>', "\n";
// Do any role re-assignments that were requested.
} else if ($toassign = optional_param('assign', array(), PARAM_SEQUENCE)) {
foreach ($toassign as $assignment) {
list($contextid, $roleid) = explode(',', $assignment);
role_assign($roleid, $user->id, 0, $contextid);
}
notify(get_string('rolesreassigned', 'report_userroles'), 'notifysuccess');
}
// Now get the role assignments for this user.
$sql = "SELECT
ra.id, ra.userid, ra.contextid, ra.roleid, ra.enrol,
c.contextlevel, c.instanceid,
r.name AS role
FROM
{role_assignments} ra,
{context} c,
{role} r
WHERE
ra.userid = :userid
AND ra.contextid = c.id
AND ra.roleid = r.id
AND ra.active = 1
ORDER BY
contextlevel DESC, contextid ASC, r.sortorder ASC";
$results = $DB->get_records_sql($sql,array('userid'=>$user->id));
// Display them.
if ($results) {
print_heading(get_string('allassignments', 'report_userroles', $fullname), '', 3);
// Start of unassign form.
echo "\n\n";
echo '<form method="post" action="', $CFG->wwwroot, '/admin/report/userroles/index.php">', "\n";
// Print all the role assingments for this user.
$stredit = get_string('edit');
$strgoto = get_string('gotoassignroles', 'report_userroles');
foreach ($results as $result) {
$result->context = print_context_name($result, true, 'ou');
$value = $result->contextid . ',' . $result->roleid;
$inputid = 'unassign' . $value;
$unassignable = in_array($result->enrol,
array('manual', 'workflowengine', 'fridayeditingcron', 'oucourserole', 'staffrequest'));
echo '<p>';
if ($unassignable) {
echo '<input type="checkbox" name="unassign[]" value="', $value, '" id="', $inputid, '" />', "\n";
echo '<label for="', $inputid, '">';
}
echo get_string('incontext', 'report_userroles', $result);
if ($unassignable) {
echo '</label>';
}
echo ' <a title="', $strgoto, '" href="', $CFG->wwwroot, '/admin/roles/assign.php?contextid=',
$result->contextid, '&amp;roleid=', $result->roleid, '"><img ',
'src="', $CFG->pixpath, '/t/edit.gif" alt="[', $stredit, ']" /></a>';
echo "</p>\n";
}
echo "\n\n";
form_fields_to_fool_mform($user->username, $mform);
echo '<input type="submit" value="', get_string('unassignasabove', 'report_userroles'), '" />', "\n";
echo '</form>', "\n";
echo '<p>', get_string('unassignexplain', 'report_userroles'), "</p>\n\n";
} else {
print_heading(get_string('noassignmentsfound', 'report_userroles', $fullname), '', 3);
}
}
}
// Always show the form, so that the user can run another report.
echo "\n<br />\n<br />\n";
$mform->display();
admin_externalpage_print_footer();
function form_fields_to_fool_mform($username, $mform) {
echo '<input type="hidden" name="username" value="', $username, '" />', "\n";
echo '<input type="hidden" name="sesskey" value="', sesskey(), '" />', "\n";
echo '<input type="hidden" name="_qf__', $mform->get_name(), '" value="1" />', "\n";
}
?>
......@@ -258,5 +258,16 @@ $string['course:managegrades'] = 'Manage grades';
$string['user:editmessageprofile'] = 'Edit user messaging profile';
$string['user:editownmessageprofile'] = 'Edit own user messaging profile';
$string['assignrolesrelativetothisuser'] = 'Assign roles relative to this user';
$string['gotoassignroles'] = 'Go to the assign roles page';
$string['incontext'] = '<b>$a->role</b> in $a->context';
$string['noassignmentsfound'] = '$a has no role assignments';
$string['rolesreassigned'] = 'Roles re-assigned';
$string['rolesunassigned'] = 'Roles unassigned';
$string['thisusersroles'] = 'This user\'s roles';
$string['unassignasabove'] = 'Delete selected role assignments';
$string['unassignexplain'] = 'The form above can only be used to unassign roles that were originally assigned manually or via the workflow. You cannot change assignments that were made though CIRCE or via metacourses.';
$string['undounassign'] = 'Help! put those role assignments back';
$string['xroleassignments'] = '$a\'s role assignments';
?>
......@@ -221,20 +221,32 @@
if (!empty($showroles) and !empty($user)) { // this variable controls whether this roles is showed, or not, so only user/view page should set this flag
$usercontext = get_context_instance(CONTEXT_USER, $user->id);
if (has_capability('moodle/role:assign',$usercontext)) {
$toprow[] = new tabobject('roles', $CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$usercontext->id.'&amp;userid='.$user->id.'&amp;courseid='.$course->id
,get_string('roles'));
if (has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride',
'moodle/role:override', 'moodle/role:manage'), $usercontext)) {
$toprow[] = new tabobject('roles', $CFG->wwwroot.'/'.$CFG->admin.'/roles/usersroles.php?userid='.$user->id.'&amp;courseid='.$course->id
,get_string('roles'));
if (in_array($currenttab, array('assign', 'override'))) {
if (in_array($currenttab, array('usersroles', 'assign', 'override', 'check'))) {
$inactive = array('roles');
$activetwo = array('roles');
$secondrow = array();
$secondrow[] = new tabobject('assign', $CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$usercontext->id.'&amp;userid='.$user->id.'&amp;courseid='.$course->id
,get_string('localroles', 'role'));
$secondrow[] = new tabobject('override', $CFG->wwwroot.'/'.$CFG->admin.'/roles/override.php?contextid='.$usercontext->id.'&amp;userid='.$user->id.'&amp;courseid='.$course->id
,get_string('overridepermissions', 'role'));
$secondrow[] = new tabobject('usersroles', $CFG->wwwroot.'/'.$CFG->admin.'/roles/usersroles.php?userid='.$user->id.'&amp;courseid='.$course->id
,get_string('thisusersroles', 'role'));
if (!empty($assignableroles) || $currenttab=='assign') {
$secondrow[] = new tabobject('assign', $CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$usercontext->id.'&amp;userid='.$user->id.'&amp;courseid='.$course->id
,get_string('assignrolesrelativetothisuser', 'role'), '', true);
}
if (!empty($overridableroles) || $currenttab=='override') {
$secondrow[] = new tabobject('override', $CFG->wwwroot.'/'.$CFG->admin.'/roles/override.php?contextid='.$usercontext->id.'&amp;userid='.$user->id.'&amp;courseid='.$course->id
,get_string('overridepermissions', 'role'), '', true);
}
if (has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride',
'moodle/role:override', 'moodle/role:assign'), $usercontext)) {
$secondrow[] = new tabobject('check',
$CFG->wwwroot.'/'.$CFG->admin.'/roles/check.php?contextid='.$usercontext->id.'&amp;userid='.$user->id.'&amp;courseid='.$course->id,
get_string('checkpermissions', 'role'));
}
}
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment