MDL-50015 mod_imscp: New WS mod_imscp_view_imscp
[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
29656fe2 27require_once(dirname(__FILE__).'/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) {
157 $title = implode('.', $numbers).' '.$title;
158 }
159
160 return $title;
161}
162
3542c4f4 163/**
f6fa4b7b 164 * Add the book TOC sticky block to the default region
3542c4f4
EL
165 *
166 * @param array $chapters
167 * @param stdClass $chapter
168 * @param stdClass $book
169 * @param stdClass $cm
170 * @param bool $edit
171 */
ad1c11bd
PS
172function book_add_fake_block($chapters, $chapter, $book, $cm, $edit) {
173 global $OUTPUT, $PAGE;
174
8cb50b2a 175 $toc = book_get_toc($chapters, $chapter, $book, $cm, $edit, 0);
ad1c11bd 176
ad1c11bd
PS
177 $bc = new block_contents();
178 $bc->title = get_string('toc', 'mod_book');
bdb66a46 179 $bc->attributes['class'] = 'block block_book_toc';
ad1c11bd
PS
180 $bc->content = $toc;
181
f6fa4b7b
ME
182 $defaultregion = $PAGE->blocks->get_default_region();
183 $PAGE->blocks->add_fake_block($bc, $defaultregion);
ad1c11bd
PS
184}
185
ee952cbb
PS
186/**
187 * Generate toc structure
188 *
189 * @param array $chapters
190 * @param stdClass $chapter
191 * @param stdClass $book
192 * @param stdClass $cm
193 * @param bool $edit
ee952cbb
PS
194 * @return string
195 */
196function book_get_toc($chapters, $chapter, $book, $cm, $edit) {
197 global $USER, $OUTPUT;
198
fb909757 199 $toc = '';
3542c4f4
EL
200 $nch = 0; // Chapter number
201 $ns = 0; // Subchapter number
ee952cbb
PS
202 $first = 1;
203
1fc9e895 204 $context = context_module::instance($cm->id);
ee952cbb
PS
205
206 switch ($book->numbering) {
29656fe2 207 case BOOK_NUM_NONE:
bdb66a46 208 $toc .= html_writer::start_tag('div', array('class' => 'book_toc_none clearfix'));
29656fe2
EL
209 break;
210 case BOOK_NUM_NUMBERS:
bdb66a46 211 $toc .= html_writer::start_tag('div', array('class' => 'book_toc_numbered clearfix'));
29656fe2
EL
212 break;
213 case BOOK_NUM_BULLETS:
bdb66a46 214 $toc .= html_writer::start_tag('div', array('class' => 'book_toc_bullets clearfix'));
29656fe2
EL
215 break;
216 case BOOK_NUM_INDENTED:
bdb66a46 217 $toc .= html_writer::start_tag('div', array('class' => 'book_toc_indented clearfix'));
29656fe2 218 break;
ee952cbb
PS
219 }
220
3542c4f4 221 if ($edit) { // Teacher's TOC
5b7e511e 222 $toc .= html_writer::start_tag('ul');
f6ca19ae 223 $i = 0;
29656fe2 224 foreach ($chapters as $ch) {
f6ca19ae 225 $i++;
dc888a31 226 $title = trim(format_string($ch->title, true, array('context'=>$context)));
f6ca19ae 227 if (!$ch->subchapter) {
5b7e511e
TT
228
229 if ($first) {
bdb66a46 230 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
5b7e511e
TT
231 } else {
232 $toc .= html_writer::end_tag('ul');
233 $toc .= html_writer::end_tag('li');
bdb66a46 234 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
5b7e511e
TT
235 }
236
f6ca19ae
PS
237 if (!$ch->hidden) {
238 $nch++;
239 $ns = 0;
240 if ($book->numbering == BOOK_NUM_NUMBERS) {
241 $title = "$nch $title";
242 }
243 } else {
244 if ($book->numbering == BOOK_NUM_NUMBERS) {
245 $title = "x $title";
246 }
5b7e511e 247 $title = html_writer::tag('span', $title, array('class' => 'dimmed_text'));
f6ca19ae 248 }
f6ca19ae 249 } else {
5b7e511e
TT
250
251 if ($first) {
bdb66a46 252 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
5b7e511e 253 $toc .= html_writer::start_tag('ul');
bdb66a46 254 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
5b7e511e 255 } else {
bdb66a46 256 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
5b7e511e
TT
257 }
258
f6ca19ae
PS
259 if (!$ch->hidden) {
260 $ns++;
261 if ($book->numbering == BOOK_NUM_NUMBERS) {
262 $title = "$nch.$ns $title";
263 }
264 } else {
265 if ($book->numbering == BOOK_NUM_NUMBERS) {
b0b30cb6
AA
266 if (empty($chapters[$ch->parent]->hidden)) {
267 $title = "$nch.x $title";
268 } else {
269 $title = "x.x $title";
270 }
f6ca19ae 271 }
5b7e511e 272 $title = html_writer::tag('span', $title, array('class' => 'dimmed_text'));
f6ca19ae
PS
273 }
274 }
275
276 if ($ch->id == $chapter->id) {
5b7e511e 277 $toc .= html_writer::tag('strong', $title);
f6ca19ae 278 } else {
5b7e511e 279 $toc .= html_writer::link(new moodle_url('view.php', array('id' => $cm->id, 'chapterid' => $ch->id)), $title, array('title' => s($title)));
f6ca19ae 280 }
bdb66a46
FM
281
282 $toc .= html_writer::start_tag('div', array('class' => 'action-list'));
f6ca19ae 283 if ($i != 1) {
5b7e511e 284 $toc .= html_writer::link(new moodle_url('move.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'up' => '1', 'sesskey' => $USER->sesskey)),
22255d8c
AA
285 $OUTPUT->pix_icon('t/up', get_string('movechapterup', 'mod_book', $title)),
286 array('title' => get_string('movechapterup', 'mod_book', $title)));
f6ca19ae
PS
287 }
288 if ($i != count($chapters)) {
5b7e511e 289 $toc .= html_writer::link(new moodle_url('move.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'up' => '0', 'sesskey' => $USER->sesskey)),
22255d8c
AA
290 $OUTPUT->pix_icon('t/down', get_string('movechapterdown', 'mod_book', $title)),
291 array('title' => get_string('movechapterdown', 'mod_book', $title)));
f6ca19ae 292 }
5b7e511e 293 $toc .= html_writer::link(new moodle_url('edit.php', array('cmid' => $cm->id, 'id' => $ch->id)),
22255d8c
AA
294 $OUTPUT->pix_icon('t/edit', get_string('editchapter', 'mod_book', $title)),
295 array('title' => get_string('editchapter', 'mod_book', $title)));
5b7e511e 296 $toc .= html_writer::link(new moodle_url('delete.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'sesskey' => $USER->sesskey)),
22255d8c
AA
297 $OUTPUT->pix_icon('t/delete', get_string('deletechapter', 'mod_book', $title)),
298 array('title' => get_string('deletechapter', 'mod_book', $title)));
f6ca19ae 299 if ($ch->hidden) {
5b7e511e 300 $toc .= html_writer::link(new moodle_url('show.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'sesskey' => $USER->sesskey)),
22255d8c
AA
301 $OUTPUT->pix_icon('t/show', get_string('showchapter', 'mod_book', $title)),
302 array('title' => get_string('showchapter', 'mod_book', $title)));
f6ca19ae 303 } else {
5b7e511e 304 $toc .= html_writer::link(new moodle_url('show.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'sesskey' => $USER->sesskey)),
22255d8c
AA
305 $OUTPUT->pix_icon('t/hide', get_string('hidechapter', 'mod_book', $title)),
306 array('title' => get_string('hidechapter', 'mod_book', $title)));
f6ca19ae 307 }
5b7e511e
TT
308 $toc .= html_writer::link(new moodle_url('edit.php', array('cmid' => $cm->id, 'pagenum' => $ch->pagenum, 'subchapter' => $ch->subchapter)),
309 $OUTPUT->pix_icon('add', get_string('addafter', 'mod_book'), 'mod_book'), array('title' => get_string('addafter', 'mod_book')));
bdb66a46 310 $toc .= html_writer::end_tag('div');
5b7e511e
TT
311
312 if (!$ch->subchapter) {
313 $toc .= html_writer::start_tag('ul');
314 } else {
315 $toc .= html_writer::end_tag('li');
316 }
f6ca19ae
PS
317 $first = 0;
318 }
5b7e511e
TT
319
320 $toc .= html_writer::end_tag('ul');
321 $toc .= html_writer::end_tag('li');
322 $toc .= html_writer::end_tag('ul');
323
3542c4f4 324 } else { // Normal students view
5b7e511e 325 $toc .= html_writer::start_tag('ul');
29656fe2 326 foreach ($chapters as $ch) {
dc888a31 327 $title = trim(format_string($ch->title, true, array('context'=>$context)));
f6ca19ae
PS
328 if (!$ch->hidden) {
329 if (!$ch->subchapter) {
330 $nch++;
331 $ns = 0;
5b7e511e
TT
332
333 if ($first) {
bdb66a46 334 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
5b7e511e
TT
335 } else {
336 $toc .= html_writer::end_tag('ul');
337 $toc .= html_writer::end_tag('li');
bdb66a46 338 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
5b7e511e
TT
339 }
340
f6ca19ae
PS
341 if ($book->numbering == BOOK_NUM_NUMBERS) {
342 $title = "$nch $title";
343 }
f6ca19ae
PS
344 } else {
345 $ns++;
5b7e511e
TT
346
347 if ($first) {
bdb66a46 348 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
5b7e511e 349 $toc .= html_writer::start_tag('ul');
bdb66a46 350 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
5b7e511e 351 } else {
bdb66a46 352 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
5b7e511e
TT
353 }
354
f6ca19ae
PS
355 if ($book->numbering == BOOK_NUM_NUMBERS) {
356 $title = "$nch.$ns $title";
357 }
358 }
359 if ($ch->id == $chapter->id) {
5b7e511e 360 $toc .= html_writer::tag('strong', $title);
f6ca19ae 361 } else {
5b7e511e 362 $toc .= html_writer::link(new moodle_url('view.php', array('id' => $cm->id, 'chapterid' => $ch->id)), $title, array('title' => s($title)));
f6ca19ae 363 }
5b7e511e
TT
364
365 if (!$ch->subchapter) {
366 $toc .= html_writer::start_tag('ul');
367 } else {
368 $toc .= html_writer::end_tag('li');
369 }
370
f6ca19ae
PS
371 $first = 0;
372 }
373 }
5b7e511e
TT
374
375 $toc .= html_writer::end_tag('ul');
376 $toc .= html_writer::end_tag('li');
377 $toc .= html_writer::end_tag('ul');
378
f6ca19ae
PS
379 }
380
5b7e511e 381 $toc .= html_writer::end_tag('div');
f6ca19ae 382
3542c4f4 383 $toc = str_replace('<ul></ul>', '', $toc); // Cleanup of invalid structures.
f6ca19ae 384
ee952cbb 385 return $toc;
f6ca19ae 386}
dd9b772b
PS
387
388
389/**
390 * File browsing support class
3542c4f4
EL
391 *
392 * @copyright 2010-2011 Petr Skoda {@link http://skodak.org}
393 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
dd9b772b
PS
394 */
395class book_file_info extends file_info {
3542c4f4 396 /** @var stdClass Course object */
dd9b772b 397 protected $course;
3542c4f4 398 /** @var stdClass Course module object */
dd9b772b 399 protected $cm;
3542c4f4 400 /** @var array Available file areas */
dd9b772b 401 protected $areas;
3542c4f4 402 /** @var string File area to browse */
dd9b772b
PS
403 protected $filearea;
404
3542c4f4
EL
405 /**
406 * Constructor
407 *
408 * @param file_browser $browser file_browser instance
409 * @param stdClass $course course object
410 * @param stdClass $cm course module object
411 * @param stdClass $context module context
412 * @param array $areas available file areas
413 * @param string $filearea file area to browse
414 */
dd9b772b
PS
415 public function __construct($browser, $course, $cm, $context, $areas, $filearea) {
416 parent::__construct($browser, $context);
417 $this->course = $course;
418 $this->cm = $cm;
419 $this->areas = $areas;
420 $this->filearea = $filearea;
421 }
422
423 /**
424 * Returns list of standard virtual file/directory identification.
425 * The difference from stored_file parameters is that null values
426 * are allowed in all fields
427 * @return array with keys contextid, filearea, itemid, filepath and filename
428 */
429 public function get_params() {
430 return array('contextid'=>$this->context->id,
431 'component'=>'mod_book',
432 'filearea' =>$this->filearea,
433 'itemid' =>null,
434 'filepath' =>null,
435 'filename' =>null);
436 }
437
438 /**
439 * Returns localised visible name.
440 * @return string
441 */
442 public function get_visible_name() {
443 return $this->areas[$this->filearea];
444 }
445
446 /**
447 * Can I add new files or directories?
448 * @return bool
449 */
450 public function is_writable() {
451 return false;
452 }
453
454 /**
455 * Is directory?
456 * @return bool
457 */
458 public function is_directory() {
459 return true;
460 }
461
462 /**
463 * Returns list of children.
464 * @return array of file_info instances
465 */
466 public function get_children() {
b8de2621
MG
467 return $this->get_filtered_children('*', false, true);
468 }
469
470 /**
471 * Help function to return files matching extensions or their count
472 *
473 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
43aadf04
MG
474 * @param bool|int $countonly if false returns the children, if an int returns just the
475 * count of children but stops counting when $countonly number of children is reached
b8de2621
MG
476 * @param bool $returnemptyfolders if true returns items that don't have matching files inside
477 * @return array|int array of file_info instances or the count
478 */
479 private function get_filtered_children($extensions = '*', $countonly = false, $returnemptyfolders = false) {
dd9b772b 480 global $DB;
b8de2621
MG
481 $params = array('contextid' => $this->context->id,
482 'component' => 'mod_book',
483 'filearea' => $this->filearea,
484 'bookid' => $this->cm->instance);
485 $sql = 'SELECT DISTINCT bc.id, bc.pagenum
486 FROM {files} f, {book_chapters} bc
487 WHERE f.contextid = :contextid
488 AND f.component = :component
489 AND f.filearea = :filearea
490 AND bc.bookid = :bookid
491 AND bc.id = f.itemid';
492 if (!$returnemptyfolders) {
493 $sql .= ' AND filename <> :emptyfilename';
494 $params['emptyfilename'] = '.';
495 }
496 list($sql2, $params2) = $this->build_search_files_sql($extensions, 'f');
497 $sql .= ' '.$sql2;
498 $params = array_merge($params, $params2);
43aadf04 499 if ($countonly === false) {
b8de2621
MG
500 $sql .= ' ORDER BY bc.pagenum';
501 }
dd9b772b 502
b8de2621 503 $rs = $DB->get_recordset_sql($sql, $params);
dd9b772b 504 $children = array();
b8de2621
MG
505 foreach ($rs as $record) {
506 if ($child = $this->browser->get_file_info($this->context, 'mod_book', $this->filearea, $record->id)) {
507 if ($returnemptyfolders || $child->count_non_empty_children($extensions)) {
508 $children[] = $child;
509 }
510 }
43aadf04 511 if ($countonly !== false && count($children) >= $countonly) {
b8de2621 512 break;
dd9b772b
PS
513 }
514 }
b8de2621 515 $rs->close();
43aadf04 516 if ($countonly !== false) {
b8de2621
MG
517 return count($children);
518 }
dd9b772b
PS
519 return $children;
520 }
521
b8de2621
MG
522 /**
523 * Returns list of children which are either files matching the specified extensions
524 * or folders that contain at least one such file.
525 *
526 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
527 * @return array of file_info instances
528 */
529 public function get_non_empty_children($extensions = '*') {
530 return $this->get_filtered_children($extensions, false);
531 }
532
533 /**
534 * Returns the number of children which are either files matching the specified extensions
535 * or folders containing at least one such file.
536 *
b8de2621 537 * @param string|array $extensions, for example '*' or array('.gif','.jpg')
43aadf04 538 * @param int $limit stop counting after at least $limit non-empty children are found
b8de2621
MG
539 * @return int
540 */
43aadf04
MG
541 public function count_non_empty_children($extensions = '*', $limit = 1) {
542 return $this->get_filtered_children($extensions, $limit);
b8de2621
MG
543 }
544
dd9b772b
PS
545 /**
546 * Returns parent file_info instance
547 * @return file_info or null for root
548 */
549 public function get_parent() {
550 return $this->browser->get_file_info($this->context);
551 }
552}