MDL-50015 mod_imscp: New WS mod_imscp_view_imscp
[moodle.git] / mod / book / show.php
CommitLineData
8d623d14 1<?php
b15ef0b0 2// This file is part of Moodle - http://moodle.org/
2c1e98e6
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/>.
02039379 16
2c1e98e6
PS
17/**
18 * Show/hide book chapter
19 *
3542c4f4
EL
20 * @package mod_book
21 * @copyright 2004-2010 Petr Skoda {@link http://skodak.org}
2c1e98e6
PS
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
02039379 24
74979715
PS
25require(dirname(__FILE__).'/../../config.php');
26require_once(dirname(__FILE__).'/locallib.php');
e355240d 27
2c1e98e6
PS
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);
74df2951 32$course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
2c1e98e6
PS
33$book = $DB->get_record('book', array('id'=>$cm->instance), '*', MUST_EXIST);
34
35require_login($course, false, $cm);
36require_sesskey();
37
1fc9e895 38$context = context_module::instance($cm->id);
2c1e98e6
PS
39require_capability('mod/book:edit', $context);
40
9d2fe179
PS
41$PAGE->set_url('/mod/book/show.php', array('id'=>$id, 'chapterid'=>$chapterid));
42
2c1e98e6
PS
43$chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id), '*', MUST_EXIST);
44
3542c4f4 45// Switch hidden state.
02039379
PS
46$chapter->hidden = $chapter->hidden ? 0 : 1;
47
3542c4f4 48// Update record.
2c1e98e6 49$DB->update_record('book_chapters', $chapter);
ef2a9ece 50\mod_book\event\chapter_updated::create_from_chapter($book, $context, $chapter)->trigger();
02039379 51
3542c4f4 52// Change visibility of subchapters too.
02039379 53if (!$chapter->subchapter) {
ef2a9ece 54 $chapters = $DB->get_recordset('book_chapters', array('bookid'=>$book->id), 'pagenum ASC');
02039379 55 $found = 0;
29656fe2 56 foreach ($chapters as $ch) {
2ced61eb 57 if ($ch->id == $chapter->id) {
02039379 58 $found = 1;
ef2a9ece 59
02039379
PS
60 } else if ($found and $ch->subchapter) {
61 $ch->hidden = $chapter->hidden;
2c1e98e6 62 $DB->update_record('book_chapters', $ch);
ef2a9ece 63 \mod_book\event\chapter_updated::create_from_chapter($book, $context, $ch)->trigger();
7f6e3eef 64
02039379
PS
65 } else if ($found) {
66 break;
67 }
68 }
ef2a9ece 69 $chapters->close();
02039379
PS
70}
71
b1a9b01e 72book_preload_chapters($book); // fix structure
0c9f1864
PS
73$DB->set_field('book', 'revision', $book->revision+1, array('id'=>$book->id));
74
02039379 75redirect('view.php?id='.$cm->id.'&chapterid='.$chapter->id);
2c1e98e6 76