Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
/lib/ -> listlib.php (source)

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

Classes for displaying and editing a nested list of items. Handles functionality for :

Copyright: Jamie Pratt
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 643 lines (23 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 2 classes

moodle_list:: (17 methods):
  __construct()
  to_html()
  find_item()
  add_item()
  set_parent()
  list_from_records()
  display_page_numbers()
  get_items_peers()
  get_child_ids()
  get_top_level_parent_id()
  move_item_up_down()
  reorder_peers()
  move_item_left()
  move_item_right()
  process_actions()
  item_is_first_on_page()
  item_is_last_on_page()

list_item:: (8 methods):
  __construct()
  item_html()
  to_html()
  set_icon_html()
  image_icon()
  image_spacer()
  create_children()
  set_parent()


Class: moodle_list  - X-Ref

Clues to reading this code:

The functions that move things around the tree structure just update the
database - they don't update the in-memory structure, instead they trigger a
page reload so everything is rebuilt from scratch.

__construct($type='ul', $attributes='', $editable = false, $pageurl=null, $page = 0, $pageparamname = 'page', $itemsperpage = 20)   X-Ref
Constructor.

param: string $type
param: string $attributes
param: boolean $editable
param: moodle_url $pageurl url for this page
param: integer $page if 0 no pagination. (These three params only used in top level list.)
param: string $pageparamname name of url param that is used for passing page no
param: integer $itemsperpage no of top level items.

to_html($indent=0, $extraargs=array()   X-Ref
Returns html string.

param: integer $indent depth of indentation.

find_item($id, $suppresserror = false)   X-Ref
Recurse down the tree and find an item by it's id.

param: integer $id
param: boolean $suppresserror error if not item found?
return: list_item *copy* or null if item is not found

add_item($item)   X-Ref
No description

set_parent($parent)   X-Ref
No description

list_from_records($paged = false, $offset = 0)   X-Ref
Produces a hierarchical tree of list items from a flat array of records.
'parent' field is expected to point to a parent record.
records are already sorted.
If the parent field doesn't point to another record in the array then this is
a top level list

param: integer $offset how many list toplevel items are there in lists before this one
return: array(boolean, integer) whether there is more than one page, $offset + how many toplevel items where there in this list.

display_page_numbers()   X-Ref
display list of page numbers for navigation


get_items_peers($itemid)   X-Ref
Returns an array of ids of peers of an item.

param: int itemid - if given, restrict records to those with this parent id.
return: array peer ids

get_child_ids()   X-Ref
Returns an array of ids of child items.

return: array peer ids

get_top_level_parent_id($item)   X-Ref
Returns the value to be used as the parent for the $item when it goes to the top level.
Override if needed.

param: list_item $item The item which its top level parent is going to be returned.
return: int

move_item_up_down($direction, $id)   X-Ref
Move a record up or down

param: string $direction up / down
param: integer $id

reorder_peers($peers)   X-Ref
No description

move_item_left($id)   X-Ref
Moves the item one step up in the tree.

param: int $id an item index.
return: list_item the item that used to be the parent of the item moved.

move_item_right($id)   X-Ref
Make item with id $id the child of the peer that is just above it in the sort order.

param: integer $id

process_actions($left, $right, $moveup, $movedown)   X-Ref
process any actions.

param: integer $left id of item to move left
param: integer $right id of item to move right
param: integer $moveup id of item to move up
param: integer $movedown id of item to move down
return: unknown

item_is_first_on_page($itemid)   X-Ref

param: integer $itemid an item id.
return: boolean Is the item with the given id the first top-level item on

item_is_last_on_page($itemid)   X-Ref

param: integer $itemid an item id.
return: boolean Is the item with the given id the last top-level item on

Class: list_item  - X-Ref


__construct($item, $parent, $attributes = '', $display = true)   X-Ref
Constructor

param: mixed $item fragment of html for list item or record
param: object $parent reference to parent of this item
param: string $attributes attributes for li tag
param: boolean $display whether this item is displayed. Some items may be loaded so we have a complete
return: list_item

item_html($extraargs = array()   X-Ref
Output the html just for this item. Called by to_html which adds html for children.


to_html($indent = 0, $extraargs = array()   X-Ref
Returns html

param: integer $indent
param: array $extraargs any extra data that is needed to print the list item
return: string html

set_icon_html($first, $last, $lastitem)   X-Ref
No description

image_icon($action, $url, $icon)   X-Ref
No description

image_spacer()   X-Ref
No description

create_children(&$records, &$children, $thisrecordid)   X-Ref
Recurse down tree creating list_items, called from moodle_list::list_from_records

param: array $records
param: array $children
param: integer $thisrecordid

set_parent($parent)   X-Ref
No description