MDL-15476
[moodle-linuxchix.git] / lib / listlib.php
blob708db97210aff6546135871cd3ee0d9a0463e167
1 <?php // $Id$
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
9 // //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
11 // //
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. //
16 // //
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: //
21 // //
22 // http://www.gnu.org/copyleft/gpl.html //
23 // //
24 ///////////////////////////////////////////////////////////////////////////
26 /**
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.
36 * @author Jamie Pratt
37 * @version $Id$
38 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
39 * @package moodlecore
43 class moodle_list{
44 var $attributes;
45 var $listitemclassname = 'list_item';
46 /**
47 * An array of $listitemclassname objects.
49 * @var array
51 var $items = array();
52 /**
53 * ol / ul
55 * @var string
57 var $type;
58 /**
60 * @var list_item or derived class
62 var $parentitem = null;
63 var $table;
64 var $fieldnamesparent = 'parent';
65 var $sortby = 'parent, sortorder, name';
66 /**
67 * Records from db, only used in top level list.
69 * @var array
71 var $records = array();
73 var $editable;
75 /**
76 * Key is child id, value is parent.
78 * @var array
80 var $childparent;
82 //------------------------------------------------------
83 //vars used for pagination.
84 var $page = 0;// 0 means no pagination
85 var $firstitem = 1;
86 var $lastitem = 999999;
87 var $pagecount;
88 var $paged = false;
89 var $offset = 0;
90 //------------------------------------------------------
91 var $pageurl;
92 var $pageparamname;
94 /**
95 * Constructor function
97 * @param string $type
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;
109 $this->type = $type;
110 $this->page = $page;
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));
116 } else {
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);
130 $first = true;
131 $itemiter = 1;
132 $lastitem = '';
133 $html = '';
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):'').">";
142 $html .= $itemhtml;
143 $html .= "</li>\n";
145 $first = false;
146 $lastitem = $item;
147 $itemiter++;
149 } else {
150 $html = '';
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";
156 } else {
157 $html ='';
159 return $html;
163 * Recurse down the tree and find an item by it's id.
165 * @param integer $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
179 if ($ref !== null){
180 return $ref;
185 if (!$suppresserror){
186 print_error('listnoitem');
188 return null;
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
207 * a top level list
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;
218 $page = $this->page;
219 if (!empty($page)) {
220 $this->firstitem = ($page-1) * $this->itemsperpage + 1;
221 $this->lastitem = $this->firstitem + $this->itemsperpage - 1;
223 $itemiter = $offset;
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;
239 } else {
240 $newattributes = '';
243 $newlistitem =& new $this->listitemclassname($record, $this, $newattributes, $inpage);
244 if ($inpage){
245 $newlistitem->create_children($records, $this->childparent, $record->id);
246 } else {
247 //don't recurse down the tree for items that are not on this page
248 $this->paged = true;
250 $itemiter++;
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() {
267 $html = '';
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";
276 else {
277 $html .= "<a href=\"".$this->pageurl->out(false, array($this->pageparamname => $currentpage))."\">";
278 $html .= " $currentpage </a>\n";
281 $html .= "</div>";
283 return $html;
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();
295 return $peerids;
299 * Returns an array of ids of child items.
301 * @return array peer ids
303 function get_child_ids() {
304 $childids = array();
305 foreach ($this->items as $child){
306 $childids[] = $child->id;
308 return $childids;
312 * Move a record up or down
314 * @param string $direction up / down
315 * @param integer $id
317 function move_item_up_down($direction, $id) {
318 $peers = $this->get_items_peers($id);
319 $itemkey = array_search($id, $peers);
320 switch ($direction) {
321 case 'down' :
322 if (isset($peers[$itemkey+1])){
323 $olditem = $peers[$itemkey+1];
324 $peers[$itemkey+1] = $id;
325 $peers[$itemkey] = $olditem;
326 } else {
327 print_error('listcantmoveup');
330 break;
332 case 'up' :
333 if (isset($peers[$itemkey-1])){
334 $olditem = $peers[$itemkey-1];
335 $peers[$itemkey-1] = $id;
336 $peers[$itemkey] = $olditem;
337 } else {
338 print_error('listcantmovedown');
340 break;
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');
355 } else {
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;
359 } else {
360 $newparent = 0; // top level item
362 if (!set_field("{$this->table}", "parent", $newparent, "id", $item->id)) {
363 print_error('listupdatefail');
364 } else {
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.
374 * @param integer $id
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');
381 } else {
382 if (!set_field("{$this->table}", "parent", $peers[$itemkey-1], "id", $peers[$itemkey])) {
383 print_error('listupdatefail');
384 } else {
385 $newparent = $this->find_item($peers[$itemkey-1]);
386 if (isset($newparent->children)){
387 $newpeers = $newparent->children->get_child_ids();
389 if ($newpeers){
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
404 * @return unknown
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))){
409 return false;
411 if (!empty($left)) {
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.
418 $this->page --;
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.
424 $this->page ++;
425 $this->pageurl->params(array($this->pageparamname => $this->page));
427 } else {
428 return false;
431 redirect($this->pageurl->out());
435 class list_item{
437 * id of record, used if list is editable
439 * @var integer
441 var $id;
443 * name of this item, used if list is editable
445 * @var string
447 var $name;
449 * The object or string representing this item.
451 * @var mixed
453 var $item;
454 var $fieldnamesname = 'name';
455 var $attributes;
456 var $display;
457 var $icons = array();
460 * @var moodle_list
462 var $parentlist;
464 * Set if there are any children of this listitem.
466 * @var moodle_list
468 var $children;
470 * Constructor
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.
477 * @return list_item
479 function list_item($item, &$parent, $attributes='', $display = true){
480 $this->item = $item;
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)){
498 $html = $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);
504 return $html;
507 * Returns html
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){
516 return '';
518 $tabs = str_repeat("\t", $indent);
520 if (isset($this->children)){
521 $childrenhtml = $this->children->to_html($indent+1, $extraargs);
522 } else {
523 $childrenhtml = '';
525 return $this->item_html($extraargs).'&nbsp;'.(join($this->icons, '')).(($childrenhtml !='')?("\n".$childrenhtml):'');
528 function set_icon_html($first, $last, &$lastitem){
529 global $CFG;
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);
539 } else {
540 $action = $strmoveleft;
542 $this->icons['left'] = $this->image_icon($action, $this->parentlist->pageurl->out_action(array('left'=>$this->id)), 'left');
543 } else {
544 $this->icons['left'] = $this->image_spacer();
547 if (!$first) {
548 $this->icons['up'] = $this->image_icon($strmoveup, $this->parentlist->pageurl->out_action(array('moveup'=>$this->id)), 'up');
549 } else {
550 $this->icons['up'] = $this->image_spacer();
553 if (!$last) {
554 $this->icons['down'] = $this->image_icon($strmovedown, $this->parentlist->pageurl->out_action(array('movedown'=>$this->id)), 'down');
555 } else {
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');
562 } else {
563 $this->icons['right'] = $this->image_spacer();
567 function image_icon($action, $url, $icon){
568 global $CFG;
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(){
574 global $CFG;
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);