Skip to content
Permalink
Browse files

MDL-56295 mod_book: Refactor of chapter delete page

  • Loading branch information...
andrewnicols committed Oct 14, 2016
1 parent 6942122 commit 985ec993591d5a0d7af69fd500a8d10de05115c1
Showing with 53 additions and 42 deletions.
  1. +51 −42 mod/book/delete.php
  2. +2 −0 mod/book/lang/en/book.php
@@ -25,72 +25,81 @@
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();
$context = context_module::instance($cm->id);
require_capability('mod/book:edit', $context);
$PAGE->set_url('/mod/book/delete.php', array('id'=>$id, 'chapterid'=>$chapterid));
$chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id), '*', MUST_EXIST);
$PAGE->set_url('/mod/book/delete.php', ['id' => $id, 'chapterid' => $chapterid]);
if ($confirm) {
// The operation was confirmed.
$fs = get_file_storage();
// Header and strings.
$PAGE->set_title($book->name);
$PAGE->set_heading($course->fullname);
$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');
// Form processing.
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;
foreach ($chapters as $ch) {
if ($ch->id == $chapter->id) {
$found = true;
} else if ($found and $ch->subchapter) {
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', array('id'=>$ch->id));
$DB->delete_records('book_chapters', ['id' => $ch->id]);
\mod_book\event\chapter_deleted::create_from_chapter($book, $context, $ch)->trigger();
} else if ($found) {
break;
$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));
redirect('view.php?id='.$cm->id);
}
echo $OUTPUT->header();
echo $OUTPUT->heading($book->name);
// Ensure that the book structure is correct.
// book_preload_chapters will fix parts including the pagenum.
book_preload_chapters($book);
// 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),
]);
}
// 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');
redirect(new moodle_url('/mod/book/view.php', ['id' => $cm->id]), $message);
}
echo '<br />';
$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("<strong>$title</strong><p>$strconfirm</p>", $continue, $cancel);
echo $OUTPUT->footer();
redirect(new moodle_url('/mod/book/view.php', ['id' => $cm->id]));
@@ -38,6 +38,8 @@
$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.

0 comments on commit 985ec99

Please sign in to comment.
You can’t perform that action at this time.