MDL-19676 MDL-14408 MDL-8776 Blog improvements
[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
9366362a 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
44$returnurl = new moodle_url($CFG->wwwroot . '/blog/index.php');
45
46if (!empty($courseid) && empty($modid)) {
47 $returnurl->param('courseid', $courseid);
48 $PAGE->set_context(get_context_instance(CONTEXT_COURSE, $courseid));
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);
56 $PAGE->set_context(get_context_instance(CONTEXT_MODULE, $modid));
57}
58
59$blogheaders = blog_get_headers();
b0e90a0c 60
673bc55d 61require_login($courseid);
eccfc1ca 62
9366362a 63if ($action == 'edit') {
64 $id = required_param('entryid', PARAM_INT);
65}
66
ab2f17b0 67if (empty($CFG->bloglevel)) {
fae11dca 68 print_error('blogdisable', 'blog');
ab2f17b0 69}
70
ec7c4f11 71if (isguestuser()) {
cae83708 72 print_error('noguestentry', 'blog');
4a173181 73}
74
d9d16e56 75$sitecontext = get_context_instance(CONTEXT_SYSTEM);
cae83708 76if (!has_capability('moodle/blog:create', $sitecontext) && !has_capability('moodle/blog:manageentries', $sitecontext)) {
77 print_error('cannoteditentryorblog');
4a173181 78}
79
1c7b8b93 80// Make sure that the person trying to edit has access right
e96f2a77 81if ($id) {
1c7b8b93 82 if (!$entry = new blog_entry($id)) {
cae83708 83 print_error('wrongentryid', 'blog');
4a173181 84 }
6524adcf 85
1c7b8b93 86 if (!blog_user_can_edit_entry($entry)) {
5a2a5331 87 print_error('notallowedtoedit', 'blog');
e96f2a77 88 }
1c7b8b93 89 $userid = $entry->userid;
4a173181 90} else {
e96f2a77 91 if (!has_capability('moodle/blog:create', $sitecontext)) {
cae83708 92 print_error('noentry', 'blog'); // manageentries is not enough for adding
e96f2a77 93 }
1c7b8b93
NC
94 $entry = new stdClass();
95 $entry->id = null;
96 $userid = $USER->id;
cae83708 97}
1c7b8b93 98$returnurl->param('userid', $userid);
48e79fd1 99
e96f2a77 100$strblogs = get_string('blogs','blog');
4a173181 101
45df7de3 102if ($action === 'delete'){
1c7b8b93 103 if (empty($entry->id)) {
cae83708 104 print_error('wrongentryid', 'blog');
f07b9627 105 }
cae83708 106 if (data_submitted() && $confirm && confirm_sesskey()) {
1c7b8b93
NC
107 // Make sure the current user is the author of the blog entry, or has some deleteanyentry capability
108 if (!blog_user_can_edit_entry($entry)) {
109 print_error('nopermissionstodeleteentry', 'blog');
110 } else {
111 $entry->delete();
112 redirect($returnurl);
113 }
114 } else if (blog_user_can_edit_entry($entry)) {
9366362a 115 $optionsyes = array('entryid'=>$id, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'courseid'=>$courseid);
1c7b8b93 116 $optionsno = array('userid'=>$entry->userid, 'courseid'=>$courseid);
e640790e 117 $PAGE->set_title("$SITE->shortname: $strblogs");
118 $PAGE->set_heading($SITE->fullname);
119 echo $OUTPUT->header();
1c7b8b93 120 $entry->print_html();
f07b9627 121 echo '<br />';
7d00c7bc 122 echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog'), new moodle_url('edit.php', $optionsyes),new moodle_url( 'index.php', $optionsno));
033e4aff 123 echo $OUTPUT->footer();
f07b9627 124 die;
125 }
1c7b8b93
NC
126} else if ($action == 'add') {
127 $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('addnewentry', 'blog'));
128 $PAGE->set_heading($SITE->shortname);
129} else if ($action == 'edit') {
130 $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('editentry', 'blog'));
131 $PAGE->set_heading($SITE->shortname);
f07b9627 132}
6524adcf 133
1c7b8b93
NC
134if (!empty($entry->id)) {
135 if ($CFG->useblogassociations && ($blogassociations = $DB->get_records('blog_association', array('blogid' => $entry->id)))) {
cae83708 136
137 foreach ($blogassociations as $assocrec) {
138 $contextrec = $DB->get_record('context', array('id' => $assocrec->contextid));
139
140 switch ($contextrec->contextlevel) {
141 case CONTEXT_COURSE:
1c7b8b93 142 $entry->courseassoc = $assocrec->contextid;
cae83708 143 break;
144 case CONTEXT_MODULE:
1c7b8b93 145 $entry->modassoc = $assocrec->contextid;
cae83708 146 break;
147 }
148 }
149 }
150}
151
1c7b8b93
NC
152require_once('edit_form.php');
153$summaryoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>true, 'context'=>$sitecontext);
154$attachmentoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes);
155
156$blogeditform = new blog_edit_form(null, compact('entry', 'summaryoptions', 'attachmentoptions', 'sitecontext', 'courseid', 'modid'));
cae83708 157
1c7b8b93
NC
158$entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog_post', $entry->id);
159$entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $sitecontext, 'blog_attachment', $entry->id);
160
161if (!empty($CFG->usetags) && !empty($entry->id)) {
162 include_once($CFG->dirroot.'/tag/lib.php');
163 $entry->tags = tag_get_tags_array('post', $entry->id);
164}
b73d1ca4 165
1c7b8b93
NC
166$entry->action = $action;
167// set defaults
168$blogeditform->set_data($entry);
f07b9627 169
1c7b8b93 170if ($blogeditform->is_cancelled()) {
f07b9627 171 redirect($returnurl);
cae83708 172
1c7b8b93
NC
173} else if ($data = $blogeditform->get_data()){
174
f07b9627 175 switch ($action) {
176 case 'add':
1c7b8b93 177 $blogentry = new blog_entry(null, $data, $blogeditform);
b73d1ca4 178 $blogentry->add();
1c7b8b93 179 $blogentry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
f07b9627 180 break;
181
182 case 'edit':
1c7b8b93 183 if (empty($entry->id)) {
cae83708 184 print_error('wrongentryid', 'blog');
e96f2a77 185 }
1c7b8b93
NC
186
187 $entry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
f07b9627 188 break;
1c7b8b93 189
f07b9627 190 default :
fae11dca 191 print_error('invalidaction');
f07b9627 192 }
1c7b8b93 193
f07b9627 194 redirect($returnurl);
195}
48e79fd1 196
f07b9627 197
198// gui setup
199switch ($action) {
200 case 'add':
201 // prepare new empty form
cae83708 202 $entry->publishstate = 'site';
e96f2a77 203 $strformheading = get_string('addnewentry', 'blog');
cae83708 204 $entry->action = $action;
205
1c7b8b93
NC
206 if ($CFG->useblogassociations) {
207
208 //pre-select the course for associations
209 if ($courseid) {
210 $context = get_context_instance(CONTEXT_COURSE, $courseid);
211 $entry->courseassoc = $context->id;
212 }
cae83708 213
1c7b8b93
NC
214 //pre-select the mod for associations
215 if ($modid) {
216 $context = get_context_instance(CONTEXT_MODULE, $modid);
217 $entry->modassoc = $context->id;
218 }
cae83708 219 }
220 break;
4a173181 221
e96f2a77 222 case 'edit':
1c7b8b93 223 if (empty($entry->id)) {
cae83708 224 print_error('wrongentryid', 'blog');
e96f2a77 225 }
1c7b8b93 226 $entry->tags = tag_get_tags_array('post', $entry->id);
e96f2a77 227 $strformheading = get_string('updateentrywithid', 'blog');
4a173181 228
cae83708 229 break;
1c7b8b93 230
f07b9627 231 default :
fae11dca 232 print_error('unknowaction');
4a173181 233}
234
cae83708 235$entry->modid = $modid;
236$entry->courseid = $courseid;
e640790e 237
238echo $OUTPUT->header();
1d284fbd 239$blogeditform->display();
033e4aff 240echo $OUTPUT->footer();
e96f2a77 241
242die;