Merge branch 'MDL-29174-master' of git://github.com/raymanuk/moodle
[moodle.git] / blog / edit.php
blob1ba7ac496dc4821314a93da57afe50742b8fedb0
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);
48 $PAGE->set_context(get_context_instance(CONTEXT_COURSE, $courseid));
51 // If a modid is given, guess courseid
52 if (!empty($modid)) {
53 $returnurl->param('modid', $modid);
54 $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
55 $returnurl->param('courseid', $courseid);
56 $PAGE->set_context(get_context_instance(CONTEXT_MODULE, $modid));
59 // If courseid is empty use the system context
60 if (empty($courseid)) {
61 $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
64 $blogheaders = blog_get_headers();
66 require_login($courseid);
68 if ($action == 'edit') {
69 $id = required_param('entryid', PARAM_INT);
72 if (empty($CFG->bloglevel)) {
73 print_error('blogdisable', 'blog');
76 if (isguestuser()) {
77 print_error('noguestentry', 'blog');
80 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
81 if (!has_capability('moodle/blog:create', $sitecontext) && !has_capability('moodle/blog:manageentries', $sitecontext)) {
82 print_error('cannoteditentryorblog');
85 // Make sure that the person trying to edit has access right
86 if ($id) {
87 if (!$entry = new blog_entry($id)) {
88 print_error('wrongentryid', 'blog');
91 if (!blog_user_can_edit_entry($entry)) {
92 print_error('notallowedtoedit', 'blog');
94 $userid = $entry->userid;
95 $entry->subject = clean_text($entry->subject);
96 $entry->summary = clean_text($entry->summary, $entry->format);
98 } else {
99 if (!has_capability('moodle/blog:create', $sitecontext)) {
100 print_error('noentry', 'blog'); // manageentries is not enough for adding
102 $entry = new stdClass();
103 $entry->id = null;
104 $userid = $USER->id;
106 $returnurl->param('userid', $userid);
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 redirect($returnurl);
122 } else if (blog_user_can_edit_entry($entry)) {
123 $optionsyes = array('entryid'=>$id, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'courseid'=>$courseid);
124 $optionsno = array('userid'=>$entry->userid, 'courseid'=>$courseid);
125 $PAGE->set_title("$SITE->shortname: $strblogs");
126 $PAGE->set_heading($SITE->fullname);
127 echo $OUTPUT->header();
128 $entry->print_html();
129 echo '<br />';
130 echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog'), new moodle_url('edit.php', $optionsyes),new moodle_url( 'index.php', $optionsno));
131 echo $OUTPUT->footer();
132 die;
134 } else if ($action == 'add') {
135 $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('addnewentry', 'blog'));
136 $PAGE->set_heading($SITE->shortname);
137 } else if ($action == 'edit') {
138 $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('editentry', 'blog'));
139 $PAGE->set_heading($SITE->shortname);
142 if (!empty($entry->id)) {
143 if ($CFG->useblogassociations && ($blogassociations = $DB->get_records('blog_association', array('blogid' => $entry->id)))) {
145 foreach ($blogassociations as $assocrec) {
146 $context = get_context_instance_by_id($assocrec->contextid);
148 switch ($context->contextlevel) {
149 case CONTEXT_COURSE:
150 $entry->courseassoc = $assocrec->contextid;
151 break;
152 case CONTEXT_MODULE:
153 $entry->modassoc = $assocrec->contextid;
154 break;
160 require_once('edit_form.php');
161 $summaryoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>true, 'context'=>$sitecontext);
162 $attachmentoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes);
164 $blogeditform = new blog_edit_form(null, compact('entry', 'summaryoptions', 'attachmentoptions', 'sitecontext', 'courseid', 'modid'));
166 $entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id);
167 $entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $sitecontext, 'blog', 'attachment', $entry->id);
169 if (!empty($CFG->usetags) && !empty($entry->id)) {
170 include_once($CFG->dirroot.'/tag/lib.php');
171 $entry->tags = tag_get_tags_array('post', $entry->id);
174 $entry->action = $action;
175 // set defaults
176 $blogeditform->set_data($entry);
178 if ($blogeditform->is_cancelled()) {
179 redirect($returnurl);
181 } else if ($data = $blogeditform->get_data()){
183 switch ($action) {
184 case 'add':
185 $blogentry = new blog_entry(null, $data, $blogeditform);
186 $blogentry->add();
187 $blogentry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
188 break;
190 case 'edit':
191 if (empty($entry->id)) {
192 print_error('wrongentryid', 'blog');
195 $entry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
196 break;
198 default :
199 print_error('invalidaction');
202 redirect($returnurl);
206 // gui setup
207 switch ($action) {
208 case 'add':
209 // prepare new empty form
210 $entry->publishstate = 'site';
211 $strformheading = get_string('addnewentry', 'blog');
212 $entry->action = $action;
214 if ($CFG->useblogassociations) {
216 //pre-select the course for associations
217 if ($courseid) {
218 $context = get_context_instance(CONTEXT_COURSE, $courseid);
219 $entry->courseassoc = $context->id;
222 //pre-select the mod for associations
223 if ($modid) {
224 $context = get_context_instance(CONTEXT_MODULE, $modid);
225 $entry->modassoc = $context->id;
228 break;
230 case 'edit':
231 if (empty($entry->id)) {
232 print_error('wrongentryid', 'blog');
234 $entry->tags = tag_get_tags_array('post', $entry->id);
235 $strformheading = get_string('updateentrywithid', 'blog');
237 break;
239 default :
240 print_error('unknowaction');
243 $entry->modid = $modid;
244 $entry->courseid = $courseid;
246 echo $OUTPUT->header();
247 $blogeditform->display();
248 echo $OUTPUT->footer();
250 die;