Merge branch 'MDL-50529_dupe' of git://github.com/andyjdavis/moodle
[moodle.git] / blog / edit.php
blob24612dd7d1a7eba90065d59feed87338cce66a74
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 $entry = new stdClass();
52 $entry->id = null;
54 if ($id) {
55 if (!$entry = new blog_entry($id)) {
56 print_error('wrongentryid', 'blog');
58 $userid = $entry->userid;
59 } else {
60 $userid = $USER->id;
63 $sitecontext = context_system::instance();
64 $usercontext = context_user::instance($userid);
65 $PAGE->set_context($usercontext);
66 $blognode = $PAGE->settingsnav->find('blogadd', null);
67 $blognode->make_active();
69 require_login($courseid);
71 if (empty($CFG->enableblogs)) {
72 print_error('blogdisable', 'blog');
75 if (isguestuser()) {
76 print_error('noguestentry', 'blog');
79 $returnurl = new moodle_url('/blog/index.php');
81 if (!empty($courseid) && empty($modid)) {
82 $returnurl->param('courseid', $courseid);
85 // If a modid is given, guess courseid.
86 if (!empty($modid)) {
87 $returnurl->param('modid', $modid);
88 $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
89 $returnurl->param('courseid', $courseid);
92 $blogheaders = blog_get_headers();
94 if (!has_capability('moodle/blog:create', $sitecontext) && !has_capability('moodle/blog:manageentries', $sitecontext)) {
95 print_error('cannoteditentryorblog');
98 // Make sure that the person trying to edit has access right.
99 if ($id) {
100 if (!blog_user_can_edit_entry($entry)) {
101 print_error('notallowedtoedit', 'blog');
103 $entry->subject = clean_text($entry->subject);
104 $entry->summary = clean_text($entry->summary, $entry->format);
105 } else {
106 if (!has_capability('moodle/blog:create', $sitecontext)) {
107 print_error('noentry', 'blog'); // The capability "manageentries" is not enough for adding.
110 $returnurl->param('userid', $userid);
112 // Blog renderer.
113 $output = $PAGE->get_renderer('blog');
115 $strblogs = get_string('blogs', 'blog');
117 if ($action === 'delete') {
118 if (empty($entry->id)) {
119 print_error('wrongentryid', 'blog');
121 if (data_submitted() && $confirm && confirm_sesskey()) {
122 // Make sure the current user is the author of the blog entry, or has some deleteanyentry capability.
123 if (!blog_user_can_edit_entry($entry)) {
124 print_error('nopermissionstodeleteentry', 'blog');
125 } else {
126 $entry->delete();
127 blog_rss_delete_file($userid);
128 redirect($returnurl);
130 } else if (blog_user_can_edit_entry($entry)) {
131 $optionsyes = array('entryid'=>$id, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'courseid'=>$courseid);
132 $optionsno = array('userid'=>$entry->userid, 'courseid'=>$courseid);
133 $PAGE->set_title("$SITE->shortname: $strblogs");
134 $PAGE->set_heading($SITE->fullname);
135 echo $OUTPUT->header();
137 // Output the entry.
138 $entry->prepare_render();
139 echo $output->render($entry);
141 echo '<br />';
142 echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog'),
143 new moodle_url('edit.php', $optionsyes),
144 new moodle_url('index.php', $optionsno));
145 echo $OUTPUT->footer();
146 die;
148 } else if ($action == 'add') {
149 $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('addnewentry', 'blog'));
150 $PAGE->set_heading(fullname($USER));
151 } else if ($action == 'edit') {
152 $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('editentry', 'blog'));
153 $PAGE->set_heading(fullname($USER));
156 if (!empty($entry->id)) {
157 if ($CFG->useblogassociations && ($blogassociations = $DB->get_records('blog_association', array('blogid' => $entry->id)))) {
159 foreach ($blogassociations as $assocrec) {
160 $context = context::instance_by_id($assocrec->contextid);
162 switch ($context->contextlevel) {
163 case CONTEXT_COURSE:
164 $entry->courseassoc = $assocrec->contextid;
165 break;
166 case CONTEXT_MODULE:
167 $entry->modassoc = $assocrec->contextid;
168 break;
174 require_once('edit_form.php');
175 $summaryoptions = array('maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>true, 'context'=>$sitecontext,
176 'subdirs'=>file_area_contains_subdirs($sitecontext, 'blog', 'post', $entry->id));
177 $attachmentoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes);
179 $blogeditform = new blog_edit_form(null, compact('entry',
180 'summaryoptions',
181 'attachmentoptions',
182 'sitecontext',
183 'courseid',
184 'modid'));
186 $entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id);
187 $entry = file_prepare_standard_filemanager($entry,
188 'attachment',
189 $attachmentoptions,
190 $sitecontext,
191 'blog',
192 'attachment',
193 $entry->id);
195 if (!empty($CFG->usetags) && !empty($entry->id)) {
196 include_once($CFG->dirroot.'/tag/lib.php');
197 $entry->tags = tag_get_tags_array('post', $entry->id);
200 $entry->action = $action;
201 // Set defaults.
202 $blogeditform->set_data($entry);
204 if ($blogeditform->is_cancelled()) {
205 redirect($returnurl);
207 } else if ($data = $blogeditform->get_data()) {
209 switch ($action) {
210 case 'add':
211 $blogentry = new blog_entry(null, $data, $blogeditform);
212 $blogentry->add();
213 $blogentry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
214 break;
216 case 'edit':
217 if (empty($entry->id)) {
218 print_error('wrongentryid', 'blog');
221 $entry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
222 break;
224 default :
225 print_error('invalidaction');
228 redirect($returnurl);
232 // GUI setup.
233 switch ($action) {
234 case 'add':
235 // Prepare new empty form.
236 $entry->publishstate = 'site';
237 $strformheading = get_string('addnewentry', 'blog');
238 $entry->action = $action;
240 if ($CFG->useblogassociations) {
242 // Pre-select the course for associations.
243 if ($courseid) {
244 $context = context_course::instance($courseid);
245 $entry->courseassoc = $context->id;
248 // Pre-select the mod for associations.
249 if ($modid) {
250 $context = context_module::instance($modid);
251 $entry->modassoc = $context->id;
254 break;
256 case 'edit':
257 if (empty($entry->id)) {
258 print_error('wrongentryid', 'blog');
260 $entry->tags = tag_get_tags_array('post', $entry->id);
261 $strformheading = get_string('updateentrywithid', 'blog');
263 break;
265 default :
266 print_error('unknowaction');
269 $entry->modid = $modid;
270 $entry->courseid = $courseid;
272 echo $OUTPUT->header();
273 $blogeditform->display();
274 echo $OUTPUT->footer();
276 die;