Merge branch 'MDL-68438_master' of https://github.com/t-schroeder/moodle
[moodle.git] / blog / edit.php
blobbf9a166626910f5cd04dc27c49a6b57903026b6e
1 <?php
2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 /**
19 * Blog entry edit page
21 * @package moodlecore
22 * @subpackage blog
23 * @copyright 2009 Nicolas Connault
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once(__DIR__ . '/../config.php');
27 require_once($CFG->dirroot . '/blog/lib.php');
28 require_once($CFG->dirroot . '/blog/locallib.php');
29 require_once($CFG->dirroot . '/comment/lib.php');
30 require_once($CFG->dirroot . '/blog/edit_form.php');
32 $action = required_param('action', PARAM_ALPHA);
33 $id = optional_param('entryid', 0, PARAM_INT);
34 $confirm = optional_param('confirm', 0, PARAM_BOOL);
35 $modid = optional_param('modid', 0, PARAM_INT); // To associate the entry with a module instance.
36 $courseid = optional_param('courseid', 0, PARAM_INT); // To associate the entry with a course.
38 if ($action == 'edit') {
39 $id = required_param('entryid', PARAM_INT);
42 $PAGE->set_url('/blog/edit.php', array('action' => $action,
43 'entryid' => $id,
44 'confirm' => $confirm,
45 'modid' => $modid,
46 'courseid' => $courseid));
48 // If action is add, we ignore $id to avoid any further problems.
49 if (!empty($id) && $action == 'add') {
50 $id = null;
53 $entry = new stdClass();
54 $entry->id = null;
56 if ($id) {
57 if (!$entry = new blog_entry($id)) {
58 print_error('wrongentryid', 'blog');
60 $userid = $entry->userid;
61 } else {
62 $userid = $USER->id;
65 $sitecontext = context_system::instance();
66 $usercontext = context_user::instance($userid);
67 if ($modid) {
68 $PAGE->set_context($sitecontext);
69 } else {
70 $PAGE->set_context($usercontext);
71 $blognode = $PAGE->settingsnav->find('blogadd', null);
72 $blognode->make_active();
75 require_login($courseid);
77 if (empty($CFG->enableblogs)) {
78 print_error('blogdisable', 'blog');
81 if (isguestuser()) {
82 print_error('noguest');
85 $returnurl = new moodle_url('/blog/index.php');
87 if (!empty($courseid) && empty($modid)) {
88 $returnurl->param('courseid', $courseid);
91 // If a modid is given, guess courseid.
92 if (!empty($modid)) {
93 $returnurl->param('modid', $modid);
94 $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
95 $returnurl->param('courseid', $courseid);
98 $blogheaders = blog_get_headers();
100 if (!has_capability('moodle/blog:create', $sitecontext) && !has_capability('moodle/blog:manageentries', $sitecontext)) {
101 print_error('cannoteditentryorblog');
104 // Make sure that the person trying to edit has access right.
105 if ($id) {
106 if (!blog_user_can_edit_entry($entry)) {
107 print_error('notallowedtoedit', 'blog');
109 $entry->subject = clean_text($entry->subject);
110 $entry->summary = clean_text($entry->summary, $entry->format);
111 } else {
112 if (!has_capability('moodle/blog:create', $sitecontext)) {
113 print_error('noentry', 'blog'); // The capability "manageentries" is not enough for adding.
116 $returnurl->param('userid', $userid);
118 // Blog renderer.
119 $output = $PAGE->get_renderer('blog');
121 $strblogs = get_string('blogs', 'blog');
123 if ($action === 'delete') {
124 // Init comment JS strings.
125 comment::init();
127 if (empty($entry->id)) {
128 print_error('wrongentryid', 'blog');
130 if (data_submitted() && $confirm && confirm_sesskey()) {
131 // Make sure the current user is the author of the blog entry, or has some deleteanyentry capability.
132 if (!blog_user_can_edit_entry($entry)) {
133 print_error('nopermissionstodeleteentry', 'blog');
134 } else {
135 $entry->delete();
136 blog_rss_delete_file($userid);
137 redirect($returnurl);
139 } else if (blog_user_can_edit_entry($entry)) {
140 $optionsyes = array('entryid' => $id,
141 'action' => 'delete',
142 'confirm' => 1,
143 'sesskey' => sesskey(),
144 'courseid' => $courseid);
145 $optionsno = array('userid' => $entry->userid, 'courseid' => $courseid);
146 $PAGE->set_title("$SITE->shortname: $strblogs");
147 $PAGE->set_heading($SITE->fullname);
148 echo $OUTPUT->header();
150 // Output edit mode title.
151 echo $OUTPUT->heading($strblogs . ': ' . get_string('deleteentry', 'blog'), 2);
153 echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog', format_string($entry->subject)),
154 new moodle_url('edit.php', $optionsyes),
155 new moodle_url('index.php', $optionsno));
157 echo '<br />';
158 // Output the entry.
159 $entry->prepare_render();
160 echo $output->render($entry);
162 echo $OUTPUT->footer();
163 die;
165 } else if ($action == 'add') {
166 $editmodetitle = $strblogs . ': ' . get_string('addnewentry', 'blog');
167 $PAGE->set_title("$SITE->shortname: $editmodetitle");
168 $PAGE->set_heading(fullname($USER));
169 } else if ($action == 'edit') {
170 $editmodetitle = $strblogs . ': ' . get_string('editentry', 'blog');
171 $PAGE->set_title("$SITE->shortname: $editmodetitle");
172 $PAGE->set_heading(fullname($USER));
175 if (!empty($entry->id)) {
176 if ($CFG->useblogassociations && ($blogassociations = $DB->get_records('blog_association', array('blogid' => $entry->id)))) {
178 foreach ($blogassociations as $assocrec) {
179 $context = context::instance_by_id($assocrec->contextid);
181 switch ($context->contextlevel) {
182 case CONTEXT_COURSE:
183 $entry->courseassoc = $assocrec->contextid;
184 break;
185 case CONTEXT_MODULE:
186 $entry->modassoc = $assocrec->contextid;
187 break;
193 $summaryoptions = array('maxfiles' => 99, 'maxbytes' => $CFG->maxbytes, 'trusttext' => true, 'context' => $sitecontext,
194 'subdirs' => file_area_contains_subdirs($sitecontext, 'blog', 'post', $entry->id));
195 $attachmentoptions = array('subdirs' => false, 'maxfiles' => 99, 'maxbytes' => $CFG->maxbytes);
197 $blogeditform = new blog_edit_form(null, compact('entry',
198 'summaryoptions',
199 'attachmentoptions',
200 'sitecontext',
201 'courseid',
202 'modid'));
204 $entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id);
205 $entry = file_prepare_standard_filemanager($entry,
206 'attachment',
207 $attachmentoptions,
208 $sitecontext,
209 'blog',
210 'attachment',
211 $entry->id);
213 if (!empty($entry->id)) {
214 $entry->tags = core_tag_tag::get_item_tags_array('core', 'post', $entry->id);
217 $entry->action = $action;
218 // Set defaults.
219 $blogeditform->set_data($entry);
221 if ($blogeditform->is_cancelled()) {
222 redirect($returnurl);
224 } else if ($data = $blogeditform->get_data()) {
226 switch ($action) {
227 case 'add':
228 $blogentry = new blog_entry(null, $data, $blogeditform);
229 $blogentry->add();
230 $blogentry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
231 break;
233 case 'edit':
234 if (empty($entry->id)) {
235 print_error('wrongentryid', 'blog');
238 $entry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
239 break;
241 default :
242 print_error('invalidaction');
245 redirect($returnurl);
249 // GUI setup.
250 switch ($action) {
251 case 'add':
252 // Prepare new empty form.
253 $entry->publishstate = 'site';
254 $strformheading = get_string('addnewentry', 'blog');
255 $entry->action = $action;
257 if ($CFG->useblogassociations) {
259 // Pre-select the course for associations.
260 if ($courseid) {
261 $context = context_course::instance($courseid);
262 $entry->courseassoc = $context->id;
265 // Pre-select the mod for associations.
266 if ($modid) {
267 $context = context_module::instance($modid);
268 $entry->modassoc = $context->id;
271 break;
273 case 'edit':
274 if (empty($entry->id)) {
275 print_error('wrongentryid', 'blog');
277 $strformheading = get_string('updateentrywithid', 'blog');
279 break;
281 default :
282 print_error('unknowaction');
285 $entry->modid = $modid;
286 $entry->courseid = $courseid;
288 echo $OUTPUT->header();
289 // Output title for editing mode.
290 if (isset($editmodetitle)) {
291 echo $OUTPUT->heading($editmodetitle, 2);
293 $blogeditform->display();
294 echo $OUTPUT->footer();
296 die;