Merge branch 'MDL-42835_26' of git://github.com/nobelium/moodle into MOODLE_26_STABLE
[moodle.git] / blog / edit.php
blobfadc8adaf2aab32edf44b9c9fdc8558a5a38d989
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 /**
20 * Blog entry edit page
22 * @package moodlecore
23 * @subpackage blog
24 * @copyright 2009 Nicolas Connault
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once(dirname(dirname(__FILE__)).'/config.php');
28 include_once('lib.php');
29 include_once('locallib.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 $PAGE->set_url('/blog/edit.php', array('action' => $action, 'entryid' => $id, 'confirm' => $confirm, 'modid' => $modid, 'courseid' => $courseid));
39 // If action is add, we ignore $id to avoid any further problems
40 if (!empty($id) && $action == 'add') {
41 $id = null;
44 $returnurl = new moodle_url('/blog/index.php');
46 if (!empty($courseid) && empty($modid)) {
47 $returnurl->param('courseid', $courseid);
50 // If a modid is given, guess courseid
51 if (!empty($modid)) {
52 $returnurl->param('modid', $modid);
53 $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
54 $returnurl->param('courseid', $courseid);
57 // Blogs are always in system context.
58 $sitecontext = context_system::instance();
59 $PAGE->set_context($sitecontext);
62 $blogheaders = blog_get_headers();
64 require_login($courseid);
66 if ($action == 'edit') {
67 $id = required_param('entryid', PARAM_INT);
70 if (empty($CFG->enableblogs)) {
71 print_error('blogdisable', 'blog');
74 if (isguestuser()) {
75 print_error('noguestentry', 'blog');
78 if (!has_capability('moodle/blog:create', $sitecontext) && !has_capability('moodle/blog:manageentries', $sitecontext)) {
79 print_error('cannoteditentryorblog');
82 // Make sure that the person trying to edit has access right
83 if ($id) {
84 if (!$entry = new blog_entry($id)) {
85 print_error('wrongentryid', 'blog');
88 if (!blog_user_can_edit_entry($entry)) {
89 print_error('notallowedtoedit', 'blog');
91 $userid = $entry->userid;
92 $entry->subject = clean_text($entry->subject);
93 $entry->summary = clean_text($entry->summary, $entry->format);
95 } else {
96 if (!has_capability('moodle/blog:create', $sitecontext)) {
97 print_error('noentry', 'blog'); // manageentries is not enough for adding
99 $entry = new stdClass();
100 $entry->id = null;
101 $userid = $USER->id;
103 $returnurl->param('userid', $userid);
105 // Blog renderer.
106 $output = $PAGE->get_renderer('blog');
108 $strblogs = get_string('blogs','blog');
110 if ($action === 'delete'){
111 if (empty($entry->id)) {
112 print_error('wrongentryid', 'blog');
114 if (data_submitted() && $confirm && confirm_sesskey()) {
115 // Make sure the current user is the author of the blog entry, or has some deleteanyentry capability
116 if (!blog_user_can_edit_entry($entry)) {
117 print_error('nopermissionstodeleteentry', 'blog');
118 } else {
119 $entry->delete();
120 blog_rss_delete_file($userid);
121 redirect($returnurl);
123 } else if (blog_user_can_edit_entry($entry)) {
124 $optionsyes = array('entryid'=>$id, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'courseid'=>$courseid);
125 $optionsno = array('userid'=>$entry->userid, 'courseid'=>$courseid);
126 $PAGE->set_title("$SITE->shortname: $strblogs");
127 $PAGE->set_heading($SITE->fullname);
128 echo $OUTPUT->header();
130 // Output the entry.
131 $entry->prepare_render();
132 echo $output->render($entry);
134 echo '<br />';
135 echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog'), new moodle_url('edit.php', $optionsyes),new moodle_url( 'index.php', $optionsno));
136 echo $OUTPUT->footer();
137 die;
139 } else if ($action == 'add') {
140 $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('addnewentry', 'blog'));
141 $PAGE->set_heading($SITE->shortname);
142 } else if ($action == 'edit') {
143 $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('editentry', 'blog'));
144 $PAGE->set_heading($SITE->shortname);
147 if (!empty($entry->id)) {
148 if ($CFG->useblogassociations && ($blogassociations = $DB->get_records('blog_association', array('blogid' => $entry->id)))) {
150 foreach ($blogassociations as $assocrec) {
151 $context = context::instance_by_id($assocrec->contextid);
153 switch ($context->contextlevel) {
154 case CONTEXT_COURSE:
155 $entry->courseassoc = $assocrec->contextid;
156 break;
157 case CONTEXT_MODULE:
158 $entry->modassoc = $assocrec->contextid;
159 break;
165 require_once('edit_form.php');
166 $summaryoptions = array('maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>true, 'context'=>$sitecontext,
167 'subdirs'=>file_area_contains_subdirs($sitecontext, 'blog', 'post', $entry->id));
168 $attachmentoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes);
170 $blogeditform = new blog_edit_form(null, compact('entry', 'summaryoptions', 'attachmentoptions', 'sitecontext', 'courseid', 'modid'));
172 $entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id);
173 $entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $sitecontext, 'blog', 'attachment', $entry->id);
175 if (!empty($CFG->usetags) && !empty($entry->id)) {
176 include_once($CFG->dirroot.'/tag/lib.php');
177 $entry->tags = tag_get_tags_array('post', $entry->id);
180 $entry->action = $action;
181 // set defaults
182 $blogeditform->set_data($entry);
184 if ($blogeditform->is_cancelled()) {
185 redirect($returnurl);
187 } else if ($data = $blogeditform->get_data()){
189 switch ($action) {
190 case 'add':
191 $blogentry = new blog_entry(null, $data, $blogeditform);
192 $blogentry->add();
193 $blogentry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
194 break;
196 case 'edit':
197 if (empty($entry->id)) {
198 print_error('wrongentryid', 'blog');
201 $entry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
202 break;
204 default :
205 print_error('invalidaction');
208 redirect($returnurl);
212 // gui setup
213 switch ($action) {
214 case 'add':
215 // prepare new empty form
216 $entry->publishstate = 'site';
217 $strformheading = get_string('addnewentry', 'blog');
218 $entry->action = $action;
220 if ($CFG->useblogassociations) {
222 //pre-select the course for associations
223 if ($courseid) {
224 $context = context_course::instance($courseid);
225 $entry->courseassoc = $context->id;
228 //pre-select the mod for associations
229 if ($modid) {
230 $context = context_module::instance($modid);
231 $entry->modassoc = $context->id;
234 break;
236 case 'edit':
237 if (empty($entry->id)) {
238 print_error('wrongentryid', 'blog');
240 $entry->tags = tag_get_tags_array('post', $entry->id);
241 $strformheading = get_string('updateentrywithid', 'blog');
243 break;
245 default :
246 print_error('unknowaction');
249 $entry->modid = $modid;
250 $entry->courseid = $courseid;
252 echo $OUTPUT->header();
253 $blogeditform->display();
254 echo $OUTPUT->footer();
256 die;