Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   1  <?php
   2  // This file is part of 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/>.
  16  
  17  /**
  18   * Delete book chapter
  19   *
  20   * @package    mod_book
  21   * @copyright  2004-2011 Petr Skoda {@link http://skodak.org}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require(__DIR__.'/../../config.php');
  26  require_once (__DIR__.'/locallib.php');
  27  
  28  // Course Module ID.
  29  $id        = required_param('id', PARAM_INT);
  30  
  31  // Chapter ID.
  32  $chapterid = required_param('chapterid', PARAM_INT);
  33  
  34  $confirm   = optional_param('confirm', 0, PARAM_BOOL);
  35  
  36  $cm = get_coursemodule_from_id('book', $id, 0, false, MUST_EXIST);
  37  $course = $DB->get_record('course', ['id' => $cm->course], '*', MUST_EXIST);
  38  $book = $DB->get_record('book', ['id' => $cm->instance], '*', MUST_EXIST);
  39  $chapter = $DB->get_record('book_chapters', ['id' => $chapterid, 'bookid' => $book->id], '*', MUST_EXIST);
  40  
  41  require_login($course, false, $cm);
  42  require_sesskey();
  43  
  44  $context = context_module::instance($cm->id);
  45  require_capability('mod/book:edit', $context);
  46  
  47  $PAGE->set_url('/mod/book/delete.php', ['id' => $id, 'chapterid' => $chapterid]);
  48  
  49  if ($confirm) {
  50      // The operation was confirmed.
  51      $fs = get_file_storage();
  52  
  53      $subchaptercount = 0;
  54      if (!$chapter->subchapter) {
  55          // This is a top-level chapter.
  56          // Make sure to remove any sub-chapters if there are any.
  57          $chapters = $DB->get_recordset_select('book_chapters', 'bookid = :bookid AND pagenum > :pagenum', [
  58                  'bookid' => $book->id,
  59                  'pagenum' => $chapter->pagenum,
  60              ], 'pagenum');
  61  
  62          foreach ($chapters as $ch) {
  63              if (!$ch->subchapter) {
  64                  // This is a new chapter. Any subsequent subchapters will be part of a different chapter.
  65                  break;
  66              } else {
  67                  // This is subchapter of the chapter being removed.
  68                  core_tag_tag::remove_all_item_tags('mod_book', 'book_chapters', $ch->id);
  69                  $fs->delete_area_files($context->id, 'mod_book', 'chapter', $ch->id);
  70                  $DB->delete_records('book_chapters', ['id' => $ch->id]);
  71                  \mod_book\event\chapter_deleted::create_from_chapter($book, $context, $ch)->trigger();
  72  
  73                  $subchaptercount++;
  74              }
  75          }
  76          $chapters->close();
  77      }
  78  
  79      // Now delete the actual chapter.
  80      core_tag_tag::remove_all_item_tags('mod_book', 'book_chapters', $chapter->id);
  81      $fs->delete_area_files($context->id, 'mod_book', 'chapter', $chapter->id);
  82      $DB->delete_records('book_chapters', ['id' => $chapter->id]);
  83  
  84      \mod_book\event\chapter_deleted::create_from_chapter($book, $context, $chapter)->trigger();
  85  
  86      // Ensure that the book structure is correct.
  87      // book_preload_chapters will fix parts including the pagenum.
  88      $chapters = book_preload_chapters($book);
  89  
  90      book_add_fake_block($chapters, $chapter, $book, $cm);
  91  
  92      // Bump the book revision.
  93      $DB->set_field('book', 'revision', $book->revision + 1, ['id' => $book->id]);
  94  
  95      if ($subchaptercount) {
  96          $message = get_string('chapterandsubchaptersdeleted', 'mod_book', (object) [
  97              'title' => format_string($chapter->title),
  98              'subchapters' => $subchaptercount,
  99          ]);
 100      } else {
 101          $message = get_string('chapterdeleted', 'mod_book', (object) [
 102              'title' => format_string($chapter->title),
 103          ]);
 104      }
 105  
 106      redirect(new moodle_url('/mod/book/view.php', ['id' => $cm->id]), $message);
 107  }
 108  
 109  redirect(new moodle_url('/mod/book/view.php', ['id' => $cm->id]));