MDL-9345:
[moodle-linuxchix.git] / blog / edit.php
blob51a45295cb9b1a9c0a0921be5e27808b490b465f
1 <?php //$Id$
3 require_once('../config.php');
4 include_once('lib.php');
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
11 require_login();
13 if (empty($CFG->bloglevel)) {
14 error('Blogging is disabled!');
17 if (isguest()) {
18 error(get_string('noguestpost', 'blog'));
21 $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
22 if (!has_capability('moodle/blog:create', $sitecontext) and !has_capability('moodle/blog:manageentries', $sitecontext)) {
23 error('You can not post or edit blogs.');
26 // Make sure that the person trying to edit have access right
27 if ($id) {
28 if (!$existing = get_record('post', 'id', $id)) {
29 error('Wrong blog post id');
32 if (!blog_user_can_edit_post($existing)) {
33 error(get_string('notallowedtoedit', 'blog'));
35 $userid = $existing->userid;
36 $returnurl = $CFG->wwwroot.'/blog/index.php?userid='.$existing->userid;
37 } else {
38 if (!has_capability('moodle/blog:create', $sitecontext)) {
39 error(get_string('nopost', 'blog')); // manageentries is not enough for adding
41 $existing = false;
42 $userid = $USER->id;
43 $returnurl = 'index.php?userid='.$USER->id;
45 if (!empty($courseid)) {
46 $returnurl .= '&amp;courseid='.$courseid;
50 $strblogs = get_string('blogs','blog');
52 if ($action=='delete'){
53 if (!$existing) {
54 error('Incorrect blog post id');
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;
71 require_once('edit_form.php');
72 $blogeditform = new blog_edit_form(null, compact('existing', 'sitecontext'));
74 if ($blogeditform->is_cancelled()){
75 redirect($returnurl);
76 } else if ($blogeditform->no_submit_button_pressed()) {
77 no_submit_button_actions($blogeditform, $sitecontext);
80 } else if ($fromform = $blogeditform->get_data()){
81 //save stuff in db
82 switch ($action) {
83 case 'add':
84 do_add($fromform, $blogeditform);
85 break;
87 case 'edit':
88 if (!$existing) {
89 error('Incorrect blog post id');
91 do_edit($fromform, $blogeditform);
92 break;
93 default :
94 error('Unknown action!');
96 redirect($returnurl);
100 // gui setup
101 switch ($action) {
102 case 'add':
103 // prepare new empty form
104 $post->publishstate = 'draft';
105 $strformheading = get_string('addnewentry', 'blog');
106 $post->action = $action;
107 break;
109 case 'edit':
110 if (!$existing) {
111 error('Incorrect blog post id');
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;
119 $strformheading = get_string('updateentrywithid', 'blog');
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}")) {
128 $post->ptags = implode(', ', $ptags);
129 } else {
130 //$idsql = " AND bti.entryid = 0";
131 //was used but seems redundant.
132 $post->ptags = '';
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);
142 break;
143 default :
144 error('Unknown action!');
147 // done here in order to allow deleting of posts with wrong user id above
148 if (!$user = get_record('user', 'id', $userid)) {
149 error('Incorrect user id');
151 $navlinks = array();
152 $navlinks[] = array('name' => fullname($user), 'link' => "$CFG->wwwroot/user/view.php?id=$userid", 'type' => 'misc');
153 $navlinks[] = array('name' => $strblogs, 'link' => "$CFG->wwwroot/blog/index.php?userid=$userid", 'type' => 'misc');
154 $navlinks[] = array('name' => $strformheading, 'link' => null, 'type' => 'misc');
155 $navigation = build_navigation($navlinks);
157 print_header("$SITE->shortname: $strblogs", $SITE->fullname, $navigation,'','',true);
158 $blogeditform->set_data($post);
159 $blogeditform->display();
162 print_footer();
165 die;
167 /***************************** edit.php functions ***************************/
168 function no_submit_button_actions(&$blogeditform, $sitecontext){
169 $mform =& $blogeditform->_form;
170 $data = $mform->exportValues();
171 //sesskey has been checked already no need to check that
172 //check for official tags to add
173 if (!empty($data['addotags']) && !empty($data['otagsadd'])){ // adding official tag
174 $error = add_otag($data['otagsadd']);
176 if (!empty($error)){
177 $mform->setElementError('otagsgrp', $error);
179 if (!empty($data['deleteotags']) && !empty($data['otags'])){ // adding official tag
180 delete_otags($data['otags'], $sitecontext);
182 $blogeditform->otags_select_setup();
185 function delete_otags($tagids, $sitecontext){
186 foreach ($tagids as $tagid) {
188 if (!$tag = get_record('tags', 'id', $tagid)) {
189 error('Can not delete tag, tag doesn\'t exist');
192 if ($tag->type == 'official' and !has_capability('moodle/blog:manageofficialtags', $sitecontext)) {
193 //can not delete
194 error('Can not delete tag, you don\'t have permission to delete an official tag');
197 if ($tag->type == 'personal' and !has_capability('moodle/blog:managepersonaltags', $sitecontext)) {
198 //can not delete
199 error('Can not delete tag, you don\'t have permission to delete a personal tag');
202 // Delete the tag itself
203 if (!delete_records('tags', 'id', $tagid)) {
204 error('Can not delete tag');
207 // Deleteing all references to this tag
208 if (!delete_records('blog_tag_instance', 'tagid', $tagid)) {
209 error('Can not delete blog tag instances');
216 function add_otag($otag){
217 global $USER;
218 $error = '';
219 if ($tag = get_record('tags', 'text', $otag)) {
220 if ($tag->type == 'official') {
221 // official tag already exist
222 $error = get_string('tagalready');
223 } else {
224 $tag->type = 'official';
225 update_record('tags', $tag);
226 $tagid = $tag->id;
228 } else { // Brand new offical tag
230 $tag = new object();
231 $tag->userid = $USER->id;
232 $tag->text = $otag;
233 $tag->type = 'official';
235 if (!$tagid = insert_record('tags', $tag)) {
236 error('Can not create tag!');
239 return $error;
243 * Delete blog post from database
245 function do_delete($post) {
246 global $returnurl;
248 $status = delete_records('post', 'id', $post->id);
249 $status = delete_records('blog_tag_instance', 'entryid', $post->id) and $status;
251 blog_delete_old_attachments($post);
253 add_to_log(SITEID, 'blog', 'delete', 'index.php?userid='. $post->userid, 'deleted blog entry with entry id# '. $post->id);
255 if (!$status) {
256 error('Error occured while deleting post', $returnurl);
261 * Write a new blog entry into database
263 function do_add($post, $blogeditform) {
264 global $CFG, $USER, $returnurl;
266 $post->module = 'blog';
267 $post->userid = $USER->id;
268 $post->lastmodified = time();
269 $post->created = time();
271 // Insert the new blog entry.
272 if ($id = insert_record('post', $post)) {
273 $post->id = $id;
274 // add blog attachment
275 $dir = blog_file_area_name($post);
276 if ($blogeditform->save_files($dir) and $newfilename = $blogeditform->get_new_filename()) {
277 set_field("post", "attachment", $newfilename, "id", $post->id);
279 add_tags_info($post->id);
280 add_to_log(SITEID, 'blog', 'add', 'index.php?userid='.$post->userid.'&postid='.$post->id, $post->subject);
282 } else {
283 error('There was an error adding this post in the database', $returnurl);
289 * @param . $post argument is a reference to the post object which is used to store information for the form
290 * @param . $bloginfo_arg argument is reference to a blogInfo object.
291 * @todo complete documenting this function. enable trackback and pingback between entries on the same server
293 function do_edit($post, $blogeditform) {
295 global $CFG, $USER, $returnurl;
298 $post->lastmodified = time();
300 $dir = blog_file_area_name($post);
301 if ($blogeditform->save_files($dir) and $newfilename = $blogeditform->get_new_filename()) {
302 $post->attachment = $newfilename;
305 // update record
306 if (update_record('post', $post)) {
307 // delete all tags associated with this entry
308 delete_records('blog_tag_instance', 'entryid', $post->id);
309 // add them back
310 add_tags_info($post->id);
313 add_to_log(SITEID, 'blog', 'update', 'index.php?userid='.$post->userid.'&postid='.$post->id, $post->subject);
315 } else {
316 error('There was an error updating this post in the database', $returnurl);
321 * function to attach tags into a post
322 * @param int postid - id of the blog
324 function add_tags_info($postid) {
326 global $USER;
328 $post = get_record('post', 'id', $postid);
330 $tag = new object();
331 $tag->entryid = $post->id;
332 $tag->userid = $post->userid;
333 $tag->timemodified = time();
335 /// Attach official tags
336 if ($otags = optional_param('otags', '', PARAM_INT)) {
337 foreach ($otags as $otag) {
338 $tag->tagid = $otag;
339 insert_record('blog_tag_instance', $tag);
343 /// Attach Personal Tags
344 if ($ptags = optional_param('ptags', '', PARAM_NOTAGS)) {
345 $ptags = explode(',', $ptags);
346 foreach ($ptags as $ptag) {
347 $ptag = trim($ptag);
348 // check for existance
349 // it does not matter whether it is an offical tag or personal tag
350 // we do not want to have 1 copy of offical tag and 1 copy of personal tag (for the same tag)
351 if ($ctag = get_record('tags', 'text', $ptag)) {
352 $tag->tagid = $ctag->id;
353 insert_record('blog_tag_instance', $tag);
354 } else { // create a personal tag
355 $ctag = new object;
356 $ctag->userid = $USER->id;
357 $ctag->text = $ptag;
358 $ctag->type = 'personal';
359 if ($tagid = insert_record('tags', $ctag)) {
360 $tag->tagid = $tagid;
361 insert_record('blog_tag_instance', $tag);