Commit e9321ad0 authored by Andrew Nicols's avatar Andrew Nicols
Browse files

MDL-59890 coursecat: Add helper to fetch nested view of coursecat

parent 02d0c435
......@@ -298,13 +298,9 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate {
* @param bool $options.returnhidden Return categories even if they are hidden
* @return coursecat[]
*/
public static function get_all($options = null) {
public static function get_all($options = []) {
global $DB;
if (null === $options) {
$options = [];
}
$coursecatrecordcache = cache::make('core', 'coursecatrecords');
$catcontextsql = \context_helper::get_preload_record_columns_sql('ctx');
......@@ -322,12 +318,11 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate {
$categories = [];
$toset = [];
foreach ($catrs as $record) {
\context_helper::preload_from_record($record);
$category = new coursecat($record);
$toset[$category->id] = $category;
if (!empty($options['returnhidden']) || $category->is_uservisible()) {
$categories[$record->id] = new coursecat($record);
$categories[$record->id] = $category;
}
}
$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() {
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
*
......
......@@ -750,6 +750,22 @@ class core_coursecatlib_testcase extends advanced_testcase {
$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
*
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment