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(dirname(dirname(__FILE__
)).'/config.php');
27 require_once('lib.php');
28 require_once('locallib.php');
30 $action = required_param('action', PARAM_ALPHA
);
31 $id = optional_param('entryid', 0, PARAM_INT
);
32 $confirm = optional_param('confirm', 0, PARAM_BOOL
);
33 $modid = optional_param('modid', 0, PARAM_INT
); // To associate the entry with a module instance.
34 $courseid = optional_param('courseid', 0, PARAM_INT
); // To associate the entry with a course.
36 if ($action == 'edit') {
37 $id = required_param('entryid', PARAM_INT
);
40 $PAGE->set_url('/blog/edit.php', array('action' => $action,
42 'confirm' => $confirm,
44 'courseid' => $courseid));
46 // If action is add, we ignore $id to avoid any further problems.
47 if (!empty($id) && $action == 'add') {
51 $entry = new stdClass();
55 if (!$entry = new blog_entry($id)) {
56 print_error('wrongentryid', 'blog');
58 $userid = $entry->userid
;
63 $sitecontext = context_system
::instance();
64 $usercontext = context_user
::instance($userid);
65 $PAGE->set_context($usercontext);
66 $blognode = $PAGE->settingsnav
->find('blogadd', null);
67 $blognode->make_active();
69 require_login($courseid);
71 if (empty($CFG->enableblogs
)) {
72 print_error('blogdisable', 'blog');
76 print_error('noguestentry', 'blog');
79 $returnurl = new moodle_url('/blog/index.php');
81 if (!empty($courseid) && empty($modid)) {
82 $returnurl->param('courseid', $courseid);
85 // If a modid is given, guess courseid.
87 $returnurl->param('modid', $modid);
88 $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
89 $returnurl->param('courseid', $courseid);
92 $blogheaders = blog_get_headers();
94 if (!has_capability('moodle/blog:create', $sitecontext) && !has_capability('moodle/blog:manageentries', $sitecontext)) {
95 print_error('cannoteditentryorblog');
98 // Make sure that the person trying to edit has access right.
100 if (!blog_user_can_edit_entry($entry)) {
101 print_error('notallowedtoedit', 'blog');
103 $entry->subject
= clean_text($entry->subject
);
104 $entry->summary
= clean_text($entry->summary
, $entry->format
);
106 if (!has_capability('moodle/blog:create', $sitecontext)) {
107 print_error('noentry', 'blog'); // The capability "manageentries" is not enough for adding.
110 $returnurl->param('userid', $userid);
113 $output = $PAGE->get_renderer('blog');
115 $strblogs = get_string('blogs', 'blog');
117 if ($action === 'delete') {
118 if (empty($entry->id
)) {
119 print_error('wrongentryid', 'blog');
121 if (data_submitted() && $confirm && confirm_sesskey()) {
122 // Make sure the current user is the author of the blog entry, or has some deleteanyentry capability.
123 if (!blog_user_can_edit_entry($entry)) {
124 print_error('nopermissionstodeleteentry', 'blog');
127 blog_rss_delete_file($userid);
128 redirect($returnurl);
130 } else if (blog_user_can_edit_entry($entry)) {
131 $optionsyes = array('entryid'=>$id, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'courseid'=>$courseid);
132 $optionsno = array('userid'=>$entry->userid
, 'courseid'=>$courseid);
133 $PAGE->set_title("$SITE->shortname: $strblogs");
134 $PAGE->set_heading($SITE->fullname
);
135 echo $OUTPUT->header();
137 // Output edit mode title.
138 echo $OUTPUT->heading($strblogs . ': ' . get_string('deleteentry', 'blog'), 2);
141 $entry->prepare_render();
142 echo $output->render($entry);
145 echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog'),
146 new moodle_url('edit.php', $optionsyes),
147 new moodle_url('index.php', $optionsno));
148 echo $OUTPUT->footer();
151 } else if ($action == 'add') {
152 $editmodetitle = $strblogs . ': ' . get_string('addnewentry', 'blog');
153 $PAGE->set_title("$SITE->shortname: $editmodetitle");
154 $PAGE->set_heading(fullname($USER));
155 } else if ($action == 'edit') {
156 $editmodetitle = $strblogs . ': ' . get_string('editentry', 'blog');
157 $PAGE->set_title("$SITE->shortname: $editmodetitle");
158 $PAGE->set_heading(fullname($USER));
161 if (!empty($entry->id
)) {
162 if ($CFG->useblogassociations
&& ($blogassociations = $DB->get_records('blog_association', array('blogid' => $entry->id
)))) {
164 foreach ($blogassociations as $assocrec) {
165 $context = context
::instance_by_id($assocrec->contextid
);
167 switch ($context->contextlevel
) {
169 $entry->courseassoc
= $assocrec->contextid
;
172 $entry->modassoc
= $assocrec->contextid
;
179 require_once('edit_form.php');
180 $summaryoptions = array('maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes
, 'trusttext'=>true, 'context'=>$sitecontext,
181 'subdirs'=>file_area_contains_subdirs($sitecontext, 'blog', 'post', $entry->id
));
182 $attachmentoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes
);
184 $blogeditform = new blog_edit_form(null, compact('entry',
191 $entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id
);
192 $entry = file_prepare_standard_filemanager($entry,
200 if (!empty($CFG->usetags
) && !empty($entry->id
)) {
201 include_once($CFG->dirroot
.'/tag/lib.php');
202 $entry->tags
= tag_get_tags_array('post', $entry->id
);
205 $entry->action
= $action;
207 $blogeditform->set_data($entry);
209 if ($blogeditform->is_cancelled()) {
210 redirect($returnurl);
212 } else if ($data = $blogeditform->get_data()) {
216 $blogentry = new blog_entry(null, $data, $blogeditform);
218 $blogentry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
222 if (empty($entry->id
)) {
223 print_error('wrongentryid', 'blog');
226 $entry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
230 print_error('invalidaction');
233 redirect($returnurl);
240 // Prepare new empty form.
241 $entry->publishstate
= 'site';
242 $strformheading = get_string('addnewentry', 'blog');
243 $entry->action
= $action;
245 if ($CFG->useblogassociations
) {
247 // Pre-select the course for associations.
249 $context = context_course
::instance($courseid);
250 $entry->courseassoc
= $context->id
;
253 // Pre-select the mod for associations.
255 $context = context_module
::instance($modid);
256 $entry->modassoc
= $context->id
;
262 if (empty($entry->id
)) {
263 print_error('wrongentryid', 'blog');
265 $entry->tags
= tag_get_tags_array('post', $entry->id
);
266 $strformheading = get_string('updateentrywithid', 'blog');
271 print_error('unknowaction');
274 $entry->modid
= $modid;
275 $entry->courseid
= $courseid;
277 echo $OUTPUT->header();
278 // Output title for editing mode.
279 if (isset($editmodetitle)) {
280 echo $OUTPUT->heading($editmodetitle, 2);
282 $blogeditform->display();
283 echo $OUTPUT->footer();