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 * Show/hide book chapter
19 *
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
23 */
24
25require(__DIR__.'/../../config.php');
26require_once(__DIR__.'/locallib.php');
27
28$id        = required_param('id', PARAM_INT);        // Course Module ID
29$chapterid = required_param('chapterid', PARAM_INT); // Chapter ID
30
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);
34
35require_login($course, false, $cm);
36require_sesskey();
37
38$context = context_module::instance($cm->id);
39require_capability('mod/book:edit', $context);
40
41$PAGE->set_url('/mod/book/show.php', array('id'=>$id, 'chapterid'=>$chapterid));
42
43$chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id), '*', MUST_EXIST);
44
45// Switch hidden state.
46$chapter->hidden = $chapter->hidden ? 0 : 1;
47
48// Update record.
49$DB->update_record('book_chapters', $chapter);
50\mod_book\event\chapter_updated::create_from_chapter($book, $context, $chapter)->trigger();
51
52// Change visibility of subchapters too.
53if (!$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;
59
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();
64
65        } else if ($found) {
66            break;
67        }
68    }
69    $chapters->close();
70}
71
72book_preload_chapters($book); // fix structure
73$DB->set_field('book', 'revision', $book->revision+1, array('id'=>$book->id));
74
75redirect('view.php?id='.$cm->id.'&chapterid='.$chapter->id);
76
77