MDL-36963 Add unit tests for directory operations in mdeploy.php
[moodle.git] / blog / edit.php
blob6a182332238f147245af9db15f2e4596690b2037
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);
48 $PAGE->set_context(context_course::instance($courseid));
51 // If a modid is given, guess courseid
52 if (!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(context_module::instance($modid));
59 // If courseid is empty use the system context
60 if (empty($courseid)) {
61 $PAGE->set_context(context_system::instance());
64 $blogheaders = blog_get_headers();
66 require_login($courseid);
68 if ($action == 'edit') {
69 $id = required_param('entryid', PARAM_INT);
72 if (empty($CFG->enableblogs)) {
73 print_error('blogdisable', 'blog');
76 if (isguestuser()) {
77 print_error('noguestentry', 'blog');
80 $sitecontext = context_system::instance();
81 if (!has_capability('moodle/blog:create', $sitecontext) && !has_capability('moodle/blog:manageentries', $sitecontext)) {
82 print_error('cannoteditentryorblog');
85 // Make sure that the person trying to edit has access right
86 if ($id) {
87 if (!$entry = new blog_entry($id)) {
88 print_error('wrongentryid', 'blog');
91 if (!blog_user_can_edit_entry($entry)) {
92 print_error('notallowedtoedit', 'blog');
94 $userid = $entry->userid;
95 $entry->subject = clean_text($entry->subject);
96 $entry->summary = clean_text($entry->summary, $entry->format);
98 } else {
99 if (!has_capability('moodle/blog:create', $sitecontext)) {
100 print_error('noentry', 'blog'); // manageentries is not enough for adding
102 $entry = new stdClass();
103 $entry->id = null;
104 $userid = $USER->id;
106 $returnurl->param('userid', $userid);
108 // Blog renderer.
109 $output = $PAGE->get_renderer('blog');
111 $strblogs = get_string('blogs','blog');
113 if ($action === 'delete'){
114 if (empty($entry->id)) {
115 print_error('wrongentryid', 'blog');
117 if (data_submitted() && $confirm && confirm_sesskey()) {
118 // Make sure the current user is the author of the blog entry, or has some deleteanyentry capability
119 if (!blog_user_can_edit_entry($entry)) {
120 print_error('nopermissionstodeleteentry', 'blog');
121 } else {
122 $entry->delete();
123 redirect($returnurl);
125 } else if (blog_user_can_edit_entry($entry)) {
126 $optionsyes = array('entryid'=>$id, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'courseid'=>$courseid);
127 $optionsno = array('userid'=>$entry->userid, 'courseid'=>$courseid);
128 $PAGE->set_title("$SITE->shortname: $strblogs");
129 $PAGE->set_heading($SITE->fullname);
130 echo $OUTPUT->header();
132 // Output the entry.
133 $entry->prepare_render();
134 echo $output->render($entry);
136 echo '<br />';
137 echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog'), new moodle_url('edit.php', $optionsyes),new moodle_url( 'index.php', $optionsno));
138 echo $OUTPUT->footer();
139 die;
141 } else if ($action == 'add') {
142 $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('addnewentry', 'blog'));
143 $PAGE->set_heading($SITE->shortname);
144 } else if ($action == 'edit') {
145 $PAGE->set_title("$SITE->shortname: $strblogs: " . get_string('editentry', 'blog'));
146 $PAGE->set_heading($SITE->shortname);
149 if (!empty($entry->id)) {
150 if ($CFG->useblogassociations && ($blogassociations = $DB->get_records('blog_association', array('blogid' => $entry->id)))) {
152 foreach ($blogassociations as $assocrec) {
153 $context = context::instance_by_id($assocrec->contextid);
155 switch ($context->contextlevel) {
156 case CONTEXT_COURSE:
157 $entry->courseassoc = $assocrec->contextid;
158 break;
159 case CONTEXT_MODULE:
160 $entry->modassoc = $assocrec->contextid;
161 break;
167 require_once('edit_form.php');
168 $summaryoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>true, 'context'=>$sitecontext);
169 $attachmentoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes);
171 $blogeditform = new blog_edit_form(null, compact('entry', 'summaryoptions', 'attachmentoptions', 'sitecontext', 'courseid', 'modid'));
173 $entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id);
174 $entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $sitecontext, 'blog', 'attachment', $entry->id);
176 if (!empty($CFG->usetags) && !empty($entry->id)) {
177 include_once($CFG->dirroot.'/tag/lib.php');
178 $entry->tags = tag_get_tags_array('post', $entry->id);
181 $entry->action = $action;
182 // set defaults
183 $blogeditform->set_data($entry);
185 if ($blogeditform->is_cancelled()) {
186 redirect($returnurl);
188 } else if ($data = $blogeditform->get_data()){
190 switch ($action) {
191 case 'add':
192 $blogentry = new blog_entry(null, $data, $blogeditform);
193 $blogentry->add();
194 $blogentry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
195 break;
197 case 'edit':
198 if (empty($entry->id)) {
199 print_error('wrongentryid', 'blog');
202 $entry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
203 break;
205 default :
206 print_error('invalidaction');
209 redirect($returnurl);
213 // gui setup
214 switch ($action) {
215 case 'add':
216 // prepare new empty form
217 $entry->publishstate = 'site';
218 $strformheading = get_string('addnewentry', 'blog');
219 $entry->action = $action;
221 if ($CFG->useblogassociations) {
223 //pre-select the course for associations
224 if ($courseid) {
225 $context = context_course::instance($courseid);
226 $entry->courseassoc = $context->id;
229 //pre-select the mod for associations
230 if ($modid) {
231 $context = context_module::instance($modid);
232 $entry->modassoc = $context->id;
235 break;
237 case 'edit':
238 if (empty($entry->id)) {
239 print_error('wrongentryid', 'blog');
241 $entry->tags = tag_get_tags_array('post', $entry->id);
242 $strformheading = get_string('updateentrywithid', 'blog');
244 break;
246 default :
247 print_error('unknowaction');
250 $entry->modid = $modid;
251 $entry->courseid = $courseid;
253 echo $OUTPUT->header();
254 $blogeditform->display();
255 echo $OUTPUT->footer();
257 die;