2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Book module local lib functions
21 * @copyright 2010-2011 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die;
27 require_once(dirname(__FILE__
).'/lib.php');
28 require_once($CFG->libdir
.'/filelib.php');
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
37 define('BOOK_NUM_NONE', '0');
38 define('BOOK_NUM_NUMBERS', '1');
39 define('BOOK_NUM_BULLETS', '2');
40 define('BOOK_NUM_INDENTED', '3');
43 * Preload book chapters and fix toc structure if necessary.
45 * Returns array of chapters with standard 'pagenum', 'id, pagenum, subchapter, title, hidden'
46 * and extra 'parent, number, subchapters, prev, next'.
47 * Please note the content/text of chapters is not included.
49 * @param stdClass $book
50 * @return array of id=>chapter
52 function book_preload_chapters($book) {
54 $chapters = $DB->get_records('book_chapters', array('bookid'=>$book->id
), 'pagenum', 'id, pagenum, subchapter, title, hidden');
65 $pagenum = 0; // chapter sort
66 $i = 0; // main chapter num
67 $j = 0; // subchapter num
68 foreach ($chapters as $id => $ch) {
71 $ch->pagenum
= $pagenum;
73 // book can not start with a subchapter
77 if (!$ch->subchapter
) {
79 if ($book->numbering
== BOOK_NUM_NUMBERS
) {
90 $hidesub = $ch->hidden
;
93 $ch->subchapters
= array();
95 $ch->parent
= $parent;
96 $ch->subchapters
= null;
97 $chapters[$parent]->subchapters
[$ch->id
] = $ch->id
;
99 // all subchapters in hidden chapter must be hidden too
103 if ($book->numbering
== BOOK_NUM_NUMBERS
) {
114 if ($oldch->subchapter
!= $ch->subchapter
or $oldch->pagenum
!= $ch->pagenum
or $oldch->hidden
!= $ch->hidden
) {
115 // update only if something changed
116 $DB->update_record('book_chapters', $ch);
118 $chapters[$id] = $ch;
125 * Returns the title for a given chapter
128 * @param array $chapters
129 * @param stdClass $book
130 * @param context_module $context
133 function book_get_chapter_title($chid, $chapters, $book, $context) {
134 $ch = $chapters[$chid];
135 $title = trim(format_string($ch->title
, true, array('context'=>$context)));
137 if ($book->numbering
== BOOK_NUM_NUMBERS
) {
138 if ($ch->parent
and $chapters[$ch->parent
]->number
) {
139 $numbers[] = $chapters[$ch->parent
]->number
;
142 $numbers[] = $ch->number
;
147 $title = implode('.', $numbers).' '.$title;
154 * Add the book TOC sticky block to the 1st region available
156 * @param array $chapters
157 * @param stdClass $chapter
158 * @param stdClass $book
159 * @param stdClass $cm
162 function book_add_fake_block($chapters, $chapter, $book, $cm, $edit) {
163 global $OUTPUT, $PAGE;
165 $toc = book_get_toc($chapters, $chapter, $book, $cm, $edit, 0);
167 $bc = new block_contents();
168 $bc->title
= get_string('toc', 'mod_book');
169 $bc->attributes
['class'] = 'block block_book_toc';
172 $regions = $PAGE->blocks
->get_regions();
173 $firstregion = reset($regions);
174 $PAGE->blocks
->add_fake_block($bc, $firstregion);
178 * Generate toc structure
180 * @param array $chapters
181 * @param stdClass $chapter
182 * @param stdClass $book
183 * @param stdClass $cm
187 function book_get_toc($chapters, $chapter, $book, $cm, $edit) {
188 global $USER, $OUTPUT;
191 $nch = 0; // Chapter number
192 $ns = 0; // Subchapter number
195 $context = context_module
::instance($cm->id
);
197 switch ($book->numbering
) {
199 $toc .= html_writer
::start_tag('div', array('class' => 'book_toc_none clearfix'));
201 case BOOK_NUM_NUMBERS
:
202 $toc .= html_writer
::start_tag('div', array('class' => 'book_toc_numbered clearfix'));
204 case BOOK_NUM_BULLETS
:
205 $toc .= html_writer
::start_tag('div', array('class' => 'book_toc_bullets clearfix'));
207 case BOOK_NUM_INDENTED
:
208 $toc .= html_writer
::start_tag('div', array('class' => 'book_toc_indented clearfix'));
212 if ($edit) { // Teacher's TOC
213 $toc .= html_writer
::start_tag('ul');
215 foreach ($chapters as $ch) {
217 $title = trim(format_string($ch->title
, true, array('context'=>$context)));
218 if (!$ch->subchapter
) {
221 $toc .= html_writer
::start_tag('li', array('class' => 'clearfix'));
223 $toc .= html_writer
::end_tag('ul');
224 $toc .= html_writer
::end_tag('li');
225 $toc .= html_writer
::start_tag('li', array('class' => 'clearfix'));
231 if ($book->numbering
== BOOK_NUM_NUMBERS
) {
232 $title = "$nch $title";
235 if ($book->numbering
== BOOK_NUM_NUMBERS
) {
238 $title = html_writer
::tag('span', $title, array('class' => 'dimmed_text'));
243 $toc .= html_writer
::start_tag('li', array('class' => 'clearfix'));
244 $toc .= html_writer
::start_tag('ul');
245 $toc .= html_writer
::start_tag('li', array('class' => 'clearfix'));
247 $toc .= html_writer
::start_tag('li', array('class' => 'clearfix'));
252 if ($book->numbering
== BOOK_NUM_NUMBERS
) {
253 $title = "$nch.$ns $title";
256 if ($book->numbering
== BOOK_NUM_NUMBERS
) {
257 if (empty($chapters[$ch->parent
]->hidden
)) {
258 $title = "$nch.x $title";
260 $title = "x.x $title";
263 $title = html_writer
::tag('span', $title, array('class' => 'dimmed_text'));
267 if ($ch->id
== $chapter->id
) {
268 $toc .= html_writer
::tag('strong', $title);
270 $toc .= html_writer
::link(new moodle_url('view.php', array('id' => $cm->id
, 'chapterid' => $ch->id
)), $title, array('title' => s($title)));
273 $toc .= html_writer
::start_tag('div', array('class' => 'action-list'));
275 $toc .= html_writer
::link(new moodle_url('move.php', array('id' => $cm->id
, 'chapterid' => $ch->id
, 'up' => '1', 'sesskey' => $USER->sesskey
)),
276 $OUTPUT->pix_icon('t/up', get_string('up')), array('title' => get_string('up')));
278 if ($i != count($chapters)) {
279 $toc .= html_writer
::link(new moodle_url('move.php', array('id' => $cm->id
, 'chapterid' => $ch->id
, 'up' => '0', 'sesskey' => $USER->sesskey
)),
280 $OUTPUT->pix_icon('t/down', get_string('down')), array('title' => get_string('down')));
282 $toc .= html_writer
::link(new moodle_url('edit.php', array('cmid' => $cm->id
, 'id' => $ch->id
)),
283 $OUTPUT->pix_icon('t/edit', get_string('edit')), array('title' => get_string('edit')));
284 $toc .= html_writer
::link(new moodle_url('delete.php', array('id' => $cm->id
, 'chapterid' => $ch->id
, 'sesskey' => $USER->sesskey
)),
285 $OUTPUT->pix_icon('t/delete', get_string('delete')), array('title' => get_string('delete')));
287 $toc .= html_writer
::link(new moodle_url('show.php', array('id' => $cm->id
, 'chapterid' => $ch->id
, 'sesskey' => $USER->sesskey
)),
288 $OUTPUT->pix_icon('t/show', get_string('show')), array('title' => get_string('show')));
290 $toc .= html_writer
::link(new moodle_url('show.php', array('id' => $cm->id
, 'chapterid' => $ch->id
, 'sesskey' => $USER->sesskey
)),
291 $OUTPUT->pix_icon('t/hide', get_string('hide')), array('title' => get_string('hide')));
293 $toc .= html_writer
::link(new moodle_url('edit.php', array('cmid' => $cm->id
, 'pagenum' => $ch->pagenum
, 'subchapter' => $ch->subchapter
)),
294 $OUTPUT->pix_icon('add', get_string('addafter', 'mod_book'), 'mod_book'), array('title' => get_string('addafter', 'mod_book')));
295 $toc .= html_writer
::end_tag('div');
297 if (!$ch->subchapter
) {
298 $toc .= html_writer
::start_tag('ul');
300 $toc .= html_writer
::end_tag('li');
305 $toc .= html_writer
::end_tag('ul');
306 $toc .= html_writer
::end_tag('li');
307 $toc .= html_writer
::end_tag('ul');
309 } else { // Normal students view
310 $toc .= html_writer
::start_tag('ul');
311 foreach ($chapters as $ch) {
312 $title = trim(format_string($ch->title
, true, array('context'=>$context)));
314 if (!$ch->subchapter
) {
319 $toc .= html_writer
::start_tag('li', array('class' => 'clearfix'));
321 $toc .= html_writer
::end_tag('ul');
322 $toc .= html_writer
::end_tag('li');
323 $toc .= html_writer
::start_tag('li', array('class' => 'clearfix'));
326 if ($book->numbering
== BOOK_NUM_NUMBERS
) {
327 $title = "$nch $title";
333 $toc .= html_writer
::start_tag('li', array('class' => 'clearfix'));
334 $toc .= html_writer
::start_tag('ul');
335 $toc .= html_writer
::start_tag('li', array('class' => 'clearfix'));
337 $toc .= html_writer
::start_tag('li', array('class' => 'clearfix'));
340 if ($book->numbering
== BOOK_NUM_NUMBERS
) {
341 $title = "$nch.$ns $title";
344 if ($ch->id
== $chapter->id
) {
345 $toc .= html_writer
::tag('strong', $title);
347 $toc .= html_writer
::link(new moodle_url('view.php', array('id' => $cm->id
, 'chapterid' => $ch->id
)), $title, array('title' => s($title)));
350 if (!$ch->subchapter
) {
351 $toc .= html_writer
::start_tag('ul');
353 $toc .= html_writer
::end_tag('li');
360 $toc .= html_writer
::end_tag('ul');
361 $toc .= html_writer
::end_tag('li');
362 $toc .= html_writer
::end_tag('ul');
366 $toc .= html_writer
::end_tag('div');
368 $toc = str_replace('<ul></ul>', '', $toc); // Cleanup of invalid structures.
375 * File browsing support class
377 * @copyright 2010-2011 Petr Skoda {@link http://skodak.org}
378 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
380 class book_file_info
extends file_info
{
381 /** @var stdClass Course object */
383 /** @var stdClass Course module object */
385 /** @var array Available file areas */
387 /** @var string File area to browse */
393 * @param file_browser $browser file_browser instance
394 * @param stdClass $course course object
395 * @param stdClass $cm course module object
396 * @param stdClass $context module context
397 * @param array $areas available file areas
398 * @param string $filearea file area to browse
400 public function __construct($browser, $course, $cm, $context, $areas, $filearea) {
401 parent
::__construct($browser, $context);
402 $this->course
= $course;
404 $this->areas
= $areas;
405 $this->filearea
= $filearea;
409 * Returns list of standard virtual file/directory identification.
410 * The difference from stored_file parameters is that null values
411 * are allowed in all fields
412 * @return array with keys contextid, filearea, itemid, filepath and filename
414 public function get_params() {
415 return array('contextid'=>$this->context
->id
,
416 'component'=>'mod_book',
417 'filearea' =>$this->filearea
,
424 * Returns localised visible name.
427 public function get_visible_name() {
428 return $this->areas
[$this->filearea
];
432 * Can I add new files or directories?
435 public function is_writable() {
443 public function is_directory() {
448 * Returns list of children.
449 * @return array of file_info instances
451 public function get_children() {
452 return $this->get_filtered_children('*', false, true);
456 * Help function to return files matching extensions or their count
458 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
459 * @param bool|int $countonly if false returns the children, if an int returns just the
460 * count of children but stops counting when $countonly number of children is reached
461 * @param bool $returnemptyfolders if true returns items that don't have matching files inside
462 * @return array|int array of file_info instances or the count
464 private function get_filtered_children($extensions = '*', $countonly = false, $returnemptyfolders = false) {
466 $params = array('contextid' => $this->context
->id
,
467 'component' => 'mod_book',
468 'filearea' => $this->filearea
,
469 'bookid' => $this->cm
->instance
);
470 $sql = 'SELECT DISTINCT bc.id, bc.pagenum
471 FROM {files} f, {book_chapters} bc
472 WHERE f.contextid = :contextid
473 AND f.component = :component
474 AND f.filearea = :filearea
475 AND bc.bookid = :bookid
476 AND bc.id = f.itemid';
477 if (!$returnemptyfolders) {
478 $sql .= ' AND filename <> :emptyfilename';
479 $params['emptyfilename'] = '.';
481 list($sql2, $params2) = $this->build_search_files_sql($extensions, 'f');
483 $params = array_merge($params, $params2);
484 if ($countonly === false) {
485 $sql .= ' ORDER BY bc.pagenum';
488 $rs = $DB->get_recordset_sql($sql, $params);
490 foreach ($rs as $record) {
491 if ($child = $this->browser
->get_file_info($this->context
, 'mod_book', $this->filearea
, $record->id
)) {
492 if ($returnemptyfolders ||
$child->count_non_empty_children($extensions)) {
493 $children[] = $child;
496 if ($countonly !== false && count($children) >= $countonly) {
501 if ($countonly !== false) {
502 return count($children);
508 * Returns list of children which are either files matching the specified extensions
509 * or folders that contain at least one such file.
511 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
512 * @return array of file_info instances
514 public function get_non_empty_children($extensions = '*') {
515 return $this->get_filtered_children($extensions, false);
519 * Returns the number of children which are either files matching the specified extensions
520 * or folders containing at least one such file.
522 * @param string|array $extensions, for example '*' or array('.gif','.jpg')
523 * @param int $limit stop counting after at least $limit non-empty children are found
526 public function count_non_empty_children($extensions = '*', $limit = 1) {
527 return $this->get_filtered_children($extensions, $limit);
531 * Returns parent file_info instance
532 * @return file_info or null for root
534 public function get_parent() {
535 return $this->browser
->get_file_info($this->context
);