Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
// This file is part of Moodle - http://moodle.org/
4
//
5
// Moodle is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
9
//
10
// Moodle is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
// GNU General Public License for more details.
14
//
15
// You should have received a copy of the GNU General Public License
16
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17
 
18
/**
19
 * Classes for displaying and editing a nested list of items.
20
 *
21
 * Handles functionality for :
22
 *
23
 *    Construction of nested list from db records with some key pointing to a parent id.
24
 *    Display of list with or without editing icons with optional pagination.
25
 *    Reordering of items works across pages.
26
 *    Processing of editing actions on list.
27
 *
28
 * @package    core
29
 * @subpackage lib
30
 * @copyright  Jamie Pratt
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1441 ariadna 32
 * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
33
 * @todo Final removal in Moodle 6.0 MDL-80804.
1 efrain 34
 */
35
 
36
defined('MOODLE_INTERNAL') || die();
37
 
38
/**
39
 * Clues to reading this code:
40
 *
41
 * The functions that move things around the tree structure just update the
42
 * database - they don't update the in-memory structure, instead they trigger a
43
 * page reload so everything is rebuilt from scratch.
44
 *
45
 * @package moodlecore
46
 * @copyright Jamie Pratt
47
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1441 ariadna 48
 * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
49
 * @todo Final removal in Moodle 6.0 MDL-80804.
1 efrain 50
 */
1441 ariadna 51
#[\core\attribute\deprecated(null, since: 4.5, reason: 'No longer used in core code', mdl: 'MDL-72397')]
1 efrain 52
abstract class moodle_list {
53
    public $attributes;
54
    public $listitemclassname = 'list_item';
55
 
56
    /** @var array of $listitemclassname objects. */
57
    public $items = array();
58
 
59
    /** @var string 'ol' or 'ul'. */
60
    public $type;
61
 
62
    /** @var list_item or derived class. */
63
    public $parentitem = null;
64
    public $table;
65
    public $fieldnamesparent = 'parent';
66
 
67
    /** @var array Records from db, only used in top level list. */
68
    public $records = array();
69
 
70
    public $editable;
71
 
72
    /** @var array keys are child ids, values are parents. */
73
    public $childparent;
74
 
75
//------------------------------------------------------
76
//vars used for pagination.
77
    public $page = 0; // 0 means no pagination
78
    public $firstitem = 1;
79
    public $lastitem = 999999;
80
    public $pagecount;
81
    public $paged = false;
82
    public $offset = 0;
83
//------------------------------------------------------
84
    public $pageurl;
85
    public $pageparamname;
86
 
87
    /** @var int no of top level items. */
88
    private $itemsperpage;
89
 
90
    /**
91
     * Constructor.
92
     *
93
     * @param string $type
94
     * @param string $attributes
95
     * @param boolean $editable
96
     * @param moodle_url $pageurl url for this page
97
     * @param integer $page if 0 no pagination. (These three params only used in top level list.)
98
     * @param string $pageparamname name of url param that is used for passing page no
99
     * @param integer $itemsperpage no of top level items.
1441 ariadna 100
     * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
101
     * @todo Final removal in Moodle 6.0 MDL-80804.
1 efrain 102
     */
103
    public function __construct($type='ul', $attributes='', $editable = false, $pageurl=null, $page = 0, $pageparamname = 'page', $itemsperpage = 20) {
1441 ariadna 104
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
1 efrain 105
        global $PAGE;
106
 
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($PAGE->url);
115
            $this->pageurl->params(array($this->pageparamname => $this->page));
116
        } else {
117
            $this->pageurl = $pageurl;
118
        }
119
    }
120
 
121
    /**
122
     * Returns html string.
123
     *
124
     * @param integer $indent depth of indentation.
1441 ariadna 125
     * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
126
     * @todo Final removal in Moodle 6.0 MDL-80804.
1 efrain 127
     */
