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/>.
21 * @copyright 2004-2011 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require(dirname(__FILE__).'/../../config.php');
26 require_once(dirname(__FILE__).'/locallib.php');
28 $id = required_param('id', PARAM_INT); // Course Module ID
29 $chapterid = required_param('chapterid', PARAM_INT); // Chapter ID
30 $confirm = optional_param('confirm', 0, PARAM_BOOL);
32 $cm = get_coursemodule_from_id('book', $id, 0, false, MUST_EXIST);
33 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
34 $book = $DB->get_record('book', array('id'=>$cm->instance), '*', MUST_EXIST);
36 require_login($course, false, $cm);
39 $context = context_module::instance($cm->id);
40 require_capability('mod/book:edit', $context);
42 $PAGE->set_url('/mod/book/delete.php', array('id'=>$id, 'chapterid'=>$chapterid));
44 $chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id), '*', MUST_EXIST);
47 // Header and strings.
48 $PAGE->set_title($book->name);
49 $PAGE->set_heading($course->fullname);
52 if ($confirm) { // the operation was confirmed.
53 $fs = get_file_storage();
54 if (!$chapter->subchapter) { // Delete all its sub-chapters if any
55 $chapters = $DB->get_recordset('book_chapters', array('bookid'=>$book->id), 'pagenum');
57 foreach ($chapters as $ch) {
58 if ($ch->id == $chapter->id) {
60 } else if ($found and $ch->subchapter) {
61 $fs->delete_area_files($context->id, 'mod_book', 'chapter', $ch->id);
62 $DB->delete_records('book_chapters', array('id'=>$ch->id));
63 \mod_book\event\chapter_deleted::create_from_chapter($book, $context, $ch)->trigger();
70 $fs->delete_area_files($context->id, 'mod_book', 'chapter', $chapter->id);
71 $DB->delete_records('book_chapters', array('id'=>$chapter->id));
73 \mod_book\event\chapter_deleted::create_from_chapter($book, $context, $chapter)->trigger();
75 book_preload_chapters($book); // Fix structure.
76 $DB->set_field('book', 'revision', $book->revision+1, array('id'=>$book->id));
78 redirect('view.php?id='.$cm->id);
81 echo $OUTPUT->header();
82 echo $OUTPUT->heading($book->name);
84 // The operation has not been confirmed yet so ask the user to do so.
85 if ($chapter->subchapter) {
86 $strconfirm = get_string('confchapterdelete', 'mod_book');
88 $strconfirm = get_string('confchapterdeleteall', 'mod_book');
91 $continue = new moodle_url('/mod/book/delete.php', array('id'=>$cm->id, 'chapterid'=>$chapter->id, 'confirm'=>1));
92 $cancel = new moodle_url('/mod/book/view.php', array('id'=>$cm->id, 'chapterid'=>$chapter->id));
93 $title = format_string($chapter->title);
94 echo $OUTPUT->confirm("<strong>$title</strong><p>$strconfirm</p>", $continue, $cancel);
96 echo $OUTPUT->footer();