Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/admin/tool/capability/index.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00027 require_once(dirname(__FILE__) . '/../../../config.php');
00028 require_once($CFG->libdir.'/adminlib.php');
00029 
00030 // Check permissions.
00031 require_login();
00032 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
00033 require_capability('moodle/role:manage', $systemcontext);
00034 
00035 // Get URL parameters.
00036 $capability = optional_param('capability', '', PARAM_CAPABILITY);
00037 $roleids = optional_param_array('roles', array('0'), PARAM_INTEGER);
00038 
00039 // Clean the passed in list of role ids. If 'All' selected as an option, or
00040 // if none were selected, do all roles.
00041 $allroles = get_all_roles();
00042 $cleanedroleids = array();
00043 foreach ($roleids as $roleid) {
00044     if ($roleid == 0) {
00045         $cleanedroleids = array_keys($allroles);
00046         break;
00047     }
00048     if (array_key_exists($roleid, $allroles)) {
00049         $cleanedroleids[] = $roleid;
00050     }
00051 }
00052 if (empty($cleanedroleids)) {
00053     $cleanedroleids = array_keys($allroles);
00054 }
00055 
00056 // Include the required JavaScript.
00057 $PAGE->requires->js_init_call('M.tool_capability.init', array(get_string('search')));
00058 
00059 // Log.
00060 add_to_log(SITEID, "admin", "tool capability", "tool/capability/index.php?capability=$capability", $capability);
00061 
00062 // Print the header.
00063 admin_externalpage_setup('toolcapability');
00064 echo $OUTPUT->header();
00065 
00066 // Prepare the list of capabilities to choose from
00067 $allcapabilities = fetch_context_capabilities($systemcontext);
00068 $capabilitychoices = array();
00069 foreach ($allcapabilities as $cap) {
00070     $capabilitychoices[$cap->name] = $cap->name . ': ' . get_capability_string($cap->name);
00071 }
00072 
00073 // Prepare the list of roles to choose from
00074 $rolechoices = array('0' => get_string('all'));
00075 foreach ($allroles as $role) {
00076     $rolechoices[$role->id] = $role->name;
00077 }
00078 if (count($cleanedroleids) == count($allroles)) {
00079     // Select 'All', rather than each role individually.
00080     $selectedroleids = array('0');
00081 } else {
00082     $selectedroleids = $cleanedroleids;
00083 }
00084 
00085 // Print the settings form.
00086 echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter');
00087 echo '<form method="get" action="" id="settingsform"><div>';
00088 echo $OUTPUT->heading(get_string('reportsettings', 'tool_capability'));
00089 echo '<p id="intro">', get_string('intro', 'tool_capability') , '</p>';
00090 echo '<p><label for="menucapability"> ' . get_string('capabilitylabel', 'tool_capability') . '</label></p>';
00091 echo  html_writer::select($capabilitychoices, 'capability', $capability, array(''=>'choose'), array('size'=>10));
00092 echo '<p><label for="menuroles"> ' . get_string('roleslabel', 'tool_capability') . '</label></p>';
00093 echo  html_writer::select($rolechoices, 'roles[]', $selectedroleids, false, array('size'=>10, 'multiple'=>'multiple'));
00094 echo '<p><input type="submit" id="settingssubmit" value="' . get_string('getreport', 'tool_capability') . '" /></p>';
00095 echo '</div></form>';
00096 echo $OUTPUT->box_end();
00097 
00098 // If we have a capability, generate the report.
00099 if ($capability) {
00100 
00101     // Work out the bits needed for the SQL WHERE clauses.
00102     $params = array($capability);
00103     $sqlroletest = '';
00104     if (count($cleanedroleids) != count($allroles)) {
00105         list($sqlroletest, $roleparams) = $DB->get_in_or_equal($cleanedroleids);
00106         $params = array_merge($params, $roleparams);
00107         $sqlroletest = 'AND roleid ' . $sqlroletest;
00108     }
00109 
00110     // Get all the role_capabilities rows for this capability - that is, all
00111     // role definitions, and all role overrides.
00112     $rolecaps = $DB->get_records_sql("
00113             SELECT id, roleid, contextid, permission
00114             FROM {role_capabilities}
00115             WHERE capability = ? $sqlroletest", $params);
00116 
00117     // In order to display a nice tree of contexts, we need to get all the
00118     // ancestors of all the contexts in the query we just did.
00119     $relevantpaths = $DB->get_records_sql_menu("
00120             SELECT DISTINCT con.path, 1
00121             FROM {context} con JOIN {role_capabilities} rc ON rc.contextid = con.id
00122             WHERE capability = ? $sqlroletest", $params);
00123     $requiredcontexts = array($systemcontext->id);
00124     foreach ($relevantpaths as $path => $notused) {
00125         $requiredcontexts = array_merge($requiredcontexts, explode('/', trim($path, '/')));
00126     }
00127     $requiredcontexts = array_unique($requiredcontexts);
00128 
00129     // Now load those contexts.
00130     list($sqlcontexttest, $contextparams) = $DB->get_in_or_equal($requiredcontexts);
00131     $contexts = get_sorted_contexts('ctx.id ' . $sqlcontexttest, $contextparams);
00132 
00133     // Prepare some empty arrays to hold the data we are about to compute.
00134     foreach ($contexts as $conid => $con) {
00135         $contexts[$conid]->children = array();
00136         $contexts[$conid]->rolecapabilities = array();
00137     }
00138 
00139     // Put the contexts into a tree structure.
00140     foreach ($contexts as $conid => $con) {
00141         $context = context::instance_by_id($conid);
00142         $parentcontextid = get_parent_contextid($context);
00143         if ($parentcontextid) {
00144             $contexts[$parentcontextid]->children[] = $conid;
00145         }
00146     }
00147 
00148     // Put the role capabilities into the context tree.
00149     foreach ($rolecaps as $rolecap) {
00150         $contexts[$rolecap->contextid]->rolecapabilities[$rolecap->roleid] = $rolecap->permission;
00151     }
00152 
00153     // Fill in any missing rolecaps for the system context.
00154     foreach ($cleanedroleids as $roleid) {
00155         if (!isset($contexts[$systemcontext->id]->rolecapabilities[$roleid])) {
00156             $contexts[$systemcontext->id]->rolecapabilities[$roleid] = CAP_INHERIT;
00157         }
00158     }
00159 
00160     // Print the report heading.
00161     echo $OUTPUT->heading(get_string('reportforcapability', 'tool_capability', get_capability_string($capability)), 2, 'main', 'report');
00162     if (count($cleanedroleids) != count($allroles)) {
00163         $rolenames = array();
00164         foreach ($cleanedroleids as $roleid) {
00165             $rolenames[] = $allroles[$roleid]->name;
00166         }
00167         echo '<p>', get_string('forroles', 'tool_capability', implode(', ', $rolenames)), '</p>';
00168     }
00169 
00170     // Now, recursively print the contexts, and the role information.
00171     print_report_tree($systemcontext->id, $contexts, $allroles);
00172 }
00173 
00174 // Footer.
00175 echo $OUTPUT->footer();
00176 
00177 function print_report_tree($contextid, $contexts, $allroles) {
00178     global $CFG;
00179 
00180     // Array for holding lang strings.
00181     static $strpermissions = null;
00182     if (is_null($strpermissions)) {
00183         $strpermissions = array(
00184             CAP_INHERIT => get_string('notset','role'),
00185             CAP_ALLOW => get_string('allow','role'),
00186             CAP_PREVENT => get_string('prevent','role'),
00187             CAP_PROHIBIT => get_string('prohibit','role')
00188         );
00189     }
00190 
00191     // Start the list item, and print the context name as a link to the place to
00192     // make changes.
00193     if ($contextid == get_system_context()->id) {
00194         $url = "$CFG->wwwroot/$CFG->admin/roles/manage.php";
00195         $title = get_string('changeroles', 'tool_capability');
00196     } else {
00197         $url = "$CFG->wwwroot/$CFG->admin/roles/override.php?contextid=$contextid";
00198         $title = get_string('changeoverrides', 'tool_capability');
00199     }
00200     $context = context::instance_by_id($contextid);
00201     echo '<h3><a href="' . $url . '" title="' . $title . '">', $context->get_context_name(), '</a></h3>';
00202 
00203     // If there are any role overrides here, print them.
00204     if (!empty($contexts[$contextid]->rolecapabilities)) {
00205         $rowcounter = 0;
00206         echo '<table class="generaltable rolecaps"><tbody>';
00207         foreach ($allroles as $role) {
00208             if (isset($contexts[$contextid]->rolecapabilities[$role->id])) {
00209                 $permission = $contexts[$contextid]->rolecapabilities[$role->id];
00210                 echo '<tr class="r' . ($rowcounter % 2) . '"><th class="cell">', $role->name,
00211                         '</th><td class="cell">' . $strpermissions[$permission] . '</td></tr>';
00212                 $rowcounter++;
00213             }
00214         }
00215         echo '</tbody></table>';
00216     }
00217 
00218     // After we have done the site context, change the string for CAP_INHERIT
00219     // from 'notset' to 'inherit'.
00220     $strpermissions[CAP_INHERIT] = get_string('inherit','role');
00221 
00222     // If there are any child contexts, print them recursively.
00223     if (!empty($contexts[$contextid]->children)) {
00224         echo '<ul>';
00225         foreach ($contexts[$contextid]->children as $childcontextid) {
00226             echo '<li>';
00227             print_report_tree($childcontextid, $contexts, $allroles);
00228             echo '</li>';
00229         }
00230         echo '</ul>';
00231     }
00232 }
 All Data Structures Namespaces Files Functions Variables Enumerations