Merge branch 'MDL-81713-main' of https://github.com/junpataleta/moodle
[moodle.git] / blog / edit.php
blob920d75f8a66c0bd69837eebb24ab66870298d949
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 $entry = new blog_entry($id); // Will trigger exception if not found.
58 $userid = $entry->userid;
59 } else {
60 $userid = $USER->id;
63 $sitecontext = context_system::instance();
64 $usercontext = context_user::instance($userid);
65 if ($modid) {
66 $PAGE->set_context($sitecontext);
67 } else {
68 $PAGE->set_context($usercontext);
69 $blognode = $PAGE->settingsnav->find('blogadd', null);
70 $blognode->make_active();
73 require_login($courseid);
75 if (empty($CFG->enableblogs)) {
76 throw new \moodle_exception('blogdisable', 'blog');
79 if (isguestuser()) {
80 throw new \moodle_exception('noguest');
83 $returnurl = new moodle_url('/blog/index.php');
85 if (!empty($courseid) && empty($modid)) {
86 $returnurl->param('courseid', $courseid);
89 // If a modid is given, guess courseid.
90 if (!empty($modid)) {
91 $returnurl->param('modid', $modid);
92 $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
93 $returnurl->param('courseid', $courseid);
96 $blogheaders = blog_get_headers();
98 if (!has_capability('moodle/blog:create', $sitecontext) && !has_capability('moodle/blog:manageentries', $sitecontext)) {
99 throw new \moodle_exception('cannoteditentryorblog', 'blog');
102 // Make sure that the person trying to edit has access right.
103 if ($id) {
104 if (!blog_user_can_edit_entry($entry)) {
105 throw new \moodle_exception('notallowedtoedit', 'blog');
107 $entry->subject = clean_text($entry->subject);
108 $entry->summary = clean_text($entry->summary, $entry->format);
109 } else {
110 if (!has_capability('moodle/blog:create', $sitecontext)) {
111 throw new \moodle_exception('noentry', 'blog'); // The capability "manageentries" is not enough for adding.
114 $returnurl->param('userid', $userid);
116 // Blog renderer.
117 $output = $PAGE->get_renderer('blog');
119 $strblogs = get_string('blogs', 'blog');
121 if ($action === 'delete') {
122 // Init comment JS strings.
123 comment::init();
125 if (empty($entry->id)) {
126 throw new \moodle_exception('wrongentryid');
128 if (data_submitted() && $confirm && confirm_sesskey()) {
129 // Make sure the current user is the author of the blog entry, or has some deleteanyentry capability.
130 if (!blog_user_can_edit_entry($entry)) {
131 throw new \moodle_exception('nopermissionstodeleteentry', 'blog');
132 } else {
133 $entry->delete();
134 blog_rss_delete_file($userid);
135 redirect($returnurl);
137 } else if (blog_user_can_edit_entry($entry)) {
138 $optionsyes = array('entryid' => $id,
139 'action' => 'delete',
140 'confirm' => 1,
141 'sesskey' => sesskey(),
142 'courseid' => $courseid);
143 $optionsno = array('userid' => $entry->userid, 'courseid' => $courseid);
144 $PAGE->set_title($strblogs);
145 $PAGE->set_heading($SITE->fullname);
146 echo $OUTPUT->header();
148 // Output edit mode title.
149 echo $OUTPUT->heading($strblogs . ': ' . get_string('deleteentry', 'blog'), 2);
151 echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog', format_string($entry->subject)),
152 new moodle_url('edit.php', $optionsyes),
153 new moodle_url('index.php', $optionsno));
155 echo '<br />';
156 // Output the entry.
157 $entry->prepare_render();
158 echo $output->render($entry);
160 echo $OUTPUT->footer();
161 die;
163 } else if ($action == 'add') {
164 $editmodetitle = $strblogs . ': ' . get_string('addnewentry', 'blog');
165 $PAGE->set_title($editmodetitle);
166 $PAGE->set_heading(fullname($USER));
167 } else if ($action == 'edit') {
168 $editmodetitle = $strblogs . ': ' . get_string('editentry', 'blog');
169 $PAGE->set_title($editmodetitle);
170 $PAGE->set_heading(fullname($USER));
173 if (!empty($entry->id)) {
174 if ($CFG->useblogassociations && ($blogassociations = $DB->get_records('blog_association', array('blogid' => $entry->id)))) {
176 foreach ($blogassociations as $assocrec) {
177 $context = context::instance_by_id($assocrec->contextid);
179 switch ($context->contextlevel) {
180 case CONTEXT_COURSE:
181 $entry->courseassoc = $assocrec->contextid;
182 break;
183 case CONTEXT_MODULE:
184 $entry->modassoc = $assocrec->contextid;
185 break;
191 [$summaryoptions, $attachmentoptions] = blog_get_editor_options($entry);
193 $blogeditform = new blog_edit_form(null, compact('entry',
194 'summaryoptions',
195 'attachmentoptions',
196 'sitecontext',
197 'courseid',
198 'modid'));
200 $entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id);
201 $entry = file_prepare_standard_filemanager($entry,
202 'attachment',
203 $attachmentoptions,
204 $sitecontext,
205 'blog',
206 'attachment',
207 $entry->id);
209 if (!empty($entry->id)) {
210 $entry->tags = core_tag_tag::get_item_tags_array('core', 'post', $entry->id);
213 $entry->action = $action;
214 // Set defaults.
215 $blogeditform->set_data($entry);
217 if ($blogeditform->is_cancelled()) {
218 redirect($returnurl);
220 } else if ($data = $blogeditform->get_data()) {
222 switch ($action) {
223 case 'add':
224 $blogentry = new blog_entry(null, $data, $blogeditform);
225 $blogentry->add();
226 $blogentry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
227 break;
229 case 'edit':
230 if (empty($entry->id)) {
231 throw new \moodle_exception('wrongentryid');
234 $entry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
235 break;
237 default :
238 throw new \moodle_exception('invalidaction');
241 redirect($returnurl);
245 // GUI setup.
246 switch ($action) {
247 case 'add':
248 // Prepare new empty form.
249 $entry->publishstate = 'site';
250 $strformheading = get_string('addnewentry', 'blog');
251 $entry->action = $action;
253 if ($CFG->useblogassociations) {
255 // Pre-select the course for associations.
256 if ($courseid) {
257 $context = context_course::instance($courseid);
258 $entry->courseassoc = $context->id;
261 // Pre-select the mod for associations.
262 if ($modid) {
263 $context = context_module::instance($modid);
264 $entry->modassoc = $context->id;
267 break;
269 case 'edit':
270 if (empty($entry->id)) {
271 throw new \moodle_exception('wrongentryid');
273 $strformheading = get_string('updateentrywithid', 'blog');
275 break;
277 default :
278 throw new \moodle_exception('unknowaction');
281 $entry->modid = $modid;
282 $entry->courseid = $courseid;
284 echo $OUTPUT->header();
285 // Output title for editing mode.
286 if (isset($editmodetitle)) {
287 echo $OUTPUT->heading($editmodetitle, 2);
289 $blogeditform->display();
290 echo $OUTPUT->footer();
292 die;