Moodle PHP Documentation 4.2
Moodle 4.2.8 (Build: 20240610) (2d41ac46f45)
|
Class to sort lists of items. More...
Public Member Functions | |
__construct (callable $getid, callable $getparentid) | |
Constructor. | |
flatten_children (array $items) | |
Take the data structure returned from "sort_into_children" and flatten it back into an array. | |
sort_into_children (array $items) | |
Sort a list of items into a parent/child data structure. | |
Class to sort lists of items.
mod_forum\local\entities\sorter::__construct | ( | callable | $getid, |
callable | $getparentid ) |
Constructor.
Allows the calling code to provide 2 functions to get the id and parent id from the list of items it is intended to process.
This allows this class to be composed in numerous different ways to support various types of items while keeping the underlying sorting algorithm consistent.
callable | $getid | Function used to get the id from an item |
callable | $getparentid | Function used to get the parent id from an item |
mod_forum\local\entities\sorter::flatten_children | ( | array | $items | ) |
Take the data structure returned from "sort_into_children" and flatten it back into an array.
It does a depth first flatten which maintains the reply ordering.
array | $items | Items in the data structure returned by "sort_into_children" |
array | A flat array. |
mod_forum\local\entities\sorter::sort_into_children | ( | array | $items | ) |
Sort a list of items into a parent/child data structure.
The resulting data structure is a recursive array of arrays where the first element is the parent and the second is an array of it's children.
For example If we have an array of items A, B, C, and D where D is a child of C, B and C are children of A.
This function would sort them into the following: [ [ A, [ [ B, [] ], [ C, [ [ D, [] ] ] ] ] ] ]
array | $items | The list of items to sort. |
array |