/mod/book/move.php

https://github.com/markn86/moodle · PHP · 188 lines · 142 code · 20 blank · 26 comment · 50 complexity · d077f86cfc1983c38b5a268777455a2d MD5 · raw file

  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. * Move book chapter
  18. *
  19. * @package mod_book
  20. * @copyright 2004-2011 Petr Skoda {@link http://skodak.org}
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. require(__DIR__.'/../../config.php');
  24. require_once(__DIR__.'/locallib.php');
  25. $id = required_param('id', PARAM_INT); // Course Module ID
  26. $chapterid = required_param('chapterid', PARAM_INT); // Chapter ID
  27. $up = optional_param('up', 0, PARAM_BOOL);
  28. $cm = get_coursemodule_from_id('book', $id, 0, false, MUST_EXIST);
  29. $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
  30. $book = $DB->get_record('book', array('id'=>$cm->instance), '*', MUST_EXIST);
  31. require_login($course, false, $cm);
  32. require_sesskey();
  33. $context = context_module::instance($cm->id);
  34. require_capability('mod/book:edit', $context);
  35. $chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id), '*', MUST_EXIST);
  36. $oldchapters = $DB->get_records('book_chapters', array('bookid'=>$book->id), 'pagenum', 'id, pagenum, subchapter');
  37. $nothing = 0;
  38. $chapters = array();
  39. $chs = 0;
  40. $che = 0;
  41. $ts = 0;
  42. $te = 0;
  43. // create new ordered array and find chapters to be moved
  44. $i = 1;
  45. $found = 0;
  46. foreach ($oldchapters as $ch) {
  47. $chapters[$i] = $ch;
  48. if ($chapter->id == $ch->id) {
  49. $chs = $i;
  50. $che = $chs;
  51. if ($ch->subchapter) {
  52. $found = 1;// Subchapter moves alone.
  53. }
  54. } else if ($chs) {
  55. if ($found) {
  56. // Nothing.
  57. } else if ($ch->subchapter) {
  58. $che = $i; // Chapter with subchapter(s).
  59. } else {
  60. $found = 1;
  61. }
  62. }
  63. $i++;
  64. }
  65. // Find target chapter(s).
  66. if ($chapters[$chs]->subchapter) { // Moving single subchapter up or down.
  67. if ($up) {
  68. if ($chs == 1) {
  69. $nothing = 1; // Already first.
  70. } else {
  71. $ts = $chs - 1;
  72. $te = $ts;
  73. }
  74. } else { // Down.
  75. if ($che == count($chapters)) {
  76. $nothing = 1; // Already last.
  77. } else {
  78. $ts = $che + 1;
  79. $te = $ts;
  80. }
  81. }
  82. } else { // Moving chapter and looking for next/previous chapter.
  83. if ($up) { // Up.
  84. if ($chs == 1) {
  85. $nothing = 1; // Already first.
  86. } else {
  87. $te = $chs - 1;
  88. for ($i = $chs-1; $i >= 1; $i--) {
  89. if ($chapters[$i]->subchapter) {
  90. $ts = $i;
  91. } else {
  92. $ts = $i;
  93. break;
  94. }
  95. }
  96. }
  97. } else { // Down.
  98. if ($che == count($chapters)) {
  99. $nothing = 1; // Already last.
  100. } else {
  101. $ts = $che + 1;
  102. $found = 0;
  103. for ($i = $che+1; $i <= count($chapters); $i++) {
  104. if ($chapters[$i]->subchapter) {
  105. $te = $i;
  106. } else {
  107. if ($found) {
  108. break;
  109. } else {
  110. $te = $i;
  111. $found = 1;
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }
  118. // Recreated newly sorted list of chapters.
  119. if (!$nothing) {
  120. $newchapters = array();
  121. if ($up) {
  122. if ($ts > 1) {
  123. for ($i=1; $i<$ts; $i++) {
  124. $newchapters[] = $chapters[$i];
  125. }
  126. }
  127. for ($i=$chs; $i<=$che; $i++) {
  128. $newchapters[$i] = $chapters[$i];
  129. }
  130. for ($i=$ts; $i<=$te; $i++) {
  131. $newchapters[$i] = $chapters[$i];
  132. }
  133. if ($che<count($chapters)) {
  134. for ($i=$che; $i<=count($chapters); $i++) {
  135. $newchapters[$i] = $chapters[$i];
  136. }
  137. }
  138. } else {
  139. if ($chs > 1) {
  140. for ($i=1; $i<$chs; $i++) {
  141. $newchapters[] = $chapters[$i];
  142. }
  143. }
  144. for ($i=$ts; $i<=$te; $i++) {
  145. $newchapters[$i] = $chapters[$i];
  146. }
  147. for ($i=$chs; $i<=$che; $i++) {
  148. $newchapters[$i] = $chapters[$i];
  149. }
  150. if ($te<count($chapters)) {
  151. for ($i=$te; $i<=count($chapters); $i++) {
  152. $newchapters[$i] = $chapters[$i];
  153. }
  154. }
  155. }
  156. // Store chapters in the new order.
  157. $i = 1;
  158. foreach ($newchapters as $ch) {
  159. $ch->pagenum = $i;
  160. $DB->update_record('book_chapters', $ch);
  161. $ch = $DB->get_record('book_chapters', array('id' => $ch->id));
  162. \mod_book\event\chapter_updated::create_from_chapter($book, $context, $ch)->trigger();
  163. $i++;
  164. }
  165. }
  166. book_preload_chapters($book); // fix structure
  167. $DB->set_field('book', 'revision', $book->revision+1, array('id'=>$book->id));
  168. redirect('view.php?id='.$cm->id.'&chapterid='.$chapter->id);