source: moodle/trunk/fuentes/admin/tool/capability/locallib.php @ 136

Last change on this file since 136 was 136, checked in by mabarracus, 4 years ago

Ported code to xenial

File size: 4.0 KB
Line 
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Functions used by the capability tool.
19 *
20 * @package    tool_capability
21 * @copyright  2013 Sam Hemelryk
22 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25/**
26 * Calculates capability data organised by context for the given roles.
27 *
28 * @param string $capability The capability to get data for.
29 * @param array $roles An array of roles to get data for.
30 * @return context[] An array of contexts.
31 */
32function tool_capability_calculate_role_data($capability, array $roles) {
33    global $DB;
34
35    $systemcontext = context_system::instance();
36    $roleids = array_keys($roles);
37
38    // Work out the bits needed for the SQL WHERE clauses.
39    $params = array($capability);
40    list($sqlroletest, $roleparams) = $DB->get_in_or_equal($roleids);
41    $params = array_merge($params, $roleparams);
42    $sqlroletest = 'AND roleid ' . $sqlroletest;
43
44    // Get all the role_capabilities rows for this capability - that is, all
45    // role definitions, and all role overrides.
46    $sql = 'SELECT id, roleid, contextid, permission
47              FROM {role_capabilities}
48             WHERE capability = ? '.$sqlroletest;
49    $rolecaps = $DB->get_records_sql($sql, $params);
50
51    // In order to display a nice tree of contexts, we need to get all the
52    // ancestors of all the contexts in the query we just did.
53    $sql = 'SELECT DISTINCT con.path, 1
54              FROM {context} con
55              JOIN {role_capabilities} rc ON rc.contextid = con.id
56             WHERE capability = ? '.$sqlroletest;
57    $relevantpaths = $DB->get_records_sql_menu($sql, $params);
58    $requiredcontexts = array($systemcontext->id);
59    foreach ($relevantpaths as $path => $notused) {
60        $requiredcontexts = array_merge($requiredcontexts, explode('/', trim($path, '/')));
61    }
62    $requiredcontexts = array_unique($requiredcontexts);
63
64    // Now load those contexts.
65    list($sqlcontexttest, $contextparams) = $DB->get_in_or_equal($requiredcontexts);
66    $contexts = get_sorted_contexts('ctx.id ' . $sqlcontexttest, $contextparams);
67
68    // Prepare some empty arrays to hold the data we are about to compute.
69    foreach ($contexts as $conid => $con) {
70        $contexts[$conid]->children = array();
71        $contexts[$conid]->rolecapabilities = array();
72    }
73
74    // Put the contexts into a tree structure.
75    foreach ($contexts as $conid => $con) {
76        $context = context::instance_by_id($conid);
77        try {
78            $parentcontext = $context->get_parent_context();
79            if ($parentcontext) { // Will be false if $context is the system context.
80                $contexts[$parentcontext->id]->children[] = $conid;
81            }
82        } catch (dml_missing_record_exception $e) {
83            // Ignore corrupt context tree structure here. Don't let it break
84            // showing the rest of the report.
85            continue;
86        }
87    }
88
89    // Put the role capabilities into the context tree.
90    foreach ($rolecaps as $rolecap) {
91        $contexts[$rolecap->contextid]->rolecapabilities[$rolecap->roleid] = $rolecap->permission;
92    }
93
94    // Fill in any missing rolecaps for the system context.
95    foreach ($roleids as $roleid) {
96        if (!isset($contexts[$systemcontext->id]->rolecapabilities[$roleid])) {
97            $contexts[$systemcontext->id]->rolecapabilities[$roleid] = CAP_INHERIT;
98        }
99    }
100
101    return $contexts;
102}
Note: See TracBrowser for help on using the repository browser.