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