MDL-49620 availability: plugins can have global settings
[moodle.git] / blog / edit.php
blob82480390c3c90ea8172f81b92915de34f11efd54
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($CFG->dirroot . '/blog/lib.php');
28 require_once($CFG->dirroot . '/blog/locallib.php');
29 require_once($CFG->dirroot . '/comment/lib.php');
30 require_once($CFG->dirroot . '/blog/edit_form.php');
32 $action = required_param('action', PARAM_ALPHA);
33 $id = optional_param('entryid', 0, PARAM_INT);
34 $confirm = optional_param('confirm', 0, PARAM_BOOL);
35 $modid = optional_param('modid', 0, PARAM_INT); // To associate the entry with a module instance.
36 $courseid = optional_param('courseid', 0, PARAM_INT); // To associate the entry with a course.
38 if ($action == 'edit') {
39 $id = required_param('entryid', PARAM_INT);
42 $PAGE->set_url('/blog/edit.php', array('action' => $action,
43 'entryid' => $id,
44 'confirm' => $confirm,
45 'modid' => $modid,
46 'courseid' => $courseid));
48 // If action is add, we ignore $id to avoid any further problems.
49 if (!empty($id) && $action == 'add') {
50 $id = null;
53 $entry = new stdClass();
54 $entry->id = null;
56 if ($id) {
57 if (!$entry = new blog_entry($id)) {
58 print_error('wrongentryid', 'blog');
60 $userid = $entry->userid;
61 } else {
62 $userid = $USER->id;
65 $sitecontext = context_system::instance();
66 $usercontext = context_user::instance($userid);
67 $PAGE->set_context($usercontext);
68 $blognode = $PAGE->settingsnav->find('blogadd', null);
69 $blognode->make_active();
71 require_login($courseid);
73 if (empty($CFG->enableblogs)) {
74 print_error('blogdisable', 'blog');
77 if (isguestuser()) {
78 print_error('noguestentry', 'blog');
81 $returnurl = new moodle_url('/blog/index.php');
83 if (!empty($courseid) && empty($modid)) {
84 $returnurl->param('courseid', $courseid);
87 // If a modid is given, guess courseid.
88 if (!empty($modid)) {
89 $returnurl->param('modid', $modid);
90 $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
91 $returnurl->param('courseid', $courseid);
94 $blogheaders = blog_get_headers();
96 if (!has_capability('moodle/blog:create', $sitecontext) && !has_capability('moodle/blog:manageentries', $sitecontext)) {
97 print_error('cannoteditentryorblog');
100 // Make sure that the person trying to edit has access right.
101 if ($id) {
102 if (!blog_user_can_edit_entry($entry)) {
103 print_error('notallowedtoedit', 'blog');
105 $entry->subject = clean_text($entry->subject);
106 $entry->summary = clean_text($entry->summary, $entry->format);
107 } else {
108 if (!has_capability('moodle/blog:create', $sitecontext)) {
109 print_error('noentry', 'blog'); // The capability "manageentries" is not enough for adding.
112 $returnurl->param('userid', $userid);
114 // Blog renderer.
115 $output = $PAGE->get_renderer('blog');
117 $strblogs = get_string('blogs', 'blog');
119 if ($action === 'delete') {
120 // Init comment JS strings.
121 comment::init();
123 if (empty($entry->id)) {
124 print_error('wrongentryid', 'blog');
126 if (data_submitted() && $confirm && confirm_sesskey()) {
127 // Make sure the current user is the author of the blog entry, or has some deleteanyentry capability.
128 if (!blog_user_can_edit_entry($entry)) {
129 print_error('nopermissionstodeleteentry', 'blog');
130 } else {
131 $entry->delete();
132 blog_rss_delete_file($userid);
133 redirect($returnurl);
135 } else if (blog_user_can_edit_entry($entry)) {
136 $optionsyes = array('entryid' => $id,
137 'action' => 'delete',
138 'confirm' => 1,
139 'sesskey' => sesskey(),
140 'courseid' => $courseid);
141 $optionsno = array('userid' => $entry->userid, 'courseid' => $courseid);
142 $PAGE->set_title("$SITE->shortname: $strblogs");
143 $PAGE->set_heading($SITE->fullname);
144 echo $OUTPUT->header();
146 // Output edit mode title.
147 echo $OUTPUT->heading($strblogs . ': ' . get_string('deleteentry', 'blog'), 2);
149 echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog', format_string($entry->subject)),
150 new moodle_url('edit.php', $optionsyes),
151 new moodle_url('index.php', $optionsno));
153 echo '<br />';
154 // Output the entry.
155 $entry->prepare_render();
156 echo $output->render($entry);
158 echo $OUTPUT->footer();
159 die;
161 } else if ($action == 'add') {
162 $editmodetitle = $strblogs . ': ' . get_string('addnewentry', 'blog');
163 $PAGE->set_title("$SITE->shortname: $editmodetitle");
164 $PAGE->set_heading(fullname($USER));
165 } else if ($action == 'edit') {
166 $editmodetitle = $strblogs . ': ' . get_string('editentry', 'blog');
167 $PAGE->set_title("$SITE->shortname: $editmodetitle");
168 $PAGE->set_heading(fullname($USER));
171 if (!empty($entry->id)) {
172 if ($CFG->useblogassociations && ($blogassociations = $DB->get_records('blog_association', array('blogid' => $entry->id)))) {
174 foreach ($blogassociations as $assocrec) {
175 $context = context::instance_by_id($assocrec->contextid);
177 switch ($context->contextlevel) {
178 case CONTEXT_COURSE:
179 $entry->courseassoc = $assocrec->contextid;
180 break;
181 case CONTEXT_MODULE:
182 $entry->modassoc = $assocrec->contextid;
183 break;
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($entry->id)) {
210 $entry->tags = core_tag_tag::get_item_tags_array('core', 'post', $entry->id);
213 $entry->action = $action;
214 // Set defaults.
215 $blogeditform->set_data($entry);
217 if ($blogeditform->is_cancelled()) {
218 redirect($returnurl);
220 } else if ($data = $blogeditform->get_data()) {
222 switch ($action) {
223 case 'add':
224 $blogentry = new blog_entry(null, $data, $blogeditform);
225 $blogentry->add();
226 $blogentry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
227 break;
229 case 'edit':
230 if (empty($entry->id)) {
231 print_error('wrongentryid', 'blog');
234 $entry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
235 break;
237 default :
238 print_error('invalidaction');
241 redirect($returnurl);
245 // GUI setup.
246 switch ($action) {
247 case 'add':
248 // Prepare new empty form.
249 $entry->publishstate = 'site';
250 $strformheading = get_string('addnewentry', 'blog');
251 $entry->action = $action;
253 if ($CFG->useblogassociations) {
255 // Pre-select the course for associations.
256 if ($courseid) {
257 $context = context_course::instance($courseid);
258 $entry->courseassoc = $context->id;
261 // Pre-select the mod for associations.
262 if ($modid) {
263 $context = context_module::instance($modid);
264 $entry->modassoc = $context->id;
267 break;
269 case 'edit':
270 if (empty($entry->id)) {
271 print_error('wrongentryid', 'blog');
273 $strformheading = get_string('updateentrywithid', 'blog');
275 break;
277 default :
278 print_error('unknowaction');
281 $entry->modid = $modid;
282 $entry->courseid = $courseid;
284 echo $OUTPUT->header();
285 // Output title for editing mode.
286 if (isset($editmodetitle)) {
287 echo $OUTPUT->heading($editmodetitle, 2);
289 $blogeditform->display();
290 echo $OUTPUT->footer();
292 die;