MDL-10833 some more course theme fixes; merged from MOODLE_18_STABLE
[moodle-pu.git] / blog / edit.php
blob1e17482f2c181c4748151625362d77eeadd91517
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();
14 if (empty($CFG->bloglevel)) {
15 error('Blogging is disabled!');
18 if (isguest()) {
19 error(get_string('noguestpost', 'blog'));
22 $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
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 error(get_string('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 error(get_string('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 ($blogeditform->no_submit_button_pressed()) {
78 no_submit_button_actions($blogeditform, $sitecontext);
81 } else if ($fromform = $blogeditform->get_data()){
82 //save stuff in db
83 switch ($action) {
84 case 'add':
85 do_add($fromform, $blogeditform);
86 break;
88 case 'edit':
89 if (!$existing) {
90 error('Incorrect blog post id');
92 do_edit($fromform, $blogeditform);
93 break;
94 default :
95 error('Unknown action!');
97 redirect($returnurl);
101 // gui setup
102 switch ($action) {
103 case 'add':
104 // prepare new empty form
105 $post->publishstate = 'site';
106 $strformheading = get_string('addnewentry', 'blog');
107 $post->action = $action;
108 break;
110 case 'edit':
111 if (!$existing) {
112 error('Incorrect blog post id');
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;
120 $strformheading = get_string('updateentrywithid', 'blog');
122 if ($ptags = records_to_menu(get_item_tags('blog', $post->id, 'ti.ordering ASC', 'id,rawname', '', '', 'default'), 'id','rawname')) {
123 $post->ptags = implode(', ', $ptags);
124 } else {
125 $post->ptags = '';
128 if ($otags = records_to_menu(get_item_tags('blog', $post->id, 'ti.ordering ASC', 'id,rawname', '', '', 'official'), 'id','rawname')) {
129 $post->otags = array_keys($otags);
131 break;
132 default :
133 error('Unknown action!');
136 // done here in order to allow deleting of posts with wrong user id above
137 if (!$user = get_record('user', 'id', $userid)) {
138 error('Incorrect user id');
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);
146 print_header("$SITE->shortname: $strblogs", $SITE->fullname, $navigation,'','',true);
147 $blogeditform->set_data($post);
148 $blogeditform->display();
151 print_footer();
154 die;
156 /***************************** edit.php functions ***************************/
157 function no_submit_button_actions(&$blogeditform, $sitecontext){
158 $mform =& $blogeditform->_form;
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']);
165 if (!empty($error)){
166 $mform->setElementError('otagsgrp', $error);
168 if (!empty($data['deleteotags']) && !empty($data['otags'])){ // adding official tag
169 delete_otags($data['otags'], $sitecontext);
171 $blogeditform->otags_select_setup();
174 function delete_otags($tagids, $sitecontext){
175 foreach ($tagids as $tagid) {
177 if (!$tag = tag_by_id($tagid)) {
178 error('Can not delete tag, tag doesn\'t exist');
180 if ($tag->tagtype != 'official') {
181 continue;
183 if ($tag->tagtype == 'official' and !has_capability('moodle/blog:manageofficialtags', $sitecontext)) {
184 //can not delete
185 error('Can not delete tag, you don\'t have permission to delete an official tag');
188 // Delete the tag itself
189 if (!tag_delete($tagid)) {
190 error('Can not delete tag');
195 function add_otag($otag){
196 global $USER;
197 $error = '';
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') {
203 // official tag already exist
204 $error = get_string('tagalready');
205 break;
206 } else {
207 // might not want to do this anymore?
208 $tag->tagtype = 'official';
209 update_record('tag', $tag);
210 $tagid = $tag->id;
213 } else { // Brand new offical tag
214 $tagid = tag_create($otag, 'official');
215 if (empty($tagid)) {
216 error('Can not create tag!');
219 return $error;
223 * Delete blog post from database
225 function do_delete($post) {
226 global $returnurl;
228 $status = delete_records('post', 'id', $post->id);
229 //$status = delete_records('blog_tag_instance', 'entryid', $post->id) and $status;
230 untag_an_item('blog', $post->id);
232 blog_delete_old_attachments($post);
234 add_to_log(SITEID, 'blog', 'delete', 'index.php?userid='. $post->userid, 'deleted blog entry with entry id# '. $post->id);
236 if (!$status) {
237 error('Error occured while deleting post', $returnurl);
242 * Write a new blog entry into database
244 function do_add($post, $blogeditform) {
245 global $CFG, $USER, $returnurl;
247 $post->module = 'blog';
248 $post->userid = $USER->id;
249 $post->lastmodified = time();
250 $post->created = time();
252 // Insert the new blog entry.
253 if ($id = insert_record('post', $post)) {
254 $post->id = $id;
255 // add blog attachment
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);
260 add_tags_info($post->id);
261 add_to_log(SITEID, 'blog', 'add', 'index.php?userid='.$post->userid.'&postid='.$post->id, $post->subject);
263 } else {
264 error('There was an error adding this post in the database', $returnurl);
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
274 function do_edit($post, $blogeditform) {
276 global $CFG, $USER, $returnurl;
279 $post->lastmodified = time();
281 $dir = blog_file_area_name($post);
282 if ($blogeditform->save_files($dir) and $newfilename = $blogeditform->get_new_filename()) {
283 $post->attachment = $newfilename;
286 // update record
287 if (update_record('post', $post)) {
288 // delete all tags associated with this entry
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);
293 // add them back
294 add_tags_info($post->id);
296 add_to_log(SITEID, 'blog', 'update', 'index.php?userid='.$post->userid.'&postid='.$post->id, $post->subject);
298 } else {
299 error('There was an error updating this post in the database', $returnurl);
304 * function to attach tags into a post
305 * @param int postid - id of the blog
307 function add_tags_info($postid) {
309 global $USER;
311 $post = get_record('post', 'id', $postid);
313 /// Attach official tags
314 if ($otags = optional_param('otags', '', PARAM_INT)) {
315 foreach ($otags as $otag) {
316 $tag->tagid = $otag;
317 //insert_record('blog_tag_instance', $tag);
318 tag_an_item('blog', $postid, $otag, 'official');
322 /// Attach Personal Tags
323 if ($ptags = optional_param('ptags', '', PARAM_NOTAGS)) {
324 $ptags = explode(',', $ptags);
325 foreach ($ptags as $ptag) {
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)
330 if ($ctag = tag_by_id($ptag)) {
331 tag_an_item('blog', $postid, $ctag);
332 } else { // create a personal tag
333 if ($tagid = tag_create($ptag)) {
334 if ($tagid = array_shift($tagid)) {
335 tag_an_item('blog', $postid, $tagid);