3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
20 * Blog entry edit page
24 * @copyright 2009 Nicolas Connault
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once(dirname(dirname(__FILE__)).'/config.php');
28 include_once('lib.php');
29 include_once('locallib.php');
31 $action = required_param('action', PARAM_ALPHA);
32 $id = optional_param('entryid', 0, PARAM_INT);
33 $confirm = optional_param('confirm', 0, PARAM_BOOL);
34 $modid = optional_param('modid', 0, PARAM_INT); // To associate the entry with a module instance
35 $courseid = optional_param('courseid', 0, PARAM_INT); // To associate the entry with a course
37 $PAGE->set_url('/blog/edit.php', array('action' => $action, 'entryid' => $id, 'confirm' => $confirm, 'modid' => $modid, 'courseid' => $courseid));
39 // If action is add, we ignore $id to avoid any further problems
40 if (!empty($id) && $action == 'add') {
44 $returnurl = new moodle_url('/blog/index.php');
46 if (!empty($courseid) && empty($modid)) {
47 $returnurl->param('courseid', $courseid);
48 $PAGE->set_context(get_context_instance(CONTEXT_COURSE, $courseid));
51 // If a modid is given, guess courseid
53 $returnurl->param('modid', $modid);
54 $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
55 $returnurl->param('courseid', $courseid);
56 $PAGE->set_context(get_context_instance(CONTEXT_MODULE, $modid));
59 $blogheaders = blog_get_headers();
61 require_login($courseid);
63 if ($action == 'edit') {
64 $id = required_param('entryid', PARAM_INT);
67 if (empty($CFG->bloglevel)) {
68 print_error('blogdisable', 'blog');
72 print_error('noguestentry', 'blog');
75 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
76 if (!has_capability('moodle/blog:create', $sitecontext) && !has_capability('moodle/blog:manageentries', $sitecontext)) {
77 print_error('cannoteditentryorblog');
80 // Make sure that the person trying to edit has access right
82 if (!$entry = new blog_entry($id)) {
83 print_error('wrongentryid', 'blog');
86 if (!blog_user_can_edit_entry($entry)) {
87 print_error('notallowedtoedit', 'blog');
89 $userid = $entry->userid;
90 $entry->subject = clean_text($entry->subject);
91 $entry->summary = clean_text($entry->summary, $entry->format);
94 if (!has_capability('moodle/blog:create', $sitecontext)) {
95 print_error('noentry', 'blog'); // manageentries is not enough for adding
97 $entry = new stdClass();
101 $returnurl->param('userid', $userid);
103 $strblogs = get_string('blogs','blog');
105 if ($action === 'delete'){
106 if (empty($entry->id)) {
107 print_error('wrongentryid', 'blog');
109 if (data_submitted() && $confirm && confirm_sesskey()) {
110 // Make sure the current user is the author of the blog entry, or has some deleteanyentry capability
111 if (!blog_user_can_edit_entry($entry)) {
112 print_error('nopermissionstodeleteentry', 'blog');
115 redirect($returnurl);
117 } else if (blog_user_can_edit_entry($entry)) {
118 $optionsyes = array('entryid'=>$id, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'courseid'=>$courseid);
119 $optionsno = array('userid'=>$entry->userid, 'courseid'=>$courseid);
120 $PAGE->set_title("$SITE->shortname: $strblogs");
121 $PAGE->set_heading($SITE->fullname);
122 echo $OUTPUT->header();
123 $entry->print_html();
125 echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog'), new moodle_url('edit.php', $optionsyes),new moodle_url( 'index.php', $optionsno));
126 echo $OUTPUT->footer();
129 } else if ($action == 'add') {
130 $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('addnewentry', 'blog'));
131 $PAGE->set_heading($SITE->shortname);
132 } else if ($action == 'edit') {
133 $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('editentry', 'blog'));
134 $PAGE->set_heading($SITE->shortname);
137 if (!empty($entry->id)) {
138 if ($CFG->useblogassociations && ($blogassociations = $DB->get_records('blog_association', array('blogid' => $entry->id)))) {
140 foreach ($blogassociations as $assocrec) {
141 $contextrec = $DB->get_record('context', array('id' => $assocrec->contextid));
143 switch ($contextrec->contextlevel) {
145 $entry->courseassoc = $assocrec->contextid;
148 $entry->modassoc = $assocrec->contextid;
155 require_once('edit_form.php');
156 $summaryoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>true, 'context'=>$sitecontext);
157 $attachmentoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes);
159 $blogeditform = new blog_edit_form(null, compact('entry', 'summaryoptions', 'attachmentoptions', 'sitecontext', 'courseid', 'modid'));
161 $entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog_post', $entry->id);
162 $entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $sitecontext, 'blog_attachment', $entry->id);
164 if (!empty($CFG->usetags) && !empty($entry->id)) {
165 include_once($CFG->dirroot.'/tag/lib.php');
166 $entry->tags = tag_get_tags_array('post', $entry->id);
169 $entry->action = $action;
171 $blogeditform->set_data($entry);
173 if ($blogeditform->is_cancelled()) {
174 redirect($returnurl);
176 } else if ($data = $blogeditform->get_data()){
180 $blogentry = new blog_entry(null, $data, $blogeditform);
182 $blogentry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
186 if (empty($entry->id)) {
187 print_error('wrongentryid', 'blog');
190 $entry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
194 print_error('invalidaction');
197 redirect($returnurl);
204 // prepare new empty form
205 $entry->publishstate = 'site';
206 $strformheading = get_string('addnewentry', 'blog');
207 $entry->action = $action;
209 if ($CFG->useblogassociations) {
211 //pre-select the course for associations
213 $context = get_context_instance(CONTEXT_COURSE, $courseid);
214 $entry->courseassoc = $context->id;
217 //pre-select the mod for associations
219 $context = get_context_instance(CONTEXT_MODULE, $modid);
220 $entry->modassoc = $context->id;
226 if (empty($entry->id)) {
227 print_error('wrongentryid', 'blog');
229 $entry->tags = tag_get_tags_array('post', $entry->id);
230 $strformheading = get_string('updateentrywithid', 'blog');
235 print_error('unknowaction');
238 $entry->modid = $modid;
239 $entry->courseid = $courseid;
241 echo $OUTPUT->header();
242 $blogeditform->display();
243 echo $OUTPUT->footer();