MDL-51395 blog: Fix output of strings for blog comments
[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
f8133217 144 // Output the entry.
2591c7ae
DM
145 $entry->prepare_render();
146 echo $output->render($entry);
147
f07b9627 148 echo '<br />';
2b6e53e8
AD
149 echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog'),
150 new moodle_url('edit.php', $optionsyes),
151 new moodle_url('index.php', $optionsno));
033e4aff 152 echo $OUTPUT->footer();
f07b9627 153 die;
154 }
1c7b8b93 155} else if ($action == 'add') {
0b3ed7f7
JP
156 $editmodetitle = $strblogs . ': ' . get_string('addnewentry', 'blog');
157 $PAGE->set_title("$SITE->shortname: $editmodetitle");
880c5073 158 $PAGE->set_heading(fullname($USER));
1c7b8b93 159} else if ($action == 'edit') {
0b3ed7f7
JP
160 $editmodetitle = $strblogs . ': ' . get_string('editentry', 'blog');
161 $PAGE->set_title("$SITE->shortname: $editmodetitle");
880c5073 162 $PAGE->set_heading(fullname($USER));
f07b9627 163}
6524adcf 164
1c7b8b93
NC
165if (!empty($entry->id)) {
166 if ($CFG->useblogassociations && ($blogassociations = $DB->get_records('blog_association', array('blogid' => $entry->id)))) {
cae83708 167
168 foreach ($blogassociations as $assocrec) {
41b38360 169 $context = context::instance_by_id($assocrec->contextid);
cae83708 170
e922fe23 171 switch ($context->contextlevel) {
cae83708 172 case CONTEXT_COURSE:
1c7b8b93 173 $entry->courseassoc = $assocrec->contextid;
cae83708 174 break;
175 case CONTEXT_MODULE:
1c7b8b93 176 $entry->modassoc = $assocrec->contextid;
cae83708 177 break;
178 }
179 }
180 }
181}
182
1c7b8b93 183require_once('edit_form.php');
5e95223e
PS
184$summaryoptions = array('maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>true, 'context'=>$sitecontext,
185 'subdirs'=>file_area_contains_subdirs($sitecontext, 'blog', 'post', $entry->id));
1c7b8b93
NC
186$attachmentoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes);
187
2b6e53e8
AD
188$blogeditform = new blog_edit_form(null, compact('entry',
189 'summaryoptions',
190 'attachmentoptions',
191 'sitecontext',
192 'courseid',
193 'modid'));
cae83708 194
64f93798 195$entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id);
2b6e53e8
AD
196$entry = file_prepare_standard_filemanager($entry,
197 'attachment',
198 $attachmentoptions,
199 $sitecontext,
200 'blog',
201 'attachment',
202 $entry->id);
1c7b8b93
NC
203
204if (!empty($CFG->usetags) && !empty($entry->id)) {
205 include_once($CFG->dirroot.'/tag/lib.php');
206 $entry->tags = tag_get_tags_array('post', $entry->id);
207}
b73d1ca4 208
1c7b8b93 209$entry->action = $action;
2b6e53e8 210// Set defaults.
1c7b8b93 211$blogeditform->set_data($entry);
f07b9627 212
1c7b8b93 213if ($blogeditform->is_cancelled()) {
f07b9627 214 redirect($returnurl);
cae83708 215
2b6e53e8 216} else if ($data = $blogeditform->get_data()) {
1c7b8b93 217
f07b9627 218 switch ($action) {
219 case 'add':
1c7b8b93 220 $blogentry = new blog_entry(null, $data, $blogeditform);
b73d1ca4 221 $blogentry->add();
1c7b8b93 222 $blogentry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
f07b9627 223 break;
224
225 case 'edit':
1c7b8b93 226 if (empty($entry->id)) {
cae83708 227 print_error('wrongentryid', 'blog');
e96f2a77 228 }
1c7b8b93
NC
229
230 $entry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
f07b9627 231 break;
1c7b8b93 232
f07b9627 233 default :
fae11dca 234 print_error('invalidaction');
f07b9627 235 }
1c7b8b93 236
f07b9627 237 redirect($returnurl);
238}
48e79fd1 239
f07b9627 240
2b6e53e8 241// GUI setup.
f07b9627 242switch ($action) {
243 case 'add':
2b6e53e8 244 // Prepare new empty form.
cae83708 245 $entry->publishstate = 'site';
e96f2a77 246 $strformheading = get_string('addnewentry', 'blog');
cae83708 247 $entry->action = $action;
248
1c7b8b93
NC
249 if ($CFG->useblogassociations) {
250
2b6e53e8 251 // Pre-select the course for associations.
1c7b8b93 252 if ($courseid) {
41b38360 253 $context = context_course::instance($courseid);
1c7b8b93
NC
254 $entry->courseassoc = $context->id;
255 }
cae83708 256
2b6e53e8 257 // Pre-select the mod for associations.
1c7b8b93 258 if ($modid) {
41b38360 259 $context = context_module::instance($modid);
1c7b8b93
NC
260 $entry->modassoc = $context->id;
261 }
cae83708 262 }
263 break;
4a173181 264
e96f2a77 265 case 'edit':
1c7b8b93 266 if (empty($entry->id)) {
cae83708 267 print_error('wrongentryid', 'blog');
e96f2a77 268 }
1c7b8b93 269 $entry->tags = tag_get_tags_array('post', $entry->id);
e96f2a77 270 $strformheading = get_string('updateentrywithid', 'blog');
4a173181 271
cae83708 272 break;
1c7b8b93 273
f07b9627 274 default :
fae11dca 275 print_error('unknowaction');
4a173181 276}
277
cae83708 278$entry->modid = $modid;
279$entry->courseid = $courseid;
e640790e 280
281echo $OUTPUT->header();
0b3ed7f7
JP
282// Output title for editing mode.
283if (isset($editmodetitle)) {
284 echo $OUTPUT->heading($editmodetitle, 2);
285}
1d284fbd 286$blogeditform->display();
033e4aff 287echo $OUTPUT->footer();
e96f2a77 288
289die;