Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
/mod/book/ -> move.php (source)
   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   * Move 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  $id        = required_param('id', PARAM_INT);        // Course Module ID
  29  $chapterid = required_param('chapterid', PARAM_INT); // Chapter ID
  30  $up        = optional_param('up', 0, PARAM_BOOL);
  31  
  32  $cm = get_coursemodule_from_id('book', $id, 0, false, MUST_EXIST);
  33  $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
  34  $book = $DB->get_record('book', array('id'=>$cm->instance), '*', MUST_EXIST);
  35  
  36  require_login($course, false, $cm);
  37  require_sesskey();
  38  
  39  $context = context_module::instance($cm->id);
  40  require_capability('mod/book:edit', $context);
  41  
  42  $chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id), '*', MUST_EXIST);
  43  
  44  
  45  $oldchapters = $DB->get_records('book_chapters', array('bookid'=>$book->id), 'pagenum', 'id, pagenum, subchapter');
  46  
  47  $nothing = 0;
  48  
  49  $chapters = array();
  50  $chs = 0;
  51  $che = 0;
  52  $ts = 0;
  53  $te = 0;
  54  // create new ordered array and find chapters to be moved
  55  $i = 1;
  56  $found = 0;
  57  foreach ($oldchapters as $ch) {
  58      $chapters[$i] = $ch;
  59      if ($chapter->id == $ch->id) {
  60          $chs = $i;
  61          $che = $chs;
  62          if ($ch->subchapter) {
  63              $found = 1;// Subchapter moves alone.
  64          }
  65      } else if ($chs) {
  66          if ($found) {
  67              // Nothing.
  68          } else if ($ch->subchapter) {
  69              $che = $i; // Chapter with subchapter(s).
  70          } else {
  71              $found = 1;
  72          }
  73      }
  74      $i++;
  75  }
  76  
  77  // Find target chapter(s).
  78  if ($chapters[$chs]->subchapter) { // Moving single subchapter up or down.
  79      if ($up) {
  80          if ($chs == 1) {
  81              $nothing = 1; // Already first.
  82          } else {
  83              $ts = $chs - 1;
  84              $te = $ts;
  85          }
  86      } else { // Down.
  87          if ($che == count($chapters)) {
  88              $nothing = 1; // Already last.
  89          } else {
  90              $ts = $che + 1;
  91              $te = $ts;
  92          }
  93      }
  94  } else { // Moving chapter and looking for next/previous chapter.
  95      if ($up) { // Up.
  96          if ($chs == 1) {
  97              $nothing = 1; // Already first.
  98          } else {
  99              $te = $chs - 1;
 100              for ($i = $chs-1; $i >= 1; $i--) {
 101                  if ($chapters[$i]->subchapter) {
 102                      $ts = $i;
 103                  } else {
 104                      $ts = $i;
 105                      break;
 106                  }
 107              }
 108          }
 109      } else { // Down.
 110          if ($che == count($chapters)) {
 111              $nothing = 1; // Already last.
 112          } else {
 113              $ts = $che + 1;
 114              $found = 0;
 115              for ($i = $che+1; $i <= count($chapters); $i++) {
 116                  if ($chapters[$i]->subchapter) {
 117                      $te = $i;
 118                  } else {
 119                      if ($found) {
 120                          break;
 121                      } else {
 122                          $te = $i;
 123                          $found = 1;
 124                      }
 125                  }
 126              }
 127          }
 128      }
 129  }
 130  
 131  // Recreated newly sorted list of chapters.
 132  if (!$nothing) {
 133      $newchapters = array();
 134  
 135      if ($up) {
 136          if ($ts > 1) {
 137              for ($i=1; $i<$ts; $i++) {
 138                  $newchapters[] = $chapters[$i];
 139              }
 140          }
 141          for ($i=$chs; $i<=$che; $i++) {
 142              $newchapters[$i] = $chapters[$i];
 143          }
 144          for ($i=$ts; $i<=$te; $i++) {
 145              $newchapters[$i] = $chapters[$i];
 146          }
 147          if ($che<count($chapters)) {
 148              for ($i=$che; $i<=count($chapters); $i++) {
 149                  $newchapters[$i] = $chapters[$i];
 150              }
 151          }
 152      } else {
 153          if ($chs > 1) {
 154              for ($i=1; $i<$chs; $i++) {
 155                  $newchapters[] = $chapters[$i];
 156              }
 157          }
 158          for ($i=$ts; $i<=$te; $i++) {
 159              $newchapters[$i] = $chapters[$i];
 160          }
 161          for ($i=$chs; $i<=$che; $i++) {
 162              $newchapters[$i] = $chapters[$i];
 163          }
 164          if ($te<count($chapters)) {
 165              for ($i=$te; $i<=count($chapters); $i++) {
 166                  $newchapters[$i] = $chapters[$i];
 167              }
 168          }
 169      }
 170  
 171      // Store chapters in the new order.
 172      $i = 1;
 173      foreach ($newchapters as $ch) {
 174          $ch->pagenum = $i;
 175          $DB->update_record('book_chapters', $ch);
 176          $ch = $DB->get_record('book_chapters', array('id' => $ch->id));
 177  
 178          \mod_book\event\chapter_updated::create_from_chapter($book, $context, $ch)->trigger();
 179  
 180          $i++;
 181      }
 182  }
 183  
 184  book_preload_chapters($book); // fix structure
 185  $DB->set_field('book', 'revision', $book->revision+1, array('id'=>$book->id));
 186  
 187  redirect('view.php?id='.$cm->id.'&chapterid='.$chapter->id);
 188