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');
29 require_once($CFG->dirroot
.'/comment/lib.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,
43 'confirm' => $confirm,
45 'courseid' => $courseid));
47 // If action is add, we ignore $id to avoid any further problems.
48 if (!empty($id) && $action == 'add') {
52 $entry = new stdClass();
56 if (!$entry = new blog_entry($id)) {
57 print_error('wrongentryid', 'blog');
59 $userid = $entry->userid
;
64 $sitecontext = context_system
::instance();
65 $usercontext = context_user
::instance($userid);
66 $PAGE->set_context($usercontext);
67 $blognode = $PAGE->settingsnav
->find('blogadd', null);
68 $blognode->make_active();
70 require_login($courseid);
72 if (empty($CFG->enableblogs
)) {
73 print_error('blogdisable', 'blog');
77 print_error('noguestentry', 'blog');
80 $returnurl = new moodle_url('/blog/index.php');
82 if (!empty($courseid) && empty($modid)) {
83 $returnurl->param('courseid', $courseid);
86 // If a modid is given, guess courseid.
88 $returnurl->param('modid', $modid);
89 $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
90 $returnurl->param('courseid', $courseid);
93 $blogheaders = blog_get_headers();
95 if (!has_capability('moodle/blog:create', $sitecontext) && !has_capability('moodle/blog:manageentries', $sitecontext)) {
96 print_error('cannoteditentryorblog');
99 // Make sure that the person trying to edit has access right.
101 if (!blog_user_can_edit_entry($entry)) {
102 print_error('notallowedtoedit', 'blog');
104 $entry->subject
= clean_text($entry->subject
);
105 $entry->summary
= clean_text($entry->summary
, $entry->format
);
107 if (!has_capability('moodle/blog:create', $sitecontext)) {
108 print_error('noentry', 'blog'); // The capability "manageentries" is not enough for adding.
111 $returnurl->param('userid', $userid);
114 $output = $PAGE->get_renderer('blog');
116 $strblogs = get_string('blogs', 'blog');
118 if ($action === 'delete') {
119 // Init comment JS strings.
122 if (empty($entry->id
)) {
123 print_error('wrongentryid', 'blog');
125 if (data_submitted() && $confirm && confirm_sesskey()) {
126 // Make sure the current user is the author of the blog entry, or has some deleteanyentry capability.
127 if (!blog_user_can_edit_entry($entry)) {
128 print_error('nopermissionstodeleteentry', 'blog');
131 blog_rss_delete_file($userid);
132 redirect($returnurl);
134 } else if (blog_user_can_edit_entry($entry)) {
135 $optionsyes = array('entryid'=>$id, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'courseid'=>$courseid);
136 $optionsno = array('userid'=>$entry->userid
, 'courseid'=>$courseid);
137 $PAGE->set_title("$SITE->shortname: $strblogs");
138 $PAGE->set_heading($SITE->fullname
);
139 echo $OUTPUT->header();
141 echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog'),
142 new moodle_url('edit.php', $optionsyes),
143 new moodle_url('index.php', $optionsno));
147 $entry->prepare_render();
148 echo $output->render($entry);
150 echo $OUTPUT->footer();
153 } else if ($action == 'add') {
154 $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('addnewentry', 'blog'));
155 $PAGE->set_heading(fullname($USER));
156 } else if ($action == 'edit') {
157 $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('editentry', 'blog'));
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 $blogeditform->display();
279 echo $OUTPUT->footer();