MDL-50015 mod_imscp: New WS mod_imscp_view_imscp
[moodle.git] / mod / book / edit.php
CommitLineData
8d623d14 1<?php
b15ef0b0 2// This file is part of Moodle - http://moodle.org/
2c1e98e6
PS
3//
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.
8//
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.
13//
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/>.
16
17/**
18 * Edit book chapter
19 *
3542c4f4
EL
20 * @package mod_book
21 * @copyright 2004-2011 Petr Skoda {@link http://skodak.org}
2c1e98e6
PS
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
02039379 24
74979715
PS
25require(dirname(__FILE__).'/../../config.php');
26require_once(dirname(__FILE__).'/locallib.php');
27require_once(dirname(__FILE__).'/edit_form.php');
02039379 28
c6d2eda2
PS
29$cmid = required_param('cmid', PARAM_INT); // Book Course Module ID
30$chapterid = optional_param('id', 0, PARAM_INT); // Chapter ID
02039379
PS
31$pagenum = optional_param('pagenum', 0, PARAM_INT);
32$subchapter = optional_param('subchapter', 0, PARAM_BOOL);
33
2c1e98e6 34$cm = get_coursemodule_from_id('book', $cmid, 0, false, MUST_EXIST);
74df2951 35$course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
2c1e98e6 36$book = $DB->get_record('book', array('id'=>$cm->instance), '*', MUST_EXIST);
02039379 37
c6d2eda2
PS
38require_login($course, false, $cm);
39
1fc9e895 40$context = context_module::instance($cm->id);
c6d2eda2 41require_capability('mod/book:edit', $context);
02039379 42
9d2fe179 43$PAGE->set_url('/mod/book/edit.php', array('cmid'=>$cmid, 'id'=>$chapterid, 'pagenum'=>$pagenum, 'subchapter'=>$subchapter));
3542c4f4 44$PAGE->set_pagelayout('admin'); // TODO: Something. This is a bloody hack!
9d2fe179 45
f4e1849e
PS
46if ($chapterid) {
47 $chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id), '*', MUST_EXIST);
c6d2eda2 48} else {
f4e1849e
PS
49 $chapter = new stdClass();
50 $chapter->id = null;
51 $chapter->subchapter = $subchapter;
52 $chapter->pagenum = $pagenum + 1;
c6d2eda2 53}
f4e1849e
PS
54$chapter->cmid = $cm->id;
55
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);
58
59$mform = new book_chapter_edit_form(null, array('chapter'=>$chapter, 'options'=>$options));
02039379 60
3542c4f4 61// If data submitted, then process and store.
c6d2eda2
PS
62if ($mform->is_cancelled()) {
63 if (empty($chapter->id)) {
64 redirect("view.php?id=$cm->id");
02039379 65 } else {
c6d2eda2 66 redirect("view.php?id=$cm->id&chapterid=$chapter->id");
02039379 67 }
c6d2eda2 68
2c1e98e6 69} else if ($data = $mform->get_data()) {
c6d2eda2
PS
70
71 if ($data->id) {
f4e1849e 72 // store the files
6b693ce8 73 $data->timemodified = time();
f4e1849e 74 $data = file_postupdate_standard_editor($data, 'content', $options, $context, 'mod_book', 'chapter', $data->id);
2c1e98e6 75 $DB->update_record('book_chapters', $data);
8e484df3 76 $DB->set_field('book', 'revision', $book->revision+1, array('id'=>$book->id));
ef2a9ece 77 $chapter = $DB->get_record('book_chapters', array('id' => $data->id));
2c1e98e6 78
ef2a9ece 79 \mod_book\event\chapter_updated::create_from_chapter($book, $context, $chapter)->trigger();
c6d2eda2 80
02039379 81 } else {
3542c4f4 82 // adding new chapter
f4e1849e
PS
83 $data->bookid = $book->id;
84 $data->hidden = 0;
85 $data->timecreated = time();
86 $data->timemodified = time();
87 $data->importsrc = '';
88 $data->content = ''; // updated later
89 $data->contentformat = FORMAT_HTML; // updated later
c6d2eda2
PS
90
91 // make room for new page
2c1e98e6 92 $sql = "UPDATE {book_chapters}
c6d2eda2 93 SET pagenum = pagenum + 1
2c1e98e6
PS
94 WHERE bookid = ? AND pagenum >= ?";
95 $DB->execute($sql, array($book->id, $data->pagenum));
96
97 $data->id = $DB->insert_record('book_chapters', $data);
c6d2eda2 98
f4e1849e
PS
99 // store the files
100 $data = file_postupdate_standard_editor($data, 'content', $options, $context, 'mod_book', 'chapter', $data->id);
101 $DB->update_record('book_chapters', $data);
0c9f1864 102 $DB->set_field('book', 'revision', $book->revision+1, array('id'=>$book->id));
ef2a9ece 103 $chapter = $DB->get_record('book_chapters', array('id' => $data->id));
f4e1849e 104
ef2a9ece 105 \mod_book\event\chapter_created::create_from_chapter($book, $context, $chapter)->trigger();
02039379
PS
106 }
107
b1a9b01e 108 book_preload_chapters($book); // fix structure
c6d2eda2 109 redirect("view.php?id=$cm->id&chapterid=$data->id");
02039379
PS
110}
111
3542c4f4 112// Otherwise fill and print the form.
5b7e511e
TT
113$PAGE->set_title($book->name);
114$PAGE->set_heading($course->fullname);
02039379 115
9d2fe179 116echo $OUTPUT->header();
d8a6d49c 117echo $OUTPUT->heading($book->name);
02039379 118
c6d2eda2 119$mform->display();
02039379 120
9d2fe179 121echo $OUTPUT->footer();