1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace core\context;
|
|
|
18 |
|
|
|
19 |
use core\context;
|
|
|
20 |
use stdClass;
|
|
|
21 |
use coding_exception, moodle_url;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Block context class
|
|
|
25 |
*
|
|
|
26 |
* @package core_access
|
|
|
27 |
* @category access
|
|
|
28 |
* @copyright Petr Skoda
|
|
|
29 |
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
30 |
* @since Moodle 4.2
|
|
|
31 |
*/
|
|
|
32 |
class block extends context {
|
|
|
33 |
/** @var int numeric context level value matching legacy CONTEXT_BLOCK */
|
|
|
34 |
public const LEVEL = 80;
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Please use \core\context\block::instance($blockinstanceid) if you need the instance of context.
|
|
|
38 |
* Alternatively if you know only the context id use \core\context::instance_by_id($contextid)
|
|
|
39 |
*
|
|
|
40 |
* @param stdClass $record
|
|
|
41 |
*/
|
|
|
42 |
protected function __construct(stdClass $record) {
|
|
|
43 |
parent::__construct($record);
|
|
|
44 |
if ($record->contextlevel != self::LEVEL) {
|
|
|
45 |
throw new coding_exception('Invalid $record->contextlevel in core\context\block constructor');
|
|
|
46 |
}
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Returns short context name.
|
|
|
51 |
*
|
|
|
52 |
* @since Moodle 4.2
|
|
|
53 |
*
|
|
|
54 |
* @return string
|
|
|
55 |
*/
|
|
|
56 |
public static function get_short_name(): string {
|
|
|
57 |
return 'block';
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Returns human readable context level name.
|
|
|
62 |
*
|
|
|
63 |
* @return string the human readable context level name.
|
|
|
64 |
*/
|
|
|
65 |
public static function get_level_name() {
|
|
|
66 |
return get_string('block');
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* Returns human readable context identifier.
|
|
|
71 |
*
|
|
|
72 |
* @param boolean $withprefix whether to prefix the name of the context with Block
|
|
|
73 |
* @param boolean $short does not apply to block context
|
|
|
74 |
* @param boolean $escape does not apply to block context
|
|
|
75 |
* @return string the human readable context name.
|
|
|
76 |
*/
|
|
|
77 |
public function get_context_name($withprefix = true, $short = false, $escape = true) {
|
11 |
efrain |
78 |
global $DB;
|
1 |
efrain |
79 |
|
|
|
80 |
$name = '';
|
|
|
81 |
if ($blockinstance = $DB->get_record('block_instances', array('id' => $this->_instanceid))) {
|
11 |
efrain |
82 |
$blockobject = block_instance($blockinstance->blockname);
|
|
|
83 |
if ($blockobject) {
|
1 |
efrain |
84 |
if ($withprefix) {
|
|
|
85 |
$name = get_string('block').': ';
|
|
|
86 |
}
|
|
|
87 |
$name .= $blockobject->title;
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
return $name;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* Returns the most relevant URL for this context.
|
|
|
96 |
*
|
|
|
97 |
* @return moodle_url
|
|
|
98 |
*/
|
|
|
99 |
public function get_url() {
|
|
|
100 |
$parentcontexts = $this->get_parent_context();
|
|
|
101 |
return $parentcontexts->get_url();
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Returns list of all possible parent context levels.
|
|
|
106 |
* @since Moodle 4.2
|
|
|
107 |
*
|
|
|
108 |
* @return int[]
|
|
|
109 |
*/
|
|
|
110 |
public static function get_possible_parent_levels(): array {
|
|
|
111 |
// Blocks may be added to any other context instance.
|
|
|
112 |
$alllevels = \core\context_helper::get_all_levels();
|
|
|
113 |
unset($alllevels[self::LEVEL]);
|
|
|
114 |
return array_keys($alllevels);
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Returns array of relevant context capability records.
|
|
|
119 |
*
|
|
|
120 |
* @param string $sort
|
|
|
121 |
* @return array
|
|
|
122 |
*/
|
|
|
123 |
public function get_capabilities(string $sort = self::DEFAULT_CAPABILITY_SORT) {
|
|
|
124 |
global $DB;
|
|
|
125 |
|
|
|
126 |
$bi = $DB->get_record('block_instances', array('id' => $this->_instanceid));
|
|
|
127 |
|
|
|
128 |
$select = '(contextlevel = :level AND component = :component)';
|
|
|
129 |
$params = [
|
|
|
130 |
'level' => self::LEVEL,
|
|
|
131 |
'component' => 'block_' . $bi->blockname,
|
|
|
132 |
];
|
|
|
133 |
|
|
|
134 |
$extracaps = block_method_result($bi->blockname, 'get_extra_capabilities');
|
|
|
135 |
if ($extracaps) {
|
|
|
136 |
list($extra, $extraparams) = $DB->get_in_or_equal($extracaps, SQL_PARAMS_NAMED, 'cap');
|
|
|
137 |
$select .= " OR name $extra";
|
|
|
138 |
$params = array_merge($params, $extraparams);
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
return $DB->get_records_select('capabilities', $select, $params, $sort);
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
/**
|
|
|
145 |
* Is this context part of any course? If yes return course context.
|
|
|
146 |
*
|
|
|
147 |
* @param bool $strict true means throw exception if not found, false means return false if not found
|
|
|
148 |
* @return course context of the enclosing course, null if not found or exception
|
|
|
149 |
*/
|
|
|
150 |
public function get_course_context($strict = true) {
|
|
|
151 |
$parentcontext = $this->get_parent_context();
|
|
|
152 |
return $parentcontext->get_course_context($strict);
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
/**
|
|
|
156 |
* Returns block context instance.
|
|
|
157 |
*
|
|
|
158 |
* @param int $blockinstanceid id from {block_instances} table.
|
|
|
159 |
* @param int $strictness
|
|
|
160 |
* @return block|false context instance
|
|
|
161 |
*/
|
|
|
162 |
public static function instance($blockinstanceid, $strictness = MUST_EXIST) {
|
|
|
163 |
global $DB;
|
|
|
164 |
|
|
|
165 |
if ($context = context::cache_get(self::LEVEL, $blockinstanceid)) {
|
|
|
166 |
return $context;
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
if (!$record = $DB->get_record('context', array('contextlevel' => self::LEVEL, 'instanceid' => $blockinstanceid))) {
|
|
|
170 |
if ($bi = $DB->get_record('block_instances', array('id' => $blockinstanceid), 'id,parentcontextid', $strictness)) {
|
|
|
171 |
$parentcontext = context::instance_by_id($bi->parentcontextid);
|
|
|
172 |
$record = context::insert_context_record(self::LEVEL, $bi->id, $parentcontext->path);
|
|
|
173 |
}
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
if ($record) {
|
|
|
177 |
$context = new block($record);
|
|
|
178 |
context::cache_add($context);
|
|
|
179 |
return $context;
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
return false;
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
/**
|
|
|
186 |
* Block do not have child contexts...
|
|
|
187 |
* @return array
|
|
|
188 |
*/
|
|
|
189 |
public function get_child_contexts() {
|
|
|
190 |
return array();
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
/**
|
|
|
194 |
* Create missing context instances at block context level
|
|
|
195 |
*/
|
|
|
196 |
protected static function create_level_instances() {
|
|
|
197 |
global $DB;
|
|
|
198 |
|
|
|
199 |
$sql = <<<EOF
|
|
|
200 |
INSERT INTO {context} (
|
|
|
201 |
contextlevel,
|
|
|
202 |
instanceid
|
|
|
203 |
) SELECT
|
|
|
204 |
:contextlevel,
|
|
|
205 |
bi.id as instanceid
|
|
|
206 |
FROM {block_instances} bi
|
|
|
207 |
WHERE NOT EXISTS (
|
|
|
208 |
SELECT 'x' FROM {context} cx WHERE bi.id = cx.instanceid AND cx.contextlevel = :existingcontextlevel
|
|
|
209 |
)
|
|
|
210 |
EOF;
|
|
|
211 |
|
|
|
212 |
$DB->execute($sql, [
|
|
|
213 |
'contextlevel' => self::LEVEL,
|
|
|
214 |
'existingcontextlevel' => self::LEVEL,
|
|
|
215 |
]);
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
/**
|
|
|
219 |
* Returns sql necessary for purging of stale context instances.
|
|
|
220 |
*
|
|
|
221 |
* @return string cleanup SQL
|
|
|
222 |
*/
|
|
|
223 |
protected static function get_cleanup_sql() {
|
|
|
224 |
$sql = "
|
|
|
225 |
SELECT c.*
|
|
|
226 |
FROM {context} c
|
|
|
227 |
LEFT OUTER JOIN {block_instances} bi ON c.instanceid = bi.id
|
|
|
228 |
WHERE bi.id IS NULL AND c.contextlevel = ".self::LEVEL."
|
|
|
229 |
";
|
|
|
230 |
|
|
|
231 |
return $sql;
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
/**
|
|
|
235 |
* Rebuild context paths and depths at block context level.
|
|
|
236 |
*
|
|
|
237 |
* @param bool $force
|
|
|
238 |
*/
|
|
|
239 |
protected static function build_paths($force) {
|
|
|
240 |
global $DB;
|
|
|
241 |
|
|
|
242 |
if ($force || $DB->record_exists_select('context', "contextlevel = ".self::LEVEL." AND (depth = 0 OR path IS NULL)")) {
|
|
|
243 |
if ($force) {
|
|
|
244 |
$ctxemptyclause = '';
|
|
|
245 |
} else {
|
|
|
246 |
$ctxemptyclause = "AND (ctx.path IS NULL OR ctx.depth = 0)";
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
// The pctx.path IS NOT NULL prevents fatal problems with broken block instances that point to invalid context parent.
|
|
|
250 |
$sql = "INSERT INTO {context_temp} (id, path, depth, locked)
|
|
|
251 |
SELECT ctx.id, ".$DB->sql_concat('pctx.path', "'/'", 'ctx.id').", pctx.depth+1, ctx.locked
|
|
|
252 |
FROM {context} ctx
|
|
|
253 |
JOIN {block_instances} bi ON (bi.id = ctx.instanceid AND ctx.contextlevel = " . self::LEVEL . ")
|
|
|
254 |
JOIN {context} pctx ON (pctx.id = bi.parentcontextid)
|
|
|
255 |
WHERE (pctx.path IS NOT NULL AND pctx.depth > 0)
|
|
|
256 |
$ctxemptyclause";
|
|
|
257 |
$trans = $DB->start_delegated_transaction();
|
|
|
258 |
$DB->delete_records('context_temp');
|
|
|
259 |
$DB->execute($sql);
|
|
|
260 |
context::merge_context_temp_table();
|
|
|
261 |
$DB->delete_records('context_temp');
|
|
|
262 |
$trans->allow_commit();
|
|
|
263 |
}
|
|
|
264 |
}
|
|
|
265 |
}
|