File: edit.php

package info (click to toggle)
moodle-book 1.6.3-1.1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 928 kB
  • ctags: 541
  • sloc: php: 3,079; sql: 56; xml: 33; makefile: 27; sh: 20
file content (156 lines) | stat: -rw-r--r-- 5,260 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?PHP // $Id$

require_once('../../config.php');
require_once('lib.php');

$id         = required_param('id', PARAM_INT);           // Course Module ID
$chapterid  = optional_param('chapterid', 0, PARAM_INT); // Chapter ID
$pagenum    = optional_param('pagenum', 0, PARAM_INT);
$subchapter = optional_param('subchapter', 0, PARAM_BOOL);

// =========================================================================
// security checks START - only teachers edit
// =========================================================================
require_login();

if (!$cm = get_record('course_modules', 'id', $id)) {
    error('Course Module ID was incorrect');
}

if (!$course = get_record('course', 'id', $cm->course)) {
    error('Course is misconfigured');
}

if (!isteacheredit($course->id)) {
    error('Only editing teachers can edit books!', $_SERVER['HTTP_REFERER']);
}

if (!$book = get_record('book', 'id', $cm->instance)) {
    error('Course module is incorrect');
}

$chapter = get_record('book_chapters', 'id', $chapterid);

//check all variables
unset($id);
unset($chapterid);
if ($chapter) {
    if ($chapter->bookid != $book->id) {//chapter id not in this book!!!!
        error('Chapter not part of this book!');
    }
    $pagenum = $chapter->pagenum;
} else {
    $pagenum = (integer)$pagenum;
}

// =========================================================================
// security checks END
// =========================================================================


/// If data submitted, then process and store.
if (($form = data_submitted()) && (confirm_sesskey())) {
    //TODO: skip it for now
    //prepare data - security checks
    //$form->title = clean_text($form->title, FORMAT_HTML);
    //$form->content = clean_text($form->content, FORMAT_HTML);

    if (isset($form->subchapter) ) {
        $form->subchapter = 1;
    } else {
        $form->subchapter = 0;
    }
    if ($chapter) {
        /// editing existing chapter
        $chapter->content = $form->content;
        $chapter->title = $form->title;
        $chapter->subchapter = $form->subchapter;
        $chapter->timemodified = time();
        $chapter->importsrc = addslashes($chapter->importsrc); //use already stored importsrc
        if (!update_record('book_chapters', $chapter)) {
            error('Could not update your book');
         }
        add_to_log($course->id, 'course', 'update mod', '../mod/book/view.php?id='.$cm->id, 'book '.$book->id);
        add_to_log($course->id, 'book', 'update', 'view.php?id='.$cm->id.'&chapterid='.$chapter->id, $book->id, $cm->id);
    } else {
        /// adding new chapter
        $chapter->bookid = $book->id;
        $chapter->pagenum = $form->pagenum + 1; //place after given pagenum, lets hope it is a number
        $chapter->subchapter = $form->subchapter;
        $chapter->title = $form->title;
        $chapter->content = $form->content;
        $chapter->hidden = 0;
        $chapter->timecreated = time();
        $chapter->timemodified = $chapter->timecreated;
        $chapter->importsrc = '';

        $chapters = get_records('book_chapters', 'bookid', $book->id, 'pagenum', 'id, pagenum');
        if ($chapters) {
            foreach($chapters as $ch) {
                if ($ch->pagenum > $pagenum) {
                    $ch->pagenum = $ch->pagenum + 1;
                     if (!update_record('book_chapters', $ch)) {
                        error('Could not update your book');
                    }
                }
            }
        }
        if (!$chapter->id = insert_record('book_chapters', $chapter)) {
            error('Could not insert a new chapter');
        }
        add_to_log($course->id, 'course', 'update mod', '../mod/book/view.php?id='.$cm->id, 'book '.$book->id);
        add_to_log($course->id, 'book', 'update', 'view.php?id='.$cm->id.'&chapterid='.$chapter->id, $book->id, $cm->id);
    }

    book_check_structure($book->id);
    redirect("view.php?id=$cm->id&chapterid=$chapter->id");
    die;
}

/// Otherwise fill and print the form.
$strbook = get_string('modulename', 'book');
$strbooks = get_string('modulenameplural', 'book');
$stredit = get_string('edit');
$pageheading = get_string('editingchapter', 'book');

$usehtmleditor = can_use_html_editor();

if (!$chapter) {
    $chapter->id = -1;
    $chapter->title = '';
    $chapter->content = '';
    $chapter->subchapter = $subchapter;
    $chapter->pagenum = $pagenum;
}

///prepare the page header
if ($course->category) {
    $navigation = '<a href="../../course/view.php?id='.$course->id.'">'.$course->shortname.'</a> ->';
} else {
    $navigation = '';
}

print_header( "$course->shortname: $book->name",
              $course->fullname,
              "$navigation <a href=\"index.php?id=$course->id\">$strbooks</A> -> <a href=\"view.php?id=$cm->id\">$book->name</A> -> $stredit",
              '',
              '',
              true,
              '',
              ''
            );


$icon = '<img align="absmiddle" height="16" width="16" src="icon_chapter.gif" />&nbsp;';
print_heading_with_help($pageheading, 'edit', 'book', $icon);
print_simple_box_start('center', '');
include('edit.html');
print_simple_box_end();

if ($usehtmleditor ) {
    use_html_editor();
}

print_footer($course);

?>