128
    public function to_html($indent=0, $extraargs=array()) {
1441 ariadna 129
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
1 efrain 130
        if (count($this->items)) {
131
            $tabs = str_repeat("\t", $indent);
132
            $first = true;
133
            $itemiter = 1;
134
            $lastitem = '';
135
            $html = '';
136
 
137
            foreach ($this->items as $item) {
138
                $last = (count($this->items) == $itemiter);
139
                if ($this->editable) {
140
                    $item->set_icon_html($first, $last, $lastitem);
141
                }
142
                if ($itemhtml = $item->to_html($indent+1, $extraargs)) {
143
                    $html .= "$tabs\t<li".((!empty($item->attributes))?(' '.$item->attributes):'').">";
144
                    $html .= $itemhtml;
145
                    $html .= "</li>\n";
146
                }
147
                $first = false;
148
                $lastitem = $item;
149
                $itemiter++;
150
            }
151
        } else {
152
            $html = '';
153
        }
154
        if ($html) { //if there are list items to display then wrap them in ul / ol tag.
155
            $tabs = str_repeat("\t", $indent);
156
            $html = $tabs.'<'.$this->type.((!empty($this->attributes))?(' '.$this->attributes):'').">\n".$html;
157
            $html .= $tabs."</".$this->type.">\n";
158
        } else {
159
            $html ='';
160
        }
161
        return $html;
162
    }
163
 
164
    /**
165
     * Recurse down the tree and find an item by it's id.
166
     *
167
     * @param integer $id
168
     * @param boolean $suppresserror error if not item found?
169
     * @return list_item *copy* or null if item is not found
1441 ariadna 170
     * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
171
     * @todo Final removal in Moodle 6.0 MDL-80804.
1 efrain 172
     */
173
    public function find_item($id, $suppresserror = false) {
1441 ariadna 174
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
1 efrain 175
        if (isset($this->items)) {
176
            foreach ($this->items as $key => $child) {
177
                if ($child->id == $id) {
178
                    return $this->items[$key];
179
                }
180
            }
181
            foreach (array_keys($this->items) as $key) {
182
                $thischild = $this->items[$key];
183
                $ref = $thischild->children->find_item($id, true);//error always reported at top level
184
                if ($ref !== null) {
185
                    return $ref;
186
                }
187
            }
188
        }
189
 
190
        if (!$suppresserror) {
191
            throw new \moodle_exception('listnoitem');
192
        }
193
        return null;
194
    }
195
 
1441 ariadna 196
    /**
197
     * Add list item
198
     *
199
     * @param $item
200
     * @return void
201
     * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
202
     * @todo Final removal in Moodle 6.0 MDL-80804.
203
     */
1 efrain 204
    public function add_item($item) {
1441 ariadna 205
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
1 efrain 206
        $this->items[] = $item;
207
    }
208
 
1441 ariadna 209
    /**
210
     * Set parent item.
211
     *
212
     * @param $parent
213
     * @return void
214
     * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
215
     * @todo Final removal in Moodle 6.0 MDL-80804.
216
     */
1 efrain 217
    public function set_parent($parent) {
1441 ariadna 218
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
1 efrain 219
        $this->parentitem = $parent;
220
    }
221
 
222
    /**
223
     * Produces a hierarchical tree of list items from a flat array of records.
224
     * 'parent' field is expected to point to a parent record.
225
     * records are already sorted.
226
     * If the parent field doesn't point to another record in the array then this is
227
     * a top level list
228
     *
229
     * @param integer $offset how many list toplevel items are there in lists before this one
230
     * @return array(boolean, integer) whether there is more than one page, $offset + how many toplevel items where there in this list.
1441 ariadna 231
     * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
232
     * @todo Final removal in Moodle 6.0 MDL-80804.
1 efrain 233
     */
