MDL-50015 mod_imscp: New WS mod_imscp_view_imscp
[moodle.git] / mod / book / move.php
CommitLineData
8d623d14 1<?php
b15ef0b0 2// This file is part of Moodle - http://moodle.org/
2c1e98e6
PS
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/>.
02039379 16
2c1e98e6
PS
17/**
18 * Move book chapter
19 *
3542c4f4
EL
20 * @package mod_book
21 * @copyright 2004-2011 Petr Skoda {@link http://skodak.org}
2c1e98e6
PS
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
e355240d 24
74979715
PS
25require(dirname(__FILE__).'/../../config.php');
26require_once(dirname(__FILE__).'/locallib.php');
e355240d 27
2c1e98e6
PS
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);
74df2951 33$course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
2c1e98e6
PS
34$book = $DB->get_record('book', array('id'=>$cm->instance), '*', MUST_EXIST);
35
36require_login($course, false, $cm);
37require_sesskey();
38
1fc9e895 39$context = context_module::instance($cm->id);
2c1e98e6
PS
40require_capability('mod/book:edit', $context);
41
42$chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id), '*', MUST_EXIST);
02039379
PS
43
44
9d2fe179 45$oldchapters = $DB->get_records('book_chapters', array('bookid'=>$book->id), 'pagenum', 'id, pagenum, subchapter');
02039379
PS
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;
57foreach ($oldchapters as $ch) {
58 $chapters[$i] = $ch;
2ced61eb 59 if ($chapter->id == $ch->id) {
02039379
PS
60 $chs = $i;
61 $che = $chs;
62 if ($ch->subchapter) {
3542c4f4 63 $found = 1;// Subchapter moves alone.
02039379
PS
64 }
65 } else if ($chs) {
66 if ($found) {
3542c4f4 67 // Nothing.
02039379 68 } else if ($ch->subchapter) {
3542c4f4 69 $che = $i; // Chapter with subchapter(s).
02039379
PS
70 } else {
71 $found = 1;
72 }
73 }
74 $i++;
75}
76
3542c4f4
EL
77// Find target chapter(s).
78if ($chapters[$chs]->subchapter) { // Moving single subchapter up or down.
02039379 79 if ($up) {
2ced61eb 80 if ($chs == 1) {
3542c4f4 81 $nothing = 1; // Already first.
02039379
PS
82 } else {
83 $ts = $chs - 1;
84 $te = $ts;
85 }
3542c4f4 86 } else { // Down.
2ced61eb 87 if ($che == count($chapters)) {
3542c4f4 88 $nothing = 1; // Already last.
02039379
PS
89 } else {
90 $ts = $che + 1;
91 $te = $ts;
92 }
93 }
3542c4f4
EL
94} else { // Moving chapter and looking for next/previous chapter.
95 if ($up) { // Up.
2ced61eb 96 if ($chs == 1) {
3542c4f4 97 $nothing = 1; // Already first.
02039379
PS
98 } else {
99 $te = $chs - 1;
29656fe2 100 for ($i = $chs-1; $i >= 1; $i--) {
02039379
PS
101 if ($chapters[$i]->subchapter) {
102 $ts = $i;
103 } else {
104 $ts = $i;
105 break;
106 }
107 }
108 }
3542c4f4 109 } else { // Down.
2ced61eb 110 if ($che == count($chapters)) {
3542c4f4 111 $nothing = 1; // Already last.
02039379
PS
112 } else {
113 $ts = $che + 1;
114 $found = 0;
29656fe2 115 for ($i = $che+1; $i <= count($chapters); $i++) {
02039379
PS
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
3542c4f4 131// Recreated newly sorted list of chapters.
02039379
PS
132if (!$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
3542c4f4 171 // Store chapters in the new order.
02039379
PS
172 $i = 1;
173 foreach ($newchapters as $ch) {
174 $ch->pagenum = $i;
2c1e98e6 175 $DB->update_record('book_chapters', $ch);
ef2a9ece 176 $ch = $DB->get_record('book_chapters', array('id' => $ch->id));
7f6e3eef 177
ef2a9ece 178 \mod_book\event\chapter_updated::create_from_chapter($book, $context, $ch)->trigger();
7f6e3eef 179
02039379
PS
180 $i++;
181 }
182}
183
b1a9b01e 184book_preload_chapters($book); // fix structure
0c9f1864
PS
185$DB->set_field('book', 'revision', $book->revision+1, array('id'=>$book->id));
186
02039379 187redirect('view.php?id='.$cm->id.'&chapterid='.$chapter->id);
2c1e98e6 188