Automatic installer.php lang files by installer_builder (20061228)
[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
e96f2a77 49$errors = array();
50$post = new object(); // editing form data
48e79fd1 51
e96f2a77 52$usehtmleditor = can_use_richtext_editor();
53$strblogs = get_string('blogs','blog');
4a173181 54
91e56853 55/// Main switch for processing blog entry
e96f2a77 56switch ($action) {
6524adcf 57
e96f2a77 58 case 'add':
59 if (data_submitted() and confirm_sesskey()) {
60 do_add($post, $errors);
61 if (empty($errors)) {
62 redirect($returnurl);
63 }
64 $post = stripslashes_safe($post); // no db access after this!!
65 // print form again
66 } else {
67 // prepare new empty form
68 $post->subject = '';
69 $post->summary = '';
70 $post->publishstate = 'draft';
71 $post->format = $usehtmleditor ? FORMAT_HTML : FORMAT_MOODLE;
48e79fd1 72
e96f2a77 73 }
74 $strformheading = get_string('addnewentry', 'blog');
75 break;
4a173181 76
e96f2a77 77 case 'edit':
78 if (!$existing) {
79 error('Incorrect blog post id');
80 }
81 if (data_submitted() and confirm_sesskey()) {
82 do_edit($post, $errors);
83 if (empty($errors)) {
84 redirect($returnurl);
85 }
86 $post = stripslashes_safe($post); // no db access after this!!
87 // print form again
88 } else {
89 $post->id = $existing->id;
90 $post->subject = $existing->subject;
91 $post->summary = $existing->summary;
92 $post->publishstate = $existing->publishstate;
93 $post->format = $existing->format;
94 }
95 $strformheading = get_string('updateentrywithid', 'blog');
96 break;
4a173181 97
e96f2a77 98 case 'delete':
99 if (!$existing) {
100 error('Incorrect blog post id');
4a173181 101 }
e96f2a77 102 if (data_submitted() and $confirm and confirm_sesskey()) {
103 do_delete($existing);
104 redirect($returnurl);
105 } else {
106 $optionsyes = array('id'=>$id, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'courseid'=>$courseid);
107 $optionsno = array('userid'=>$existing->userid, 'courseid'=>$courseid);
108 print_header("$SITE->shortname: $strblogs", $SITE->fullname);
109 blog_print_entry($existing);
110 echo '<br />';
111 notice_yesno(get_string('blogdeleteconfirm', 'blog'), 'edit.php', 'index.php', $optionsyes, $optionsno, 'post', 'get');
112 print_footer();
113 die;
4a173181 114 }
e96f2a77 115 break;
4a173181 116
e96f2a77 117 default:
118 error('Unknown action!');
119 break;
4a173181 120}
121
e96f2a77 122// gui setup
4a173181 123
e96f2a77 124// done here in order to allow deleting of posts with wrong user id above
125if (!$user = get_record('user', 'id', $userid)) {
126 error('Incorrect user id');
4a173181 127}
128
e96f2a77 129print_header("$SITE->shortname: $strblogs", $SITE->fullname,
130 '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$userid.'">'.fullname($user).'</a> ->
131 <a href="'.$CFG->wwwroot.'/blog/index.php?userid='.$userid.'">'.$strblogs.'</a> -> '.$strformheading,'','',true);
4a173181 132
e96f2a77 133echo '<br />';
134print_simple_box_start('center');
4a173181 135require('edit.html');
136print_simple_box_end();
137
e96f2a77 138if ($usehtmleditor) {
139 use_html_editor();
140}
141
142print_footer();
143
144die;
4a173181 145
4a173181 146/***************************** edit.php functions ***************************/
147/*
e96f2a77 148* Delete blog post from database
4a173181 149*/
e96f2a77 150function do_delete($post) {
151 global $returnurl;
4a173181 152
e96f2a77 153 $status = delete_records('post', 'id', $post->id);
154 $status = delete_records('blog_tag_instance', 'entryid', $post->id) and $status;
9ffcd15f 155
e96f2a77 156 add_to_log(SITEID, 'blog', 'delete', 'index.php?userid='. $post->userid, 'deleted blog entry with entry id# '. $post->id);
157
158 if (!$status) {
159 error('Error occured while deleting post', $returnurl);
160 }
4a173181 161}
162
163/**
e96f2a77 164 * Write a new blog entry into database
165 */
166function do_add(&$post, &$errors) {
167 global $CFG, $USER, $returnurl;
4a173181 168
e96f2a77 169 $post->subject = required_param('subject', PARAM_MULTILANG);
170 $post->summary = required_param('summary', PARAM_RAW);
171 $post->format = required_param('format', PARAM_INT);
172 $post->publishstate = required_param('publishstate', PARAM_ALPHA);;
4a173181 173
e96f2a77 174 if ($post->summary == '<br />') {
175 $post->summary = '';
176 }
c2ee4e87 177
e96f2a77 178 if ($post->subject == '') {
179 $errors['subject'] = get_string('emptytitle', 'blog');
180 }
181 if ($post->summary == '') {
182 $errors['summary'] = get_string('emptybody', 'blog');
183 }
c2ee4e87 184
e96f2a77 185 if (!empty($errors)) {
186 return; // no saving
4a173181 187 }
e96f2a77 188
189 $post->module = 'blog';
190 $post->userid = $USER->id;
191 $post->lastmodified = time();
192 $post->created = time();
7d0e5a95 193
e96f2a77 194 // Insert the new blog entry.
195 if ($id = insert_record('post', $post)) {
196 $post->id = $id;
7d0e5a95 197 // add blog attachment
198 if ($post->attachment = blog_add_attachment($post, 'attachment',$message)) {
199 set_field("post", "attachment", $post->attachment, "id", $post->id);
200 }
e96f2a77 201 add_tags_info($post->id);
202 add_to_log(SITEID, 'blog', 'add', 'index.php?userid='.$post->userid.'&postid='.$posz->id, $post->subject);
203
204 } else {
205 error('There was an error adding this post in the database', $returnurl);
206 }
207
4a173181 208}
209
210/**
211 * @param . $post argument is a reference to the post object which is used to store information for the form
212 * @param . $bloginfo_arg argument is reference to a blogInfo object.
213 * @todo complete documenting this function. enable trackback and pingback between entries on the same server
214 */
e96f2a77 215function do_edit(&$post, &$errors) {
216
217 global $CFG, $USER, $returnurl;
218
219 $post->id = required_param('id', PARAM_INT);
220 $post->subject = required_param('subject', PARAM_MULTILANG);
221 $post->summary = required_param('summary', PARAM_RAW);
222 $post->format = required_param('format', PARAM_INT);
223 $post->publishstate = required_param('publishstate', PARAM_ALPHA);;
224
225 if ($post->summary == '<br />') {
226 $post->summary = '';
c2ee4e87 227 }
4a173181 228
e96f2a77 229 if ($post->subject == '') {
230 $errors['subject'] = get_string('emptytitle', 'blog');
231 }
232 if ($post->summary == '') {
233 $errors['summary'] = get_string('emptybody', 'blog');
234 }
235
236 if (!empty($errors)) {
237 return; // no saving
238 }
239
240 $post->lastmodified = time();
7d0e5a95 241
242 if ($newfilename = blog_add_attachment($post, 'attachment',$message)) {
243 $post->attachment = $newfilename;
244 } else {
245 unset($post->attachment);
246 }
e96f2a77 247 // update record
248 if (update_record('post', $post)) {
91e56853 249 // delete all tags associated with this entry
e96f2a77 250 delete_records('blog_tag_instance', 'entryid', $post->id);
91e56853 251 // add them back
e96f2a77 252 add_tags_info($post->id);
7d0e5a95 253
254
e96f2a77 255 add_to_log(SITEID, 'blog', 'update', 'index.php?userid='.$post->userid.'&postid='.$post->id, $post->subject);
256
257 } else {
258 error('There was an error updating this post in the database', $returnurl);
259 }
260
261}
262
91e56853 263/**
264 * function to attach tags into a post
265 * @param int postid - id of the blog
266 */
e96f2a77 267function add_tags_info($postid) {
91e56853 268
269 global $USER;
270
e96f2a77 271 $post = get_record('post', 'id', $postid);
272
273 $tag = new object();
274 $tag->entryid = $post->id;
275 $tag->userid = $post->userid;
91e56853 276 $tag->timemodified = time();
1ca20e02 277
91e56853 278 /// Attach official tags
279 if ($otags = optional_param('otags','', PARAM_INT)) {
280 foreach ($otags as $otag) {
281 $tag->tagid = $otag;
e96f2a77 282 insert_record('blog_tag_instance', $tag);
4a173181 283 }
e96f2a77 284 }
285
91e56853 286 /// Attach Personal Tags
287 if ($ptags = optional_param('ptags','', PARAM_NOTAGS)) {
288 $ptags = explode(',',$ptags);
e96f2a77 289 foreach ($ptags as $ptag) {
91e56853 290 $ptag = trim($ptag);
291 // check for existance
292 // it does not matter whether it is an offical tag or personal tag
293 // we do not want to have 1 copy of offical tag and 1 copy of personal tag (for the same tag)
294 if ($ctag = get_record('tags', 'text', $ptag)) {
295 $tag->tagid = $ctag->id;
296 insert_record('blog_tag_instance', $tag);
297 } else { // create a personal tag
298 $ctag = new object;
299 $ctag->userid = $USER->id;
300 $ctag->text = $ptag;
301 $ctag->type = 'personal';
302 if ($tagid = insert_record('tags', $ctag)) {
303 $tag->tagid = $tagid;
304 insert_record('blog_tag_instance', $tag);
305 }
306 }
4a173181 307 }
4a173181 308 }
309}
d7bf6d17 310?>