Merge branch 'MDL-61519-master' of https://github.com/MartinGauk/moodle

This commit is contained in:
Andrew Nicols
2018-03-20 13:53:36 +08:00
7 changed files with 146 additions and 77 deletions
+26
View File
@@ -1171,6 +1171,32 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate {
return $rv;
}
/**
* Returns an array of ids of categories that are (direct and indirect) children
* of this category.
*
* @return int[]
*/
public function get_all_children_ids() {
$coursecattreecache = cache::make('core', 'coursecattree');
$children = $coursecattreecache->get($this->id . 'allchildren');
if ($children === false) {
$children = [];
$walk = [$this->id];
while (count($walk) > 0) {
$catid = array_pop($walk);
$directchildren = self::get_tree($catid);
if ($directchildren !== false && count($directchildren) > 0) {
$walk = array_merge($walk, $directchildren);
$children = array_merge($children, $directchildren);
}
}
$coursecattreecache->set($this->id . 'allchildren', $children);
}
return $children;
}
/**
* Returns true if the user has the manage capability on any category.
*
+11
View File
@@ -127,6 +127,17 @@ $definitions = array(
'staticacceleration' => true,
),
// Cache the course categories where the user has any enrolment and all categories that this user can manage.
'calendar_categories' => array(
'mode' => cache_store::MODE_SESSION,
'simplekeys' => true,
'simpledata' => true,
'invalidationevents' => array(
'changesincoursecat',
),
'ttl' => 900,
),
// Cache the capabilities list DB table. See get_all_capabilities in accesslib.
'capabilities' => array(
'mode' => cache_store::MODE_APPLICATION,
+31
View File
@@ -381,6 +381,37 @@ class core_coursecatlib_testcase extends advanced_testcase {
$this->assertEquals(4, $category1->get_children_count());
}
/**
* Test the get_all_children_ids function.
*/
public function test_get_all_children_ids() {
$category1 = coursecat::create(array('name' => 'Cat1'));
$category2 = coursecat::create(array('name' => 'Cat2'));
$category11 = coursecat::create(array('name' => 'Cat11', 'parent' => $category1->id));
$category12 = coursecat::create(array('name' => 'Cat12', 'parent' => $category1->id));
$category13 = coursecat::create(array('name' => 'Cat13', 'parent' => $category1->id));
$category111 = coursecat::create(array('name' => 'Cat111', 'parent' => $category11->id));
$category112 = coursecat::create(array('name' => 'Cat112', 'parent' => $category11->id));
$category1121 = coursecat::create(array('name' => 'Cat1121', 'parent' => $category112->id));
$this->assertCount(0, $category2->get_all_children_ids());
$this->assertCount(6, $category1->get_all_children_ids());
$cmpchildrencat1 = array($category11->id, $category12->id, $category13->id, $category111->id, $category112->id,
$category1121->id);
$childrencat1 = $category1->get_all_children_ids();
// Order of values does not matter. Compare sorted arrays.
sort($cmpchildrencat1);
sort($childrencat1);
$this->assertEquals($cmpchildrencat1, $childrencat1);
$this->assertCount(3, $category11->get_all_children_ids());
$this->assertCount(0, $category111->get_all_children_ids());
$this->assertCount(1, $category112->get_all_children_ids());
$this->assertEquals(array($category1121->id), $category112->get_all_children_ids());
}
/**
* Test the countall function
*/