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!');
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
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
;
39 if (!has_capability('moodle/blog:create', $sitecontext)) {
40 print_error('nopost', 'blog'); // manageentries is not enough for adding
44 $returnurl = 'index.php?userid='.$USER->id
;
46 if (!empty($courseid)) {
47 $returnurl .= '&courseid='.$courseid;
51 $strblogs = get_string('blogs','blog');
53 if ($action=='delete'){
55 error('Incorrect blog post id');
57 if (data_submitted() and $confirm and confirm_sesskey()) {
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);
66 notice_yesno(get_string('blogdeleteconfirm', 'blog'), 'edit.php', 'index.php', $optionsyes, $optionsno, 'post', 'get');
72 require_once('edit_form.php');
73 $blogeditform = new blog_edit_form(null, compact('existing', 'sitecontext'));
75 if ($blogeditform->is_cancelled()){
77 } else if ($fromform = $blogeditform->get_data()){
81 do_add($fromform, $blogeditform);
86 error('Incorrect blog post id');
88 do_edit($fromform, $blogeditform);
91 error('Unknown action!');
100 // prepare new empty form
101 $post->publishstate
= 'site';
102 $strformheading = get_string('addnewentry', 'blog');
103 $post->action
= $action;
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);
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');
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();
150 /***************************** edit.php functions ***************************/
153 * Delete blog post from database
155 function do_delete($post) {
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
);
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)) {
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
);
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;
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);
224 add_tags_info($post->id
);
226 add_to_log(SITEID
, 'blog', 'update', 'index.php?userid='.$USER->id
.'&postid='.$post->id
, $post->subject
);
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) {
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);