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