weekly release 3.0.4+
[moodle.git] / blog / edit.php
blob27c412fe89e2e1f153b1e0f327d720ea59310c1d
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(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,
42 'entryid' => $id,
43 'confirm' => $confirm,
44 'modid' => $modid,
45 'courseid' => $courseid));
47 // If action is add, we ignore $id to avoid any further problems.
48 if (!empty($id) && $action == 'add') {
49 $id = null;
52 $entry = new stdClass();
53 $entry->id = null;
55 if ($id) {
56 if (!$entry = new blog_entry($id)) {
57 print_error('wrongentryid', 'blog');
59 $userid = $entry->userid;
60 } else {
61 $userid = $USER->id;
64 $sitecontext = context_system::instance();
65 $usercontext = context_user::instance($userid);
66 if ($modid) {
67 $PAGE->set_context($sitecontext);
68 } else {
69 $PAGE->set_context($usercontext);
70 $blognode = $PAGE->settingsnav->find('blogadd', null);
71 $blognode->make_active();
74 require_login($courseid);
76 if (empty($CFG->enableblogs)) {
77 print_error('blogdisable', 'blog');
80 if (isguestuser()) {
81 print_error('noguestentry', 'blog');
84 $returnurl = new moodle_url('/blog/index.php');
86 if (!empty($courseid) && empty($modid)) {
87 $returnurl->param('courseid', $courseid);
90 // If a modid is given, guess courseid.
91 if (!empty($modid)) {
92 $returnurl->param('modid', $modid);
93 $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
94 $returnurl->param('courseid', $courseid);
97 $blogheaders = blog_get_headers();
99 if (!has_capability('moodle/blog:create', $sitecontext) && !has_capability('moodle/blog:manageentries', $sitecontext)) {
100 print_error('cannoteditentryorblog');
103 // Make sure that the person trying to edit has access right.
104 if ($id) {
105 if (!blog_user_can_edit_entry($entry)) {
106 print_error('notallowedtoedit', 'blog');
108 $entry->subject = clean_text($entry->subject);
109 $entry->summary = clean_text($entry->summary, $entry->format);
110 } else {
111 if (!has_capability('moodle/blog:create', $sitecontext)) {
112 print_error('noentry', 'blog'); // The capability "manageentries" is not enough for adding.
115 $returnurl->param('userid', $userid);
117 // Blog renderer.
118 $output = $PAGE->get_renderer('blog');
120 $strblogs = get_string('blogs', 'blog');
122 if ($action === 'delete') {
123 // Init comment JS strings.
124 comment::init();
126 if (empty($entry->id)) {
127 print_error('wrongentryid', 'blog');
129 if (data_submitted() && $confirm && confirm_sesskey()) {
130 // Make sure the current user is the author of the blog entry, or has some deleteanyentry capability.
131 if (!blog_user_can_edit_entry($entry)) {
132 print_error('nopermissionstodeleteentry', 'blog');
133 } else {
134 $entry->delete();
135 blog_rss_delete_file($userid);
136 redirect($returnurl);
138 } else if (blog_user_can_edit_entry($entry)) {
139 $optionsyes = array('entryid'=>$id, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'courseid'=>$courseid);
140 $optionsno = array('userid'=>$entry->userid, 'courseid'=>$courseid);
141 $PAGE->set_title("$SITE->shortname: $strblogs");
142 $PAGE->set_heading($SITE->fullname);
143 echo $OUTPUT->header();
145 // Output edit mode title.
146 echo $OUTPUT->heading($strblogs . ': ' . get_string('deleteentry', 'blog'), 2);
148 echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog'),
149 new moodle_url('edit.php', $optionsyes),
150 new moodle_url('index.php', $optionsno));
152 echo '<br />';
153 // Output the entry.
154 $entry->prepare_render();
155 echo $output->render($entry);
157 echo $OUTPUT->footer();
158 die;
160 } else if ($action == 'add') {
161 $editmodetitle = $strblogs . ': ' . get_string('addnewentry', 'blog');
162 $PAGE->set_title("$SITE->shortname: $editmodetitle");
163 $PAGE->set_heading(fullname($USER));
164 } else if ($action == 'edit') {
165 $editmodetitle = $strblogs . ': ' . get_string('editentry', 'blog');
166 $PAGE->set_title("$SITE->shortname: $editmodetitle");
167 $PAGE->set_heading(fullname($USER));
170 if (!empty($entry->id)) {
171 if ($CFG->useblogassociations && ($blogassociations = $DB->get_records('blog_association', array('blogid' => $entry->id)))) {
173 foreach ($blogassociations as $assocrec) {
174 $context = context::instance_by_id($assocrec->contextid);
176 switch ($context->contextlevel) {
177 case CONTEXT_COURSE:
178 $entry->courseassoc = $assocrec->contextid;
179 break;
180 case CONTEXT_MODULE:
181 $entry->modassoc = $assocrec->contextid;
182 break;
188 require_once('edit_form.php');
189 $summaryoptions = array('maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>true, 'context'=>$sitecontext,
190 'subdirs'=>file_area_contains_subdirs($sitecontext, 'blog', 'post', $entry->id));
191 $attachmentoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes);
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($CFG->usetags) && !empty($entry->id)) {
210 include_once($CFG->dirroot.'/tag/lib.php');
211 $entry->tags = tag_get_tags_array('post', $entry->id);
214 $entry->action = $action;
215 // Set defaults.
216 $blogeditform->set_data($entry);
218 if ($blogeditform->is_cancelled()) {
219 redirect($returnurl);
221 } else if ($data = $blogeditform->get_data()) {
223 switch ($action) {
224 case 'add':
225 $blogentry = new blog_entry(null, $data, $blogeditform);
226 $blogentry->add();
227 $blogentry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
228 break;
230 case 'edit':
231 if (empty($entry->id)) {
232 print_error('wrongentryid', 'blog');
235 $entry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
236 break;
238 default :
239 print_error('invalidaction');
242 redirect($returnurl);
246 // GUI setup.
247 switch ($action) {
248 case 'add':
249 // Prepare new empty form.
250 $entry->publishstate = 'site';
251 $strformheading = get_string('addnewentry', 'blog');
252 $entry->action = $action;
254 if ($CFG->useblogassociations) {
256 // Pre-select the course for associations.
257 if ($courseid) {
258 $context = context_course::instance($courseid);
259 $entry->courseassoc = $context->id;
262 // Pre-select the mod for associations.
263 if ($modid) {
264 $context = context_module::instance($modid);
265 $entry->modassoc = $context->id;
268 break;
270 case 'edit':
271 if (empty($entry->id)) {
272 print_error('wrongentryid', 'blog');
274 $entry->tags = tag_get_tags_array('post', $entry->id);
275 $strformheading = get_string('updateentrywithid', 'blog');
277 break;
279 default :
280 print_error('unknowaction');
283 $entry->modid = $modid;
284 $entry->courseid = $courseid;
286 echo $OUTPUT->header();
287 // Output title for editing mode.
288 if (isset($editmodetitle)) {
289 echo $OUTPUT->heading($editmodetitle, 2);
291 $blogeditform->display();
292 echo $OUTPUT->footer();
294 die;