MDL-50576 mod_forum: Correct use of movedicussions cap when posting This is a partial...
[moodle.git] / blog / edit.php
blobebf4c969b5f15b67e4fd4a40bed58074ebab87f1
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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/>.
19 /**
20 * Blog entry edit page
22 * @package moodlecore
23 * @subpackage blog
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 if ($action == 'edit') {
38 $id = required_param('entryid', PARAM_INT);
41 $PAGE->set_url('/blog/edit.php', array('action' => $action, 'entryid' => $id, 'confirm' => $confirm, 'modid' => $modid, 'courseid' => $courseid));
43 // If action is add, we ignore $id to avoid any further problems
44 if (!empty($id) && $action == 'add') {
45 $id = null;
48 // Blogs are always in system context.
49 $sitecontext = context_system::instance();
50 $PAGE->set_context($sitecontext);
52 require_login($courseid);
54 if (empty($CFG->enableblogs)) {
55 print_error('blogdisable', 'blog');
58 if (isguestuser()) {
59 print_error('noguestentry', 'blog');
62 $returnurl = new moodle_url('/blog/index.php');
64 if (!empty($courseid) && empty($modid)) {
65 $returnurl->param('courseid', $courseid);
68 // If a modid is given, guess courseid
69 if (!empty($modid)) {
70 $returnurl->param('modid', $modid);
71 $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
72 $returnurl->param('courseid', $courseid);
75 $blogheaders = blog_get_headers();
77 if (!has_capability('moodle/blog:create', $sitecontext) && !has_capability('moodle/blog:manageentries', $sitecontext)) {
78 print_error('cannoteditentryorblog');
81 // Make sure that the person trying to edit has access right
82 if ($id) {
83 if (!$entry = new blog_entry($id)) {
84 print_error('wrongentryid', 'blog');
87 if (!blog_user_can_edit_entry($entry)) {
88 print_error('notallowedtoedit', 'blog');
90 $userid = $entry->userid;
91 $entry->subject = clean_text($entry->subject);
92 $entry->summary = clean_text($entry->summary, $entry->format);
94 } else {
95 if (!has_capability('moodle/blog:create', $sitecontext)) {
96 print_error('noentry', 'blog'); // manageentries is not enough for adding
98 $entry = new stdClass();
99 $entry->id = null;
100 $userid = $USER->id;
102 $returnurl->param('userid', $userid);
104 // Blog renderer.
105 $output = $PAGE->get_renderer('blog');
107 $strblogs = get_string('blogs','blog');
109 if ($action === 'delete'){
110 if (empty($entry->id)) {
111 print_error('wrongentryid', 'blog');
113 if (data_submitted() && $confirm && confirm_sesskey()) {
114 // Make sure the current user is the author of the blog entry, or has some deleteanyentry capability
115 if (!blog_user_can_edit_entry($entry)) {
116 print_error('nopermissionstodeleteentry', 'blog');
117 } else {
118 $entry->delete();
119 blog_rss_delete_file($userid);
120 redirect($returnurl);
122 } else if (blog_user_can_edit_entry($entry)) {
123 $optionsyes = array('entryid'=>$id, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'courseid'=>$courseid);
124 $optionsno = array('userid'=>$entry->userid, 'courseid'=>$courseid);
125 $PAGE->set_title("$SITE->shortname: $strblogs");
126 $PAGE->set_heading($SITE->fullname);
127 echo $OUTPUT->header();
129 // Output the entry.
130 $entry->prepare_render();
131 echo $output->render($entry);
133 echo '<br />';
134 echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog'), new moodle_url('edit.php', $optionsyes),new moodle_url( 'index.php', $optionsno));
135 echo $OUTPUT->footer();
136 die;
138 } else if ($action == 'add') {
139 $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('addnewentry', 'blog'));
140 $PAGE->set_heading($SITE->shortname);
141 } else if ($action == 'edit') {
142 $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('editentry', 'blog'));
143 $PAGE->set_heading($SITE->shortname);
146 if (!empty($entry->id)) {
147 if ($CFG->useblogassociations && ($blogassociations = $DB->get_records('blog_association', array('blogid' => $entry->id)))) {
149 foreach ($blogassociations as $assocrec) {
150 $context = context::instance_by_id($assocrec->contextid);
152 switch ($context->contextlevel) {
153 case CONTEXT_COURSE:
154 $entry->courseassoc = $assocrec->contextid;
155 break;
156 case CONTEXT_MODULE:
157 $entry->modassoc = $assocrec->contextid;
158 break;
164 require_once('edit_form.php');
165 $summaryoptions = array('maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>true, 'context'=>$sitecontext,
166 'subdirs'=>file_area_contains_subdirs($sitecontext, 'blog', 'post', $entry->id));
167 $attachmentoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes);
169 $blogeditform = new blog_edit_form(null, compact('entry', 'summaryoptions', 'attachmentoptions', 'sitecontext', 'courseid', 'modid'));
171 $entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id);
172 $entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $sitecontext, 'blog', 'attachment', $entry->id);
174 if (!empty($CFG->usetags) && !empty($entry->id)) {
175 include_once($CFG->dirroot.'/tag/lib.php');
176 $entry->tags = tag_get_tags_array('post', $entry->id);
179 $entry->action = $action;
180 // set defaults
181 $blogeditform->set_data($entry);
183 if ($blogeditform->is_cancelled()) {
184 redirect($returnurl);
186 } else if ($data = $blogeditform->get_data()){
188 switch ($action) {
189 case 'add':
190 $blogentry = new blog_entry(null, $data, $blogeditform);
191 $blogentry->add();
192 $blogentry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
193 break;
195 case 'edit':
196 if (empty($entry->id)) {
197 print_error('wrongentryid', 'blog');
200 $entry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
201 break;
203 default :
204 print_error('invalidaction');
207 redirect($returnurl);
211 // gui setup
212 switch ($action) {
213 case 'add':
214 // prepare new empty form
215 $entry->publishstate = 'site';
216 $strformheading = get_string('addnewentry', 'blog');
217 $entry->action = $action;
219 if ($CFG->useblogassociations) {
221 //pre-select the course for associations
222 if ($courseid) {
223 $context = context_course::instance($courseid);
224 $entry->courseassoc = $context->id;
227 //pre-select the mod for associations
228 if ($modid) {
229 $context = context_module::instance($modid);
230 $entry->modassoc = $context->id;
233 break;
235 case 'edit':
236 if (empty($entry->id)) {
237 print_error('wrongentryid', 'blog');
239 $entry->tags = tag_get_tags_array('post', $entry->id);
240 $strformheading = get_string('updateentrywithid', 'blog');
242 break;
244 default :
245 print_error('unknowaction');
248 $entry->modid = $modid;
249 $entry->courseid = $courseid;
251 echo $OUTPUT->header();
252 $blogeditform->display();
253 echo $OUTPUT->footer();
255 die;