234
    public function list_from_records($paged = false, $offset = 0) {
1441 ariadna 235
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
1 efrain 236
        $this->paged = $paged;
237
        $this->offset = $offset;
238
        $this->get_records();
239
        $records = $this->records;
240
        $page = $this->page;
241
        if (!empty($page)) {
242
            $this->firstitem = ($page - 1) * $this->itemsperpage;
243
            $this->lastitem = $this->firstitem + $this->itemsperpage - 1;
244
        }
245
        $itemiter = $offset;
246
        //make a simple array which is easier to search
247
        $this->childparent = array();
248
        foreach ($records as $record) {
249
            $this->childparent[$record->id] = $record->parent;
250
        }
251
 
252
        //create top level list items and they're responsible for creating their children
253
        foreach ($records as $record) {
254
            if (array_key_exists($record->parent, $this->childparent)) {
255
                // This record is a child of another record, so it will be dealt
256
                // with by a call to list_item::create_children, not here.
257
                continue;
258
            }
259
 
260
            $inpage = $itemiter >= $this->firstitem && $itemiter <= $this->lastitem;
261
 
262
            // Make list item for top level for all items
263
            // we need the info about the top level items for reordering peers.
264
            if ($this->parentitem !== null) {
265
                $newattributes = $this->parentitem->attributes;
266
            } else {
267
                $newattributes = '';
268
            }
269
 
270
            $this->items[$itemiter] = new $this->listitemclassname($record, $this, $newattributes, $inpage);
271
 
272
            if ($inpage) {
273
                $this->items[$itemiter]->create_children($records, $this->childparent, $record->id);
274
            } else {
275
                // Don't recurse down the tree for items that are not on this page
276
                $this->paged = true;
277
            }
278
 
279
            $itemiter++;
280
        }
281
        return array($this->paged, $itemiter);
282
    }
283
 
284
    /**
285
     * Should be overriden to return an array of records of list items.
1441 ariadna 286
     *
287
     * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
288
     * @todo Final removal in Moodle 6.0 MDL-80804.
1 efrain 289
     */
290
    abstract public function get_records();
291
 
292
    /**
293
     * display list of page numbers for navigation
1441 ariadna 294
     *
295
     * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
296
     * @todo Final removal in Moodle 6.0 MDL-80804.
1 efrain 297
     */
298
    public function display_page_numbers() {
1441 ariadna 299
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
1 efrain 300
        $html = '';
301
        $topcount = count($this->items);
302
        $this->pagecount = (integer) ceil(($topcount + $this->offset)/ QUESTION_PAGE_LENGTH );
303
        if (!empty($this->page) && ($this->paged)) {
304
            $html = "<div class=\"paging\">".get_string('page').":\n";
305
            foreach (range(1,$this->pagecount) as $currentpage) {
306
                if ($this->page == $currentpage) {
307
                    $html .= " $currentpage \n";
308
                }
309
                else {
310
                    $html .= "<a href=\"".$this->pageurl->out(true, array($this->pageparamname => $currentpage))."\">";
311
                    $html .= " $currentpage </a>\n";
312
                }
313
            }
314
            $html .= "</div>";
315
        }
316
        return $html;
317
    }
318
 
319
    /**
320
     * Returns an array of ids of peers of an item.
321
     *
322
     * @param    int itemid - if given, restrict records to those with this parent id.
323
     * @return   array peer ids
1441 ariadna 324
     * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
325
     * @todo Final removal in Moodle 6.0 MDL-80804.
1 efrain 326
     */
327
    public function get_items_peers($itemid) {
1441 ariadna 328
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
1 efrain 329
        $itemref = $this->find_item($itemid);
330
        $peerids = $itemref->parentlist->get_child_ids();
331
        return $peerids;
332
    }
333
 
334
    /**
335
     * Returns an array of ids of child items.
336
     *
337
     * @return   array peer ids
1441 ariadna 338
     * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
339
     * @todo Final removal in Moodle 6.0 MDL-80804.
1 efrain 340
     */
341
    public function get_child_ids() {
1441 ariadna 342
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
1 efrain 343
        $childids = array();
344
        foreach ($this->items as $child) {
345
           $childids[] = $child->id;
346
        }
347
        return $childids;
348
    }
349
 
350
    /**
351
     * Returns the value to be used as the parent for the $item when it goes to the top level.
352
     * Override if needed.
353
     *
354
     * @param list_item $item The item which its top level parent is going to be returned.
355
     * @return int
1441 ariadna 356
     * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
357
     * @todo Final removal in Moodle 6.0 MDL-80804.
1 efrain 358
     */
