MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / lesson / action / moveit.php
blob2e2d0959165017843f6409a3862d0cb47cc952e8
1 <?php // $Id$
2 /**
3 * Action for actually moving the page (database changes)
5 * @version $Id$
6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
7 * @package lesson
8 **/
9 confirm_sesskey();
11 $pageid = required_param('pageid', PARAM_INT); // page to move
12 if (!$page = get_record("lesson_pages", "id", $pageid)) {
13 error("Moveit: page not found");
15 $after = required_param('after', PARAM_INT); // target page
17 // first step. determine the new first page
18 // (this is done first as the current first page will be lost in the next step)
19 if (!$after) {
20 // the moved page is the new first page
21 $newfirstpageid = $pageid;
22 // reset $after so that is points to the last page
23 // (when the pages are in a ring this will in effect be the first page)
24 if ($page->nextpageid) {
25 if (!$after = get_field("lesson_pages", "id", "lessonid", $lesson->id, "nextpageid", 0)) {
26 error("Moveit: last page id not found");
28 } else {
29 // the page being moved is the last page, so the new last page will be
30 $after = $page->prevpageid;
32 } elseif (!$page->prevpageid) {
33 // the page to be moved was the first page, so the following page must be the new first page
34 $newfirstpageid = $page->nextpageid;
35 } else {
36 // the current first page remains the first page
37 if (!$newfirstpageid = get_field("lesson_pages", "id", "lessonid", $lesson->id, "prevpageid", 0)) {
38 error("Moveit: current first page id not found");
41 // the rest is all unconditional...
43 // second step. join pages into a ring
44 if (!$firstpageid = get_field("lesson_pages", "id", "lessonid", $lesson->id, "prevpageid", 0)) {
45 error("Moveit: firstpageid not found");
47 if (!$lastpageid = get_field("lesson_pages", "id", "lessonid", $lesson->id, "nextpageid", 0)) {
48 error("Moveit: lastpage not found");
50 if (!set_field("lesson_pages", "prevpageid", $lastpageid, "id", $firstpageid)) {
51 error("Moveit: unable to update link");
53 if (!set_field("lesson_pages", "nextpageid", $firstpageid, "id", $lastpageid)) {
54 error("Moveit: unable to update link");
57 // third step. remove the page to be moved
58 if (!$prevpageid = get_field("lesson_pages", "prevpageid", "id", $pageid)) {
59 error("Moveit: prevpageid not found");
61 if (!$nextpageid = get_field("lesson_pages", "nextpageid", "id", $pageid)) {
62 error("Moveit: nextpageid not found");
64 if (!set_field("lesson_pages", "nextpageid", $nextpageid, "id", $prevpageid)) {
65 error("Moveit: unable to update link");
67 if (!set_field("lesson_pages", "prevpageid", $prevpageid, "id", $nextpageid)) {
68 error("Moveit: unable to update link");
71 // fourth step. insert page to be moved in new place...
72 if (!$nextpageid = get_field("lesson_pages", "nextpageid", "id", $after)) {
73 error("Movit: nextpageid not found");
75 if (!set_field("lesson_pages", "nextpageid", $pageid, "id", $after)) {
76 error("Moveit: unable to update link");
78 if (!set_field("lesson_pages", "prevpageid", $pageid, "id", $nextpageid)) {
79 error("Moveit: unable to update link");
81 // ...and set the links in the moved page
82 if (!set_field("lesson_pages", "prevpageid", $after, "id", $pageid)) {
83 error("Moveit: unable to update link");
85 if (!set_field("lesson_pages", "nextpageid", $nextpageid, "id", $pageid)) {
86 error("Moveit: unable to update link");
89 // fifth step. break the ring
90 if (!$newlastpageid = get_field("lesson_pages", "prevpageid", "id", $newfirstpageid)) {
91 error("Moveit: newlastpageid not found");
93 if (!set_field("lesson_pages", "prevpageid", 0, "id", $newfirstpageid)) {
94 error("Moveit: unable to update link");
96 if (!set_field("lesson_pages", "nextpageid", 0, "id", $newlastpageid)) {
97 error("Moveit: unable to update link");
99 lesson_set_message(get_string('movedpage', 'lesson'), 'notifysuccess');
100 redirect("$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id");