fixed log action inserts
[moodle.git] / edit.php
CommitLineData
02039379
PS
1<?PHP // $Id: edit.php,v 1.1 2006/03/12 18:39:59 skodak Exp $
2
3require_once('../../config.php');
4require_once('lib.php');
5
6$id = required_param('id', PARAM_INT); // Course Module ID
7$chapterid = optional_param('chapterid', 0, PARAM_INT); // Chapter ID
8$pagenum = optional_param('pagenum', 0, PARAM_INT);
9$subchapter = optional_param('subchapter', 0, PARAM_BOOL);
10
11// =========================================================================
12// security checks START - only teachers edit
13// =========================================================================
14require_login();
15
16if (!$cm = get_record('course_modules', 'id', $id)) {
17 error('Course Module ID was incorrect');
18}
19
20if (!$course = get_record('course', 'id', $cm->course)) {
21 error('Course is misconfigured');
22}
23
24if (!isteacheredit($course->id)) {
25 error('Only editing teachers can edit books!', $_SERVER['HTTP_REFERER']);
26}
27
28if (!$book = get_record('book', 'id', $cm->instance)) {
29 error('Course module is incorrect');
30}
31
32$chapter = get_record('book_chapters', 'id', $chapterid);
33
34//check all variables
35unset($id);
36unset($chapterid);
37if ($chapter) {
38 if ($chapter->bookid != $book->id) {//chapter id not in this book!!!!
39 error('Chapter not part of this book!');
40 }
41 $pagenum = $chapter->pagenum;
42} else {
43 $pagenum = (integer)$pagenum;
44}
45
46// =========================================================================
47// security checks END
48// =========================================================================
49
50
51/// If data submitted, then process and store.
52if (($form = data_submitted()) && (confirm_sesskey())) {
53 //TODO: skip it for now
54 //prepare data - security checks
55 //$form->title = clean_text($form->title, FORMAT_HTML);
56 //$form->content = clean_text($form->content, FORMAT_HTML);
57
58 if (isset($form->subchapter) ) {
59 $form->subchapter = 1;
60 } else {
61 $form->subchapter = 0;
62 }
63 if ($chapter) {
64 /// editing existing chapter
65 $chapter->content = $form->content;
66 $chapter->title = $form->title;
67 $chapter->subchapter = $form->subchapter;
68 $chapter->timemodified = time();
69 $chapter->importsrc = addslashes($chapter->importsrc); //use already stored importsrc
70 if (!update_record('book_chapters', $chapter)) {
71 error('Could not update your book');
72 }
73 add_to_log($course->id, 'course', 'update mod', '../mod/book/view.php?id='.$cm->id, 'book '.$book->id);
74 add_to_log($course->id, 'book', 'update', 'view.php?id='.$cm->id.'&chapterid='.$chapter->id, $book->id, $cm->id);
75 } else {
76 /// adding new chapter
77 $chapter->bookid = $book->id;
78 $chapter->pagenum = $form->pagenum + 1; //place after given pagenum, lets hope it is a number
79 $chapter->subchapter = $form->subchapter;
80 $chapter->title = $form->title;
81 $chapter->content = $form->content;
82 $chapter->hidden = 0;
83 $chapter->timecreated = time();
84 $chapter->timemodified = $chapter->timecreated;
85 $chapter->importsrc = '';
86
87 $chapters = get_records('book_chapters', 'bookid', $book->id, 'pagenum', 'id, pagenum');
88 if ($chapters) {
89 foreach($chapters as $ch) {
90 if ($ch->pagenum > $pagenum) {
91 $ch->pagenum = $ch->pagenum + 1;
92 if (!update_record('book_chapters', $ch)) {
93 error('Could not update your book');
94 }
95 }
96 }
97 }
98 if (!$chapter->id = insert_record('book_chapters', $chapter)) {
99 error('Could not insert a new chapter');
100 }
101 add_to_log($course->id, 'course', 'update mod', '../mod/book/view.php?id='.$cm->id, 'book '.$book->id);
102 add_to_log($course->id, 'book', 'update', 'view.php?id='.$cm->id.'&chapterid='.$chapter->id, $book->id, $cm->id);
103 }
104
105 book_check_structure($book->id);
106 redirect("view.php?id=$cm->id&chapterid=$chapter->id");
107 die;
108}
109
110/// Otherwise fill and print the form.
111$strbook = get_string('modulename', 'book');
112$strbooks = get_string('modulenameplural', 'book');
113$stredit = get_string('edit');
114$pageheading = get_string('editingchapter', 'book');
115
116$usehtmleditor = can_use_html_editor();
117
118if (!$chapter) {
119 $chapter->id = -1;
120 $chapter->title = '';
121 $chapter->content = '';
122 $chapter->subchapter = $subchapter;
123 $chapter->pagenum = $pagenum;
124}
125
126///prepare the page header
127if ($course->category) {
128 $navigation = '<a href="../../course/view.php?id='.$course->id.'">'.$course->shortname.'</a> ->';
129} else {
130 $navigation = '';
131}
132
133print_header( "$course->shortname: $book->name",
134 $course->fullname,
135 "$navigation <a href=\"index.php?id=$course->id\">$strbooks</A> -> <a href=\"view.php?id=$cm->id\">$book->name</A> -> $stredit",
136 '',
137 '',
138 true,
139 '',
140 ''
141 );
142
143
144$icon = '<img align="absmiddle" height="16" width="16" src="icon_chapter.gif" />&nbsp;';
145print_heading_with_help($pageheading, 'edit', 'book', $icon);
146print_simple_box_start('center', '');
147include('edit.html');
148print_simple_box_end();
149
150if ($usehtmleditor ) {
151 use_html_editor();
152}
153
154print_footer($course);
155
156?>