remove old hack
[moodle.git] / 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
2c1e98e6 26require('../../config.php');
e355240d
PS
27require_once($CFG->dirroot.'/mod/book/locallib.php');
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.
55 if (!$chapter->subchapter) { //delete all its subchapters if any
2c1e98e6 56 $chapters = $DB->get_records('book_chapters', array('bookid'=>$book->id), 'pagenum', 'id, subchapter');
02039379
PS
57 $found = false;
58 foreach($chapters as $ch) {
2ced61eb 59 if ($ch->id == $chapter->id) {
02039379
PS
60 $found = true;
61 } else if ($found and $ch->subchapter) {
2c1e98e6 62 $DB->delete_records('book_chapters', array('id'=>$ch->id));
02039379
PS
63 } else if ($found) {
64 break;
65 }
66 }
67 }
2c1e98e6 68 $DB->delete_records('book_chapters', array('id'=>$chapter->id));
02039379
PS
69
70 add_to_log($course->id, 'course', 'update mod', '../mod/book/view.php?id='.$cm->id, 'book '.$book->id);
71 add_to_log($course->id, 'book', 'update', 'view.php?id='.$cm->id, $book->id, $cm->id);
2c1e98e6 72
b1a9b01e 73 book_preload_chapters($book); //fix structure
02039379 74 redirect('view.php?id='.$cm->id);
2c1e98e6 75
9d2fe179
PS
76}
77
78echo $OUTPUT->header();
79
80// the operation has not been confirmed yet so ask the user to do so
81if ($chapter->subchapter) {
1cc316b7 82 $strconfirm = get_string('confchapterdelete','mod_book');
02039379 83} else {
1cc316b7 84 $strconfirm = get_string('confchapterdeleteall','mod_book');
02039379 85}
9d2fe179
PS
86echo '<br />';
87$continue = new moodle_url('/mod/book/delete.php', array('id'=>$cm->id, 'chapterid'=>$chapter->id, 'confirm'=>1));
88$cancel = new moodle_url('/mod/book/view.php', array('id'=>$cm->id, 'chapterid'=>$chapter->id));
89echo $OUTPUT->confirm("<strong>$chapter->title</strong><p>$strconfirm</p>", $continue, $cancel);
02039379 90
9d2fe179 91echo $OUTPUT->footer();