2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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);
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');
54 // create new ordered array and find chapters to be moved
57 foreach ($oldchapters as $ch) {
59 if ($chapter->id
== $ch->id
) {
62 if ($ch->subchapter
) {
63 $found = 1;// Subchapter moves alone.
68 } else if ($ch->subchapter
) {
69 $che = $i; // Chapter with subchapter(s).
77 // Find target chapter(s).
78 if ($chapters[$chs]->subchapter
) { // Moving single subchapter up or down.
81 $nothing = 1; // Already first.
87 if ($che == count($chapters)) {
88 $nothing = 1; // Already last.
94 } else { // Moving chapter and looking for next/previous chapter.
97 $nothing = 1; // Already first.
100 for ($i = $chs-1; $i >= 1; $i--) {
101 if ($chapters[$i]->subchapter
) {
110 if ($che == count($chapters)) {
111 $nothing = 1; // Already last.
115 for ($i = $che+
1; $i <= count($chapters); $i++
) {
116 if ($chapters[$i]->subchapter
) {
131 // Recreated newly sorted list of chapters.
133 $newchapters = array();
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];
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.
173 foreach ($newchapters as $ch) {
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();
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
);