MDL-10181, help files should not be modified, a new file has been created
[moodle.git] / blog / edit.php
CommitLineData
4a173181 1<?php //$Id$
2
3require_once('../config.php');
4include_once('lib.php');
eccfc1ca 5
e96f2a77 6$action = required_param('action', PARAM_ALPHA);
7$id = optional_param('id', 0, PARAM_INT);
8$confirm = optional_param('confirm', 0, PARAM_BOOL);
9$courseid = optional_param('courseid', 0, PARAM_INT); // needed for user tab - does nothing here
10
11require_login();
eccfc1ca 12
ab2f17b0 13if (empty($CFG->bloglevel)) {
14 error('Blogging is disabled!');
15}
16
e96f2a77 17if (isguest()) {
18 error(get_string('noguestpost', 'blog'));
4a173181 19}
20
e96f2a77 21$sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
22if (!has_capability('moodle/blog:create', $sitecontext) and !has_capability('moodle/blog:manageentries', $sitecontext)) {
23 error('You can not post or edit blogs.');
4a173181 24}
25
bbbf2d40 26// Make sure that the person trying to edit have access right
e96f2a77 27if ($id) {
28 if (!$existing = get_record('post', 'id', $id)) {
29 error('Wrong blog post id');
4a173181 30 }
6524adcf 31
e96f2a77 32 if (!blog_user_can_edit_post($existing)) {
33 error(get_string('notallowedtoedit', 'blog'));
34 }
35 $userid = $existing->userid;
36 $returnurl = $CFG->wwwroot.'/blog/index.php?userid='.$existing->userid;
4a173181 37} else {
e96f2a77 38 if (!has_capability('moodle/blog:create', $sitecontext)) {
39 error(get_string('nopost', 'blog')); // manageentries is not enough for adding
40 }
41 $existing = false;
42 $userid = $USER->id;
43 $returnurl = 'index.php?userid='.$USER->id;
4a173181 44}
e96f2a77 45if (!empty($courseid)) {
46 $returnurl .= '&amp;courseid='.$courseid;
48e79fd1 47}
48
49
e96f2a77 50$strblogs = get_string('blogs','blog');
4a173181 51
f07b9627 52if ($action=='delete'){
53 if (!$existing) {
54 error('Incorrect blog post id');
55 }
56 if (data_submitted() and $confirm and confirm_sesskey()) {
57 do_delete($existing);
58 redirect($returnurl);
59 } else {
60 $optionsyes = array('id'=>$id, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'courseid'=>$courseid);
61 $optionsno = array('userid'=>$existing->userid, 'courseid'=>$courseid);
62 print_header("$SITE->shortname: $strblogs", $SITE->fullname);
63 blog_print_entry($existing);
64 echo '<br />';
65 notice_yesno(get_string('blogdeleteconfirm', 'blog'), 'edit.php', 'index.php', $optionsyes, $optionsno, 'post', 'get');
66 print_footer();
67 die;
68 }
69}
6524adcf 70
f07b9627 71require_once('edit_form.php');
1d284fbd 72$blogeditform = new blog_edit_form(null, compact('existing', 'sitecontext'));
f07b9627 73
1d284fbd 74if ($blogeditform->is_cancelled()){
f07b9627 75 redirect($returnurl);
feaf5d06 76} else if ($blogeditform->no_submit_button_pressed()) {
1d284fbd 77 no_submit_button_actions($blogeditform, $sitecontext);
f07b9627 78
79
beac4717 80} else if ($fromform = $blogeditform->get_data()){
f07b9627 81 //save stuff in db
82 switch ($action) {
83 case 'add':
feaf5d06 84 do_add($fromform, $blogeditform);
f07b9627 85 break;
86
87 case 'edit':
88 if (!$existing) {
89 error('Incorrect blog post id');
e96f2a77 90 }
feaf5d06 91 do_edit($fromform, $blogeditform);
f07b9627 92 break;
93 default :
94 error('Unknown action!');
95 }
96 redirect($returnurl);
97}
48e79fd1 98
f07b9627 99
100// gui setup
101switch ($action) {
102 case 'add':
103 // prepare new empty form
104 $post->publishstate = 'draft';
e96f2a77 105 $strformheading = get_string('addnewentry', 'blog');
f07b9627 106 $post->action = $action;
e96f2a77 107 break;
4a173181 108
e96f2a77 109 case 'edit':
110 if (!$existing) {
111 error('Incorrect blog post id');
112 }
f07b9627 113 $post->id = $existing->id;
114 $post->subject = $existing->subject;
115 $post->summary = $existing->summary;
116 $post->publishstate = $existing->publishstate;
117 $post->format = $existing->format;
118 $post->action = $action;
e96f2a77 119 $strformheading = get_string('updateentrywithid', 'blog');
4a173181 120
f07b9627 121 if ($ptags = get_records_sql_menu("SELECT t.id, t.text FROM
122 {$CFG->prefix}tags t,
123 {$CFG->prefix}blog_tag_instance bti
124 WHERE t.id = bti.tagid
125 AND t.type = 'personal'
126 AND bti.entryid = {$post->id}")) {
127
128 $post->ptags = implode(', ', $ptags);
e96f2a77 129 } else {
f07b9627 130 //$idsql = " AND bti.entryid = 0";
131 //was used but seems redundant.
132 $post->ptags = '';
133 }
134 if ($otags = get_records_sql_menu("SELECT t.id, t.text FROM
135 {$CFG->prefix}tags t,
136 {$CFG->prefix}blog_tag_instance bti
137 WHERE t.id = bti.tagid
138 AND t.type = 'official'
139 AND bti.entryid = {$post->id}")){
140 $post->otags = array_keys($otags);
4a173181 141 }
e96f2a77 142 break;
f07b9627 143 default :
e96f2a77 144 error('Unknown action!');
4a173181 145}
146
e96f2a77 147// done here in order to allow deleting of posts with wrong user id above
148if (!$user = get_record('user', 'id', $userid)) {
149 error('Incorrect user id');
4a173181 150}
e295df44 151$navlinks = array();
152$navlinks[] = array('name' => fullname($user), 'link' => "$CFG->wwwroot/user/view.php?id=$userid", 'type' => 'misc');
153$navlinks[] = array('name' => $strblogs, 'link' => "$CFG->wwwroot/blog/index.php?userid=$userid", 'type' => 'misc');
154$navlinks[] = array('name' => $strformheading, 'link' => null, 'type' => 'misc');
155$navigation = build_navigation($navlinks);
4a173181 156
e295df44 157print_header("$SITE->shortname: $strblogs", $SITE->fullname, $navigation,'','',true);
beac4717 158$blogeditform->set_data($post);
1d284fbd 159$blogeditform->display();
4a173181 160
e96f2a77 161
162print_footer();
163
f07b9627 164
e96f2a77 165die;
4a173181 166
4a173181 167/***************************** edit.php functions ***************************/
1d284fbd 168function no_submit_button_actions(&$blogeditform, $sitecontext){
169 $mform =& $blogeditform->_form;
f07b9627 170 $data = $mform->exportValues();
171 //sesskey has been checked already no need to check that
172 //check for official tags to add
173 if (!empty($data['addotags']) && !empty($data['otagsadd'])){ // adding official tag
174 $error = add_otag($data['otagsadd']);
175 }
176 if (!empty($error)){
177 $mform->setElementError('otagsgrp', $error);
178 }
179 if (!empty($data['deleteotags']) && !empty($data['otags'])){ // adding official tag
180 delete_otags($data['otags'], $sitecontext);
181 }
1d284fbd 182 $blogeditform->otags_select_setup();
f07b9627 183}
feaf5d06 184
f07b9627 185function delete_otags($tagids, $sitecontext){
186 foreach ($tagids as $tagid) {
187
188 if (!$tag = get_record('tags', 'id', $tagid)) {
189 error('Can not delete tag, tag doesn\'t exist');
190 }
191
192 if ($tag->type == 'official' and !has_capability('moodle/blog:manageofficialtags', $sitecontext)) {
193 //can not delete
194 error('Can not delete tag, you don\'t have permission to delete an official tag');
195 }
196
197 if ($tag->type == 'personal' and !has_capability('moodle/blog:managepersonaltags', $sitecontext)) {
198 //can not delete
199 error('Can not delete tag, you don\'t have permission to delete a personal tag');
200 }
201
202 // Delete the tag itself
203 if (!delete_records('tags', 'id', $tagid)) {
204 error('Can not delete tag');
205 }
206
207 // Deleteing all references to this tag
208 if (!delete_records('blog_tag_instance', 'tagid', $tagid)) {
209 error('Can not delete blog tag instances');
210 }
211
212
213 }
214}
feaf5d06 215
f07b9627 216function add_otag($otag){
217 global $USER;
218 $error = '';
219 if ($tag = get_record('tags', 'text', $otag)) {
220 if ($tag->type == 'official') {
221 // official tag already exist
222 $error = get_string('tagalready');
223 } else {
224 $tag->type = 'official';
225 update_record('tags', $tag);
226 $tagid = $tag->id;
227 }
228 } else { // Brand new offical tag
229
230 $tag = new object();
231 $tag->userid = $USER->id;
232 $tag->text = $otag;
233 $tag->type = 'official';
234
235 if (!$tagid = insert_record('tags', $tag)) {
236 error('Can not create tag!');
237 }
238 }
239 return $error;
240}
feaf5d06 241
4a173181 242/*
e96f2a77 243* Delete blog post from database
4a173181 244*/
e96f2a77 245function do_delete($post) {
246 global $returnurl;
4a173181 247
e96f2a77 248 $status = delete_records('post', 'id', $post->id);
249 $status = delete_records('blog_tag_instance', 'entryid', $post->id) and $status;
e295df44 250
2878babd 251 blog_delete_old_attachments($post);
e295df44 252
e96f2a77 253 add_to_log(SITEID, 'blog', 'delete', 'index.php?userid='. $post->userid, 'deleted blog entry with entry id# '. $post->id);
254
255 if (!$status) {
256 error('Error occured while deleting post', $returnurl);
257 }
4a173181 258}
259
260/**
e96f2a77 261 * Write a new blog entry into database
262 */
feaf5d06 263function do_add($post, $blogeditform) {
e96f2a77 264 global $CFG, $USER, $returnurl;
4a173181 265
e96f2a77 266 $post->module = 'blog';
267 $post->userid = $USER->id;
268 $post->lastmodified = time();
269 $post->created = time();
f07b9627 270
e96f2a77 271 // Insert the new blog entry.
272 if ($id = insert_record('post', $post)) {
273 $post->id = $id;
7d0e5a95 274 // add blog attachment
feaf5d06 275 $dir = blog_file_area_name($post);
276 if ($blogeditform->save_files($dir) and $newfilename = $blogeditform->get_new_filename()) {
277 set_field("post", "attachment", $newfilename, "id", $post->id);
f07b9627 278 }
e96f2a77 279 add_tags_info($post->id);
f07b9627 280 add_to_log(SITEID, 'blog', 'add', 'index.php?userid='.$post->userid.'&postid='.$post->id, $post->subject);
e96f2a77 281
282 } else {
283 error('There was an error adding this post in the database', $returnurl);
284 }
285
4a173181 286}
287
288/**
289 * @param . $post argument is a reference to the post object which is used to store information for the form
290 * @param . $bloginfo_arg argument is reference to a blogInfo object.
291 * @todo complete documenting this function. enable trackback and pingback between entries on the same server
292 */
feaf5d06 293function do_edit($post, $blogeditform) {
e96f2a77 294
295 global $CFG, $USER, $returnurl;
296
e96f2a77 297
298 $post->lastmodified = time();
f07b9627 299
feaf5d06 300 $dir = blog_file_area_name($post);
301 if ($blogeditform->save_files($dir) and $newfilename = $blogeditform->get_new_filename()) {
7d0e5a95 302 $post->attachment = $newfilename;
feaf5d06 303 }
304
e96f2a77 305 // update record
306 if (update_record('post', $post)) {
91e56853 307 // delete all tags associated with this entry
e96f2a77 308 delete_records('blog_tag_instance', 'entryid', $post->id);
91e56853 309 // add them back
e96f2a77 310 add_tags_info($post->id);
f07b9627 311
7d0e5a95 312
e96f2a77 313 add_to_log(SITEID, 'blog', 'update', 'index.php?userid='.$post->userid.'&postid='.$post->id, $post->subject);
314
315 } else {
316 error('There was an error updating this post in the database', $returnurl);
317 }
e96f2a77 318}
319
91e56853 320/**
321 * function to attach tags into a post
322 * @param int postid - id of the blog
323 */
e96f2a77 324function add_tags_info($postid) {
f07b9627 325
91e56853 326 global $USER;
f07b9627 327
e96f2a77 328 $post = get_record('post', 'id', $postid);
329
330 $tag = new object();
331 $tag->entryid = $post->id;
332 $tag->userid = $post->userid;
91e56853 333 $tag->timemodified = time();
f07b9627 334
91e56853 335 /// Attach official tags
f07b9627 336 if ($otags = optional_param('otags', '', PARAM_INT)) {
91e56853 337 foreach ($otags as $otag) {
338 $tag->tagid = $otag;
e96f2a77 339 insert_record('blog_tag_instance', $tag);
4a173181 340 }
e96f2a77 341 }
342
91e56853 343 /// Attach Personal Tags
f07b9627 344 if ($ptags = optional_param('ptags', '', PARAM_NOTAGS)) {
345 $ptags = explode(',', $ptags);
e96f2a77 346 foreach ($ptags as $ptag) {
91e56853 347 $ptag = trim($ptag);
348 // check for existance
349 // it does not matter whether it is an offical tag or personal tag
350 // we do not want to have 1 copy of offical tag and 1 copy of personal tag (for the same tag)
351 if ($ctag = get_record('tags', 'text', $ptag)) {
352 $tag->tagid = $ctag->id;
353 insert_record('blog_tag_instance', $tag);
354 } else { // create a personal tag
355 $ctag = new object;
356 $ctag->userid = $USER->id;
357 $ctag->text = $ptag;
358 $ctag->type = 'personal';
359 if ($tagid = insert_record('tags', $ctag)) {
360 $tag->tagid = $tagid;
361 insert_record('blog_tag_instance', $tag);
f07b9627 362 }
91e56853 363 }
4a173181 364 }
4a173181 365 }
366}
e295df44 367?>