2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * Blog entry edit page
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,
44 'confirm' => $confirm,
46 'courseid' => $courseid));
48 // If action is add, we ignore $id to avoid any further problems.
49 if (!empty($id) && $action == 'add') {
53 $entry = new stdClass();
57 $entry = new blog_entry($id); // Will trigger exception if not found.
58 $userid = $entry->userid
;
63 $sitecontext = context_system
::instance();
64 $usercontext = context_user
::instance($userid);
66 $PAGE->set_context($sitecontext);
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');
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.
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.
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
);
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);
117 $output = $PAGE->get_renderer('blog');
119 $strblogs = get_string('blogs', 'blog');
121 if ($action === 'delete') {
122 // Init comment JS strings.
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');
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',
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));
157 $entry->prepare_render();
158 echo $output->render($entry);
160 echo $OUTPUT->footer();
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
) {
181 $entry->courseassoc
= $assocrec->contextid
;
184 $entry->modassoc
= $assocrec->contextid
;
191 [$summaryoptions, $attachmentoptions] = blog_get_editor_options($entry);
193 $blogeditform = new blog_edit_form(null, compact('entry',
200 $entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id
);
201 $entry = file_prepare_standard_filemanager($entry,
209 if (!empty($entry->id
)) {
210 $entry->tags
= core_tag_tag
::get_item_tags_array('core', 'post', $entry->id
);
213 $entry->action
= $action;
215 $blogeditform->set_data($entry);
217 if ($blogeditform->is_cancelled()) {
218 redirect($returnurl);
220 } else if ($data = $blogeditform->get_data()) {
224 $blogentry = new blog_entry(null, $data, $blogeditform);
226 $blogentry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
230 if (empty($entry->id
)) {
231 throw new \
moodle_exception('wrongentryid');
234 $entry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
238 throw new \
moodle_exception('invalidaction');
241 redirect($returnurl);
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.
257 $context = context_course
::instance($courseid);
258 $entry->courseassoc
= $context->id
;
261 // Pre-select the mod for associations.
263 $context = context_module
::instance($modid);
264 $entry->modassoc
= $context->id
;
270 if (empty($entry->id
)) {
271 throw new \
moodle_exception('wrongentryid');
273 $strformheading = get_string('updateentrywithid', 'blog');
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();