File: moveit.php

package info (click to toggle)
moodle 1.6.3-2%2Betch3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 37,172 kB
  • ctags: 51,688
  • sloc: php: 231,916; sql: 5,631; xml: 2,688; sh: 1,185; perl: 638; makefile: 48; pascal: 36
file content (102 lines) | stat: -rw-r--r-- 4,255 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php

/****************** moveit ************************************/
   
    if (!isteacher($course->id)) {
        error("Only teachers can look at this page");
    }

    confirm_sesskey();

    $pageid = required_param('pageid', PARAM_INT); //  page to move
    if (!$page = get_record("lesson_pages", "id", $pageid)) {
        error("Moveit: page not found");
    }
    $after = required_param('after', PARAM_INT); // target page

    print_heading(get_string("moving", "lesson", format_string($page->title)));
    
    // first step. determine the new first page
    // (this is done first as the current first page will be lost in the next step)
    if (!$after) {
        // the moved page is the new first page
        $newfirstpageid = $pageid;
        // reset $after so that is points to the last page 
        // (when the pages are in a ring this will in effect be the first page)
        if ($page->nextpageid) {
            if (!$after = get_field("lesson_pages", "id", "lessonid", $lesson->id, "nextpageid", 0)) {
                error("Moveit: last page id not found");
            }
        } else {
            // the page being moved is the last page, so the new last page will be
            $after = $page->prevpageid;
        }
    } elseif (!$page->prevpageid) {
        // the page to be moved was the first page, so the following page must be the new first page
        $newfirstpageid = $page->nextpageid;
    } else {
        // the current first page remains the first page
        if (!$newfirstpageid = get_field("lesson_pages", "id", "lessonid", $lesson->id, "prevpageid", 0)) {
            error("Moveit: current first page id not found");
        }
    }
    // the rest is all unconditional...
    
    // second step. join pages into a ring 
    if (!$firstpageid = get_field("lesson_pages", "id", "lessonid", $lesson->id, "prevpageid", 0)) {
        error("Moveit: firstpageid not found");
    }
    if (!$lastpageid = get_field("lesson_pages", "id", "lessonid", $lesson->id, "nextpageid", 0)) {
        error("Moveit: lastpage not found");
    }
    if (!set_field("lesson_pages", "prevpageid", $lastpageid, "id", $firstpageid)) {
        error("Moveit: unable to update link");
    }
    if (!set_field("lesson_pages", "nextpageid", $firstpageid, "id", $lastpageid)) {
        error("Moveit: unable to update link");
    }

    // third step. remove the page to be moved
    if (!$prevpageid = get_field("lesson_pages", "prevpageid", "id", $pageid)) {
        error("Moveit: prevpageid not found");
    }
    if (!$nextpageid = get_field("lesson_pages", "nextpageid", "id", $pageid)) {
        error("Moveit: nextpageid not found");
    }
    if (!set_field("lesson_pages", "nextpageid", $nextpageid, "id", $prevpageid)) {
        error("Moveit: unable to update link");
    }
    if (!set_field("lesson_pages", "prevpageid", $prevpageid, "id", $nextpageid)) {
        error("Moveit: unable to update link");
    }
    
    // fourth step. insert page to be moved in new place...
    if (!$nextpageid = get_field("lesson_pages", "nextpageid", "id", $after)) {
        error("Movit: nextpageid not found");
    }
    if (!set_field("lesson_pages", "nextpageid", $pageid, "id", $after)) {
        error("Moveit: unable to update link");
    }
    if (!set_field("lesson_pages", "prevpageid", $pageid, "id", $nextpageid)) {
        error("Moveit: unable to update link");
    }
    // ...and set the links in the moved page
    if (!set_field("lesson_pages", "prevpageid", $after, "id", $pageid)) {
        error("Moveit: unable to update link");
    }
    if (!set_field("lesson_pages", "nextpageid", $nextpageid, "id", $pageid)) {
        error("Moveit: unable to update link");
    }
    
    // fifth step. break the ring
    if (!$newlastpageid = get_field("lesson_pages", "prevpageid", "id", $newfirstpageid)) {
        error("Moveit: newlastpageid not found");
    }
    if (!set_field("lesson_pages", "prevpageid", 0, "id", $newfirstpageid)) {
        error("Moveit: unable to update link");
    }
    if (!set_field("lesson_pages", "nextpageid", 0, "id", $newlastpageid)) {
            error("Moveit: unable to update link");
    }
    redirect("view.php?id=$cm->id", get_string('movedpage', 'lesson'));
?>