diff --git a/mod/book/delete.php b/mod/book/delete.php
index fcac77772ef..d6ee5ef584a 100644
--- a/mod/book/delete.php
+++ b/mod/book/delete.php
@@ -25,13 +25,18 @@
require(__DIR__.'/../../config.php');
require_once(__DIR__.'/locallib.php');
-$id = required_param('id', PARAM_INT); // Course Module ID
-$chapterid = required_param('chapterid', PARAM_INT); // Chapter ID
+// Course Module ID.
+$id = required_param('id', PARAM_INT);
+
+// Chapter ID.
+$chapterid = required_param('chapterid', PARAM_INT);
+
$confirm = optional_param('confirm', 0, PARAM_BOOL);
$cm = get_coursemodule_from_id('book', $id, 0, false, MUST_EXIST);
-$course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
-$book = $DB->get_record('book', array('id'=>$cm->instance), '*', MUST_EXIST);
+$course = $DB->get_record('course', ['id' => $cm->course], '*', MUST_EXIST);
+$book = $DB->get_record('book', ['id' => $cm->instance], '*', MUST_EXIST);
+$chapter = $DB->get_record('book_chapters', ['id' => $chapterid, 'bookid' => $book->id], '*', MUST_EXIST);
require_login($course, false, $cm);
require_sesskey();
@@ -39,61 +44,64 @@ require_sesskey();
$context = context_module::instance($cm->id);
require_capability('mod/book:edit', $context);
-$PAGE->set_url('/mod/book/delete.php', array('id'=>$id, 'chapterid'=>$chapterid));
+$PAGE->set_url('/mod/book/delete.php', ['id' => $id, 'chapterid' => $chapterid]);
-$chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id), '*', MUST_EXIST);
-
-
-// Header and strings.
-$PAGE->set_title($book->name);
-$PAGE->set_heading($course->fullname);
-
-// Form processing.
-if ($confirm) { // the operation was confirmed.
+if ($confirm) {
+ // The operation was confirmed.
$fs = get_file_storage();
- if (!$chapter->subchapter) { // Delete all its sub-chapters if any
- $chapters = $DB->get_recordset('book_chapters', array('bookid'=>$book->id), 'pagenum');
- $found = false;
+
+ $subchaptercount = 0;
+ if (!$chapter->subchapter) {
+ // This is a top-level chapter.
+ // Make sure to remove any sub-chapters if there are any.
+ $chapters = $DB->get_recordset_select('book_chapters', 'bookid = :bookid AND pagenum > :pagenum', [
+ 'bookid' => $book->id,
+ 'pagenum' => $chapter->pagenum,
+ ], 'pagenum');
+
foreach ($chapters as $ch) {
- if ($ch->id == $chapter->id) {
- $found = true;
- } else if ($found and $ch->subchapter) {
- $fs->delete_area_files($context->id, 'mod_book', 'chapter', $ch->id);
- $DB->delete_records('book_chapters', array('id'=>$ch->id));
- \mod_book\event\chapter_deleted::create_from_chapter($book, $context, $ch)->trigger();
- } else if ($found) {
+ if (!$ch->subchapter) {
+ // This is a new chapter. Any subsequent subchapters will be part of a different chapter.
break;
+ } else {
+ // This is subchapter of the chapter being removed.
+ $fs->delete_area_files($context->id, 'mod_book', 'chapter', $ch->id);
+ $DB->delete_records('book_chapters', ['id' => $ch->id]);
+ \mod_book\event\chapter_deleted::create_from_chapter($book, $context, $ch)->trigger();
+
+ $subchaptercount++;
}
}
$chapters->close();
}
+
+ // Now delete the actual chapter.
$fs->delete_area_files($context->id, 'mod_book', 'chapter', $chapter->id);
- $DB->delete_records('book_chapters', array('id'=>$chapter->id));
+ $DB->delete_records('book_chapters', ['id' => $chapter->id]);
\mod_book\event\chapter_deleted::create_from_chapter($book, $context, $chapter)->trigger();
- book_preload_chapters($book); // Fix structure.
- $DB->set_field('book', 'revision', $book->revision+1, array('id'=>$book->id));
+ // Ensure that the book structure is correct.
+ // book_preload_chapters will fix parts including the pagenum.
+ $chapters = book_preload_chapters($book);
- redirect('view.php?id='.$cm->id);
+ book_add_fake_block($chapters, $chapter, $book, $cm);
+
+ // Bump the book revision.
+ $DB->set_field('book', 'revision', $book->revision + 1, ['id' => $book->id]);
+
+ if ($subchaptercount) {
+ $message = get_string('chapterandsubchaptersdeleted', 'mod_book', (object) [
+ 'title' => format_string($chapter->title),
+ 'subchapters' => $subchaptercount,
+ ]);
+ } else {
+ $message = get_string('chapterdeleted', 'mod_book', (object) [
+ 'title' => format_string($chapter->title),
+ ]);
+ }
+
+ redirect(new moodle_url('/mod/book/view.php', ['id' => $cm->id]), $message);
}
-$chapters = book_preload_chapters($book);
-book_add_fake_block($chapters, $chapter, $book, $cm);
-
-echo $OUTPUT->header();
-echo $OUTPUT->heading($book->name);
-
-// The operation has not been confirmed yet so ask the user to do so.
-if ($chapter->subchapter) {
- $strconfirm = get_string('confchapterdelete', 'mod_book');
-} else {
- $strconfirm = get_string('confchapterdeleteall', 'mod_book');
-}
-echo '
';
-$continue = new moodle_url('/mod/book/delete.php', array('id'=>$cm->id, 'chapterid'=>$chapter->id, 'confirm'=>1));
-$cancel = new moodle_url('/mod/book/view.php', array('id'=>$cm->id, 'chapterid'=>$chapter->id));
-$title = format_string($chapter->title);
-echo $OUTPUT->confirm("$title
$strconfirm
", $continue, $cancel); - -echo $OUTPUT->footer(); +redirect(new moodle_url('/mod/book/view.php', ['id' => $cm->id])); diff --git a/mod/book/lang/en/book.php b/mod/book/lang/en/book.php index f1724de5fbe..e67db57cf0b 100644 --- a/mod/book/lang/en/book.php +++ b/mod/book/lang/en/book.php @@ -38,6 +38,8 @@ $string['pluginname'] = 'Book'; $string['pluginadministration'] = 'Book administration'; $string['toc'] = 'Table of contents'; +$string['chapterandsubchaptersdeleted'] = 'Chapter "{$a->title}" and its {$a->subchapters} subchapters were deleted'; +$string['chapterdeleted'] = 'Chapter "{$a->title}" was deleted'; $string['customtitles'] = 'Custom titles'; $string['customtitles_help'] = 'Normally the chapter title is displayed in the table of contents (TOC) AND as a heading above the content. diff --git a/mod/book/locallib.php b/mod/book/locallib.php index feab075a3d1..cbae03465c5 100644 --- a/mod/book/locallib.php +++ b/mod/book/locallib.php @@ -311,9 +311,20 @@ function book_get_toc($chapters, $chapter, $book, $cm, $edit) { $toc .= html_writer::link(new moodle_url('edit.php', array('cmid' => $cm->id, 'id' => $ch->id)), $OUTPUT->pix_icon('t/edit', get_string('editchapter', 'mod_book', $title)), array('title' => get_string('editchapter', 'mod_book', $titleunescaped))); - $toc .= html_writer::link(new moodle_url('delete.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'sesskey' => $USER->sesskey)), - $OUTPUT->pix_icon('t/delete', get_string('deletechapter', 'mod_book', $title)), - array('title' => get_string('deletechapter', 'mod_book', $titleunescaped))); + + $deleteaction = new confirm_action(get_string('deletechapter', 'mod_book', $titleunescaped)); + $toc .= $OUTPUT->action_icon( + new moodle_url('delete.php', [ + 'id' => $cm->id, + 'chapterid' => $ch->id, + 'sesskey' => sesskey(), + 'confirm' => 1, + ]), + new pix_icon('t/delete', get_string('deletechapter', 'mod_book', $title)), + $deleteaction, + ['title' => get_string('deletechapter', 'mod_book', $titleunescaped)] + ); + if ($ch->hidden) { $toc .= html_writer::link(new moodle_url('show.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'sesskey' => $USER->sesskey)), $OUTPUT->pix_icon('t/show', get_string('showchapter', 'mod_book', $title)),