Skip to content
This repository has been archived by the owner on Apr 8, 2022. It is now read-only.

Commit

Permalink
Merge branch '35597-27' of git://github.com/samhemelryk/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Jan 14, 2014
2 parents 1b92203 + e5e0d39 commit da47e29
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 3 deletions.
3 changes: 3 additions & 0 deletions admin/settings/plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
*/

if ($hassiteconfig) {
/* @var admin_root $ADMIN */
$ADMIN->locate('modules')->set_sorting(true);

$ADMIN->add('modules', new admin_page_pluginsoverview());

// activity modules
Expand Down
113 changes: 110 additions & 3 deletions lib/adminlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -750,8 +750,8 @@ public function add($destinationname, $something, $beforesibling = null);
*/
class admin_category implements parentable_part_of_admin_tree {

/** @var mixed An array of part_of_admin_tree objects that are this object's children */
public $children;
/** @var part_of_admin_tree[] An array of part_of_admin_tree objects that are this object's children */
protected $children;
/** @var string An internal name for this category. Must be unique amongst ALL part_of_admin_tree objects */
public $name;
/** @var string The displayed name for this category. Usually obtained through get_string() */
Expand All @@ -766,6 +766,15 @@ class admin_category implements parentable_part_of_admin_tree {
/** @var array fast lookup category cache, all categories of one tree point to one cache */
protected $category_cache;

/** @var bool If set to true children will be sorted when calling {@link admin_category::get_children()} */
protected $sort = false;
/** @var bool If set to true children will be sorted in ascending order. */
protected $sortasc = true;
/** @var bool If set to true sub categories and pages will be split and then sorted.. */
protected $sortsplit = true;
/** @var bool $sorted True if the children have been sorted and don't need resorting */
protected $sorted = false;

/**
* Constructor for an empty admin category
*
Expand Down Expand Up @@ -830,7 +839,7 @@ public function locate($name, $findpath=false) {
*/
public function search($query) {
$result = array();
foreach ($this->children as $child) {
foreach ($this->get_children() as $child) {
$subsearch = $child->search($query);
if (!is_array($subsearch)) {
debugging('Incorrect search result from '.$child->name);
Expand Down Expand Up @@ -991,6 +1000,104 @@ public function show_save() {
}
return false;
}

/**
* Sets sorting on this category.
*
* Please note this function doesn't actually do the sorting.
* It can be called anytime.
* Sorting occurs when the user calls get_children.
* Code using the children array directly won't see the sorted results.
*
* @param bool $sort If set to true children will be sorted, if false they won't be.
* @param bool $asc If true sorting will be ascending, otherwise descending.
* @param bool $split If true we sort pages and sub categories separately.
*/
public function set_sorting($sort, $asc = true, $split = true) {
$this->sort = (bool)$sort;
$this->sortasc = (bool)$asc;
$this->sortsplit = (bool)$split;
}

/**
* Returns the children associated with this category.
*
* @return part_of_admin_tree[]
*/
public function get_children() {
// If we should sort and it hasn't already been sorted.
if ($this->sort && !$this->sorted) {
if ($this->sortsplit) {
$categories = array();
$pages = array();
foreach ($this->children as $child) {
if ($child instanceof admin_category) {
$categories[] = $child;
} else {
$pages[] = $child;
}
}
core_collator::asort_objects_by_property($categories, 'visiblename');
core_collator::asort_objects_by_property($pages, 'visiblename');
if (!$this->sortasc) {
$categories = array_reverse($categories);
$pages = array_reverse($pages);
}
$this->children = array_merge($pages, $categories);
} else {
core_collator::asort_objects_by_property($this->children, 'visiblename');
if (!$this->sortasc) {
$this->children = array_reverse($this->children);
}
}
$this->sorted = true;
}
return $this->children;
}

/**
* Magically gets a property from this object.
*
* @param $property
* @return part_of_admin_tree[]
* @throws coding_exception
*/
public function __get($property) {
if ($property === 'children') {
return $this->get_children();
}
throw new coding_exception('Invalid property requested.');
}

/**
* Magically sets a property against this object.
*
* @param string $property
* @param mixed $value
* @throws coding_exception
*/
public function __set($property, $value) {
if ($property === 'children') {
$this->sorted = false;
$this->children = $value;
} else {
throw new coding_exception('Invalid property requested.');
}
}

/**
* Checks if an inaccessible property is set.
*
* @param string $property
* @return bool
* @throws coding_exception
*/
public function __isset($property) {
if ($property === 'children') {
return isset($this->children);
}
throw new coding_exception('Invalid property requested.');
}
}


Expand Down

0 comments on commit da47e29

Please sign in to comment.