3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
12 // This program is free software; you can redistribute it and/or modify //
13 // it under the terms of the GNU General Public License as published by //
14 // the Free Software Foundation; either version 2 of the License, or //
15 // (at your option) any later version. //
17 // This program is distributed in the hope that it will be useful, //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20 // GNU General Public License for more details: //
22 // http://www.gnu.org/copyleft/gpl.html //
24 ///////////////////////////////////////////////////////////////////////////
27 * Classes for displaying and editing a nested list of items.
29 * Handles functionality for :
31 * Construction of nested list from db records with some key pointing to a parent id.
32 * Display of list with or without editing icons with optional pagination.
33 * Reordering of items works across pages.
34 * Processing of editing actions on list.
37 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
42 * Clues to reading this code:
44 * The functions that move things around the tree structure just update the
45 * database - they don't update the in-memory structure, instead they trigger a
46 * page reload so everything is rebuilt from scratch.
50 var $listitemclassname = 'list_item';
52 * An array of $listitemclassname objects.
62 * @var list_item or derived class
64 var $parentitem = null;
66 var $fieldnamesparent = 'parent';
67 var $sortby = 'parent, sortorder, name';
69 * Records from db, only used in top level list.
72 var $records = array();
77 * Key is child id, value is parent.
82 //------------------------------------------------------
83 //vars used for pagination.
84 var $page = 0;// 0 means no pagination
86 var $lastitem = 999999;
90 //------------------------------------------------------
95 * Constructor function
98 * @param string $attributes
99 * @param boolean $editable
100 * @param moodle_url $pageurl url for this page
101 * @param integer $page if 0 no pagination. (These three params only used in top level list.)
102 * @param string $pageparamname name of url param that is used for passing page no
103 * @param integer $itemsperpage no of top level items.
104 * @return moodle_list
106 function moodle_list($type='ul', $attributes='', $editable = false, $pageurl=null, $page = 0, $pageparamname = 'page', $itemsperpage = 20) {
107 $this->editable
= $editable;
108 $this->attributes
= $attributes;
111 $this->pageparamname
= $pageparamname;
112 $this->itemsperpage
= $itemsperpage;
113 if ($pageurl === null) {
114 $this->pageurl
= new moodle_url();
115 $this->pageurl
->params(array($this->pageparamname
=> $this->page
));
117 $this->pageurl
= $pageurl;
122 * Returns html string.
124 * @param integer $indent depth of indentation.
126 function to_html($indent=0, $extraargs=array()) {
127 if (count($this->items
)) {
128 $tabs = str_repeat("\t", $indent);
134 foreach ($this->items
as $item) {
135 $last = (count($this->items
) == $itemiter);
136 if ($this->editable
) {
137 $item->set_icon_html($first, $last, $lastitem);
139 if ($itemhtml = $item->to_html($indent+
1, $extraargs)) {
140 $html .= "$tabs\t<li".((!empty($item->attributes
))?
(' '.$item->attributes
):'').">";
151 if ($html) { //if there are list items to display then wrap them in ul / ol tag.
152 $tabs = str_repeat("\t", $indent);
153 $html = $tabs.'<'.$this->type
.((!empty($this->attributes
))?
(' '.$this->attributes
):'').">\n".$html;
154 $html .= $tabs."</".$this->type
.">\n";
162 * Recurse down the tree and find an item by it's id.
165 * @param boolean $suppresserror error if not item found?
166 * @return list_item *copy* or null if item is not found
168 function find_item($id, $suppresserror = false) {
169 if (isset($this->items
)) {
170 foreach ($this->items
as $key => $child) {
171 if ($child->id
== $id) {
172 return $this->items
[$key];
175 foreach (array_keys($this->items
) as $key) {
176 $thischild =& $this->items
[$key];
177 $ref = $thischild->children
->find_item($id, true);//error always reported at top level
184 if (!$suppresserror) {
185 print_error('listnoitem');
190 function add_item(&$item) {
191 $this->items
[] =& $item;
194 function set_parent(&$parent) {
195 $this->parentitem
=& $parent;
199 * Produces a hierarchical tree of list items from a flat array of records.
200 * 'parent' field is expected to point to a parent record.
201 * records are already sorted.
202 * If the parent field doesn't point to another record in the array then this is
205 * @param integer $offset how many list toplevel items are there in lists before this one
206 * @return array(boolean, integer) whether there is more than one page, $offset + how many toplevel items where there in this list.
209 function list_from_records($paged = false, $offset = 0) {
210 $this->paged
= $paged;
211 $this->offset
= $offset;
212 $this->get_records();
213 $records = $this->records
;
216 $this->firstitem
= ($page - 1) * $this->itemsperpage
;
217 $this->lastitem
= $this->firstitem +
$this->itemsperpage
- 1;
220 //make a simple array which is easier to search
221 $this->childparent
= array();
222 foreach ($records as $record) {
223 $this->childparent
[$record->id
] = $record->parent
;
225 //create top level list items and they're responsible for creating their children
226 foreach ($records as $record) {
227 if (!array_key_exists($record->parent
, $this->childparent
)) {
228 //if this record is not a child of another record then
230 $inpage = ($itemiter >= $this->firstitem
&& $itemiter <= $this->lastitem
);
231 //make list item for top level for all items
232 //we need the info about the top level items for reordering peers.
233 if ($this->parentitem
!==null) {
234 $newattributes = $this->parentitem
->attributes
;
239 $this->items
[$itemiter] =& new $this->listitemclassname($record, $this, $newattributes, $inpage);
241 $this->items
[$itemiter]->create_children($records, $this->childparent
, $record->id
);
243 //don't recurse down the tree for items that are not on this page
249 return array($this->paged
, $itemiter);
253 * Should be overriden to return an array of records of list items.
256 function get_records() {
260 * display list of page numbers for navigation
262 function display_page_numbers() {
264 $topcount = count($this->items
);
265 $this->pagecount
= (integer) ceil(($topcount +
$this->offset
)/ QUESTION_PAGE_LENGTH
);
266 if (!empty($this->page
) && ($this->paged
)) {
267 $html = "<div class=\"paging\">".get_string('page').":\n";
268 foreach (range(1,$this->pagecount
) as $currentpage) {
269 if ($this->page
== $currentpage) {
270 $html .= " $currentpage \n";
273 $html .= "<a href=\"".$this->pageurl
->out(false, array($this->pageparamname
=> $currentpage))."\">";
274 $html .= " $currentpage </a>\n";
283 * Returns an array of ids of peers of an item.
285 * @param int itemid - if given, restrict records to those with this parent id.
286 * @return array peer ids
288 function get_items_peers($itemid) {
289 $itemref = $this->find_item($itemid);
290 $peerids = $itemref->parentlist
->get_child_ids();
295 * Returns an array of ids of child items.
297 * @return array peer ids
299 function get_child_ids() {
301 foreach ($this->items
as $child) {
302 $childids[] = $child->id
;
308 * Move a record up or down
310 * @param string $direction up / down
313 function move_item_up_down($direction, $id) {
314 $peers = $this->get_items_peers($id);
315 $itemkey = array_search($id, $peers);
316 switch ($direction) {
318 if (isset($peers[$itemkey+
1])) {
319 $olditem = $peers[$itemkey+
1];
320 $peers[$itemkey+
1] = $id;
321 $peers[$itemkey] = $olditem;
323 print_error('listcantmoveup');
328 if (isset($peers[$itemkey-1])) {
329 $olditem = $peers[$itemkey-1];
330 $peers[$itemkey-1] = $id;
331 $peers[$itemkey] = $olditem;
333 print_error('listcantmovedown');
337 $this->reorder_peers($peers);
340 function reorder_peers($peers) {
341 foreach ($peers as $key => $peer) {
342 if (!set_field("{$this->table}", "sortorder", $key, "id", $peer)) {
343 print_error('listupdatefail');
349 * @param integer $id an item index.
350 * @return object the item that used to be the parent of the item moved.
352 function move_item_left($id) {
353 $item = $this->find_item($id);
354 if (!isset($item->parentlist
->parentitem
->parentlist
)) {
355 print_error('listcantmoveleft');
357 $newpeers = $this->get_items_peers($item->parentlist
->parentitem
->id
);
358 if (isset($item->parentlist
->parentitem
->parentlist
->parentitem
)) {
359 $newparent = $item->parentlist
->parentitem
->parentlist
->parentitem
->id
;
361 $newparent = 0; // top level item
363 if (!set_field("{$this->table}", "parent", $newparent, "id", $item->id
)) {
364 print_error('listupdatefail');
366 $oldparentkey = array_search($item->parentlist
->parentitem
->id
, $newpeers);
367 $neworder = array_merge(array_slice($newpeers, 0, $oldparentkey+
1), array($item->id
), array_slice($newpeers, $oldparentkey+
1));
368 $this->reorder_peers($neworder);
371 return $item->parentlist
->parentitem
;
375 * Make item with id $id the child of the peer that is just above it in the sort order.
379 function move_item_right($id) {
380 $peers = $this->get_items_peers($id);
381 $itemkey = array_search($id, $peers);
382 if (!isset($peers[$itemkey-1])) {
383 print_error('listcantmoveright');
385 if (!set_field("{$this->table}", "parent", $peers[$itemkey-1], "id", $peers[$itemkey])) {
386 print_error('listupdatefail');
388 $newparent = $this->find_item($peers[$itemkey-1]);
389 if (isset($newparent->children
)) {
390 $newpeers = $newparent->children
->get_child_ids();
393 $newpeers[] = $peers[$itemkey];
394 $this->reorder_peers($newpeers);
401 * process any actions.
403 * @param integer $left id of item to move left
404 * @param integer $right id of item to move right
405 * @param integer $moveup id of item to move up
406 * @param integer $movedown id of item to move down
409 function process_actions($left, $right, $moveup, $movedown) {
410 //should this action be processed by this list object?
411 if (!(array_key_exists($left, $this->records
) ||
array_key_exists($right, $this->records
) ||
array_key_exists($moveup, $this->records
) ||
array_key_exists($movedown, $this->records
))) {
415 $oldparentitem = $this->move_item_left($left);
416 if ($this->item_is_last_on_page($oldparentitem->id
)) {
417 // Item has jumped onto the next page, change page when we redirect.
419 $this->pageurl
->params(array($this->pageparamname
=> $this->page
));
421 } else if (!empty($right)) {
422 $this->move_item_right($right);
423 if ($this->item_is_first_on_page($right)) {
424 // Item has jumped onto the previous page, change page when we redirect.
426 $this->pageurl
->params(array($this->pageparamname
=> $this->page
));
428 } else if (!empty($moveup)) {
429 $this->move_item_up_down('up', $moveup);
430 if ($this->item_is_first_on_page($moveup)) {
431 // Item has jumped onto the previous page, change page when we redirect.
433 $this->pageurl
->params(array($this->pageparamname
=> $this->page
));
435 } else if (!empty($movedown)) {
436 $this->move_item_up_down('down', $movedown);
437 if ($this->item_is_last_on_page($movedown)) {
438 // Item has jumped onto the next page, change page when we redirect.
440 $this->pageurl
->params(array($this->pageparamname
=> $this->page
));
446 redirect($this->pageurl
->out());
450 * @param integer $itemid an item id.
451 * @return boolean Is the item with the given id the first top-level item on
454 function item_is_first_on_page($itemid) {
455 return $this->page
&& isset($this->items
[$this->firstitem
]) &&
456 $itemid == $this->items
[$this->firstitem
]->id
;
460 * @param integer $itemid an item id.
461 * @return boolean Is the item with the given id the last top-level item on
464 function item_is_last_on_page($itemid) {
465 return $this->page
&& isset($this->items
[$this->lastitem
]) &&
466 $itemid == $this->items
[$this->lastitem
]->id
;
472 * id of record, used if list is editable
477 * name of this item, used if list is editable
482 * The object or string representing this item.
486 var $fieldnamesname = 'name';
489 var $icons = array();
495 * Set if there are any children of this listitem.
502 * @param mixed $item fragment of html for list item or record
503 * @param object &$parent reference to parent of this item
504 * @param string $attributes attributes for li tag
505 * @param boolean $display whether this item is displayed. Some items may be loaded so we have a complete
506 * structure in memory to work with for actions but are not displayed.
509 function list_item($item, &$parent, $attributes='', $display = true) {
511 if (is_object($this->item
)) {
512 $this->id
= $this->item
->id
;
513 $this->name
= $this->item
->{$this->fieldnamesname
};
515 $this->set_parent($parent);
516 $this->attributes
= $attributes;
517 $parentlistclass = get_class($parent);
518 $this->children
=& new $parentlistclass($parent->type
, $parent->attributes
, $parent->editable
, $parent->pageurl
, 0);
519 $this->children
->set_parent($this);
520 $this->display
= $display;
524 * Output the html just for this item. Called by to_html which adds html for children.
527 function item_html($extraargs = array()) {
528 if (is_string($this->item
)) {
530 } elseif (is_object($this->item
)) {
531 //for debug purposes only. You should create a sub class to
532 //properly handle the record
533 $html = join(', ', (array)$this->item
);
541 * @param integer $indent
542 * @param array $extraargs any extra data that is needed to print the list item
543 * may be used by sub class.
544 * @return string html
546 function to_html($indent=0, $extraargs = array()) {
547 if (!$this->display
) {
550 $tabs = str_repeat("\t", $indent);
552 if (isset($this->children
)) {
553 $childrenhtml = $this->children
->to_html($indent+
1, $extraargs);
557 return $this->item_html($extraargs).' '.(join($this->icons
, '')).(($childrenhtml !='')?
("\n".$childrenhtml):'');
560 function set_icon_html($first, $last, &$lastitem) {
562 $strmoveup = get_string('moveup');
563 $strmovedown = get_string('movedown');
564 $strmoveleft = get_string('maketoplevelitem', 'question');
565 $pixpath = $CFG->pixpath
;
567 if (isset($this->parentlist
->parentitem
)) {
568 $parentitem =& $this->parentlist
->parentitem
;
569 if (isset($parentitem->parentlist
->parentitem
)) {
570 $action = get_string('makechildof', 'question', $parentitem->parentlist
->parentitem
->name
);
572 $action = $strmoveleft;
574 $this->icons
['left'] = $this->image_icon($action, $this->parentlist
->pageurl
->out_action(array('left'=>$this->id
)), 'left');
576 $this->icons
['left'] = $this->image_spacer();
580 $this->icons
['up'] = $this->image_icon($strmoveup, $this->parentlist
->pageurl
->out_action(array('moveup'=>$this->id
)), 'up');
582 $this->icons
['up'] = $this->image_spacer();
586 $this->icons
['down'] = $this->image_icon($strmovedown, $this->parentlist
->pageurl
->out_action(array('movedown'=>$this->id
)), 'down');
588 $this->icons
['down'] = $this->image_spacer();
591 if (!empty($lastitem)) {
592 $makechildof = get_string('makechildof', 'question', $lastitem->name
);
593 $this->icons
['right'] = $this->image_icon($makechildof, $this->parentlist
->pageurl
->out_action(array('right'=>$this->id
)), 'right');
595 $this->icons
['right'] = $this->image_spacer();
599 function image_icon($action, $url, $icon) {
601 $pixpath = $CFG->pixpath
;
602 return '<a title="' . $action .'" href="'.$url.'">
603 <img src="' . $pixpath . '/t/'.$icon.'.gif" class="iconsmall" alt="' . $action. '" /></a> ';
606 function image_spacer() {
608 $pixpath = $CFG->pixpath
;
609 return '<img src="' . $pixpath . '/spacer.gif" class="iconsmall" alt="" />';
613 * Recurse down tree creating list_items, called from moodle_list::list_from_records
615 * @param array $records
616 * @param array $children
617 * @param integer $thisrecordid
619 function create_children(&$records, &$children, $thisrecordid) {
620 //keys where value is $thisrecordid
621 $thischildren = array_keys($children, $thisrecordid);
622 if (count($thischildren)) {
623 foreach ($thischildren as $child) {
624 $thisclass = get_class($this);
625 $newlistitem =& new $thisclass($records[$child], $this->children
, $this->attributes
);
626 $this->children
->add_item($newlistitem);
627 $newlistitem->create_children($records, $children, $records[$child]->id
);
632 function set_parent(&$parent) {
633 $this->parentlist
=& $parent;