Merge branch 'MDL-38885-master-int' of git://github.com/FMCorz/moodle
[moodle.git] / blog / edit.php
blob2c3676309889a169d17cac781fe93d6f93ce09c6
1 <?php
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.
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/>.
19 /**
20 * Blog entry edit page
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
27 require_once(dirname(dirname(__FILE__)).'/config.php');
28 include_once('lib.php');
29 include_once('locallib.php');
31 $action = required_param('action', PARAM_ALPHA);
32 $id = optional_param('entryid', 0, PARAM_INT);
33 $confirm = optional_param('confirm', 0, PARAM_BOOL);
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
37 $PAGE->set_url('/blog/edit.php', array('action' => $action, 'entryid' => $id, 'confirm' => $confirm, 'modid' => $modid, 'courseid' => $courseid));
39 // If action is add, we ignore $id to avoid any further problems
40 if (!empty($id) && $action == 'add') {
41 $id = null;
44 $returnurl = new moodle_url('/blog/index.php');
46 if (!empty($courseid) && empty($modid)) {
47 $returnurl->param('courseid', $courseid);
50 // If a modid is given, guess courseid
51 if (!empty($modid)) {
52 $returnurl->param('modid', $modid);
53 $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
54 $returnurl->param('courseid', $courseid);
57 // Blogs are always in system context.
58 $sitecontext = context_system::instance();
59 $PAGE->set_context($sitecontext);
62 $blogheaders = blog_get_headers();
64 require_login($courseid);
66 if ($action == 'edit') {
67 $id = required_param('entryid', PARAM_INT);
70 if (empty($CFG->enableblogs)) {
71 print_error('blogdisable', 'blog');
74 if (isguestuser()) {
75 print_error('noguestentry', 'blog');
78 if (!has_capability('moodle/blog:create', $sitecontext) && !has_capability('moodle/blog:manageentries', $sitecontext)) {
79 print_error('cannoteditentryorblog');
82 // Make sure that the person trying to edit has access right
83 if ($id) {
84 if (!$entry = new blog_entry($id)) {
85 print_error('wrongentryid', 'blog');
88 if (!blog_user_can_edit_entry($entry)) {
89 print_error('notallowedtoedit', 'blog');
91 $userid = $entry->userid;
92 $entry->subject = clean_text($entry->subject);
93 $entry->summary = clean_text($entry->summary, $entry->format);
95 } else {
96 if (!has_capability('moodle/blog:create', $sitecontext)) {
97 print_error('noentry', 'blog'); // manageentries is not enough for adding
99 $entry = new stdClass();
100 $entry->id = null;
101 $userid = $USER->id;
103 $returnurl->param('userid', $userid);
105 // Blog renderer.
106 $output = $PAGE->get_renderer('blog');
108 $strblogs = get_string('blogs','blog');
110 if ($action === 'delete'){
111 if (empty($entry->id)) {
112 print_error('wrongentryid', 'blog');
114 if (data_submitted() && $confirm && confirm_sesskey()) {
115 // Make sure the current user is the author of the blog entry, or has some deleteanyentry capability
116 if (!blog_user_can_edit_entry($entry)) {
117 print_error('nopermissionstodeleteentry', 'blog');
118 } else {
119 $entry->delete();
120 redirect($returnurl);
122 } else if (blog_user_can_edit_entry($entry)) {
123 $optionsyes = array('entryid'=>$id, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'courseid'=>$courseid);
124 $optionsno = array('userid'=>$entry->userid, 'courseid'=>$courseid);
125 $PAGE->set_title("$SITE->shortname: $strblogs");
126 $PAGE->set_heading($SITE->fullname);
127 echo $OUTPUT->header();
129 // Output the entry.
130 $entry->prepare_render();
131 echo $output->render($entry);
133 echo '<br />';
134 echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog'), new moodle_url('edit.php', $optionsyes),new moodle_url( 'index.php', $optionsno));
135 echo $OUTPUT->footer();
136 die;
138 } else if ($action == 'add') {
139 $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('addnewentry', 'blog'));
140 $PAGE->set_heading($SITE->shortname);
141 } else if ($action == 'edit') {
142 $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('editentry', 'blog'));
143 $PAGE->set_heading($SITE->shortname);
146 if (!empty($entry->id)) {
147 if ($CFG->useblogassociations && ($blogassociations = $DB->get_records('blog_association', array('blogid' => $entry->id)))) {
149 foreach ($blogassociations as $assocrec) {
150 $context = context::instance_by_id($assocrec->contextid);
152 switch ($context->contextlevel) {
153 case CONTEXT_COURSE:
154 $entry->courseassoc = $assocrec->contextid;
155 break;
156 case CONTEXT_MODULE:
157 $entry->modassoc = $assocrec->contextid;
158 break;
164 require_once('edit_form.php');
165 $summaryoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>true, 'context'=>$sitecontext);
166 $attachmentoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes);
168 $blogeditform = new blog_edit_form(null, compact('entry', 'summaryoptions', 'attachmentoptions', 'sitecontext', 'courseid', 'modid'));
170 $entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id);
171 $entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $sitecontext, 'blog', 'attachment', $entry->id);
173 if (!empty($CFG->usetags) && !empty($entry->id)) {
174 include_once($CFG->dirroot.'/tag/lib.php');
175 $entry->tags = tag_get_tags_array('post', $entry->id);
178 $entry->action = $action;
179 // set defaults
180 $blogeditform->set_data($entry);
182 if ($blogeditform->is_cancelled()) {
183 redirect($returnurl);
185 } else if ($data = $blogeditform->get_data()){
187 switch ($action) {
188 case 'add':
189 $blogentry = new blog_entry(null, $data, $blogeditform);
190 $blogentry->add();
191 $blogentry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
192 break;
194 case 'edit':
195 if (empty($entry->id)) {
196 print_error('wrongentryid', 'blog');
199 $entry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
200 break;
202 default :
203 print_error('invalidaction');
206 redirect($returnurl);
210 // gui setup
211 switch ($action) {
212 case 'add':
213 // prepare new empty form
214 $entry->publishstate = 'site';
215 $strformheading = get_string('addnewentry', 'blog');
216 $entry->action = $action;
218 if ($CFG->useblogassociations) {
220 //pre-select the course for associations
221 if ($courseid) {
222 $context = context_course::instance($courseid);
223 $entry->courseassoc = $context->id;
226 //pre-select the mod for associations
227 if ($modid) {
228 $context = context_module::instance($modid);
229 $entry->modassoc = $context->id;
232 break;
234 case 'edit':
235 if (empty($entry->id)) {
236 print_error('wrongentryid', 'blog');
238 $entry->tags = tag_get_tags_array('post', $entry->id);
239 $strformheading = get_string('updateentrywithid', 'blog');
241 break;
243 default :
244 print_error('unknowaction');
247 $entry->modid = $modid;
248 $entry->courseid = $courseid;
250 echo $OUTPUT->header();
251 $blogeditform->display();
252 echo $OUTPUT->footer();
254 die;