mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 08:56:36 +02:00
MDL-59890 coursecat: Add helper to fetch nested view of coursecat
This commit is contained in:
parent
02d0c4355e
commit
e9321ad07d
2 changed files with 47 additions and 7 deletions
|
@ -298,13 +298,9 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate {
|
||||||
* @param bool $options.returnhidden Return categories even if they are hidden
|
* @param bool $options.returnhidden Return categories even if they are hidden
|
||||||
* @return coursecat[]
|
* @return coursecat[]
|
||||||
*/
|
*/
|
||||||
public static function get_all($options = null) {
|
public static function get_all($options = []) {
|
||||||
global $DB;
|
global $DB;
|
||||||
|
|
||||||
if (null === $options) {
|
|
||||||
$options = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
$coursecatrecordcache = cache::make('core', 'coursecatrecords');
|
$coursecatrecordcache = cache::make('core', 'coursecatrecords');
|
||||||
|
|
||||||
$catcontextsql = \context_helper::get_preload_record_columns_sql('ctx');
|
$catcontextsql = \context_helper::get_preload_record_columns_sql('ctx');
|
||||||
|
@ -322,12 +318,11 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate {
|
||||||
$categories = [];
|
$categories = [];
|
||||||
$toset = [];
|
$toset = [];
|
||||||
foreach ($catrs as $record) {
|
foreach ($catrs as $record) {
|
||||||
\context_helper::preload_from_record($record);
|
|
||||||
$category = new coursecat($record);
|
$category = new coursecat($record);
|
||||||
$toset[$category->id] = $category;
|
$toset[$category->id] = $category;
|
||||||
|
|
||||||
if (!empty($options['returnhidden']) || $category->is_uservisible()) {
|
if (!empty($options['returnhidden']) || $category->is_uservisible()) {
|
||||||
$categories[$record->id] = new coursecat($record);
|
$categories[$record->id] = $category;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$catrs->close();
|
$catrs->close();
|
||||||
|
@ -1306,6 +1301,9 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Get the link used to view this course category.
|
||||||
|
*
|
||||||
|
* @return \moodle_url
|
||||||
*/
|
*/
|
||||||
public function get_view_link() {
|
public function get_view_link() {
|
||||||
return new \moodle_url('/course/index.php', [
|
return new \moodle_url('/course/index.php', [
|
||||||
|
@ -2213,6 +2211,32 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the nested name of this category, with all of it's parents.
|
||||||
|
*
|
||||||
|
* @param bool $includelinks Whether to wrap each name in the view link for that category.
|
||||||
|
* @param string $separator The string between each name.
|
||||||
|
* @param array $options Formatting options.
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function get_nested_name($includelinks = true, $separator = ' / ', $options = []) {
|
||||||
|
// Get the name of hierarchical name of this category.
|
||||||
|
$parents = $this->get_parents();
|
||||||
|
$categories = static::get_many($parents);
|
||||||
|
$categories[] = $this;
|
||||||
|
|
||||||
|
$names = array_map(function($category) use ($options, $includelinks) {
|
||||||
|
if ($includelinks) {
|
||||||
|
return html_writer::link($category->get_view_link(), $category->get_formatted_name($options));
|
||||||
|
} else {
|
||||||
|
return $category->get_formatted_name($options);
|
||||||
|
}
|
||||||
|
|
||||||
|
}, $categories);
|
||||||
|
|
||||||
|
return implode($separator, $names);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns ids of all parents of the category. Last element in the return array is the direct parent
|
* Returns ids of all parents of the category. Last element in the return array is the direct parent
|
||||||
*
|
*
|
||||||
|
|
|
@ -750,6 +750,22 @@ class core_coursecatlib_testcase extends advanced_testcase {
|
||||||
$this->assertEquals(1, count($courses[$c5->id]->get_course_overviewfiles()));
|
$this->assertEquals(1, count($courses[$c5->id]->get_course_overviewfiles()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_get_nested_name() {
|
||||||
|
$cat1name = 'Cat1';
|
||||||
|
$cat2name = 'Cat2';
|
||||||
|
$cat3name = 'Cat3';
|
||||||
|
$cat4name = 'Cat4';
|
||||||
|
$category1 = coursecat::create(array('name' => $cat1name));
|
||||||
|
$category2 = coursecat::create(array('name' => $cat2name, 'parent' => $category1->id));
|
||||||
|
$category3 = coursecat::create(array('name' => $cat3name, 'parent' => $category2->id));
|
||||||
|
$category4 = coursecat::create(array('name' => $cat4name, 'parent' => $category2->id));
|
||||||
|
|
||||||
|
$this->assertEquals($cat1name, $category1->get_nested_name(false));
|
||||||
|
$this->assertEquals("{$cat1name} / {$cat2name}", $category2->get_nested_name(false));
|
||||||
|
$this->assertEquals("{$cat1name} / {$cat2name} / {$cat3name}", $category3->get_nested_name(false));
|
||||||
|
$this->assertEquals("{$cat1name} / {$cat2name} / {$cat4name}", $category4->get_nested_name(false));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a draft area for current user and fills it with fake files
|
* Creates a draft area for current user and fills it with fake files
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue