MDL-63660 tool_dataprivacy: Increase expected export file size
[moodle.git] / mod / book / move.php
blobea6dd9fcd1c800e322d5ac2e91f349d8c5d9bb14
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.
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/>.
17 /**
18 * Move book chapter
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
25 require(__DIR__.'/../../config.php');
26 require_once(__DIR__.'/locallib.php');
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);
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);
36 require_login($course, false, $cm);
37 require_sesskey();
39 $context = context_module::instance($cm->id);
40 require_capability('mod/book:edit', $context);
42 $chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id), '*', MUST_EXIST);
45 $oldchapters = $DB->get_records('book_chapters', array('bookid'=>$book->id), 'pagenum', 'id, pagenum, subchapter');
47 $nothing = 0;
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.
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;
74 $i++;
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;
86 } else { // Down.
87 if ($che == count($chapters)) {
88 $nothing = 1; // Already last.
89 } else {
90 $ts = $che + 1;
91 $te = $ts;
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;
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;
131 // Recreated newly sorted list of chapters.
132 if (!$nothing) {
133 $newchapters = array();
135 if ($up) {
136 if ($ts > 1) {
137 for ($i=1; $i<$ts; $i++) {
138 $newchapters[] = $chapters[$i];
141 for ($i=$chs; $i<=$che; $i++) {
142 $newchapters[$i] = $chapters[$i];
144 for ($i=$ts; $i<=$te; $i++) {
145 $newchapters[$i] = $chapters[$i];
147 if ($che<count($chapters)) {
148 for ($i=$che; $i<=count($chapters); $i++) {
149 $newchapters[$i] = $chapters[$i];
152 } else {
153 if ($chs > 1) {
154 for ($i=1; $i<$chs; $i++) {
155 $newchapters[] = $chapters[$i];
158 for ($i=$ts; $i<=$te; $i++) {
159 $newchapters[$i] = $chapters[$i];
161 for ($i=$chs; $i<=$che; $i++) {
162 $newchapters[$i] = $chapters[$i];
164 if ($te<count($chapters)) {
165 for ($i=$te; $i<=count($chapters); $i++) {
166 $newchapters[$i] = $chapters[$i];
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));
178 \mod_book\event\chapter_updated::create_from_chapter($book, $context, $ch)->trigger();
180 $i++;
184 book_preload_chapters($book); // fix structure
185 $DB->set_field('book', 'revision', $book->revision+1, array('id'=>$book->id));
187 redirect('view.php?id='.$cm->id.'&chapterid='.$chapter->id);