Moved some strings!
[moodle.git] / blog / edit.php
CommitLineData
4a173181 1<?php //$Id$
2
3require_once('../config.php');
4include_once('lib.php');
5include_once('class.BlogInfo.php');
6require_login();
7// detemine where the user is coming from in case we need to send them back there
8if (isset($_SERVER['HTTP_REFERER'])) {
9 $referrer = $_SERVER['HTTP_REFERER'];
10} else {
11 $referrer = $CFG->wwwroot;
12}
13
14//first verify that user is not a guest
15if (isguest()) {
16 error(get_string('noguestpost', 'forum'), $referrer);
17}
18
9154b440 19$userid = optional_param('userid', 0);
20$editid = optional_param('editid', '');
4a173181 21
22global $USER, $CFG;
23
24//check to see if there is a requested blog to edit
25if (!empty($userid) && $userid != 0) {
26 if (blog_isLoggedIn() && $userid == $USER->id ) {
27 ; // Daryl Hawes note: is this a placeholder for missing functionality?
28 }
29} else if ( blog_isLoggedIn() ) {
30 //the user is logged in and have not specified a blog - so they will be editing their own
31 $tempBlogInfo = blog_user_bloginfo();
32 $userid = $tempBlogInfo->userid;
33 unset($tempBlogInfo); //free memory from temp object - bloginfo will be created again in the included header
34} else {
35 error(get_string('noblogspecified', 'blog') .'<a href="'. $CFG->blog_blogurl .'">' .get_string('viewentries', 'blog') .'</a>');
36}
37
38$pageNavigation = 'edit';
39
40include($CFG->dirroot .'/blog/header.php');
41
4a173181 42//print_object($PAGE->bloginfo); //debug
43
44//check if user is in blog's acl
45if ( !blog_user_has_rights($PAGE->bloginfo) ) {
46 if ($editid != '') {
47 $blogEntry = $PAGE->bloginfo->get_blog_entry_by_id($editid);
48 if (! (isteacher($blogEntry->$entryCourseId)) ) {
49// error( get_string('notallowedtoedit'.' You do not teach in this course.', 'blog'), $CFG->wwwroot .'/login/index.php');
50 error( get_string('notallowedtoedit', 'blog'), $CFG->wwwroot .'/login/index.php');
51 }
52 } else {
53 error( get_string('notallowedtoedit', 'blog'), $CFG->wwwroot .'/login/index.php');
54 }
55}
56
57//////////// SECURITY AND SETUP COMPLETE - NOW PAGE LOGIC ///////////////////
58
59if (isset($act) && $act == 'del')
60{
61 require_variable($postid);
62 do_delete($PAGE->bloginfo, $postid);
63}
64if ($usehtmleditor = can_use_richtext_editor()) {
65 $defaultformat = FORMAT_HTML;
66 $onsubmit = '';
67} else {
68 $defaultformat = FORMAT_MOODLE;
69 $onsubmit = '';
70}
71
9154b440 72if (($post = data_submitted( get_referer() )) && confirm_sesskey()) {
4a173181 73 if (!empty($post->editform)) { //make sure we're processing the edit form here
9154b440 74 print_object($post); //debug
4a173181 75
76 ///these varaibles needs to be changed because of the javascript hack
77 ///post->courseid
78 ///post->groupid
79 $post->courseid = $post->realcourse; //might not need either, if javascript re-written
80 $post->groupid = $post->realgroup; //might not need
81 $courseid = $post->realcourse;
82 //end of yu's code
83
84 if (!$post->etitle or !$post->body) {
85 $post->error = get_string('emptymessage', 'forum');
86 }
87 if ($post->act == 'save') {
9154b440 88 do_save($post, $PAGE->bloginfo);
4a173181 89 } else if ($post->act == 'update') {
9154b440 90 do_update($post, $PAGE->bloginfo);
4a173181 91 } else if ($post->act == 'del') {
92 require_variable($postid);
93 do_delete($PAGE->bloginfo, $postid);
94 }
95 }
96} else {
97
98 //no post data yet, so load up the post array with default information
99 $post->etitle = '';
100 $post->userid = $USER->id;
101 $post->body = '';
102 $post->extendedbody = '';
103 $post->useextendedbody = $PAGE->bloginfo->get_blog_use_extended_body();
104 $post->format = $defaultformat;
105 $post->categoryid = array(1);
106 $post->publishstate = 'draft';
107 $post->courseid = $courseid;
108
109
110}
111
112if ($editid != '') { // User is editing a post
113 // ensure that editing is allowed first - admin users can edit any posts
114 if (!isadmin() && $CFG->blog_enable_moderation && $blogEntry->entryPublishState != 'draft') {
115 error('You are not allowed to modify a published entry. A teacher must first change this post back to draft status.'); //Daryl Hawes note: localize this line
116 }
117 $blogEntry = $PAGE->bloginfo->get_blog_entry_by_id($editid);
118
119 //using an unformatted entry body here so that extra formatting information is not stored in the db
120 $post->body = $blogEntry->get_unformatted_entry_body();
121 $post->extendedbody = $blogEntry->get_unformatted_entry_extended_body();
122 $post->useextendedbody = $PAGE->bloginfo->get_blog_use_extended_body();
123 $post->etitle = $blogEntry->entryTitle;
124 $post->postid = $editid;
125 $post->userid = $PAGE->bloginfo->userid;
126 $post->categoryid = $blogEntry->entryCategoryIds;
127 $post->format = $blogEntry->entryFormat;
128 $post->publishstate = $blogEntry->entryPublishState;
129 $post->courseid = $blogEntry->entryCourseId;
130 $post->groupid = (int)$blogEntry->entryGroupId;
131}
132
133if (isset($post->postid) && ($post->postid != -1) ) {
9154b440 134
135 $formHeading = get_string('updateentrywithid', 'blog');
136
4a173181 137} else {
138 $formHeading = get_string('addnewentry', 'blog');
139}
140
141if (isset($post->error)) {
142 notify($post->error);
143}
144
145print_simple_box_start("center");
146require('edit.html');
147print_simple_box_end();
148
149 // Janne comment: Let's move this in here
150 // so IE gets more time to load the
151 // Page.
152 if ($usehtmleditor) {
153 // Janne comment: there are two text fields in form
154 // so lets try to replace them both with
155 // HTMLArea editors
156 use_html_editor();
157 }
158
159include($CFG->dirroot .'/blog/footer.php');
160
161
162/***************************** edit.php functions ***************************/
163/*
164* do_delete
165* takes $bloginfo_arg argument as reference to a blogInfo object.
166* also takes the postid - the id of the entry to be removed
167*/
168function do_delete(&$bloginfo_arg, $postid) {
169 global $CFG;
170 // make sure this user is authorized to delete this entry.
171 // cannot use $post->pid because it may not have been initialized yet. Also the pid may be in get format rather than post.
172 if ($bloginfo_arg->delete_blog_entry_by_id($postid)) {
173 //echo "bloginfo_arg:"; //debug
174 print_object($bloginfo_arg); //debug
175 //echo "pid to delete:".$postid; //debug
176 delete_records('blog_tag_instance', 'entryid', $postid);
177 print '<strong>'. get_string('entrydeleted', 'blog') .'</strong><p>';
178
179 //record a log message of this entry deletion
180 if ($site = get_site()) {
181 add_to_log($site->id, 'blog', 'delete', 'index.php?userid='. $bloginfo_arg->userid, 'deleted blog entry with entry id# '. $postid);
182 }
183 } else {
184 error(get_string('entryerrornotyours', 'blog'));
185 }
186
187 //comment out this redirect to debug the deletion of entries
188 redirect($CFG->wwwroot .'/blog/index.php?userid='. $bloginfo_arg->userid);
189}
190
191/**
192* do_save
193*
194* @param object $post argument is a reference to the post object which is used to store information for the form
195* @param object $bloginfo_arg argument is reference to a blogInfo object.
196*/
9154b440 197function do_save(&$post, &$bloginfo_arg) {
4a173181 198 global $USER, $CFG;
199// echo 'Debug: Post object in do_save function of edit.php<br />'; //debug
200// print_object($post); //debug
201
202 if ($post->body == '') {
203 $post->error = get_string('nomessagebodyerror', 'blog');
204 } else {
205
206 //initialize courseid and groupid if specified
207 if (isset($post->courseid)) {
208 $courseid = $post->courseid;
209 } else {
210 $courseid = 1;
211 }
212 if (isset($post->groupid)) {
213 $groupid = $post->groupid;
214 } else {
215 $groupid = '';
216 }
217
218/*
219 //group pseudocode
220 if ($groupid != '') {
221 if (! ismember($post->groupid) ) {
222 error('You are not a member of the specified group. Group with id#('.$groupid.')'); //Daryl Hawes note: LOCALIZATION NEEDED FOR THIS LINE
223 }
224 }*/
225
226 // Insert the new blog entry.
227 $entryID = $bloginfo_arg->insert_blog_entry($post->etitle, $post->body, $post->extendedbody, $USER->id, $post->format, $post->publishstate, $courseid, $groupid);
228
229// print 'Debug: created a new entry - entryId = '.$entryID.'<br />'; //debug
230// echo 'Debug: do_save() in edit.php calling blog_do_*back_pings<br />'."\n"; //debug
231 $otags = optional_param('otags');
232 $ptags = optional_param('ptags');
233 // Add tags information
234 foreach ($otags as $otag) {
235 $tag->entryid = $entryID;
236 $tag->tagid = $otag;
237 $tag->groupid = $groupid;
238 $tag->courseid = $courseid;
239 $tag->userid = $USER->id;
240
241 insert_record('blog_tag_instance',$tag);
242 }
243
244 foreach ($ptags as $ptag) {
245 $tag->entryid = $entryID;
246 $tag->tagid = $ptag;
247 $tag->groupid = $groupid;
248 $tag->courseid = $courseid;
249 $tag->userid = $USER->id;
250
251 insert_record('blog_tag_instance',$tag);
252 }
253
254 print '<strong>'. get_string('entrysaved', 'blog') .'</strong><br />';
255 //record a log message of this entry addition
256 if ($site = get_site()) {
257 add_to_log($site->id, 'blog', 'add', 'archive.php?userid='. $bloginfo_arg->userid .'&postid='. $entryID, 'created new blog entry with entry id# '. $entryID);
258 }
259 //to debug this save function comment out the following redirect code
9154b440 260 if ($courseid == SITEID || $courseid == 0 || $courseid == '') {
4a173181 261 redirect($CFG->wwwroot .'/blog/index.php?userid='. $bloginfo_arg->userid);
262 } else {
263 redirect($CFG->wwwroot .'/course/view.php?id='. $courseid);
264 }
265 }
266}
267
268/**
269 * @param . $post argument is a reference to the post object which is used to store information for the form
270 * @param . $bloginfo_arg argument is reference to a blogInfo object.
271 * @todo complete documenting this function. enable trackback and pingback between entries on the same server
272 */
9154b440 273function do_update(&$post, &$bloginfo) {
4a173181 274
275 global $CFG, $USER;
276
277 //initialize courseid and groupid if specified
278 if (isset($post->courseid)) {
279 $courseid = $post->courseid;
280 } else {
281 $courseid = 1;
282 }
283 if (isset($post->groupid)) {
284 $groupid = $post->groupid;
285 } else {
286 $groupid = '';
287 }
288
289/*
290 //pseudocode for handling groups
291 if ($groupid != '') {
292 if (! ismember($groupid) ) {
293 error('You are not a member of the specified group. Group with id#('. $groupid .')'); //Daryl Hawes note: LOCALIZATION NEEDED FOR THIS LINE
294 }
295 }*/
296
297 $blogentry = $bloginfo->get_blog_entry_by_id($post->postid);
298 echo "id id ".$post->postid;
299// print_object($blogentry); //debug
300
301 $blogentry->set_title($post->etitle);
302 $blogentry->set_body($post->body);
303 if (isset($post->extendedbody)) {
304 $blogentry->set_extendedbody($post->extendedbody);
305 }
306 $blogentry->set_format($post->format);
307 $blogentry->set_publishstate($post->publishstate); //we don't care about the return value here
308 $blogentry->set_courseid($courseid);
309 $blogentry->set_groupid($groupid);
310
311 if ( !$error = $blogentry->save() ) {
312// echo 'Debug: do_update in edit.php calling do_pings<br />'."\n"; //debug
313 delete_records('blog_tag_instance', 'entryid', $blogentry->entryId);
314
315 $otags = optional_param('otags');
316 $ptags = optional_param('ptags');
317 // Add tags information
318 foreach ($otags as $otag) {
319 $tag->entryid = $blogentry->entryId;
320 $tag->tagid = $otag;
321 $tag->groupid = $groupid;
322 $tag->courseid = $courseid;
323 $tag->userid = $USER->id;
324
325 insert_record('blog_tag_instance',$tag);
326 }
327
328 foreach ($ptags as $ptag) {
329 $tag->entryid = $blogentry->entryId;
330 $tag->tagid = $ptag;
331 $tag->groupid = $groupid;
332 $tag->courseid = $courseid;
333 $tag->userid = $USER->id;
334
335 insert_record('blog_tag_instance',$tag);
336 }
337 // only do pings if the entry is published to the world
338 // Daryl Hawes note - eventually should check if it's on the same server
339 // and if so allow pb/tb as well - especially now that moderation is in place
340 print '<strong>'. get_string('entryupdated', 'blog') .'</strong><p>';
341
342 //record a log message of this entry update action
343 if ($site = get_site()) {
344 add_to_log($site->id, 'blog', 'update', 'archive.php?userid='. $bloginfo->userid .'&postid='. $post->postid, 'updated existing blog entry with entry id# '. $post->postid);
345 }
346
347 redirect($CFG->wwwroot .'/blog/index.php?userid='. $bloginfo->userid);
348 } else {
349// get_string('', 'blog') //Daryl Hawes note: localize this line
350 $post->error = 'There was an error updating this post in the database: '. $error;
351 }
352}
353?>