Updated the 19 build version to 20100806
[moodle.git] / blog / edit.php
blob367a1508c804553c07ada30accdd7b6e6471422a
1 <?php //$Id$
3 require_once('../config.php');
4 include_once('lib.php');
5 include_once($CFG->dirroot.'/tag/lib.php');
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
12 require_login($courseid);
14 if (empty($CFG->bloglevel)) {
15 error('Blogging is disabled!');
18 if (isguest()) {
19 print_error('noguestpost', 'blog');
22 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
23 if (!has_capability('moodle/blog:create', $sitecontext) and !has_capability('moodle/blog:manageentries', $sitecontext)) {
24 error('You can not post or edit blogs.');
27 // Make sure that the person trying to edit have access right
28 if ($id) {
29 if (!$existing = get_record('post', 'id', $id)) {
30 error('Wrong blog post id');
33 if (!blog_user_can_edit_post($existing)) {
34 print_error('notallowedtoedit', 'blog');
36 $userid = $existing->userid;
37 $returnurl = $CFG->wwwroot.'/blog/index.php?userid='.$existing->userid;
38 } else {
39 if (!has_capability('moodle/blog:create', $sitecontext)) {
40 print_error('nopost', 'blog'); // manageentries is not enough for adding
42 $existing = false;
43 $userid = $USER->id;
44 $returnurl = 'index.php?userid='.$USER->id;
46 if (!empty($courseid)) {
47 $returnurl .= '&amp;courseid='.$courseid;
51 $strblogs = get_string('blogs','blog');
53 if ($action=='delete'){
54 if (!$existing) {
55 error('Incorrect blog post id');
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;
72 require_once('edit_form.php');
73 $blogeditform = new blog_edit_form(null, compact('existing', 'sitecontext'));
75 if ($blogeditform->is_cancelled()){
76 redirect($returnurl);
77 } else if ($fromform = $blogeditform->get_data()){
78 //save stuff in db
79 switch ($action) {
80 case 'add':
81 do_add($fromform, $blogeditform);
82 break;
84 case 'edit':
85 if (!$existing) {
86 error('Incorrect blog post id');
88 do_edit($fromform, $blogeditform);
89 break;
90 default :
91 error('Unknown action!');
93 redirect($returnurl);
97 // gui setup
98 switch ($action) {
99 case 'add':
100 // prepare new empty form
101 $post->publishstate = 'site';
102 $strformheading = get_string('addnewentry', 'blog');
103 $post->action = $action;
104 break;
106 case 'edit':
107 if (!$existing) {
108 error('Incorrect blog post id');
110 $post->id = $existing->id;
111 $post->subject = clean_text($existing->subject);
112 $post->summary = clean_text($existing->summary, $existing->format);
113 $post->publishstate = $existing->publishstate;
114 $post->format = $existing->format;
115 $post->action = $action;
116 $strformheading = get_string('updateentrywithid', 'blog');
118 if ($itemptags = tag_get_tags_csv('post', $post->id, TAG_RETURN_TEXT, 'default')) {
119 $post->ptags = $itemptags;
122 if ($itemotags = tag_get_tags_array('post', $post->id, 'official')) {
123 $post->otags = array_keys($itemotags);
125 break;
126 default :
127 error('Unknown action!');
130 // done here in order to allow deleting of posts with wrong user id above
131 if (!$user = get_record('user', 'id', $userid)) {
132 error('Incorrect user id');
134 $navlinks = array();
135 $navlinks[] = array('name' => fullname($user), 'link' => "$CFG->wwwroot/user/view.php?id=$userid", 'type' => 'misc');
136 $navlinks[] = array('name' => $strblogs, 'link' => "$CFG->wwwroot/blog/index.php?userid=$userid", 'type' => 'misc');
137 $navlinks[] = array('name' => $strformheading, 'link' => null, 'type' => 'misc');
138 $navigation = build_navigation($navlinks);
140 print_header("$SITE->shortname: $strblogs", $SITE->fullname, $navigation,'','',true);
141 $blogeditform->set_data($post);
142 $blogeditform->display();
145 print_footer();
148 die;
150 /***************************** edit.php functions ***************************/
153 * Delete blog post from database
155 function do_delete($post) {
156 global $returnurl;
158 $status = delete_records('post', 'id', $post->id);
159 //$status = delete_records('blog_tag_instance', 'entryid', $post->id) and $status;
160 tag_set('post', $post->id, array());
162 blog_delete_old_attachments($post);
164 add_to_log(SITEID, 'blog', 'delete', 'index.php?userid='. $post->userid, 'deleted blog entry with entry id# '. $post->id);
166 if (!$status) {
167 error('Error occured while deleting post', $returnurl);
172 * Write a new blog entry into database
174 function do_add($post, $blogeditform) {
175 global $CFG, $USER, $returnurl;
177 $post->module = 'blog';
178 $post->userid = $USER->id;
179 $post->lastmodified = time();
180 $post->created = time();
182 // Insert the new blog entry.
183 if ($id = insert_record('post', $post)) {
184 $post->id = $id;
185 // add blog attachment
186 $dir = blog_file_area_name($post);
187 if ($blogeditform->save_files($dir) and $newfilename = $blogeditform->get_new_filename()) {
188 set_field("post", "attachment", $newfilename, "id", $post->id);
190 add_tags_info($post->id);
191 add_to_log(SITEID, 'blog', 'add', 'index.php?userid='.$post->userid.'&postid='.$post->id, $post->subject);
193 } else {
194 error('There was an error adding this post in the database', $returnurl);
200 * @param . $post argument is a reference to the post object which is used to store information for the form
201 * @param . $bloginfo_arg argument is reference to a blogInfo object.
202 * @todo complete documenting this function. enable trackback and pingback between entries on the same server
204 function do_edit($post, $blogeditform) {
206 global $CFG, $USER, $returnurl;
209 $post->lastmodified = time();
211 $dir = blog_file_area_name($post);
212 if ($blogeditform->save_files($dir) and $newfilename = $blogeditform->get_new_filename()) {
213 $post->attachment = $newfilename;
216 // update record
217 if (update_record('post', $post)) {
218 // delete all tags associated with this entry
220 //delete_records('blog_tag_instance', 'entryid', $post->id);
221 //delete_records('tag_instance', 'itemid', $post->id, 'itemtype', 'blog');
222 //untag_an_item('post', $post->id);
223 // add them back
224 add_tags_info($post->id);
226 add_to_log(SITEID, 'blog', 'update', 'index.php?userid='.$USER->id.'&postid='.$post->id, $post->subject);
228 } else {
229 error('There was an error updating this post in the database', $returnurl);
234 * function to attach tags into a post
235 * @param int postid - id of the blog
237 function add_tags_info($postid) {
239 $tags = array();
240 if ($otags = optional_param('otags', '', PARAM_INT)) {
241 foreach ($otags as $tagid) {
242 // TODO : make this use the tag name in the form
243 if ($tag = tag_get('id', $tagid)) {
244 $tags[] = $tag->name;
249 $manual_tags = optional_param('ptags', '', PARAM_NOTAGS);
250 $tags = array_merge($tags, explode(',', $manual_tags));
252 tag_set('post', $postid, $tags);