weekly release 2.5dev
[moodle.git] / blog / edit.php
CommitLineData
cae83708 1<?php
2
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.
14//
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/>.
4a173181 17
cae83708 18
19/**
20 * Blog entry edit page
21 *
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
26 */
0a941490 27require_once(dirname(dirname(__FILE__)).'/config.php');
4a173181 28include_once('lib.php');
cae83708 29include_once('locallib.php');
eccfc1ca 30
e96f2a77 31$action = required_param('action', PARAM_ALPHA);
9366362a 32$id = optional_param('entryid', 0, PARAM_INT);
e96f2a77 33$confirm = optional_param('confirm', 0, PARAM_BOOL);
1c7b8b93
NC
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
e96f2a77 36
a6855934 37$PAGE->set_url('/blog/edit.php', array('action' => $action, 'entryid' => $id, 'confirm' => $confirm, 'modid' => $modid, 'courseid' => $courseid));
cae83708 38
1c7b8b93
NC
39// If action is add, we ignore $id to avoid any further problems
40if (!empty($id) && $action == 'add') {
41 $id = null;
42}
43
a6855934 44$returnurl = new moodle_url('/blog/index.php');
1c7b8b93
NC
45
46if (!empty($courseid) && empty($modid)) {
47 $returnurl->param('courseid', $courseid);
41b38360 48 $PAGE->set_context(context_course::instance($courseid));
1c7b8b93
NC
49}
50
51// If a modid is given, guess courseid
52if (!empty($modid)) {
53 $returnurl->param('modid', $modid);
54 $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
55 $returnurl->param('courseid', $courseid);
41b38360 56 $PAGE->set_context(context_module::instance($modid));
1c7b8b93
NC
57}
58
45f22487
SH
59// If courseid is empty use the system context
60if (empty($courseid)) {
41b38360 61 $PAGE->set_context(context_system::instance());
45f22487
SH
62}
63
1c7b8b93 64$blogheaders = blog_get_headers();
b0e90a0c 65
673bc55d 66require_login($courseid);
eccfc1ca 67
9366362a 68if ($action == 'edit') {
69 $id = required_param('entryid', PARAM_INT);
70}
71
850d2db8 72if (empty($CFG->enableblogs)) {
fae11dca 73 print_error('blogdisable', 'blog');
ab2f17b0 74}
75
ec7c4f11 76if (isguestuser()) {
cae83708 77 print_error('noguestentry', 'blog');
4a173181 78}
79
41b38360 80$sitecontext = context_system::instance();
cae83708 81if (!has_capability('moodle/blog:create', $sitecontext) && !has_capability('moodle/blog:manageentries', $sitecontext)) {
82 print_error('cannoteditentryorblog');
4a173181 83}
84
1c7b8b93 85// Make sure that the person trying to edit has access right
e96f2a77 86if ($id) {
1c7b8b93 87 if (!$entry = new blog_entry($id)) {
cae83708 88 print_error('wrongentryid', 'blog');
4a173181 89 }
6524adcf 90
1c7b8b93 91 if (!blog_user_can_edit_entry($entry)) {
5a2a5331 92 print_error('notallowedtoedit', 'blog');
e96f2a77 93 }
1c7b8b93 94 $userid = $entry->userid;
32148582
PS
95 $entry->subject = clean_text($entry->subject);
96 $entry->summary = clean_text($entry->summary, $entry->format);
64f93798 97
4a173181 98} else {
e96f2a77 99 if (!has_capability('moodle/blog:create', $sitecontext)) {
cae83708 100 print_error('noentry', 'blog'); // manageentries is not enough for adding
e96f2a77 101 }
1c7b8b93
NC
102 $entry = new stdClass();
103 $entry->id = null;
104 $userid = $USER->id;
cae83708 105}
1c7b8b93 106$returnurl->param('userid', $userid);
48e79fd1 107
f8133217 108// Blog renderer.
2591c7ae
DM
109$output = $PAGE->get_renderer('blog');
110
e96f2a77 111$strblogs = get_string('blogs','blog');
4a173181 112
45df7de3 113if ($action === 'delete'){
1c7b8b93 114 if (empty($entry->id)) {
cae83708 115 print_error('wrongentryid', 'blog');
f07b9627 116 }
cae83708 117 if (data_submitted() && $confirm && confirm_sesskey()) {
1c7b8b93
NC
118 // Make sure the current user is the author of the blog entry, or has some deleteanyentry capability
119 if (!blog_user_can_edit_entry($entry)) {
120 print_error('nopermissionstodeleteentry', 'blog');
121 } else {
122 $entry->delete();
123 redirect($returnurl);
124 }
125 } else if (blog_user_can_edit_entry($entry)) {
9366362a 126 $optionsyes = array('entryid'=>$id, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'courseid'=>$courseid);
1c7b8b93 127 $optionsno = array('userid'=>$entry->userid, 'courseid'=>$courseid);
e640790e 128 $PAGE->set_title("$SITE->shortname: $strblogs");
129 $PAGE->set_heading($SITE->fullname);
130 echo $OUTPUT->header();
2591c7ae 131
f8133217 132 // Output the entry.
2591c7ae
DM
133 $entry->prepare_render();
134 echo $output->render($entry);
135
f07b9627 136 echo '<br />';
7d00c7bc 137 echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog'), new moodle_url('edit.php', $optionsyes),new moodle_url( 'index.php', $optionsno));
033e4aff 138 echo $OUTPUT->footer();
f07b9627 139 die;
140 }
1c7b8b93
NC
141} else if ($action == 'add') {
142 $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('addnewentry', 'blog'));
143 $PAGE->set_heading($SITE->shortname);
144} else if ($action == 'edit') {
145 $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('editentry', 'blog'));
146 $PAGE->set_heading($SITE->shortname);
f07b9627 147}
6524adcf 148
1c7b8b93
NC
149if (!empty($entry->id)) {
150 if ($CFG->useblogassociations && ($blogassociations = $DB->get_records('blog_association', array('blogid' => $entry->id)))) {
cae83708 151
152 foreach ($blogassociations as $assocrec) {
41b38360 153 $context = context::instance_by_id($assocrec->contextid);
cae83708 154
e922fe23 155 switch ($context->contextlevel) {
cae83708 156 case CONTEXT_COURSE:
1c7b8b93 157 $entry->courseassoc = $assocrec->contextid;
cae83708 158 break;
159 case CONTEXT_MODULE:
1c7b8b93 160 $entry->modassoc = $assocrec->contextid;
cae83708 161 break;
162 }
163 }
164 }
165}
166
1c7b8b93
NC
167require_once('edit_form.php');
168$summaryoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>true, 'context'=>$sitecontext);
169$attachmentoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes);
170
171$blogeditform = new blog_edit_form(null, compact('entry', 'summaryoptions', 'attachmentoptions', 'sitecontext', 'courseid', 'modid'));
cae83708 172
64f93798
PS
173$entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id);
174$entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $sitecontext, 'blog', 'attachment', $entry->id);
1c7b8b93
NC
175
176if (!empty($CFG->usetags) && !empty($entry->id)) {
177 include_once($CFG->dirroot.'/tag/lib.php');
178 $entry->tags = tag_get_tags_array('post', $entry->id);
179}
b73d1ca4 180
1c7b8b93
NC
181$entry->action = $action;
182// set defaults
183$blogeditform->set_data($entry);
f07b9627 184
1c7b8b93 185if ($blogeditform->is_cancelled()) {
f07b9627 186 redirect($returnurl);
cae83708 187
1c7b8b93
NC
188} else if ($data = $blogeditform->get_data()){
189
f07b9627 190 switch ($action) {
191 case 'add':
1c7b8b93 192 $blogentry = new blog_entry(null, $data, $blogeditform);
b73d1ca4 193 $blogentry->add();
1c7b8b93 194 $blogentry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
f07b9627 195 break;
196
197 case 'edit':
1c7b8b93 198 if (empty($entry->id)) {
cae83708 199 print_error('wrongentryid', 'blog');
e96f2a77 200 }
1c7b8b93
NC
201
202 $entry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
f07b9627 203 break;
1c7b8b93 204
f07b9627 205 default :
fae11dca 206 print_error('invalidaction');
f07b9627 207 }
1c7b8b93 208
f07b9627 209 redirect($returnurl);
210}
48e79fd1 211
f07b9627 212
213// gui setup
214switch ($action) {
215 case 'add':
216 // prepare new empty form
cae83708 217 $entry->publishstate = 'site';
e96f2a77 218 $strformheading = get_string('addnewentry', 'blog');
cae83708 219 $entry->action = $action;
220
1c7b8b93
NC
221 if ($CFG->useblogassociations) {
222
223 //pre-select the course for associations
224 if ($courseid) {
41b38360 225 $context = context_course::instance($courseid);
1c7b8b93
NC
226 $entry->courseassoc = $context->id;
227 }
cae83708 228
1c7b8b93
NC
229 //pre-select the mod for associations
230 if ($modid) {
41b38360 231 $context = context_module::instance($modid);
1c7b8b93
NC
232 $entry->modassoc = $context->id;
233 }
cae83708 234 }
235 break;
4a173181 236
e96f2a77 237 case 'edit':
1c7b8b93 238 if (empty($entry->id)) {
cae83708 239 print_error('wrongentryid', 'blog');
e96f2a77 240 }
1c7b8b93 241 $entry->tags = tag_get_tags_array('post', $entry->id);
e96f2a77 242 $strformheading = get_string('updateentrywithid', 'blog');
4a173181 243
cae83708 244 break;
1c7b8b93 245
f07b9627 246 default :
fae11dca 247 print_error('unknowaction');
4a173181 248}
249
cae83708 250$entry->modid = $modid;
251$entry->courseid = $courseid;
e640790e 252
253echo $OUTPUT->header();
1d284fbd 254$blogeditform->display();
033e4aff 255echo $OUTPUT->footer();
e96f2a77 256
257die;