359
    public function get_top_level_parent_id($item) {
1441 ariadna 360
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
1 efrain 361
        return 0; // Top level items have no parent.
362
    }
363
 
364
    /**
365
     * Move a record up or down
366
     *
367
     * @param string $direction up / down
368
     * @param integer $id
1441 ariadna 369
     * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
370
     * @todo Final removal in Moodle 6.0 MDL-80804.
1 efrain 371
     */
372
    public function move_item_up_down($direction, $id) {
1441 ariadna 373
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
1 efrain 374
        $peers = $this->get_items_peers($id);
375
        $itemkey = array_search($id, $peers);
376
        switch ($direction) {
377
            case 'down' :
378
                if (isset($peers[$itemkey+1])) {
379
                    $olditem = $peers[$itemkey+1];
380
                    $peers[$itemkey+1] = $id;
381
                    $peers[$itemkey] = $olditem;
382
                } else {
383
                    throw new \moodle_exception('listcantmoveup');
384
                }
385
                break;
386
 
387
            case 'up' :
388
                if (isset($peers[$itemkey-1])) {
389
                    $olditem = $peers[$itemkey-1];
390
                    $peers[$itemkey-1] = $id;
391
                    $peers[$itemkey] = $olditem;
392
                } else {
393
                    throw new \moodle_exception('listcantmovedown');
394
                }
395
                break;
396
        }
397
        $this->reorder_peers($peers);
398
    }
399
 
1441 ariadna 400
    /**
401
     * Reorder peers
402
     *
403
     * @param $peers
404
     * @return void
405
     * @throws dml_exception
406
     * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
407
     * @todo Final removal in Moodle 6.0 MDL-80804.
408
     */
1 efrain 409
    public function reorder_peers($peers) {
1441 ariadna 410
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
1 efrain 411
        global $DB;
412
        foreach ($peers as $key => $peer) {
413
            $DB->set_field($this->table, "sortorder", $key, array("id"=>$peer));
414
        }
415
    }
416
 
417
    /**
418
     * Moves the item one step up in the tree.
419
     *
420
     * @param int $id an item index.
421
     * @return list_item the item that used to be the parent of the item moved.
1441 ariadna 422
     * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
423
     * @todo Final removal in Moodle 6.0 MDL-80804.
1 efrain 424
     */
425
    public function move_item_left($id) {
1441 ariadna 426
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
1 efrain 427
        global $DB;
428
 
429
        $item = $this->find_item($id);
430
        if (!isset($item->parentlist->parentitem->parentlist)) {
431
            throw new \moodle_exception('listcantmoveleft');
432
        } else {
433
            $newpeers = $this->get_items_peers($item->parentlist->parentitem->id);
434
            if (isset($item->parentlist->parentitem->parentlist->parentitem)) {
435
                $newparent = $item->parentlist->parentitem->parentlist->parentitem->id;
436
            } else {
437
                $newparent = $this->get_top_level_parent_id($item);
438
            }
439
            $DB->set_field($this->table, "parent", $newparent, array("id"=>$item->id));
440
            $oldparentkey = array_search($item->parentlist->parentitem->id, $newpeers);
441
            $neworder = array_merge(array_slice($newpeers, 0, $oldparentkey+1), array($item->id), array_slice($newpeers, $oldparentkey+1));
442
            $this->reorder_peers($neworder);
443
        }
444
        return $item->parentlist->parentitem;
445
    }
446
 
447
    /**
448
     * Make item with id $id the child of the peer that is just above it in the sort order.
449
     *
450
     * @param integer $id
1441 ariadna 451
     * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
452
     * @todo Final removal in Moodle 6.0 MDL-80804.
1 efrain 453
     */
454
    public function move_item_right($id) {
1441 ariadna 455
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
1 efrain 456
        global $DB;
457
 
458
        $peers = $this->get_items_peers($id);
459
        $itemkey = array_search($id, $peers);
460
        if (!isset($peers[$itemkey-1])) {
461
            throw new \moodle_exception('listcantmoveright');
462
        } else {
463
            $DB->set_field($this->table, "parent", $peers[$itemkey-1], array("id"=>$peers[$itemkey]));
464
            $newparent = $this->find_item($peers[$itemkey-1]);
465
            if (isset($newparent->children)) {
466
                $newpeers = $newparent->children->get_child_ids();
467
            }
468
            if ($newpeers) {
469
                $newpeers[] = $peers[$itemkey];
470
                $this->reorder_peers($newpeers);
471
            }
472
        }
473
    }
