Subtree merged git://github.com/skodak/moodle-mod_book.git (master branch) into mod...
[moodle.git] / mod / book / delete.php
CommitLineData
8d623d14 1<?php
2c1e98e6
PS
2// This file is part of Book module for 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/>.
02039379 16
2c1e98e6
PS
17/**
18 * Delete book chapter
19 *
20 * @package mod
21 * @subpackage book
ad1c11bd 22 * @copyright 2004-2011 Petr Skoda {@link http://skodak.org}
2c1e98e6
PS
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
e355240d 25
74979715
PS
26require(dirname(__FILE__).'/../../config.php');
27require_once(dirname(__FILE__).'/locallib.php');
e355240d 28
2c1e98e6
PS
29$id = required_param('id', PARAM_INT); // Course Module ID
30$chapterid = required_param('chapterid', PARAM_INT); // Chapter ID
31$confirm = optional_param('confirm', 0, PARAM_BOOL);
32
33$cm = get_coursemodule_from_id('book', $id, 0, false, MUST_EXIST);
34$course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
35$book = $DB->get_record('book', array('id'=>$cm->instance), '*', MUST_EXIST);
36
37require_login($course, false, $cm);
38require_sesskey();
39
40$context = get_context_instance(CONTEXT_MODULE, $cm->id);
41require_capability('mod/book:edit', $context);
42
9d2fe179
PS
43$PAGE->set_url('/mod/book/delete.php', array('id'=>$id, 'chapterid'=>$chapterid));
44
2c1e98e6 45$chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id), '*', MUST_EXIST);
02039379
PS
46
47
48///header and strings
9d2fe179
PS
49$PAGE->set_title(format_string($book->name));
50$PAGE->add_body_class('mod_book');
51$PAGE->set_heading(format_string($course->fullname));
02039379
PS
52
53///form processing
54if ($confirm) { // the operation was confirmed.
2ecc0ff5 55 $fs = get_file_storage();
02039379 56 if (!$chapter->subchapter) { //delete all its subchapters if any
2c1e98e6 57 $chapters = $DB->get_records('book_chapters', array('bookid'=>$book->id), 'pagenum', 'id, subchapter');
02039379
PS
58 $found = false;
59 foreach($chapters as $ch) {
2ced61eb 60 if ($ch->id == $chapter->id) {
02039379
PS
61 $found = true;
62 } else if ($found and $ch->subchapter) {
2ecc0ff5 63 $fs->delete_area_files($context->id, 'mod_book', 'chapter', $ch->id);
2c1e98e6 64 $DB->delete_records('book_chapters', array('id'=>$ch->id));
02039379
PS
65 } else if ($found) {
66 break;
67 }
68 }
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
PS
72
73 add_to_log($course->id, 'course', 'update mod', '../mod/book/view.php?id='.$cm->id, 'book '.$book->id);
74 add_to_log($course->id, 'book', 'update', 'view.php?id='.$cm->id, $book->id, $cm->id);
2c1e98e6 75
b1a9b01e 76 book_preload_chapters($book); //fix structure
0c9f1864 77 $DB->set_field('book', 'revision', $book->revision+1, array('id'=>$book->id));
2c1e98e6 78
0c9f1864 79 redirect('view.php?id='.$cm->id);
9d2fe179
PS
80}
81
82echo $OUTPUT->header();
83
84// the operation has not been confirmed yet so ask the user to do so
85if ($chapter->subchapter) {
1cc316b7 86 $strconfirm = get_string('confchapterdelete','mod_book');
02039379 87} else {
1cc316b7 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));
93echo $OUTPUT->confirm("<strong>$chapter->title</strong><p>$strconfirm</p>", $continue, $cancel);
02039379 94
9d2fe179 95echo $OUTPUT->footer();