2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
21 * @copyright 2004-2011 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require(dirname(__FILE__
).'/../../config.php');
26 require_once(dirname(__FILE__
).'/locallib.php');
27 require_once(dirname(__FILE__
).'/edit_form.php');
29 $cmid = required_param('cmid', PARAM_INT
); // Book Course Module ID
30 $chapterid = optional_param('id', 0, PARAM_INT
); // Chapter ID
31 $pagenum = optional_param('pagenum', 0, PARAM_INT
);
32 $subchapter = optional_param('subchapter', 0, PARAM_BOOL
);
34 $cm = get_coursemodule_from_id('book', $cmid, 0, false, MUST_EXIST
);
35 $course = $DB->get_record('course', array('id'=>$cm->course
), '*', MUST_EXIST
);
36 $book = $DB->get_record('book', array('id'=>$cm->instance
), '*', MUST_EXIST
);
38 require_login($course, false, $cm);
40 $context = context_module
::instance($cm->id
);
41 require_capability('mod/book:edit', $context);
43 $PAGE->set_url('/mod/book/edit.php', array('cmid'=>$cmid, 'id'=>$chapterid, 'pagenum'=>$pagenum, 'subchapter'=>$subchapter));
44 $PAGE->set_pagelayout('admin'); // TODO: Something. This is a bloody hack!
47 $chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id
), '*', MUST_EXIST
);
49 $chapter = new stdClass();
51 $chapter->subchapter
= $subchapter;
52 $chapter->pagenum
= $pagenum +
1;
54 $chapter->cmid
= $cm->id
;
56 $options = array('noclean'=>true, 'subdirs'=>true, 'maxfiles'=>-1, 'maxbytes'=>0, 'context'=>$context);
57 $chapter = file_prepare_standard_editor($chapter, 'content', $options, $context, 'mod_book', 'chapter', $chapter->id
);
59 $mform = new book_chapter_edit_form(null, array('chapter'=>$chapter, 'options'=>$options));
61 // If data submitted, then process and store.
62 if ($mform->is_cancelled()) {
63 if (empty($chapter->id
)) {
64 redirect("view.php?id=$cm->id");
66 redirect("view.php?id=$cm->id&chapterid=$chapter->id");
69 } else if ($data = $mform->get_data()) {
73 $data = file_postupdate_standard_editor($data, 'content', $options, $context, 'mod_book', 'chapter', $data->id
);
74 $DB->update_record('book_chapters', $data);
76 add_to_log($course->id
, 'course', 'update mod', '../mod/book/view.php?id='.$cm->id
, 'book '.$book->id
);
77 add_to_log($course->id
, 'book', 'update chapter', 'view.php?id='.$cm->id
.'&chapterid='.$data->id
, $data->id
, $cm->id
);
81 $data->bookid
= $book->id
;
83 $data->timecreated
= time();
84 $data->timemodified
= time();
85 $data->importsrc
= '';
86 $data->content
= ''; // updated later
87 $data->contentformat
= FORMAT_HTML
; // updated later
89 // make room for new page
90 $sql = "UPDATE {book_chapters}
91 SET pagenum = pagenum + 1
92 WHERE bookid = ? AND pagenum >= ?";
93 $DB->execute($sql, array($book->id
, $data->pagenum
));
95 $data->id
= $DB->insert_record('book_chapters', $data);
98 $data = file_postupdate_standard_editor($data, 'content', $options, $context, 'mod_book', 'chapter', $data->id
);
99 $DB->update_record('book_chapters', $data);
100 $DB->set_field('book', 'revision', $book->revision+
1, array('id'=>$book->id
));
102 add_to_log($course->id
, 'course', 'update mod', '../mod/book/view.php?id='.$cm->id
, 'book '.$book->id
);
103 add_to_log($course->id
, 'book', 'add chapter', 'view.php?id='.$cm->id
.'&chapterid='.$data->id
, $data->id
, $cm->id
);
106 book_preload_chapters($book); // fix structure
107 redirect("view.php?id=$cm->id&chapterid=$data->id");
110 // Otherwise fill and print the form.
111 $PAGE->set_title($book->name
);
112 $PAGE->set_heading($course->fullname
);
114 echo $OUTPUT->header();
115 echo $OUTPUT->heading(get_string('editingchapter', 'mod_book'));
119 echo $OUTPUT->footer();