Merge branch 'MDL-47736_master' of git://github.com/markn86/moodle
[moodle.git] / blog / edit.php
blobc3052f796d5118832dbd45d50ec513e0648297cc
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');
30 $action = required_param('action', PARAM_ALPHA);
31 $id = optional_param('entryid', 0, PARAM_INT);
32 $confirm = optional_param('confirm', 0, PARAM_BOOL);
33 $modid = optional_param('modid', 0, PARAM_INT); // To associate the entry with a module instance.
34 $courseid = optional_param('courseid', 0, PARAM_INT); // To associate the entry with a course.
36 if ($action == 'edit') {
37 $id = required_param('entryid', PARAM_INT);
40 $PAGE->set_url('/blog/edit.php', array('action' => $action,
41 'entryid' => $id,
42 'confirm' => $confirm,
43 'modid' => $modid,
44 'courseid' => $courseid));
46 // If action is add, we ignore $id to avoid any further problems.
47 if (!empty($id) && $action == 'add') {
48 $id = null;
51 // Blogs are always in system context.
52 $sitecontext = context_system::instance();
53 $PAGE->set_context($sitecontext);
55 require_login($courseid);
57 if (empty($CFG->enableblogs)) {
58 print_error('blogdisable', 'blog');
61 if (isguestuser()) {
62 print_error('noguestentry', 'blog');
65 $returnurl = new moodle_url('/blog/index.php');
67 if (!empty($courseid) && empty($modid)) {
68 $returnurl->param('courseid', $courseid);
71 // If a modid is given, guess courseid.
72 if (!empty($modid)) {
73 $returnurl->param('modid', $modid);
74 $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
75 $returnurl->param('courseid', $courseid);
78 $blogheaders = blog_get_headers();
80 if (!has_capability('moodle/blog:create', $sitecontext) && !has_capability('moodle/blog:manageentries', $sitecontext)) {
81 print_error('cannoteditentryorblog');
84 // Make sure that the person trying to edit has access right.
85 if ($id) {
86 if (!$entry = new blog_entry($id)) {
87 print_error('wrongentryid', 'blog');
90 if (!blog_user_can_edit_entry($entry)) {
91 print_error('notallowedtoedit', 'blog');
93 $userid = $entry->userid;
94 $entry->subject = clean_text($entry->subject);
95 $entry->summary = clean_text($entry->summary, $entry->format);
97 } else {
98 if (!has_capability('moodle/blog:create', $sitecontext)) {
99 print_error('noentry', 'blog'); // The capability "manageentries" is not enough for adding.
101 $entry = new stdClass();
102 $entry->id = null;
103 $userid = $USER->id;
105 $returnurl->param('userid', $userid);
107 // Blog renderer.
108 $output = $PAGE->get_renderer('blog');
110 $strblogs = get_string('blogs', 'blog');
112 if ($action === 'delete') {
113 if (empty($entry->id)) {
114 print_error('wrongentryid', 'blog');
116 if (data_submitted() && $confirm && confirm_sesskey()) {
117 // Make sure the current user is the author of the blog entry, or has some deleteanyentry capability.
118 if (!blog_user_can_edit_entry($entry)) {
119 print_error('nopermissionstodeleteentry', 'blog');
120 } else {
121 $entry->delete();
122 blog_rss_delete_file($userid);
123 redirect($returnurl);
125 } else if (blog_user_can_edit_entry($entry)) {
126 $optionsyes = array('entryid'=>$id, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'courseid'=>$courseid);
127 $optionsno = array('userid'=>$entry->userid, 'courseid'=>$courseid);
128 $PAGE->set_title("$SITE->shortname: $strblogs");
129 $PAGE->set_heading($SITE->fullname);
130 echo $OUTPUT->header();
132 // Output the entry.
133 $entry->prepare_render();
134 echo $output->render($entry);
136 echo '<br />';
137 echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog'),
138 new moodle_url('edit.php', $optionsyes),
139 new moodle_url('index.php', $optionsno));
140 echo $OUTPUT->footer();
141 die;
143 } else if ($action == 'add') {
144 $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('addnewentry', 'blog'));
145 $PAGE->set_heading($SITE->shortname);
146 } else if ($action == 'edit') {
147 $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('editentry', 'blog'));
148 $PAGE->set_heading($SITE->shortname);
151 if (!empty($entry->id)) {
152 if ($CFG->useblogassociations && ($blogassociations = $DB->get_records('blog_association', array('blogid' => $entry->id)))) {
154 foreach ($blogassociations as $assocrec) {
155 $context = context::instance_by_id($assocrec->contextid);
157 switch ($context->contextlevel) {
158 case CONTEXT_COURSE:
159 $entry->courseassoc = $assocrec->contextid;
160 break;
161 case CONTEXT_MODULE:
162 $entry->modassoc = $assocrec->contextid;
163 break;
169 require_once('edit_form.php');
170 $summaryoptions = array('maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>true, 'context'=>$sitecontext,
171 'subdirs'=>file_area_contains_subdirs($sitecontext, 'blog', 'post', $entry->id));
172 $attachmentoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes);
174 $blogeditform = new blog_edit_form(null, compact('entry',
175 'summaryoptions',
176 'attachmentoptions',
177 'sitecontext',
178 'courseid',
179 'modid'));
181 $entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id);
182 $entry = file_prepare_standard_filemanager($entry,
183 'attachment',
184 $attachmentoptions,
185 $sitecontext,
186 'blog',
187 'attachment',
188 $entry->id);
190 if (!empty($CFG->usetags) && !empty($entry->id)) {
191 include_once($CFG->dirroot.'/tag/lib.php');
192 $entry->tags = tag_get_tags_array('post', $entry->id);
195 $entry->action = $action;
196 // Set defaults.
197 $blogeditform->set_data($entry);
199 if ($blogeditform->is_cancelled()) {
200 redirect($returnurl);
202 } else if ($data = $blogeditform->get_data()) {
204 switch ($action) {
205 case 'add':
206 $blogentry = new blog_entry(null, $data, $blogeditform);
207 $blogentry->add();
208 $blogentry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
209 break;
211 case 'edit':
212 if (empty($entry->id)) {
213 print_error('wrongentryid', 'blog');
216 $entry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
217 break;
219 default :
220 print_error('invalidaction');
223 redirect($returnurl);
227 // GUI setup.
228 switch ($action) {
229 case 'add':
230 // Prepare new empty form.
231 $entry->publishstate = 'site';
232 $strformheading = get_string('addnewentry', 'blog');
233 $entry->action = $action;
235 if ($CFG->useblogassociations) {
237 // Pre-select the course for associations.
238 if ($courseid) {
239 $context = context_course::instance($courseid);
240 $entry->courseassoc = $context->id;
243 // Pre-select the mod for associations.
244 if ($modid) {
245 $context = context_module::instance($modid);
246 $entry->modassoc = $context->id;
249 break;
251 case 'edit':
252 if (empty($entry->id)) {
253 print_error('wrongentryid', 'blog');
255 $entry->tags = tag_get_tags_array('post', $entry->id);
256 $strformheading = get_string('updateentrywithid', 'blog');
258 break;
260 default :
261 print_error('unknowaction');
264 $entry->modid = $modid;
265 $entry->courseid = $courseid;
267 echo $OUTPUT->header();
268 $blogeditform->display();
269 echo $OUTPUT->footer();
271 die;