Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Book module local lib functions
19
 *
20
 * @package    mod_book
21
 * @copyright  2010-2011 Petr Skoda {@link http://skodak.org}
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
defined('MOODLE_INTERNAL') || die;
26
 
27
require_once(__DIR__.'/lib.php');
28
require_once($CFG->libdir.'/filelib.php');
29
 
30
/**
31
 * The following defines are used to define how the chapters and subchapters of a book should be displayed in that table of contents.
32
 * BOOK_NUM_NONE        No special styling will applied and the editor will be able to do what ever thay want in the title
33
 * BOOK_NUM_NUMBERS     Chapters and subchapters are numbered (1, 1.1, 1.2, 2, ...)
34
 * BOOK_NUM_BULLETS     Subchapters are indented and displayed with bullets
35
 * BOOK_NUM_INDENTED    Subchapters are indented
36
 */
37
define('BOOK_NUM_NONE',     '0');
38
define('BOOK_NUM_NUMBERS',  '1');
39
define('BOOK_NUM_BULLETS',  '2');
40
define('BOOK_NUM_INDENTED', '3');
41
 
42
/**
43
 * Preload book chapters and fix toc structure if necessary.
44
 *
45
 * Returns array of chapters with standard 'pagenum', 'id, pagenum, subchapter, title, content, contentformat, hidden'
46
 * and extra 'parent, number, subchapters, prev, next'.
47
 * Please note the content/text of chapters is not included.
48
 *
49
 * @param  stdClass $book
50
 * @return array of id=>chapter
51
 */
52
function book_preload_chapters($book) {
53
    global $DB;
54
    $chapters = $DB->get_records('book_chapters', array('bookid' => $book->id), 'pagenum', 'id, pagenum,
55
            subchapter, title, content, contentformat, hidden');
56
    if (!$chapters) {
57
        return array();
58
    }
59
 
60
    $prev = null;
61
    $prevsub = null;
62
 
63
    $first = true;
64
    $hidesub = true;
65
    $parent = null;
66
    $pagenum = 0; // chapter sort
67
    $i = 0;       // main chapter num
68
    $j = 0;       // subchapter num
69
    foreach ($chapters as $id => $ch) {
70
        $oldch = clone($ch);
71
        $pagenum++;
72
        $ch->pagenum = $pagenum;
73
        if ($first) {
74
            // book can not start with a subchapter
75
            $ch->subchapter = 0;
76
            $first = false;
77
        }
78
        if (!$ch->subchapter) {
79
            if ($ch->hidden) {
80
                if ($book->numbering == BOOK_NUM_NUMBERS) {
81
                    $ch->number = 'x';
82
                } else {
83
                    $ch->number = null;
84
                }
85
            } else {
86
                $i++;
87
                $ch->number = $i;
88
            }
89
            $j = 0;
90
            $prevsub = null;
91
            $hidesub = $ch->hidden;
92
            $parent = $ch->id;
93
            $ch->parent = null;
94
            $ch->subchapters = array();
95
        } else {
96
            $ch->parent = $parent;
97
            $ch->subchapters = null;
98
            $chapters[$parent]->subchapters[$ch->id] = $ch->id;
99
            if ($hidesub) {
100
                // all subchapters in hidden chapter must be hidden too
101
                $ch->hidden = 1;
102
            }
103
            if ($ch->hidden) {
104
                if ($book->numbering == BOOK_NUM_NUMBERS) {
105
                    $ch->number = 'x';
106
                } else {
107
                    $ch->number = null;
108
                }
109
            } else {
110
                $j++;
111
                $ch->number = $j;
112
            }
113
        }
114
 
115
        if ($oldch->subchapter != $ch->subchapter or $oldch->pagenum != $ch->pagenum or $oldch->hidden != $ch->hidden) {
116
            // update only if something changed
117
            $DB->update_record('book_chapters', $ch);
118
        }
119
        $chapters[$id] = $ch;
120
    }
121
 
122
    return $chapters;
123
}
124
 
125
/**
126
 * Returns the title for a given chapter
127
 *
128
 * @param int $chid
129
 * @param array $chapters
130
 * @param stdClass $book
131
 * @param context_module $context
132
 * @return string
133
 */
134
function book_get_chapter_title($chid, $chapters, $book, $context) {
135
    $ch = $chapters[$chid];
136
    $title = trim(format_string($ch->title, true, array('context'=>$context)));
137
    $numbers = array();
138
    if ($book->numbering == BOOK_NUM_NUMBERS) {
139
        if ($ch->parent and $chapters[$ch->parent]->number) {
140
            $numbers[] = $chapters[$ch->parent]->number;
141
        }
142
        if ($ch->number) {
143
            $numbers[] = $ch->number;
144
        }
145
    }
146
 
147
    if ($numbers) {
148
        $title = implode('.', $numbers) . '. ' . $title;
149
    }
150
 
151
    return $title;
152
}
153
 
154
/**
155
 * Add the book TOC sticky block to the default region.
156
 *
157
 * @param   array       $chapters   The Chapters in the book
158
 * @param   stdClass    $chapter    The current chapter
159
 * @param   stdClass    $book       The book
160
 * @param   stdClass    $cm         The course module
161
 * @param   bool|null   $edit       Whether the user is editing
162
 */
163
function book_add_fake_block($chapters, $chapter, $book, $cm, $edit = null) {
164
    global $PAGE, $USER;
165
 
166
    if ($edit === null) {
167
        if (has_capability('mod/book:edit', context_module::instance($cm->id))) {
168
            if (isset($USER->editing)) {
169
                $edit = $USER->editing;
170
            } else {
171
                $edit = 0;
172
            }
173
        } else {
174
            $edit = 0;
175
        }
176
    }
177
 
178
    $toc = book_get_toc($chapters, $chapter, $book, $cm, $edit);
179
 
180
    $bc = new block_contents();
181
    $bc->title = get_string('toc', 'mod_book');
182
    $bc->attributes['class'] = 'block block_book_toc';
183
    $bc->content = $toc;
184
 
185
    $defaultregion = $PAGE->blocks->get_default_region();
186
    $PAGE->blocks->add_fake_block($bc, $defaultregion);
187
}
188
 
189
/**
190
 * Generate toc structure
191
 *
192
 * @param array $chapters
193
 * @param stdClass $chapter
194
 * @param stdClass $book
195
 * @param stdClass $cm
196
 * @param bool $edit
197
 * @return string
198
 */
199
function book_get_toc($chapters, $chapter, $book, $cm, $edit) {
200
    global $USER, $OUTPUT;
201
 
202
    $toc = '';
203
    $nch = 0;   // Chapter number
204
    $ns = 0;    // Subchapter number
205
    $first = 1;
206
 
207
    $context = context_module::instance($cm->id);
208
    $viewhidden = has_capability('mod/book:viewhiddenchapters', $context);
209
 
210
    switch ($book->numbering) {
211
        case BOOK_NUM_NONE:
212
            $toc .= html_writer::start_tag('div', array('class' => 'book_toc book_toc_none clearfix'));
213
            break;
214
        case BOOK_NUM_NUMBERS:
215
            $toc .= html_writer::start_tag('div', array('class' => 'book_toc book_toc_numbered clearfix'));
216
            break;
217
        case BOOK_NUM_BULLETS:
218
            $toc .= html_writer::start_tag('div', array('class' => 'book_toc book_toc_bullets clearfix'));
219
            break;
220
        case BOOK_NUM_INDENTED:
221
            $toc .= html_writer::start_tag('div', array('class' => 'book_toc book_toc_indented clearfix'));
222
            break;
223
    }
224
 
225
    if ($edit) { // Editing on (Teacher's TOC).
226
        $toc .= html_writer::start_tag('ul');
227
        $i = 0;
228
        foreach ($chapters as $ch) {
229
            $i++;
230
            $title = trim(format_string($ch->title, true, array('context' => $context)));
231
            $titleunescaped = trim(format_string($ch->title, true, array('context' => $context, 'escape' => false)));
232
            $titleout = $title;
233
 
234
            if (!$ch->subchapter) {
235
 
236
                if ($first) {
237
                    $toc .= html_writer::start_tag('li');
238
                } else {
239
                    $toc .= html_writer::end_tag('ul');
240
                    $toc .= html_writer::end_tag('li');
241
                    $toc .= html_writer::start_tag('li');
242
                }
243
 
244
                if (!$ch->hidden) {
245
                    $nch++;
246
                    $ns = 0;
247
                    if ($book->numbering == BOOK_NUM_NUMBERS) {
248
                        $title = "$nch. $title";
249
                        $titleout = $title;
250
                    }
251
                } else {
252
                    if ($book->numbering == BOOK_NUM_NUMBERS) {
253
                        $title = "x. $title";
254
                    }
255
                    $titleout = html_writer::tag('span', $title, array('class' => 'dimmed_text'));
256
                }
257
            } else {
258
 
259
                if ($first) {
260
                    $toc .= html_writer::start_tag('li');
261
                    $toc .= html_writer::start_tag('ul');
262
                    $toc .= html_writer::start_tag('li');
263
                } else {
264
                    $toc .= html_writer::start_tag('li');
265
                }
266
 
267
                if (!$ch->hidden) {
268
                    $ns++;
269
                    if ($book->numbering == BOOK_NUM_NUMBERS) {
270
                        $title = "$nch.$ns. $title";
271
                        $titleout = $title;
272
                    }
273
                } else {
274
                    if ($book->numbering == BOOK_NUM_NUMBERS) {
275
                        if (empty($chapters[$ch->parent]->hidden)) {
276
                            $title = "$nch.x. $title";
277
                        } else {
278
                            $title = "x.x. $title";
279
                        }
280
                    }
281
                    $titleout = html_writer::tag('span', $title, array('class' => 'dimmed_text'));
282
                }
283
            }
284
            $toc .= html_writer::start_tag('div', array('class' => 'd-flex'));
285
            if ($ch->id == $chapter->id) {
286
                $toc .= html_writer::tag('strong', $titleout, array('class' => 'text-truncate'));
287
            } else {
288
                $toc .= html_writer::link(new moodle_url('view.php', array('id' => $cm->id, 'chapterid' => $ch->id)), $titleout,
289
                    array('title' => $titleunescaped, 'class' => 'text-truncate'));
290
            }
291
 
292
            $toc .= html_writer::start_tag('div', array('class' => 'action-list d-flex ml-auto'));
293
            if ($i != 1) {
294
                $toc .= html_writer::link(new moodle_url('move.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'up' => '1', 'sesskey' => $USER->sesskey)),
295
                        $OUTPUT->pix_icon('t/up', get_string('movechapterup', 'mod_book', $title)),
296
                        array('title' => get_string('movechapterup', 'mod_book', $titleunescaped)));
297
            }
298
            if ($i != count($chapters)) {
299
                $toc .= html_writer::link(new moodle_url('move.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'up' => '0', 'sesskey' => $USER->sesskey)),
300
                        $OUTPUT->pix_icon('t/down', get_string('movechapterdown', 'mod_book', $title)),
301
                        array('title' => get_string('movechapterdown', 'mod_book', $titleunescaped)));
302
            }
303
            $toc .= html_writer::link(new moodle_url('edit.php', array('cmid' => $cm->id, 'id' => $ch->id)),
304
                    $OUTPUT->pix_icon('t/edit', get_string('editchapter', 'mod_book', $title)),
305
                    array('title' => get_string('editchapter', 'mod_book', $titleunescaped)));
306
 
307
            $deleteaction = new confirm_action(get_string('deletechapter', 'mod_book', $titleunescaped));
308
            $toc .= $OUTPUT->action_icon(
309
                    new moodle_url('delete.php', [
310
                            'id'        => $cm->id,
311
                            'chapterid' => $ch->id,
312
                            'sesskey'   => sesskey(),
313
                            'confirm'   => 1,
314
                        ]),
315
                    new pix_icon('t/delete', get_string('deletechapter', 'mod_book', $title)),
316
                    $deleteaction,
317
                    ['title' => get_string('deletechapter', 'mod_book', $titleunescaped)]
318
                );
319
 
320
            if ($ch->hidden) {
321
                $toc .= html_writer::link(new moodle_url('show.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'sesskey' => $USER->sesskey)),
322
                        $OUTPUT->pix_icon('t/show', get_string('showchapter', 'mod_book', $title)),
323
                        array('title' => get_string('showchapter', 'mod_book', $titleunescaped)));
324
            } else {
325
                $toc .= html_writer::link(new moodle_url('show.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'sesskey' => $USER->sesskey)),
326
                        $OUTPUT->pix_icon('t/hide', get_string('hidechapter', 'mod_book', $title)),
327
                        array('title' => get_string('hidechapter', 'mod_book', $titleunescaped)));
328
            }
329
 
330
            $buttontitle = get_string('addafterchapter', 'mod_book', ['title' => $ch->title]);
331
            $toc .= html_writer::link(new moodle_url('edit.php', array('cmid' => $cm->id, 'pagenum' => $ch->pagenum, 'subchapter' => $ch->subchapter)),
332
                                            $OUTPUT->pix_icon('add', $buttontitle, 'mod_book'), array('title' => $buttontitle));
333
            $toc .= html_writer::end_tag('div');
334
            $toc .= html_writer::end_tag('div');
335
 
336
            if (!$ch->subchapter) {
337
                $toc .= html_writer::start_tag('ul');
338
            } else {
339
                $toc .= html_writer::end_tag('li');
340
            }
341
            $first = 0;
342
        }
343
 
344
        $toc .= html_writer::end_tag('ul');
345
        $toc .= html_writer::end_tag('li');
346
        $toc .= html_writer::end_tag('ul');
347
 
348
    } else { // Editing off. Normal students, teachers view.
349
        $toc .= html_writer::start_tag('ul');
350
        foreach ($chapters as $ch) {
351
            $title = trim(format_string($ch->title, true, array('context'=>$context)));
352
            $titleunescaped = trim(format_string($ch->title, true, array('context' => $context, 'escape' => false)));
353
            if (!$ch->hidden || ($ch->hidden && $viewhidden)) {
354
                if (!$ch->subchapter) {
355
                    $nch++;
356
                    $ns = 0;
357
 
358
                    if ($first) {
359
                        $toc .= html_writer::start_tag('li');
360
                    } else {
361
                        $toc .= html_writer::end_tag('ul');
362
                        $toc .= html_writer::end_tag('li');
363
                        $toc .= html_writer::start_tag('li');
364
                    }
365
 
366
                    if ($book->numbering == BOOK_NUM_NUMBERS) {
367
                          $title = "$nch. $title";
368
                    }
369
                } else {
370
                    $ns++;
371
 
372
                    if ($first) {
373
                        $toc .= html_writer::start_tag('li');
374
                        $toc .= html_writer::start_tag('ul');
375
                        $toc .= html_writer::start_tag('li');
376
                    } else {
377
                        $toc .= html_writer::start_tag('li');
378
                    }
379
 
380
                    if ($book->numbering == BOOK_NUM_NUMBERS) {
381
                          $title = "$nch.$ns. $title";
382
                    }
383
                }
384
 
385
                $cssclass = ($ch->hidden && $viewhidden) ? 'dimmed_text' : '';
386
 
387
                if ($ch->id == $chapter->id) {
388
                    $toc .= html_writer::tag('strong', $title, array('class' => $cssclass));
389
                } else {
390
                    $toc .= html_writer::link(new moodle_url('view.php',
391
                                              array('id' => $cm->id, 'chapterid' => $ch->id)),
392
                                              $title, array('title' => s($titleunescaped), 'class' => $cssclass));
393
                }
394
 
395
                if (!$ch->subchapter) {
396
                    $toc .= html_writer::start_tag('ul');
397
                } else {
398
                    $toc .= html_writer::end_tag('li');
399
                }
400
 
401
                $first = 0;
402
            }
403
        }
404
 
405
        $toc .= html_writer::end_tag('ul');
406
        $toc .= html_writer::end_tag('li');
407
        $toc .= html_writer::end_tag('ul');
408
 
409
    }
410
 
411
    $toc .= html_writer::end_tag('div');
412
 
413
    $toc = str_replace('<ul></ul>', '', $toc); // Cleanup of invalid structures.
414
 
415
    return $toc;
416
}
417
 
418
/**
419
 * Returns book chapters tagged with a specified tag.
420
 *
421
 * This is a callback used by the tag area mod_book/book_chapters to search for book chapters
422
 * tagged with a specific tag.
423
 *
424
 * @param core_tag_tag $tag
425
 * @param bool $exclusivemode if set to true it means that no other entities tagged with this tag
426
 *             are displayed on the page and the per-page limit may be bigger
427
 * @param int $fromctx context id where the link was displayed, may be used by callbacks
428
 *            to display items in the same context first
429
 * @param int $ctx context id where to search for records
430
 * @param bool $rec search in subcontexts as well
431
 * @param int $page 0-based number of page being displayed
432
 * @return \core_tag\output\tagindex
433
 */
434
function mod_book_get_tagged_chapters($tag, $exclusivemode = false, $fromctx = 0, $ctx = 0, $rec = true, $page = 0) {
435
    global $OUTPUT;
436
    $perpage = $exclusivemode ? 20 : 5;
437
 
438
    // Build the SQL query.
439
    $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
440
    $query = "SELECT bc.id, bc.title, bc.bookid, bc.hidden,
441
                    cm.id AS cmid, c.id AS courseid, c.shortname, c.fullname, $ctxselect
442
                FROM {book_chapters} bc
443
                JOIN {book} b ON b.id = bc.bookid
444
                JOIN {modules} m ON m.name='book'
445
                JOIN {course_modules} cm ON cm.module = m.id AND cm.instance = b.id
446
                JOIN {tag_instance} tt ON bc.id = tt.itemid
447
                JOIN {course} c ON cm.course = c.id
448
                JOIN {context} ctx ON ctx.instanceid = cm.id AND ctx.contextlevel = :coursemodulecontextlevel
449
               WHERE tt.itemtype = :itemtype AND tt.tagid = :tagid AND tt.component = :component
450
                 AND cm.deletioninprogress = 0
451
                 AND bc.id %ITEMFILTER% AND c.id %COURSEFILTER%";
452
 
453
    $params = array('itemtype' => 'book_chapters', 'tagid' => $tag->id, 'component' => 'mod_book',
454
                    'coursemodulecontextlevel' => CONTEXT_MODULE);
455
 
456
    if ($ctx) {
457
        $context = $ctx ? context::instance_by_id($ctx) : context_system::instance();
458
        $query .= $rec ? ' AND (ctx.id = :contextid OR ctx.path LIKE :path)' : ' AND ctx.id = :contextid';
459
        $params['contextid'] = $context->id;
460
        $params['path'] = $context->path.'/%';
461
    }
462
 
463
    $query .= " ORDER BY ";
464
    if ($fromctx) {
465
        // In order-clause specify that modules from inside "fromctx" context should be returned first.
466
        $fromcontext = context::instance_by_id($fromctx);
467
        $query .= ' (CASE WHEN ctx.id = :fromcontextid OR ctx.path LIKE :frompath THEN 0 ELSE 1 END),';
468
        $params['fromcontextid'] = $fromcontext->id;
469
        $params['frompath'] = $fromcontext->path.'/%';
470
    }
471
    $query .= ' c.sortorder, cm.id, bc.id';
472
 
473
    $totalpages = $page + 1;
474
 
475
    // Use core_tag_index_builder to build and filter the list of items.
476
    $builder = new core_tag_index_builder('mod_book', 'book_chapters', $query, $params, $page * $perpage, $perpage + 1);
477
    while ($item = $builder->has_item_that_needs_access_check()) {
478
        context_helper::preload_from_record($item);
479
        $courseid = $item->courseid;
480
        if (!$builder->can_access_course($courseid)) {
481
            $builder->set_accessible($item, false);
482
            continue;
483
        }
484
        $modinfo = get_fast_modinfo($builder->get_course($courseid));
485
        // Set accessibility of this item and all other items in the same course.
486
        $builder->walk(function ($taggeditem) use ($courseid, $modinfo, $builder) {
487
            if ($taggeditem->courseid == $courseid) {
488
                $accessible = false;
489
                if (($cm = $modinfo->get_cm($taggeditem->cmid)) && $cm->uservisible) {
490
                    if (empty($taggeditem->hidden)) {
491
                        $accessible = true;
492
                    } else {
493
                        $accessible = has_capability('mod/book:viewhiddenchapters', context_module::instance($cm->id));
494
                    }
495
                }
496
                $builder->set_accessible($taggeditem, $accessible);
497
            }
498
        });
499
    }
500
 
501
    $items = $builder->get_items();
502
    if (count($items) > $perpage) {
503
        $totalpages = $page + 2; // We don't need exact page count, just indicate that the next page exists.
504
        array_pop($items);
505
    }
506
 
507
    // Build the display contents.
508
    if ($items) {
509
        $tagfeed = new core_tag\output\tagfeed();
510
        foreach ($items as $item) {
511
            context_helper::preload_from_record($item);
512
            $modinfo = get_fast_modinfo($item->courseid);
513
            $cm = $modinfo->get_cm($item->cmid);
514
            $pageurl = new moodle_url('/mod/book/view.php', array('chapterid' => $item->id, 'b' => $item->bookid));
515
            $pagename = format_string($item->title, true, array('context' => context_module::instance($item->cmid)));
516
            $pagename = html_writer::link($pageurl, $pagename);
517
            $courseurl = course_get_url($item->courseid, $cm->sectionnum);
518
            $cmname = html_writer::link($cm->url, $cm->get_formatted_name());
519
            $coursename = format_string($item->fullname, true, array('context' => context_course::instance($item->courseid)));
520
            $coursename = html_writer::link($courseurl, $coursename);
521
            $icon = html_writer::link($pageurl, html_writer::empty_tag('img', array('src' => $cm->get_icon_url())));
522
            $tagfeed->add($icon, $pagename, $cmname.'<br>'.$coursename);
523
        }
524
 
525
        $content = $OUTPUT->render_from_template('core_tag/tagfeed',
526
            $tagfeed->export_for_template($OUTPUT));
527
 
528
        return new core_tag\output\tagindex($tag, 'mod_book', 'book_chapters', $content,
529
            $exclusivemode, $fromctx, $ctx, $rec, $page, $totalpages);
530
    }
531
}
532
 
533
/**
534
 * File browsing support class
535
 *
536
 * @copyright  2010-2011 Petr Skoda {@link http://skodak.org}
537
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
538
 */
539
class book_file_info extends file_info {
540
    /** @var stdClass Course object */
541
    protected $course;
542
    /** @var stdClass Course module object */
543
    protected $cm;
544
    /** @var array Available file areas */
545
    protected $areas;
546
    /** @var string File area to browse */
547
    protected $filearea;
548
 
549
    /**
550
     * Constructor
551
     *
552
     * @param file_browser $browser file_browser instance
553
     * @param stdClass $course course object
554
     * @param stdClass $cm course module object
555
     * @param stdClass $context module context
556
     * @param array $areas available file areas
557
     * @param string $filearea file area to browse
558
     */
559
    public function __construct($browser, $course, $cm, $context, $areas, $filearea) {
560
        parent::__construct($browser, $context);
561
        $this->course   = $course;
562
        $this->cm       = $cm;
563
        $this->areas    = $areas;
564
        $this->filearea = $filearea;
565
    }
566
 
567
    /**
568
     * Returns list of standard virtual file/directory identification.
569
     * The difference from stored_file parameters is that null values
570
     * are allowed in all fields
571
     * @return array with keys contextid, filearea, itemid, filepath and filename
572
     */
573
    public function get_params() {
574
        return array('contextid'=>$this->context->id,
575
                     'component'=>'mod_book',
576
                     'filearea' =>$this->filearea,
577
                     'itemid'   =>null,
578
                     'filepath' =>null,
579
                     'filename' =>null);
580
    }
581
 
582
    /**
583
     * Returns localised visible name.
584
     * @return string
585
     */
586
    public function get_visible_name() {
587
        return $this->areas[$this->filearea];
588
    }
589
 
590
    /**
591
     * Can I add new files or directories?
592
     * @return bool
593
     */
594
    public function is_writable() {
595
        return false;
596
    }
597
 
598
    /**
599
     * Is directory?
600
     * @return bool
601
     */
602
    public function is_directory() {
603
        return true;
604
    }
605
 
606
    /**
607
     * Returns list of children.
608
     * @return array of file_info instances
609
     */
610
    public function get_children() {
611
        return $this->get_filtered_children('*', false, true);
612
    }
613
 
614
    /**
615
     * Help function to return files matching extensions or their count
616
     *
617
     * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
618
     * @param bool|int $countonly if false returns the children, if an int returns just the
619
     *    count of children but stops counting when $countonly number of children is reached
620
     * @param bool $returnemptyfolders if true returns items that don't have matching files inside
621
     * @return array|int array of file_info instances or the count
622
     */
623
    private function get_filtered_children($extensions = '*', $countonly = false, $returnemptyfolders = false) {
624
        global $DB;
625
        $params = array('contextid' => $this->context->id,
626
            'component' => 'mod_book',
627
            'filearea' => $this->filearea,
628
            'bookid' => $this->cm->instance);
629
        $sql = 'SELECT DISTINCT bc.id, bc.pagenum
630
                    FROM {files} f, {book_chapters} bc
631
                    WHERE f.contextid = :contextid
632
                    AND f.component = :component
633
                    AND f.filearea = :filearea
634
                    AND bc.bookid = :bookid
635
                    AND bc.id = f.itemid';
636
        if (!$returnemptyfolders) {
637
            $sql .= ' AND filename <> :emptyfilename';
638
            $params['emptyfilename'] = '.';
639
        }
640
        list($sql2, $params2) = $this->build_search_files_sql($extensions, 'f');
641
        $sql .= ' '.$sql2;
642
        $params = array_merge($params, $params2);
643
        if ($countonly === false) {
644
            $sql .= ' ORDER BY bc.pagenum';
645
        }
646
 
647
        $rs = $DB->get_recordset_sql($sql, $params);
648
        $children = array();
649
        foreach ($rs as $record) {
650
            if ($child = $this->browser->get_file_info($this->context, 'mod_book', $this->filearea, $record->id)) {
651
                if ($returnemptyfolders || $child->count_non_empty_children($extensions)) {
652
                    $children[] = $child;
653
                }
654
            }
655
            if ($countonly !== false && count($children) >= $countonly) {
656
                break;
657
            }
658
        }
659
        $rs->close();
660
        if ($countonly !== false) {
661
            return count($children);
662
        }
663
        return $children;
664
    }
665
 
666
    /**
667
     * Returns list of children which are either files matching the specified extensions
668
     * or folders that contain at least one such file.
669
     *
670
     * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
671
     * @return array of file_info instances
672
     */
673
    public function get_non_empty_children($extensions = '*') {
674
        return $this->get_filtered_children($extensions, false);
675
    }
676
 
677
    /**
678
     * Returns the number of children which are either files matching the specified extensions
679
     * or folders containing at least one such file.
680
     *
681
     * @param string|array $extensions, for example '*' or array('.gif','.jpg')
682
     * @param int $limit stop counting after at least $limit non-empty children are found
683
     * @return int
684
     */
685
    public function count_non_empty_children($extensions = '*', $limit = 1) {
686
        return $this->get_filtered_children($extensions, $limit);
687
    }
688
 
689
    /**
690
     * Returns parent file_info instance
691
     * @return file_info or null for root
692
     */
693
    public function get_parent() {
694
        return $this->browser->get_file_info($this->context);
695
    }
696
}