Skip to content

Commit

Permalink
MDL-54035 accesslib: Add context->get_parent_context_paths()
Browse files Browse the repository at this point in the history
Returns full context paths for parent contexts. Useful for exact
matches, such as when checking dirtycontexts.
  • Loading branch information
jrchamp committed Sep 21, 2018
1 parent 1d049e0 commit 1113e34
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
43 changes: 37 additions & 6 deletions lib/accesslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -5319,7 +5319,7 @@ public function get_child_contexts() {
* Returns parent contexts of this context in reversed order, i.e. parent first,
* then grand parent, etc.
*
* @param bool $includeself tre means include self too
* @param bool $includeself true means include self too
* @return array of context instances
*/
public function get_parent_contexts($includeself = false) {
Expand All @@ -5337,10 +5337,10 @@ public function get_parent_contexts($includeself = false) {
}

/**
* Returns parent contexts of this context in reversed order, i.e. parent first,
* Returns parent context ids of this context in reversed order, i.e. parent first,
* then grand parent, etc.
*
* @param bool $includeself tre means include self too
* @param bool $includeself true means include self too
* @return array of context ids
*/
public function get_parent_context_ids($includeself = false) {
Expand All @@ -5357,6 +5357,35 @@ public function get_parent_context_ids($includeself = false) {
return array_reverse($parentcontexts);
}

/**
* Returns parent context paths of this context.
*
* @param bool $includeself true means include self too
* @return array of context paths
*/
public function get_parent_context_paths($includeself = false) {
if (empty($this->_path)) {
return array();
}

$contextids = explode('/', $this->_path);

$path = '';
$paths = array();
foreach ($contextids as $contextid) {
if ($contextid) {
$path .= '/' . $contextid;
$paths[$contextid] = $path;
}
}

if (!$includeself) {
unset($paths[$this->_id]);
}

return $paths;
}

/**
* Returns parent context
*
Expand Down Expand Up @@ -5455,9 +5484,11 @@ public function reload_if_dirty() {

if (!empty($ACCESSLIB_PRIVATE->dirtyusers[$USER->id])) {
$dirty = true;
} else {
foreach ($ACCESSLIB_PRIVATE->dirtycontexts as $path=>$unused) {
if ($path === $this->_path or strpos($this->_path, $path.'/') === 0) {
} else if (!empty($ACCESSLIB_PRIVATE->dirtycontexts)) {
$paths = $this->get_parent_context_paths(true);

foreach ($paths as $path) {
if (isset($ACCESSLIB_PRIVATE->dirtycontexts[$path])) {
$dirty = true;
break;
}
Expand Down
5 changes: 5 additions & 0 deletions lib/tests/accesslib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2733,12 +2733,17 @@ public function test_permission_evaluation() {
$this->assertEquals(array($systemcontext->id=>$systemcontext), $systemcontext->get_parent_contexts(true));
$this->assertEquals(array(), $systemcontext->get_parent_context_ids());
$this->assertEquals(array($systemcontext->id), $systemcontext->get_parent_context_ids(true));
$this->assertEquals(array(), $systemcontext->get_parent_context_paths());
$this->assertEquals(array($systemcontext->id => $systemcontext->path), $systemcontext->get_parent_context_paths(true));

$this->assertEquals($systemcontext, $frontpagecontext->get_parent_context());
$this->assertEquals(array($systemcontext->id=>$systemcontext), $frontpagecontext->get_parent_contexts());
$this->assertEquals(array($frontpagecontext->id=>$frontpagecontext, $systemcontext->id=>$systemcontext), $frontpagecontext->get_parent_contexts(true));
$this->assertEquals(array($systemcontext->id), $frontpagecontext->get_parent_context_ids());
$this->assertEquals(array($frontpagecontext->id, $systemcontext->id), $frontpagecontext->get_parent_context_ids(true));
$this->assertEquals(array($systemcontext->id => $systemcontext->path), $frontpagecontext->get_parent_context_paths());
$expected = array($systemcontext->id => $systemcontext->path, $frontpagecontext->id => $frontpagecontext->path);
$this->assertEquals($expected, $frontpagecontext->get_parent_context_paths(true));

$this->assertFalse($systemcontext->get_parent_context());
$frontpagecontext = context_course::instance($SITE->id);
Expand Down
1 change: 1 addition & 0 deletions lib/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ information provided here is intended especially for developers.
- Unassigning roles from users.
- Enroling users into courses.
- Unenroling users from courses.
* New function \context->get_parent_context_paths() efficiently returns all parent context paths for a context.

=== 3.5 ===

Expand Down

0 comments on commit 1113e34

Please sign in to comment.