Merge branch 'MDL-51483_29' of https://github.com/jinhofer/moodle into MOODLE_29_STABLE
[moodle.git] / blog / edit.php
blobb8686e04b5fa4255e3524314acab3eecd47384c5
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 $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');
76 if (isguestuser()) {
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.
87 if (!empty($modid)) {
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.
100 if ($id) {
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);
106 } else {
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);
113 // Blog renderer.
114 $output = $PAGE->get_renderer('blog');
116 $strblogs = get_string('blogs', 'blog');
118 if ($action === 'delete') {
119 // Init comment JS strings.
120 comment::init();
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');
129 } else {
130 $entry->delete();
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));
145 echo '<br />';
146 // Output the entry.
147 $entry->prepare_render();
148 echo $output->render($entry);
150 echo $OUTPUT->footer();
151 die;
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) {
168 case CONTEXT_COURSE:
169 $entry->courseassoc = $assocrec->contextid;
170 break;
171 case CONTEXT_MODULE:
172 $entry->modassoc = $assocrec->contextid;
173 break;
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',
185 'summaryoptions',
186 'attachmentoptions',
187 'sitecontext',
188 'courseid',
189 'modid'));
191 $entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id);
192 $entry = file_prepare_standard_filemanager($entry,
193 'attachment',
194 $attachmentoptions,
195 $sitecontext,
196 'blog',
197 'attachment',
198 $entry->id);
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;
206 // Set defaults.
207 $blogeditform->set_data($entry);
209 if ($blogeditform->is_cancelled()) {
210 redirect($returnurl);
212 } else if ($data = $blogeditform->get_data()) {
214 switch ($action) {
215 case 'add':
216 $blogentry = new blog_entry(null, $data, $blogeditform);
217 $blogentry->add();
218 $blogentry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
219 break;
221 case 'edit':
222 if (empty($entry->id)) {
223 print_error('wrongentryid', 'blog');
226 $entry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
227 break;
229 default :
230 print_error('invalidaction');
233 redirect($returnurl);
237 // GUI setup.
238 switch ($action) {
239 case 'add':
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.
248 if ($courseid) {
249 $context = context_course::instance($courseid);
250 $entry->courseassoc = $context->id;
253 // Pre-select the mod for associations.
254 if ($modid) {
255 $context = context_module::instance($modid);
256 $entry->modassoc = $context->id;
259 break;
261 case 'edit':
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');
268 break;
270 default :
271 print_error('unknowaction');
274 $entry->modid = $modid;
275 $entry->courseid = $courseid;
277 echo $OUTPUT->header();
278 $blogeditform->display();
279 echo $OUTPUT->footer();
281 die;