more help cleanup
[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
22 * @copyright 2004-2010 Petr Skoda {@link http://skodak.org}
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
43$chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id), '*', MUST_EXIST);
02039379
PS
44
45
46///header and strings
47$strbooks = get_string('modulenameplural', 'book');
48$strbook = get_string('modulename', 'book');
49
50c53e20 50$navigation = build_navigation('', $cm);
02039379 51
50c53e20 52print_header("$course->shortname: $book->name", $course->fullname, $navigation);
02039379
PS
53
54///form processing
55if ($confirm) { // the operation was confirmed.
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) {
2c1e98e6 63 $DB->delete_records('book_chapters', array('id'=>$ch->id));
02039379
PS
64 } else if ($found) {
65 break;
66 }
67 }
68 }
2c1e98e6 69 $DB->delete_records('book_chapters', array('id'=>$chapter->id));
02039379
PS
70
71 add_to_log($course->id, 'course', 'update mod', '../mod/book/view.php?id='.$cm->id, 'book '.$book->id);
72 add_to_log($course->id, 'book', 'update', 'view.php?id='.$cm->id, $book->id, $cm->id);
2c1e98e6 73
02039379
PS
74 book_check_structure($book->id);
75 redirect('view.php?id='.$cm->id);
2c1e98e6 76
02039379
PS
77} else {
78 // the operation has not been confirmed yet so ask the user to do so
79 if ($chapter->subchapter) {
80 $strconfirm = get_string('confchapterdelete','book');
81 } else {
82 $strconfirm = get_string('confchapterdeleteall','book');
83 }
84 echo '<br />';
85 notice_yesno("<b>$chapter->title</b><p>$strconfirm</p>",
86 "delete.php?id=$cm->id&chapterid=$chapter->id&confirm=1&sesskey=$USER->sesskey",
87 "view.php?id=$cm->id&chapterid=$chapter->id");
88}
89
90print_footer($course);