Merge branch 'MDL-56295-master' of git://github.com/andrewnicols/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2016-10-18 20:02:22 +02:00
commit ffe9505d2b
3 changed files with 71 additions and 50 deletions

View File

@ -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 '<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]));

View File

@ -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.

View File

@ -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)),