MDL-50015 mod_imscp: New WS mod_imscp_view_imscp
[moodle.git] / mod / book / delete.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 * Delete book chapter
19 *
3542c4f4
EL
20 * @package mod_book
21 * @copyright 2004-2011 Petr Skoda {@link http://skodak.org}
2c1e98e6
PS
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
e355240d 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$confirm = optional_param('confirm', 0, PARAM_BOOL);
31
32$cm = get_coursemodule_from_id('book', $id, 0, false, MUST_EXIST);
74df2951 33$course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
2c1e98e6
PS
34$book = $DB->get_record('book', array('id'=>$cm->instance), '*', MUST_EXIST);
35
36require_login($course, false, $cm);
37require_sesskey();
38
1fc9e895 39$context = context_module::instance($cm->id);
2c1e98e6
PS
40require_capability('mod/book:edit', $context);
41
9d2fe179
PS
42$PAGE->set_url('/mod/book/delete.php', array('id'=>$id, 'chapterid'=>$chapterid));
43
2c1e98e6 44$chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id), '*', MUST_EXIST);
02039379
PS
45
46
3542c4f4 47// Header and strings.
5b7e511e
TT
48$PAGE->set_title($book->name);
49$PAGE->set_heading($course->fullname);
02039379 50
3542c4f4 51// Form processing.
02039379 52if ($confirm) { // the operation was confirmed.
2ecc0ff5 53 $fs = get_file_storage();
3542c4f4 54 if (!$chapter->subchapter) { // Delete all its sub-chapters if any
7f6e3eef 55 $chapters = $DB->get_recordset('book_chapters', array('bookid'=>$book->id), 'pagenum');
02039379 56 $found = false;
29656fe2 57 foreach ($chapters as $ch) {
2ced61eb 58 if ($ch->id == $chapter->id) {
02039379
PS
59 $found = true;
60 } else if ($found and $ch->subchapter) {
2ecc0ff5 61 $fs->delete_area_files($context->id, 'mod_book', 'chapter', $ch->id);
2c1e98e6 62 $DB->delete_records('book_chapters', array('id'=>$ch->id));
ef2a9ece 63 \mod_book\event\chapter_deleted::create_from_chapter($book, $context, $ch)->trigger();
02039379
PS
64 } else if ($found) {
65 break;
66 }
67 }
7f6e3eef 68 $chapters->close();
02039379 69 }
2ecc0ff5 70 $fs->delete_area_files($context->id, 'mod_book', 'chapter', $chapter->id);
2c1e98e6 71 $DB->delete_records('book_chapters', array('id'=>$chapter->id));
02039379 72
ef2a9ece 73 \mod_book\event\chapter_deleted::create_from_chapter($book, $context, $chapter)->trigger();
2c1e98e6 74
3542c4f4 75 book_preload_chapters($book); // Fix structure.
0c9f1864 76 $DB->set_field('book', 'revision', $book->revision+1, array('id'=>$book->id));
2c1e98e6 77
0c9f1864 78 redirect('view.php?id='.$cm->id);
9d2fe179
PS
79}
80
81echo $OUTPUT->header();
d8a6d49c 82echo $OUTPUT->heading($book->name);
9d2fe179 83
3542c4f4 84// The operation has not been confirmed yet so ask the user to do so.
9d2fe179 85if ($chapter->subchapter) {
3542c4f4 86 $strconfirm = get_string('confchapterdelete', 'mod_book');
02039379 87} else {
3542c4f4 88 $strconfirm = get_string('confchapterdeleteall', 'mod_book');
02039379 89}
9d2fe179
PS
90echo '<br />';
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));
683f7179
SG
93$title = format_string($chapter->title);
94echo $OUTPUT->confirm("<strong>$title</strong><p>$strconfirm</p>", $continue, $cancel);
02039379 95
29656fe2 96echo $OUTPUT->footer();