Merge branch 'MDL-33509-master' of git://github.com/mihailges/moodle
[moodle.git] / mod / book / show.php
blob2055de92bbd0b6897890621e56a1cc6a4bb45b87
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.
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/>.
17 /**
18 * Show/hide book chapter
20 * @package mod_book
21 * @copyright 2004-2010 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require(__DIR__.'/../../config.php');
26 require_once(__DIR__.'/locallib.php');
28 $id = required_param('id', PARAM_INT); // Course Module ID
29 $chapterid = required_param('chapterid', PARAM_INT); // Chapter ID
31 $cm = get_coursemodule_from_id('book', $id, 0, false, MUST_EXIST);
32 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
33 $book = $DB->get_record('book', array('id'=>$cm->instance), '*', MUST_EXIST);
35 require_login($course, false, $cm);
36 require_sesskey();
38 $context = context_module::instance($cm->id);
39 require_capability('mod/book:edit', $context);
41 $PAGE->set_url('/mod/book/show.php', array('id'=>$id, 'chapterid'=>$chapterid));
43 $chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id), '*', MUST_EXIST);
45 // Switch hidden state.
46 $chapter->hidden = $chapter->hidden ? 0 : 1;
48 // Update record.
49 $DB->update_record('book_chapters', $chapter);
50 \mod_book\event\chapter_updated::create_from_chapter($book, $context, $chapter)->trigger();
52 // Change visibility of subchapters too.
53 if (!$chapter->subchapter) {
54 $chapters = $DB->get_recordset('book_chapters', array('bookid'=>$book->id), 'pagenum ASC');
55 $found = 0;
56 foreach ($chapters as $ch) {
57 if ($ch->id == $chapter->id) {
58 $found = 1;
60 } else if ($found and $ch->subchapter) {
61 $ch->hidden = $chapter->hidden;
62 $DB->update_record('book_chapters', $ch);
63 \mod_book\event\chapter_updated::create_from_chapter($book, $context, $ch)->trigger();
65 } else if ($found) {
66 break;
69 $chapters->close();
72 book_preload_chapters($book); // fix structure
73 $DB->set_field('book', 'revision', $book->revision+1, array('id'=>$book->id));
75 redirect('view.php?id='.$cm->id.'&chapterid='.$chapter->id);