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