better code formatting
[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);
1d284fbd 76} elseif ($blogeditform->no_submit_button_pressed()) {
77 no_submit_button_actions($blogeditform, $sitecontext);
f07b9627 78
79
1d284fbd 80} elseif ($fromform = $blogeditform->data_submitted()){
f07b9627 81 //save stuff in db
82 switch ($action) {
83 case 'add':
84 do_add($fromform);
85 break;
86
87 case 'edit':
88 if (!$existing) {
89 error('Incorrect blog post id');
e96f2a77 90 }
f07b9627 91 do_edit($fromform);
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}
151
e96f2a77 152print_header("$SITE->shortname: $strblogs", $SITE->fullname,
153 '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$userid.'">'.fullname($user).'</a> ->
154 <a href="'.$CFG->wwwroot.'/blog/index.php?userid='.$userid.'">'.$strblogs.'</a> -> '.$strformheading,'','',true);
1d284fbd 155$blogeditform->set_defaults($post);
156$blogeditform->display();
4a173181 157
e96f2a77 158
159print_footer();
160
f07b9627 161
e96f2a77 162die;
4a173181 163
4a173181 164/***************************** edit.php functions ***************************/
1d284fbd 165function no_submit_button_actions(&$blogeditform, $sitecontext){
166 $mform =& $blogeditform->_form;
f07b9627 167 $data = $mform->exportValues();
168 //sesskey has been checked already no need to check that
169 //check for official tags to add
170 if (!empty($data['addotags']) && !empty($data['otagsadd'])){ // adding official tag
171 $error = add_otag($data['otagsadd']);
172 }
173 if (!empty($error)){
174 $mform->setElementError('otagsgrp', $error);
175 }
176 if (!empty($data['deleteotags']) && !empty($data['otags'])){ // adding official tag
177 delete_otags($data['otags'], $sitecontext);
178 }
1d284fbd 179 $blogeditform->otags_select_setup();
f07b9627 180}
181function delete_otags($tagids, $sitecontext){
182 foreach ($tagids as $tagid) {
183
184 if (!$tag = get_record('tags', 'id', $tagid)) {
185 error('Can not delete tag, tag doesn\'t exist');
186 }
187
188 if ($tag->type == 'official' and !has_capability('moodle/blog:manageofficialtags', $sitecontext)) {
189 //can not delete
190 error('Can not delete tag, you don\'t have permission to delete an official tag');
191 }
192
193 if ($tag->type == 'personal' and !has_capability('moodle/blog:managepersonaltags', $sitecontext)) {
194 //can not delete
195 error('Can not delete tag, you don\'t have permission to delete a personal tag');
196 }
197
198 // Delete the tag itself
199 if (!delete_records('tags', 'id', $tagid)) {
200 error('Can not delete tag');
201 }
202
203 // Deleteing all references to this tag
204 if (!delete_records('blog_tag_instance', 'tagid', $tagid)) {
205 error('Can not delete blog tag instances');
206 }
207
208
209 }
210}
211function add_otag($otag){
212 global $USER;
213 $error = '';
214 if ($tag = get_record('tags', 'text', $otag)) {
215 if ($tag->type == 'official') {
216 // official tag already exist
217 $error = get_string('tagalready');
218 } else {
219 $tag->type = 'official';
220 update_record('tags', $tag);
221 $tagid = $tag->id;
222 }
223 } else { // Brand new offical tag
224
225 $tag = new object();
226 $tag->userid = $USER->id;
227 $tag->text = $otag;
228 $tag->type = 'official';
229
230 if (!$tagid = insert_record('tags', $tag)) {
231 error('Can not create tag!');
232 }
233 }
234 return $error;
235}
4a173181 236/*
e96f2a77 237* Delete blog post from database
4a173181 238*/
e96f2a77 239function do_delete($post) {
240 global $returnurl;
4a173181 241
e96f2a77 242 $status = delete_records('post', 'id', $post->id);
243 $status = delete_records('blog_tag_instance', 'entryid', $post->id) and $status;
9ffcd15f 244
e96f2a77 245 add_to_log(SITEID, 'blog', 'delete', 'index.php?userid='. $post->userid, 'deleted blog entry with entry id# '. $post->id);
246
247 if (!$status) {
248 error('Error occured while deleting post', $returnurl);
249 }
4a173181 250}
251
252/**
e96f2a77 253 * Write a new blog entry into database
254 */
f07b9627 255function do_add($post) {
e96f2a77 256 global $CFG, $USER, $returnurl;
4a173181 257
e96f2a77 258 if ($post->summary == '<br />') {
259 $post->summary = '';
260 }
c2ee4e87 261
e96f2a77 262 if ($post->subject == '') {
263 $errors['subject'] = get_string('emptytitle', 'blog');
264 }
265 if ($post->summary == '') {
266 $errors['summary'] = get_string('emptybody', 'blog');
267 }
c2ee4e87 268
e96f2a77 269 if (!empty($errors)) {
270 return; // no saving
4a173181 271 }
e96f2a77 272
273 $post->module = 'blog';
274 $post->userid = $USER->id;
275 $post->lastmodified = time();
276 $post->created = time();
f07b9627 277
e96f2a77 278 // Insert the new blog entry.
279 if ($id = insert_record('post', $post)) {
280 $post->id = $id;
7d0e5a95 281 // add blog attachment
282 if ($post->attachment = blog_add_attachment($post, 'attachment',$message)) {
283 set_field("post", "attachment", $post->attachment, "id", $post->id);
f07b9627 284 }
e96f2a77 285 add_tags_info($post->id);
f07b9627 286 add_to_log(SITEID, 'blog', 'add', 'index.php?userid='.$post->userid.'&postid='.$post->id, $post->subject);
e96f2a77 287
288 } else {
289 error('There was an error adding this post in the database', $returnurl);
290 }
291
4a173181 292}
293
294/**
295 * @param . $post argument is a reference to the post object which is used to store information for the form
296 * @param . $bloginfo_arg argument is reference to a blogInfo object.
297 * @todo complete documenting this function. enable trackback and pingback between entries on the same server
298 */
f07b9627 299function do_edit($post) {
e96f2a77 300
301 global $CFG, $USER, $returnurl;
302
e96f2a77 303
304 $post->lastmodified = time();
f07b9627 305
306/* TODO add attachment processing
7d0e5a95 307 if ($newfilename = blog_add_attachment($post, 'attachment',$message)) {
308 $post->attachment = $newfilename;
309 } else {
310 unset($post->attachment);
f07b9627 311 }*/
e96f2a77 312 // update record
313 if (update_record('post', $post)) {
91e56853 314 // delete all tags associated with this entry
e96f2a77 315 delete_records('blog_tag_instance', 'entryid', $post->id);
91e56853 316 // add them back
e96f2a77 317 add_tags_info($post->id);
f07b9627 318
7d0e5a95 319
e96f2a77 320 add_to_log(SITEID, 'blog', 'update', 'index.php?userid='.$post->userid.'&postid='.$post->id, $post->subject);
321
322 } else {
323 error('There was an error updating this post in the database', $returnurl);
324 }
e96f2a77 325}
326
91e56853 327/**
328 * function to attach tags into a post
329 * @param int postid - id of the blog
330 */
e96f2a77 331function add_tags_info($postid) {
f07b9627 332
91e56853 333 global $USER;
f07b9627 334
e96f2a77 335 $post = get_record('post', 'id', $postid);
336
337 $tag = new object();
338 $tag->entryid = $post->id;
339 $tag->userid = $post->userid;
91e56853 340 $tag->timemodified = time();
f07b9627 341
91e56853 342 /// Attach official tags
f07b9627 343 if ($otags = optional_param('otags', '', PARAM_INT)) {
91e56853 344 foreach ($otags as $otag) {
345 $tag->tagid = $otag;
e96f2a77 346 insert_record('blog_tag_instance', $tag);
4a173181 347 }
e96f2a77 348 }
349
91e56853 350 /// Attach Personal Tags
f07b9627 351 if ($ptags = optional_param('ptags', '', PARAM_NOTAGS)) {
352 $ptags = explode(',', $ptags);
e96f2a77 353 foreach ($ptags as $ptag) {
91e56853 354 $ptag = trim($ptag);
355 // check for existance
356 // it does not matter whether it is an offical tag or personal tag
357 // we do not want to have 1 copy of offical tag and 1 copy of personal tag (for the same tag)
358 if ($ctag = get_record('tags', 'text', $ptag)) {
359 $tag->tagid = $ctag->id;
360 insert_record('blog_tag_instance', $tag);
361 } else { // create a personal tag
362 $ctag = new object;
363 $ctag->userid = $USER->id;
364 $ctag->text = $ptag;
365 $ctag->type = 'personal';
366 if ($tagid = insert_record('tags', $ctag)) {
367 $tag->tagid = $tagid;
368 insert_record('blog_tag_instance', $tag);
f07b9627 369 }
91e56853 370 }
4a173181 371 }
4a173181 372 }
373}
d7bf6d17 374?>