fixed log action inserts
[moodle.git] / move.php
CommitLineData
02039379
PS
1<?PHP // $Id: move.php,v 1.1 2006/03/12 18:39:59 skodak Exp $
2
3require('teacheraccess.php'); //page only for teachers
4$up = optional_param('up', 0, PARAM_BOOL);
5
6
7$oldchapters = get_records('book_chapters', 'bookid', $book->id, 'pagenum', 'id, pagenum, subchapter');
8
9$nothing = 0;
10
11$chapters = array();
12$chs = 0;
13$che = 0;
14$ts = 0;
15$te = 0;
16// create new ordered array and find chapters to be moved
17$i = 1;
18$found = 0;
19foreach ($oldchapters as $ch) {
20 $chapters[$i] = $ch;
21 if ($chapter->id === $ch->id) {
22 $chs = $i;
23 $che = $chs;
24 if ($ch->subchapter) {
25 $found = 1;//subchapter moves alone
26 }
27 } else if ($chs) {
28 if ($found) {
29 //nothing
30 } else if ($ch->subchapter) {
31 $che = $i; // chapter with subchapter(s)
32 } else {
33 $found = 1;
34 }
35 }
36 $i++;
37}
38
39// find target chapter(s)
40if ($chapters[$chs]->subchapter) { //moving single subchapter up or down
41 if ($up) {
42 if ($chs === 1) {
43 $nothing = 1; //already first
44 } else {
45 $ts = $chs - 1;
46 $te = $ts;
47 }
48 } else { //down
49 if ($che === count($chapters)) {
50 $nothing = 1; //already last
51 } else {
52 $ts = $che + 1;
53 $te = $ts;
54 }
55 }
56} else { // moving chapter and looking for next/previous chapter
57 if ($up) { //up
58 if ($chs === 1) {
59 $nothing = 1; //already first
60 } else {
61 $te = $chs - 1;
62 for($i = $chs-1; $i >= 1; $i--) {
63 if ($chapters[$i]->subchapter) {
64 $ts = $i;
65 } else {
66 $ts = $i;
67 break;
68 }
69 }
70 }
71 } else { //down
72 if ($che === count($chapters)) {
73 $nothing = 1; //already last
74 } else {
75 $ts = $che + 1;
76 $found = 0;
77 for($i = $che+1; $i <= count($chapters); $i++) {
78 if ($chapters[$i]->subchapter) {
79 $te = $i;
80 } else {
81 if ($found) {
82 break;
83 } else {
84 $te = $i;
85 $found = 1;
86 }
87 }
88 }
89 }
90 }
91}
92
93//recreated newly sorted list of chapters
94if (!$nothing) {
95 $newchapters = array();
96
97 if ($up) {
98 if ($ts > 1) {
99 for ($i=1; $i<$ts; $i++) {
100 $newchapters[] = $chapters[$i];
101 }
102 }
103 for ($i=$chs; $i<=$che; $i++) {
104 $newchapters[$i] = $chapters[$i];
105 }
106 for ($i=$ts; $i<=$te; $i++) {
107 $newchapters[$i] = $chapters[$i];
108 }
109 if ($che<count($chapters)) {
110 for ($i=$che; $i<=count($chapters); $i++) {
111 $newchapters[$i] = $chapters[$i];
112 }
113 }
114 } else {
115 if ($chs > 1) {
116 for ($i=1; $i<$chs; $i++) {
117 $newchapters[] = $chapters[$i];
118 }
119 }
120 for ($i=$ts; $i<=$te; $i++) {
121 $newchapters[$i] = $chapters[$i];
122 }
123 for ($i=$chs; $i<=$che; $i++) {
124 $newchapters[$i] = $chapters[$i];
125 }
126 if ($te<count($chapters)) {
127 for ($i=$te; $i<=count($chapters); $i++) {
128 $newchapters[$i] = $chapters[$i];
129 }
130 }
131 }
132
133 //store chapters in the new order
134 $i = 1;
135 foreach ($newchapters as $ch) {
136 $ch->pagenum = $i;
137 update_record('book_chapters', $ch);
138 $i++;
139 }
140}
141
142add_to_log($course->id, 'course', 'update mod', '../mod/book/view.php?id='.$cm->id, 'book '.$book->id);
143add_to_log($course->id, 'book', 'update', 'view.php?id='.$cm->id, $book->id, $cm->id);
144book_check_structure($book->id);
145redirect('view.php?id='.$cm->id.'&chapterid='.$chapter->id);
146die;
147
148?>