Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
/lib/ -> listlib.php (source)

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

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: 640 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.

return: list_item *copy* or null if item is not found
param: integer $id
param: boolean $suppresserror error if not item 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

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

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.

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

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.

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

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.

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

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.

return: unknown
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

item_is_first_on_page($itemid)   X-Ref

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

item_is_last_on_page($itemid)   X-Ref

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

Class: list_item  - X-Ref


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

return: list_item
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

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

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

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