Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/admin/roles/usersroles.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00029 require_once(dirname(__FILE__) . '/../../config.php');
00030 
00031 // Get params.
00032 $userid = required_param('userid', PARAM_INT);
00033 $courseid = required_param('courseid', PARAM_INT);
00034 
00035 // Validate them and get the corresponding objects.
00036 $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
00037 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
00038 
00039 $usercontext = get_context_instance(CONTEXT_USER, $user->id);
00040 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
00041 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
00042 
00043 $baseurl = new moodle_url('/admin/roles/usersroles.php', array('userid'=>$userid, 'courseid'=>$courseid));
00044 
00045 $PAGE->set_url($baseurl);
00046 $PAGE->set_context($coursecontext);
00047 $PAGE->set_pagelayout('admin');
00048 
00050 require_login($course);
00051 $canview = has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride',
00052         'moodle/role:override', 'moodle/role:manage'), $usercontext);
00053 if (!$canview) {
00054     print_error('nopermissions', 'error', '', get_string('checkpermissions', 'role'));
00055 }
00056 
00058 $sql = "SELECT
00059         ra.id, ra.userid, ra.contextid, ra.roleid, ra.component, ra.itemid,
00060         c.path,
00061         r.name AS rolename,
00062         COALESCE(rn.name, r.name) AS localname
00063     FROM
00064         {role_assignments} ra
00065         JOIN {context} c ON ra.contextid = c.id
00066         JOIN {role} r ON ra.roleid = r.id
00067         LEFT JOIN {role_names} rn ON rn.roleid = ra.roleid AND rn.contextid = ra.contextid
00068     WHERE
00069         ra.userid = ?
00070     "./*AND ra.active = 1*/"
00071     ORDER BY
00072         contextlevel DESC, contextid ASC, r.sortorder ASC";
00073 $roleassignments = $DB->get_records_sql($sql, array($user->id));
00074 
00077 $requiredcontexts = array();
00078 foreach ($roleassignments as $ra) {
00079     $requiredcontexts = array_merge($requiredcontexts, explode('/', trim($ra->path, '/')));
00080 }
00081 $requiredcontexts = array_unique($requiredcontexts);
00082 
00084 if ($requiredcontexts) {
00085     list($sqlcontexttest, $contextparams) = $DB->get_in_or_equal($requiredcontexts);
00086     $contexts = get_sorted_contexts('ctx.id ' . $sqlcontexttest, $contextparams);
00087 } else {
00088     $contexts = array();
00089 }
00090 
00092 foreach ($contexts as $conid => $con) {
00093     $contexts[$conid]->children = array();
00094     $contexts[$conid]->roleassignments = array();
00095 }
00096 
00098 foreach ($contexts as $conid => $con) {
00099     $context = context::instance_by_id($conid);
00100     $parentcontextid = get_parent_contextid($context);
00101     if ($parentcontextid) {
00102         $contexts[$parentcontextid]->children[] = $conid;
00103     }
00104 }
00105 
00107 foreach ($roleassignments as $ra) {
00108     $contexts[$ra->contextid]->roleassignments[$ra->roleid] = $ra;
00109 }
00110 
00111 $assignableroles = get_assignable_roles($usercontext, ROLENAME_BOTH);
00112 $overridableroles = get_overridable_roles($usercontext, ROLENAME_BOTH);
00113 
00115 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $coursecontext));
00116 $straction = get_string('thisusersroles', 'role');
00117 $title = get_string('xroleassignments', 'role', $fullname);
00118 
00120 $PAGE->set_title($title);
00121 if ($courseid != SITEID) {
00122     $PAGE->set_heading($fullname);
00123     if (has_capability('moodle/course:viewparticipants', $coursecontext)) {
00124         $PAGE->navbar->add(get_string('participants'),new moodle_url('/user/index.php', array('id'=>$courseid)));
00125     }
00126 } else {
00127     $PAGE->set_heading($course->fullname);
00128 }
00129 $PAGE->navbar->add($fullname, new moodle_url("$CFG->wwwroot/user/view.php", array('id'=>$userid,'course'=>$courseid)));
00130 $PAGE->navbar->add($straction);
00131 echo $OUTPUT->header();
00132 echo $OUTPUT->heading($title, 3);
00133 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthnormal');
00134 
00135 // Display them.
00136 if (!$roleassignments) {
00137     echo '<p>', get_string('noroleassignments', 'role'), '</p>';
00138 } else {
00139     print_report_tree($systemcontext->id, $contexts, $systemcontext, $fullname);
00140 }
00141 
00143 echo $OUTPUT->box_end();
00144 echo $OUTPUT->footer();
00145 
00146 function print_report_tree($contextid, $contexts, $systemcontext, $fullname) {
00147     global $CFG, $OUTPUT;
00148 
00149     // Only compute lang strings, etc once.
00150     static $stredit = null, $strcheckpermissions, $globalroleassigner, $assignurl, $checkurl;
00151     if (is_null($stredit)) {
00152         $stredit = get_string('edit');
00153         $strcheckpermissions = get_string('checkpermissions', 'role');
00154         $globalroleassigner = has_capability('moodle/role:assign', $systemcontext);
00155         $assignurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/assign.php';
00156         $checkurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/check.php';
00157     }
00158 
00159     // Pull the current context into an array for convinience.
00160     $context = context::instance_by_id($contextid);
00161 
00162     // Print the context name.
00163     echo $OUTPUT->heading($context->get_context_name(), 4, 'contextname');
00164 
00165     // If there are any role assignments here, print them.
00166     foreach ($contexts[$contextid]->roleassignments as $ra) {
00167         $value = $ra->contextid . ',' . $ra->roleid;
00168         $inputid = 'unassign' . $value;
00169 
00170         echo '<p>';
00171         if ($ra->rolename == $ra->localname) {
00172             echo strip_tags(format_string($ra->localname));
00173         } else {
00174             echo strip_tags(format_string($ra->localname . ' (' . $ra->rolename . ')'));
00175         }
00176         if (has_capability('moodle/role:assign', $context)) {
00177             $raurl = $assignurl . '?contextid=' . $ra->contextid . '&amp;roleid=' .
00178                     $ra->roleid . '&amp;removeselect[]=' . $ra->userid;
00179             $churl = $checkurl . '?contextid=' . $ra->contextid . '&amp;reportuser=' . $ra->userid;
00180             if ($context->contextlevel == CONTEXT_USER) {
00181                 $raurl .= '&amp;userid=' . $context->instanceid;
00182                 $churl .= '&amp;userid=' . $context->instanceid;
00183             }
00184             $a = new stdClass;
00185             $a->fullname = $fullname;
00186             $a->contextlevel = get_contextlevel_name($context->contextlevel);
00187             if ($context->contextlevel == CONTEXT_SYSTEM) {
00188                 $strgoto = get_string('gotoassignsystemroles', 'role');
00189                 $strcheck = get_string('checksystempermissionsfor', 'role', $a);
00190             } else {
00191                 $strgoto = get_string('gotoassignroles', 'role', $a);
00192                 $strcheck = get_string('checkuserspermissionshere', 'role', $a);
00193             }
00194             echo ' <a title="' . $strgoto . '" href="' . $raurl . '"><img class="iconsmall" src="' .
00195                     $OUTPUT->pix_url('t/edit') . '" alt="' . $stredit . '" /></a> ';
00196             echo ' <a title="' . $strcheck . '" href="' . $churl . '"><img class="iconsmall" src="' .
00197                     $OUTPUT->pix_url('t/preview') . '" alt="' . $strcheckpermissions . '" /></a> ';
00198             echo "</p>\n";
00199         }
00200     }
00201 
00202     // If there are any child contexts, print them recursively.
00203     if (!empty($contexts[$contextid]->children)) {
00204         echo '<ul>';
00205         foreach ($contexts[$contextid]->children as $childcontextid) {
00206             echo '<li>';
00207             print_report_tree($childcontextid, $contexts, $systemcontext, $fullname);
00208             echo '</li>';
00209         }
00210         echo '</ul>';
00211     }
00212 }
 All Data Structures Namespaces Files Functions Variables Enumerations