3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
20 * Blog entry edit page
24 * @copyright 2009 Nicolas Connault
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once(dirname(dirname(__FILE__
)).'/config.php');
28 include_once('lib.php');
29 include_once('locallib.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 $PAGE->set_url('/blog/edit.php', array('action' => $action, 'entryid' => $id, 'confirm' => $confirm, 'modid' => $modid, 'courseid' => $courseid));
39 // If action is add, we ignore $id to avoid any further problems
40 if (!empty($id) && $action == 'add') {
44 $returnurl = new moodle_url('/blog/index.php');
46 if (!empty($courseid) && empty($modid)) {
47 $returnurl->param('courseid', $courseid);
50 // If a modid is given, guess courseid
52 $returnurl->param('modid', $modid);
53 $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
54 $returnurl->param('courseid', $courseid);
57 // Blogs are always in system context.
58 $sitecontext = context_system
::instance();
59 $PAGE->set_context($sitecontext);
62 $blogheaders = blog_get_headers();
64 require_login($courseid);
66 if ($action == 'edit') {
67 $id = required_param('entryid', PARAM_INT
);
70 if (empty($CFG->enableblogs
)) {
71 print_error('blogdisable', 'blog');
75 print_error('noguestentry', 'blog');
78 if (!has_capability('moodle/blog:create', $sitecontext) && !has_capability('moodle/blog:manageentries', $sitecontext)) {
79 print_error('cannoteditentryorblog');
82 // Make sure that the person trying to edit has access right
84 if (!$entry = new blog_entry($id)) {
85 print_error('wrongentryid', 'blog');
88 if (!blog_user_can_edit_entry($entry)) {
89 print_error('notallowedtoedit', 'blog');
91 $userid = $entry->userid
;
92 $entry->subject
= clean_text($entry->subject
);
93 $entry->summary
= clean_text($entry->summary
, $entry->format
);
96 if (!has_capability('moodle/blog:create', $sitecontext)) {
97 print_error('noentry', 'blog'); // manageentries is not enough for adding
99 $entry = new stdClass();
103 $returnurl->param('userid', $userid);
106 $output = $PAGE->get_renderer('blog');
108 $strblogs = get_string('blogs','blog');
110 if ($action === 'delete'){
111 if (empty($entry->id
)) {
112 print_error('wrongentryid', 'blog');
114 if (data_submitted() && $confirm && confirm_sesskey()) {
115 // Make sure the current user is the author of the blog entry, or has some deleteanyentry capability
116 if (!blog_user_can_edit_entry($entry)) {
117 print_error('nopermissionstodeleteentry', 'blog');
120 redirect($returnurl);
122 } else if (blog_user_can_edit_entry($entry)) {
123 $optionsyes = array('entryid'=>$id, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'courseid'=>$courseid);
124 $optionsno = array('userid'=>$entry->userid
, 'courseid'=>$courseid);
125 $PAGE->set_title("$SITE->shortname: $strblogs");
126 $PAGE->set_heading($SITE->fullname
);
127 echo $OUTPUT->header();
130 $entry->prepare_render();
131 echo $output->render($entry);
134 echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog'), new moodle_url('edit.php', $optionsyes),new moodle_url( 'index.php', $optionsno));
135 echo $OUTPUT->footer();
138 } else if ($action == 'add') {
139 $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('addnewentry', 'blog'));
140 $PAGE->set_heading($SITE->shortname
);
141 } else if ($action == 'edit') {
142 $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('editentry', 'blog'));
143 $PAGE->set_heading($SITE->shortname
);
146 if (!empty($entry->id
)) {
147 if ($CFG->useblogassociations
&& ($blogassociations = $DB->get_records('blog_association', array('blogid' => $entry->id
)))) {
149 foreach ($blogassociations as $assocrec) {
150 $context = context
::instance_by_id($assocrec->contextid
);
152 switch ($context->contextlevel
) {
154 $entry->courseassoc
= $assocrec->contextid
;
157 $entry->modassoc
= $assocrec->contextid
;
164 require_once('edit_form.php');
165 $summaryoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes
, 'trusttext'=>true, 'context'=>$sitecontext);
166 $attachmentoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes
);
168 $blogeditform = new blog_edit_form(null, compact('entry', 'summaryoptions', 'attachmentoptions', 'sitecontext', 'courseid', 'modid'));
170 $entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id
);
171 $entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $sitecontext, 'blog', 'attachment', $entry->id
);
173 if (!empty($CFG->usetags
) && !empty($entry->id
)) {
174 include_once($CFG->dirroot
.'/tag/lib.php');
175 $entry->tags
= tag_get_tags_array('post', $entry->id
);
178 $entry->action
= $action;
180 $blogeditform->set_data($entry);
182 if ($blogeditform->is_cancelled()) {
183 redirect($returnurl);
185 } else if ($data = $blogeditform->get_data()){
189 $blogentry = new blog_entry(null, $data, $blogeditform);
191 $blogentry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
195 if (empty($entry->id
)) {
196 print_error('wrongentryid', 'blog');
199 $entry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
203 print_error('invalidaction');
206 redirect($returnurl);
213 // prepare new empty form
214 $entry->publishstate
= 'site';
215 $strformheading = get_string('addnewentry', 'blog');
216 $entry->action
= $action;
218 if ($CFG->useblogassociations
) {
220 //pre-select the course for associations
222 $context = context_course
::instance($courseid);
223 $entry->courseassoc
= $context->id
;
226 //pre-select the mod for associations
228 $context = context_module
::instance($modid);
229 $entry->modassoc
= $context->id
;
235 if (empty($entry->id
)) {
236 print_error('wrongentryid', 'blog');
238 $entry->tags
= tag_get_tags_array('post', $entry->id
);
239 $strformheading = get_string('updateentrywithid', 'blog');
244 print_error('unknowaction');
247 $entry->modid
= $modid;
248 $entry->courseid
= $courseid;
250 echo $OUTPUT->header();
251 $blogeditform->display();
252 echo $OUTPUT->footer();