474
 
475
    /**
476
     * process any actions.
477
     *
478
     * @param integer $left id of item to move left
479
     * @param integer $right id of item to move right
480
     * @param integer $moveup id of item to move up
481
     * @param integer $movedown id of item to move down
482
     * @return unknown
1441 ariadna 483
     * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
484
     * @todo Final removal in Moodle 6.0 MDL-80804.
1 efrain 485
     */
486
    public function process_actions($left, $right, $moveup, $movedown) {
1441 ariadna 487
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
1 efrain 488
        //should this action be processed by this list object?
489
        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))) {
490
            return false;
491
        }
492
        if (!empty($left)) {
493
            $oldparentitem = $this->move_item_left($left);
494
            if ($this->item_is_last_on_page($oldparentitem->id)) {
495
                // Item has jumped onto the next page, change page when we redirect.
496
                $this->page ++;
497
                $this->pageurl->params(array($this->pageparamname => $this->page));
498
            }
499
        } else if (!empty($right)) {
500
            $this->move_item_right($right);
501
            if ($this->item_is_first_on_page($right)) {
502
                // Item has jumped onto the previous page, change page when we redirect.
503
                $this->page --;
504
                $this->pageurl->params(array($this->pageparamname => $this->page));
505
            }
506
        } else if (!empty($moveup)) {
507
            $this->move_item_up_down('up', $moveup);
508
            if ($this->item_is_first_on_page($moveup)) {
509
                // Item has jumped onto the previous page, change page when we redirect.
510
                $this->page --;
511
                $this->pageurl->params(array($this->pageparamname => $this->page));
512
            }
513
        } else if (!empty($movedown)) {
514
            $this->move_item_up_down('down', $movedown);
515
            if ($this->item_is_last_on_page($movedown)) {
516
                // Item has jumped onto the next page, change page when we redirect.
517
                $this->page ++;
518
                $this->pageurl->params(array($this->pageparamname => $this->page));
519
            }
520
        } else {
521
            return false;
522
        }
523
 
524
        redirect($this->pageurl);
525
    }
526
 
527
    /**
528
     * @param integer $itemid an item id.
529
     * @return boolean Is the item with the given id the first top-level item on
530
     * the current page?
1441 ariadna 531
     * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
532
     * @todo Final removal in Moodle 6.0 MDL-80804.
1 efrain 533
     */
534
    public function item_is_first_on_page($itemid) {
1441 ariadna 535
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
1 efrain 536
        return $this->page && isset($this->items[$this->firstitem]) &&
537
                $itemid == $this->items[$this->firstitem]->id;
538
    }
539
 
540
    /**
541
     * @param integer $itemid an item id.
542
     * @return boolean Is the item with the given id the last top-level item on
543
     * the current page?
1441 ariadna 544
     * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
545
     * @todo Final removal in Moodle 6.0 MDL-80804.
1 efrain 546
     */
547
    public function item_is_last_on_page($itemid) {
1441 ariadna 548
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
1 efrain 549
        return $this->page && isset($this->items[$this->lastitem]) &&
550
                $itemid == $this->items[$this->lastitem]->id;
551
    }
552
}
553
 
554
/**
555
 * @package moodlecore
556
 * @copyright Jamie Pratt
557
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1441 ariadna 558
 * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
559
 * @todo Final removal in Moodle 6.0 MDL-80804.
1 efrain 560
 */
