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