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.
38 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
45 var $listitemclassname = 'list_item';
47 * An array of $listitemclassname objects.
60 * @var list_item or derived class
62 var $parentitem = null;
64 var $fieldnamesparent = 'parent';
65 var $sortby = 'parent, sortorder, name';
67 * Records from db, only used in top level list.
71 var $records = array();
76 * 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;
123 * Returns html string.
125 * @param integer $indent depth of indentation.
127 function to_html($indent=0, $extraargs=array()){
128 if (count($this->items
)){
129 $tabs = str_repeat("\t", $indent);
135 foreach ($this->items
as $item){
136 $last = (count($this->items
) == $itemiter);
137 if ($this->editable
){
138 $item->set_icon_html($first, $last, $lastitem);
140 if ($itemhtml = $item->to_html($indent+
1, $extraargs)){
141 $html .= "$tabs\t<li".((!empty($item->attributes
))?
(' '.$item->attributes
):'').">";
152 if ($html){ //if there are list items to display then wrap them in ul / ol tag.
153 $tabs = str_repeat("\t", $indent);
154 $html = $tabs.'<'.$this->type
.((!empty($this->attributes
))?
(' '.$this->attributes
):'').">\n".$html;
155 $html .= $tabs."</".$this->type
.">\n";
163 * Recurse down the tree and find an item by it's id.
166 * @param boolean $suppresserror error if not item found?
167 * @return list_item *copy* or null if item is not found
169 function find_item($id, $suppresserror = false){
170 if (isset($this->items
)){
171 foreach ($this->items
as $key => $child){
172 if ($child->id
== $id){
173 return $this->items
[$key];
176 foreach (array_keys($this->items
) as $key){
177 $thischild =& $this->items
[$key];
178 $ref = $thischild->children
->find_item($id, true);//error always reported at top level
185 if (!$suppresserror){
186 print_error('listnoitem');
193 function add_item(&$item){
194 $this->items
[] =& $item;
197 function set_parent(&$parent){
198 $this->parentitem
=& $parent;
203 * Produces a hierarchical tree of list items from a flat array of records.
204 * 'parent' field is expected to point to a parent record.
205 * records are already sorted.
206 * If the parent field doesn't point to another record in the array then this is
209 * @param integer $offset how many list toplevel items are there in lists before this one
210 * @return integer $offset + how many toplevel items where there in this list.
213 function list_from_records($paged = false, $offset =1){
214 $this->paged
= $paged;
215 $this->offset
= $offset;
216 $this->get_records();
217 $records = $this->records
;
220 $this->firstitem
= ($page-1) * $this->itemsperpage +
1;
221 $this->lastitem
= $this->firstitem +
$this->itemsperpage
- 1;
224 //make a simple array which is easier to search
225 $this->childparent
= array();
226 foreach ($records as $record){
227 $this->childparent
[$record->id
] = $record->parent
;
229 //create top level list items and they're responsible for creating their children
230 foreach ($records as $record){
231 if (!array_key_exists($record->parent
, $this->childparent
)){
232 //if this record is not a child of another record then
234 $inpage = ($itemiter >= $this->firstitem
&& $itemiter <= $this->lastitem
);
235 //make list item for top level for all items
236 //we need the info about the top level items for reordering peers.
237 if ($this->parentitem
!==null){
238 $newattributes = $this->parentitem
->attributes
;
243 $newlistitem =& new $this->listitemclassname($record, $this, $newattributes, $inpage);
245 $newlistitem->create_children($records, $this->childparent
, $record->id
);
247 //don't recurse down the tree for items that are not on this page
253 return array($this->paged
, $itemiter);
257 * Should be overriden to return an array of records of list items.
260 function get_records() {
264 * display list of page numbers for navigation
266 function display_page_numbers() {
268 $topcount = count($this->items
);
269 $this->pagecount
= (integer) ceil(($topcount +
$this->offset
)/ QUESTION_PAGE_LENGTH
);
270 if (!empty($this->page
) && ($this->paged
)){
271 $html = "<div class=\"paging\">".get_string('page').":\n";
272 foreach (range(1,$this->pagecount
) as $currentpage) {
273 if ($this->page
== $currentpage) {
274 $html .= " $currentpage \n";
277 $html .= "<a href=\"".$this->pageurl
->out(false, array($this->pageparamname
=> $currentpage))."\">";
278 $html .= " $currentpage </a>\n";
287 * Returns an array of ids of peers of an item.
289 * @param int itemid - if given, restrict records to those with this parent id.
290 * @return array peer ids
292 function get_items_peers($itemid) {
293 $itemref = $this->find_item($itemid);
294 $peerids = $itemref->parentlist
->get_child_ids();
299 * Returns an array of ids of child items.
301 * @return array peer ids
303 function get_child_ids() {
305 foreach ($this->items
as $child){
306 $childids[] = $child->id
;
312 * Move a record up or down
314 * @param string $direction up / down
317 function move_item_up_down($direction, $id) {
318 $peers = $this->get_items_peers($id);
319 $itemkey = array_search($id, $peers);
320 switch ($direction) {
322 if (isset($peers[$itemkey+
1])){
323 $olditem = $peers[$itemkey+
1];
324 $peers[$itemkey+
1] = $id;
325 $peers[$itemkey] = $olditem;
327 print_error('listcantmoveup');
333 if (isset($peers[$itemkey-1])){
334 $olditem = $peers[$itemkey-1];
335 $peers[$itemkey-1] = $id;
336 $peers[$itemkey] = $olditem;
338 print_error('listcantmovedown');
342 $this->reorder_peers($peers);
344 function reorder_peers($peers){
345 foreach ($peers as $key => $peer) {
346 if (! set_field("{$this->table}", "sortorder", $key, "id", $peer)) {
347 print_error('listupdatefail');
351 function move_item_left($id) {
352 $item = $this->find_item($id);
353 if (!isset($item->parentlist
->parentitem
->parentlist
)){
354 print_error('listcantmoveleft');
356 $newpeers = $this->get_items_peers($item->parentlist
->parentitem
->id
);
357 if (isset($item->parentlist
->parentitem
->parentlist
->parentitem
)){
358 $newparent = $item->parentlist
->parentitem
->parentlist
->parentitem
->id
;
360 $newparent = 0; // top level item
362 if (!set_field("{$this->table}", "parent", $newparent, "id", $item->id
)) {
363 print_error('listupdatefail');
365 $oldparentkey = array_search($item->parentlist
->parentitem
->id
, $newpeers);
366 $neworder = array_merge(array_slice($newpeers, 0, $oldparentkey+
1), array($item->id
), array_slice($newpeers, $oldparentkey+
1));
367 $this->reorder_peers($neworder);
372 * Make item with id $id the child of the peer that is just above it in the sort order.
376 function move_item_right($id) {
377 $peers = $this->get_items_peers($id);
378 $itemkey = array_search($id, $peers);
379 if (!isset($peers[$itemkey-1])){
380 print_error('listcantmoveright');
382 if (!set_field("{$this->table}", "parent", $peers[$itemkey-1], "id", $peers[$itemkey])) {
383 print_error('listupdatefail');
385 $newparent = $this->find_item($peers[$itemkey-1]);
386 if (isset($newparent->children
)){
387 $newpeers = $newparent->children
->get_child_ids();
390 $newpeers[] = $peers[$itemkey];
391 $this->reorder_peers($newpeers);
398 * process any actions.
400 * @param integer $left id of item to move left
401 * @param integer $right id of item to move right
402 * @param integer $moveup id of item to move up
403 * @param integer $movedown id of item to move down
406 function process_actions($left, $right, $moveup, $movedown){
407 //should this action be processed by this list object?
408 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
))){
412 $this->move_item_left($left);
413 } else if (!empty($right)) {
414 $this->move_item_right($right);
415 } else if (!empty($moveup)) {
416 $this->move_item_up_down('up', $moveup);
417 if ($moveup == $this->items
[$this->firstitem
-1]->id
){//redirect to page that item has been moved to.
419 $this->pageurl
->params(array($this->pageparamname
=> $this->page
));
421 } else if (!empty($movedown)) {
422 $this->move_item_up_down('down', $movedown);
423 if ($movedown == $this->items
[$this->lastitem
-1]->id
){//redirect to page that item has been moved to.
425 $this->pageurl
->params(array($this->pageparamname
=> $this->page
));
431 redirect($this->pageurl
->out());
437 * id of record, used if list is editable
443 * name of this item, used if list is editable
449 * The object or string representing this item.
454 var $fieldnamesname = 'name';
457 var $icons = array();
464 * Set if there are any children of this listitem.
472 * @param mixed $item fragment of html for list item or record
473 * @param object &$parent reference to parent of this item
474 * @param string $attributes attributes for li tag
475 * @param boolean $display whether this item is displayed. Some items may be loaded so we have a complete
476 * structure in memory to work with for actions but are not displayed.
479 function list_item($item, &$parent, $attributes='', $display = true){
481 if (is_object($this->item
)) {
482 $this->id
= $this->item
->id
;
483 $this->name
= $this->item
->{$this->fieldnamesname
};
485 $this->set_parent($parent);
486 $this->attributes
= $attributes;
487 $parentlistclass = get_class($parent);
488 $this->children
=& new $parentlistclass($parent->type
, $parent->attributes
, $parent->editable
, $parent->pageurl
, 0);
489 $this->children
->set_parent($this);
490 $this->display
= $display;
493 * Output the html just for this item. Called by to_html which adds html for children.
496 function item_html($extraargs = array()){
497 if (is_string($this->item
)){
499 } elseif (is_object($this->item
)) {
500 //for debug purposes only. You should create a sub class to
501 //properly handle the record
502 $html = join(', ', (array)$this->item
);
509 * @param integer $indent
510 * @param array $extraargs any extra data that is needed to print the list item
511 * may be used by sub class.
512 * @return string html
514 function to_html($indent=0, $extraargs = array()){
515 if (!$this->display
){
518 $tabs = str_repeat("\t", $indent);
520 if (isset($this->children
)){
521 $childrenhtml = $this->children
->to_html($indent+
1, $extraargs);
525 return $this->item_html($extraargs).' '.(join($this->icons
, '')).(($childrenhtml !='')?
("\n".$childrenhtml):'');
528 function set_icon_html($first, $last, &$lastitem){
530 $strmoveup = get_string('moveup');
531 $strmovedown = get_string('movedown');
532 $strmoveleft = get_string('maketoplevelitem', 'question');
533 $pixpath = $CFG->pixpath
;
535 if (isset($this->parentlist
->parentitem
)) {
536 $parentitem =& $this->parentlist
->parentitem
;
537 if (isset($parentitem->parentlist
->parentitem
)){
538 $action = get_string('makechildof', 'question', $parentitem->parentlist
->parentitem
->name
);
540 $action = $strmoveleft;
542 $this->icons
['left'] = $this->image_icon($action, $this->parentlist
->pageurl
->out_action(array('left'=>$this->id
)), 'left');
544 $this->icons
['left'] = $this->image_spacer();
548 $this->icons
['up'] = $this->image_icon($strmoveup, $this->parentlist
->pageurl
->out_action(array('moveup'=>$this->id
)), 'up');
550 $this->icons
['up'] = $this->image_spacer();
554 $this->icons
['down'] = $this->image_icon($strmovedown, $this->parentlist
->pageurl
->out_action(array('movedown'=>$this->id
)), 'down');
556 $this->icons
['down'] = $this->image_spacer();
559 if (!empty($lastitem)) {
560 $makechildof = get_string('makechildof', 'question', $lastitem->name
);
561 $this->icons
['right'] = $this->image_icon($makechildof, $this->parentlist
->pageurl
->out_action(array('right'=>$this->id
)), 'right');
563 $this->icons
['right'] = $this->image_spacer();
567 function image_icon($action, $url, $icon){
569 $pixpath = $CFG->pixpath
;
570 return '<a title="' . $action .'" href="'.$url.'">
571 <img src="' . $pixpath . '/t/'.$icon.'.gif" class="iconsmall" alt="' . $action. '" /></a> ';
573 function image_spacer(){
575 $pixpath = $CFG->pixpath
;
576 return '<img src="' . $pixpath . '/spacer.gif" class="iconsmall" alt="" />';
579 * Recurse down tree creating list_items, called from moodle_list::list_from_records
581 * @param array $records
582 * @param array $children
583 * @param integer $thisrecordid
585 function create_children(&$records, &$children, $thisrecordid){
586 //keys where value is $thisrecordid
587 $thischildren = array_keys($children, $thisrecordid);
588 if (count($thischildren)){
589 foreach ($thischildren as $child){
590 $thisclass = get_class($this);
591 $newlistitem =& new $thisclass($records[$child], $this->children
, $this->attributes
);
592 $newlistitem->create_children($records, $children, $records[$child]->id
);
596 function set_parent(&$parent){
597 $this->parentlist
=& $parent;
598 $parent->add_item($this);