1441 ariadna 561
#[\core\attribute\deprecated(null, since: '4.5', reason: 'No longer used in core code.', mdl: 'MDL-72397')]
1 efrain 562
abstract class list_item {
563
    /** @var integer id of record, used if list is editable. */
564
    public $id;
565
 
566
    /** @var string name of this item, used if list is editable. */
567
    public $name;
568
 
569
    /** @var mixed The object or string representing this item. */
570
    public $item;
571
    public $fieldnamesname = 'name';
572
    public $attributes;
573
    public $display;
574
    public $icons = array();
575
 
576
    /** @var moodle_list */
577
    public $parentlist;
578
 
579
    /** @var moodle_list Set if there are any children of this listitem. */
580
    public $children;
581
 
582
    /**
583
     * Constructor
1441 ariadna 584
     *
1 efrain 585
     * @param mixed $item fragment of html for list item or record
586
     * @param object $parent reference to parent of this item
587
     * @param string $attributes attributes for li tag
588
     * @param boolean $display whether this item is displayed. Some items may be loaded so we have a complete
589
     *                              structure in memory to work with for actions but are not displayed.
590
     * @return list_item
1441 ariadna 591
     * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
592
     * @todo Final removal in Moodle 6.0 MDL-80804.
1 efrain 593
     */
594
    public function __construct($item, $parent, $attributes = '', $display = true) {
1441 ariadna 595
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
1 efrain 596
        $this->item = $item;
597
        if (is_object($this->item)) {
598
            $this->id = $this->item->id;
599
            $this->name = $this->item->{$this->fieldnamesname};
600
        }
601
        $this->set_parent($parent);
602
        $this->attributes = $attributes;
603
        $parentlistclass = get_class($parent);
604
        $this->children = new $parentlistclass($parent->type, $parent->attributes, $parent->editable, $parent->pageurl, 0);
605
        $this->children->set_parent($this);
606
        $this->display = $display;
607
    }
608
 
609
    /**
610
     * Output the html just for this item. Called by to_html which adds html for children.
611
     *
1441 ariadna 612
     * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
613
     * @todo Final removal in Moodle 6.0 MDL-80804.
1 efrain 614
     */
615
    public function item_html($extraargs = array()) {
1441 ariadna 616
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
1 efrain 617
        if (is_string($this->item)) {
618
            $html = $this->item;
619
        } elseif (is_object($this->item)) {
620
            //for debug purposes only. You should create a sub class to
621
            //properly handle the record
622
            $html = join(', ', (array)$this->item);
623
        }
624
        return $html;
625
    }
626
 
627
    /**
628
     * Returns html
629
     *
630
     * @param integer $indent
631
     * @param array $extraargs any extra data that is needed to print the list item
632
     *                            may be used by sub class.
633
     * @return string html
1441 ariadna 634
     * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
635
     * @todo Final removal in Moodle 6.0 MDL-80804.
1 efrain 636
     */
637
    public function to_html($indent = 0, $extraargs = array()) {
1441 ariadna 638
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
1 efrain 639
        if (!$this->display) {
640
            return '';
641
        }
642
        $tabs = str_repeat("\t", $indent);
643
 
644
        if (isset($this->children)) {
645
            $childrenhtml = $this->children->to_html($indent+1, $extraargs);
646
        } else {
647
            $childrenhtml = '';
648
        }
649
        return $this->item_html($extraargs).'&nbsp;'.(join('', $this->icons)).(($childrenhtml !='')?("\n".$childrenhtml):'');
650
    }
651
 
1441 ariadna 652
    /**
653
     * Set icon HTML
654
     *
655
     * @param $first
656
     * @param $last
657
     * @param $lastitem
658
     * @return void
659
     * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
660
     * @todo Final removal in Moodle 6.0 MDL-80804.
661
     */
1 efrain 662
    public function set_icon_html($first, $last, $lastitem) {
1441 ariadna 663
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
1 efrain 664
        global $CFG;
665
        $strmoveup = get_string('moveup');
666
        $strmovedown = get_string('movedown');
667
        $strmoveleft = get_string('maketoplevelitem', 'question');
668
 
669
        if (right_to_left()) {   // Exchange arrows on RTL
670
            $rightarrow = 'left';
671
            $leftarrow  = 'right';
672
        } else {
673
            $rightarrow = 'right';
674
            $leftarrow  = 'left';
675
        }
676
 
677
        if (isset($this->parentlist->parentitem)) {
678
            $parentitem = $this->parentlist->parentitem;
679
            if (isset($parentitem->parentlist->parentitem)) {
680
                $action = get_string('makechildof', 'question', $parentitem->parentlist->parentitem->name);
681
            } else {
682
                $action = $strmoveleft;
683
            }
684
            $url = new moodle_url($this->parentlist->pageurl, (array('sesskey'=>sesskey(), 'left'=>$this->id)));
685
            $this->icons['left'] = $this->image_icon($action, $url, $leftarrow);
686
        } else {
687
            $this->icons['left'] =  $this->image_spacer();
688
        }
689
 
690
        if (!$first) {
691
            $url = new moodle_url($this->parentlist->pageurl, (array('sesskey'=>sesskey(), 'moveup'=>$this->id)));
692
            $this->icons['up'] = $this->image_icon($strmoveup, $url, 'up');
693
        } else {
694
            $this->icons['up'] =  $this->image_spacer();
695
        }
696
 
697
        if (!$last) {
698
            $url = new moodle_url($this->parentlist->pageurl, (array('sesskey'=>sesskey(), 'movedown'=>$this->id)));
699
            $this->icons['down'] = $this->image_icon($strmovedown, $url, 'down');
700
        } else {
701
            $this->icons['down'] =  $this->image_spacer();
702
        }
703
 
704
        if (!empty($lastitem)) {
705
            $makechildof = get_string('makechildof', 'question', $lastitem->name);
706
            $url = new moodle_url($this->parentlist->pageurl, (array('sesskey'=>sesskey(), 'right'=>$this->id)));
707
            $this->icons['right'] = $this->image_icon($makechildof, $url, $rightarrow);
708
        } else {
709
            $this->icons['right'] =  $this->image_spacer();
710
        }
711
    }
712
 
1441 ariadna 713
    /**
714
     * Return image icon HTML
715
     *
716
     * @param $action
717
     * @param $url
718
     * @param $icon
719
     * @return string
720
     * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
721
     * @todo Final removal in Moodle 6.0 MDL-80804.
722
     */
1 efrain 723
    public function image_icon($action, $url, $icon) {
1441 ariadna 724
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
1 efrain 725
        global $OUTPUT;
726
        return '<a title="' . s($action) .'" href="'.$url.'">' .
727
                $OUTPUT->pix_icon('t/' . $icon, $action) . '</a> ';
728
    }
729
 
1441 ariadna 730
    /**
731
     * Return image spacer HTML
732
     *
733
     * @return mixed
734
     * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
735
     * @todo Final removal in Moodle 6.0 MDL-80804.
736
     */
1 efrain 737
    public function image_spacer() {
1441 ariadna 738
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
1 efrain 739
        global $OUTPUT;
740
        return $OUTPUT->spacer();
741
    }
742
 
743
    /**
744
     * Recurse down tree creating list_items, called from moodle_list::list_from_records
745
     *
746
     * @param array $records
747
     * @param array $children
748
     * @param integer $thisrecordid
1441 ariadna 749
     * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
750
     * @todo Final removal in Moodle 6.0 MDL-80804.
1 efrain 751
     */
752
    public function create_children(&$records, &$children, $thisrecordid) {
1441 ariadna 753
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
1 efrain 754
        //keys where value is $thisrecordid
755
        $thischildren = moodle_array_keys_filter($children, $thisrecordid);
756
        foreach ($thischildren as $child) {
757
            $thisclass = get_class($this);
758
            $newlistitem = new $thisclass($records[$child], $this->children, $this->attributes);
759
            $this->children->add_item($newlistitem);
760
            $newlistitem->create_children($records, $children, $records[$child]->id);
761
        }
762
    }
763
 
1441 ariadna 764
    /**
765
     * Set parent list
766
     *
767
     * @param $parent
768
     * @return void
769
     * @deprecated Since Moodle 4.5 MDL-72397. This is no longer used in core code.
770
     * @todo Final removal in Moodle 6.0 MDL-80804.
771
     */
1 efrain 772
    public function set_parent($parent) {
1441 ariadna 773
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
1 efrain 774
        $this->parentlist = $parent;
775
    }
776
}