From: tjhunt Date: Fri, 21 Nov 2008 08:23:57 +0000 (+0000) Subject: roles / profile: MDL-14937 A the user's roles report from contrib to core. X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=74f6cf8037d759502b7ea323e4206b4185f81356;p=moodle.git 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. --- diff --git a/admin/roles/check.php b/admin/roles/check.php index 59dd8425c3..2589a91a1a 100755 --- a/admin/roles/check.php +++ b/admin/roles/check.php @@ -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 ''; - if (!empty($contextuserid)) { - echo ''; + if (!empty($userid)) { + echo ''; } if ($courseid && $courseid != SITEID) { echo ''; diff --git a/admin/roles/tabs.php b/admin/roles/tabs.php index bebfb77c0b..05116468e0 100755 --- a/admin/roles/tabs.php +++ b/admin/roles/tabs.php @@ -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.'&return=true&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')); } } diff --git a/admin/roles/usersroles.php b/admin/roles/usersroles.php new file mode 100644 index 0000000000..c7018deb4c --- /dev/null +++ b/admin/roles/usersroles.php @@ -0,0 +1,214 @@ +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.'&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&course=$courseid", 'type' => 'misc'); + $navlinks[] = array('name' => $straction, 'link' => null, 'type' => 'misc'); + $navigation = build_navigation($navlinks); + + print_header($title, $fullname, $navigation, '', '', true, ' ', navmenu($course)); + +/// Site header +} else { + $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)); +} + +$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 '
', "\n"; + foreach ($tounassign as $assignment) { + list($contextid, $roleid) = explode(',', $assignment); + role_unassign($roleid, $user->id, 0, $contextid); + echo '', "\n"; + } + notify(get_string('rolesunassigned', 'report_userroles'), 'notifysuccess'); + form_fields_to_fool_mform($user->username, $mform); + echo '', "\n"; + echo '
', "\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 '
', "\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 '

'; + if ($unassignable) { + echo '', "\n"; + echo ''; + } + echo ' [', $stredit, ']'; + echo "

\n"; + } + + echo "\n\n"; + form_fields_to_fool_mform($user->username, $mform); + echo '', "\n"; + echo '
', "\n"; + echo '

', get_string('unassignexplain', 'report_userroles'), "

\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
\n
\n"; +$mform->display(); + +admin_externalpage_print_footer(); + +function form_fields_to_fool_mform($username, $mform) { + echo '', "\n"; + echo '', "\n"; + echo '', "\n"; +} +?> diff --git a/lang/en_utf8/role.php b/lang/en_utf8/role.php index a6d472ec8d..b19f50b43d 100644 --- a/lang/en_utf8/role.php +++ b/lang/en_utf8/role.php @@ -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'] = '$a->role 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'; ?> diff --git a/user/tabs.php b/user/tabs.php index 7fd23b2565..390e422e63 100644 --- a/user/tabs.php +++ b/user/tabs.php @@ -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.'&userid='.$user->id.'&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.'&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.'&userid='.$user->id.'&courseid='.$course->id - ,get_string('localroles', 'role')); - $secondrow[] = new tabobject('override', $CFG->wwwroot.'/'.$CFG->admin.'/roles/override.php?contextid='.$usercontext->id.'&userid='.$user->id.'&courseid='.$course->id - ,get_string('overridepermissions', 'role')); - + $secondrow[] = new tabobject('usersroles', $CFG->wwwroot.'/'.$CFG->admin.'/roles/usersroles.php?userid='.$user->id.'&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.'&userid='.$user->id.'&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.'&userid='.$user->id.'&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.'&userid='.$user->id.'&courseid='.$course->id, + get_string('checkpermissions', 'role')); + } } } }