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