Moodle APIs  3.8
Moodle 3.8.6 (Build: 20201109)
Public Member Functions | List of all members
mod_forum\local\entities\sorter Class Reference

Class to sort lists of items. More...

Public Member Functions

 __construct (callable $getid, callable $getparentid)
 Constructor. More...
 
 flatten_children (array $items)
 Take the data structure returned from "sort_into_children" and flatten it back into an array. More...
 
 sort_into_children (array $items)
 Sort a list of items into a parent/child data structure. More...
 

Detailed Description

Class to sort lists of items.

License
http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later

Constructor & Destructor Documentation

◆ __construct()

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.

Parameters
callable$getidFunction used to get the id from an item
callable$getparentidFunction used to get the parent id from an item

Member Function Documentation

◆ flatten_children()

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.

Parameters
array$itemsItems in the data structure returned by "sort_into_children"
Return values
arrayA flat array.

◆ sort_into_children()

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, [] ] ] ] ] ] ]

Parameters
array$itemsThe list of items to sort.
Return values
array

The documentation for this class was generated from the following file: