2006-03-10 06:53:01 +00:00
< ? php //$Id$
require_once ( '../config.php' );
include_once ( 'lib.php' );
require_login ();
2006-04-10 07:27:03 +00:00
$courseid = optional_param ( 'courseid' , SITEID , PARAM_INT );
$act = optional_param ( 'act' , '' , PARAM_ALPHA );
2006-03-10 06:53:01 +00:00
// detemine where the user is coming from in case we need to send them back there
2006-04-12 03:02:53 +00:00
if ( ! $referrer = optional_param ( 'referrer' , '' , PARAM_URL )) {
if ( isset ( $_SERVER [ 'HTTP_REFERER' ])) {
$referrer = $_SERVER [ 'HTTP_REFERER' ];
} else {
$referrer = $CFG -> wwwroot ;
}
2006-03-10 06:53:01 +00:00
}
//first verify that user is not a guest
if ( isguest ()) {
2006-03-14 06:36:51 +00:00
error ( get_string ( 'noguestpost' , 'blog' ), $referrer );
2006-03-10 06:53:01 +00:00
}
2006-04-12 08:58:49 +00:00
// make sure that the person trying to edit have access right
if ( $editid = optional_param ( 'editid' , 0 , PARAM_INT )) {
$blogEntry = get_record ( 'post' , 'id' , $editid );
if ( ! blog_user_can_edit_post ( $blogEntry )) {
error ( get_string ( 'notallowedtoedit' , 'blog' ), $CFG -> wwwroot . '/login/index.php' );
2006-03-10 06:53:01 +00:00
}
2006-04-12 08:58:49 +00:00
}
//check to see if there is a requested blog to edit
if ( isloggedin () && ! isguest ()) {
2006-04-20 02:14:17 +00:00
$userid = $USER -> id ;
2006-03-10 06:53:01 +00:00
} else {
error ( get_string ( 'noblogspecified' , 'blog' ) . '<a href="' . $CFG -> blog_blogurl . '">' . get_string ( 'viewentries' , 'blog' ) . '</a>' );
}
2006-04-20 02:14:17 +00:00
// if we are trying to delete an non-existing blog entry
if ( isset ( $act ) && ( $act == 'del' ) && ( empty ( $blogEntry ))) {
error ( 'the entry you are trying to delete does not exist' );
}
2006-03-10 06:53:01 +00:00
$pageNavigation = 'edit' ;
include ( $CFG -> dirroot . '/blog/header.php' );
//////////// SECURITY AND SETUP COMPLETE - NOW PAGE LOGIC ///////////////////
2006-04-10 07:27:03 +00:00
if ( isset ( $act ) && ( $act == 'del' ) && confirm_sesskey ())
2006-03-10 06:53:01 +00:00
{
2006-04-12 02:05:46 +00:00
$postid = required_param ( 'editid' , PARAM_INT );
2006-03-14 06:56:58 +00:00
if ( optional_param ( 'confirm' , 0 , PARAM_INT )) {
2006-04-12 02:05:46 +00:00
do_delete ( $postid );
2006-03-14 06:36:51 +00:00
} else {
2006-04-12 08:58:49 +00:00
2006-03-14 06:36:51 +00:00
/// prints blog entry and what confirmation form
2006-03-14 06:56:58 +00:00
echo '<div align="center"><form method="GET" action="edit.php">' ;
echo '<input type="hidden" name="act" value="del" />' ;
echo '<input type="hidden" name="confirm" value="1" />' ;
2006-04-12 02:05:46 +00:00
echo '<input type="hidden" name="editid" value="' . $postid . '" />' ;
2006-03-14 06:56:58 +00:00
echo '<input type="hidden" name="sesskey" value="' . sesskey () . '" />' ;
2006-04-12 08:58:49 +00:00
2006-03-14 06:56:58 +00:00
print_string ( 'blogdeleteconfirm' , 'blog' );
2006-04-12 08:58:49 +00:00
blog_print_entry ( $blogEntry );
2006-03-14 06:56:58 +00:00
echo '<br />' ;
echo '<input type="submit" value="' . get_string ( 'delete' ) . '" /> ' ;
echo ' <input type="button" value="' . get_string ( 'cancel' ) . '" onclick="javascript:history.go(-1)" />' ;
echo '</form></div>' ;
print_footer ( $course );
2006-04-20 02:14:17 +00:00
exit ;
2006-03-14 06:36:51 +00:00
}
2006-03-10 06:53:01 +00:00
}
2006-04-20 02:14:17 +00:00
2006-03-10 06:53:01 +00:00
if ( $usehtmleditor = can_use_richtext_editor ()) {
$defaultformat = FORMAT_HTML ;
$onsubmit = '' ;
} else {
$defaultformat = FORMAT_MOODLE ;
$onsubmit = '' ;
}
2006-03-13 06:05:44 +00:00
if (( $post = data_submitted ( get_referer () )) && confirm_sesskey ()) {
2006-03-10 06:53:01 +00:00
if ( ! empty ( $post -> editform )) { //make sure we're processing the edit form here
2006-03-14 04:23:00 +00:00
//print_object($post); //debug
2006-03-10 06:53:01 +00:00
if ( ! $post -> etitle or ! $post -> body ) {
$post -> error = get_string ( 'emptymessage' , 'forum' );
}
if ( $post -> act == 'save' ) {
2006-04-12 02:05:46 +00:00
do_save ( $post );
2006-03-10 06:53:01 +00:00
} else if ( $post -> act == 'update' ) {
2006-04-12 02:05:46 +00:00
do_update ( $post );
2006-03-10 06:53:01 +00:00
} else if ( $post -> act == 'del' ) {
2006-04-06 12:56:37 +00:00
$postid = required_param ( 'postid' , PARAM_INT );
2006-04-12 02:05:46 +00:00
do_delete ( $postid );
2006-03-10 06:53:01 +00:00
}
}
} else {
//no post data yet, so load up the post array with default information
$post -> etitle = '' ;
$post -> userid = $USER -> id ;
$post -> body = '' ;
$post -> format = $defaultformat ;
$post -> publishstate = 'draft' ;
}
2006-04-20 02:14:17 +00:00
if ( $editid ) { // User is editing a post
2006-03-10 06:53:01 +00:00
// ensure that editing is allowed first - admin users can edit any posts
2006-03-17 05:53:52 +00:00
2006-04-12 02:05:46 +00:00
$blogEntry = get_record ( 'post' , 'id' , $editid );
2006-03-10 06:53:01 +00:00
//using an unformatted entry body here so that extra formatting information is not stored in the db
2006-04-20 02:26:23 +00:00
$post -> body = stripslashes_safe ( $blogEntry -> summary );
2006-04-20 02:25:09 +00:00
$post -> etitle = stripslashes_safe ( $blogEntry -> subject );
2006-03-10 06:53:01 +00:00
$post -> postid = $editid ;
2006-04-12 02:05:46 +00:00
$post -> userid = $blogEntry -> userid ;
$post -> format = $blogEntry -> format ;
$post -> publishstate = $blogEntry -> publishstate ;
2006-03-10 06:53:01 +00:00
}
if ( isset ( $post -> postid ) && ( $post -> postid != - 1 ) ) {
2006-03-13 06:05:44 +00:00
$formHeading = get_string ( 'updateentrywithid' , 'blog' );
2006-03-10 06:53:01 +00:00
} else {
$formHeading = get_string ( 'addnewentry' , 'blog' );
}
if ( isset ( $post -> error )) {
notify ( $post -> error );
}
print_simple_box_start ( " center " );
require ( 'edit.html' );
print_simple_box_end ();
// Janne comment: Let's move this in here
// so IE gets more time to load the
// Page.
if ( $usehtmleditor ) {
// Janne comment: there are two text fields in form
// so lets try to replace them both with
// HTMLArea editors
use_html_editor ();
}
include ( $CFG -> dirroot . '/blog/footer.php' );
/***************************** edit.php functions ***************************/
/*
* do_delete
* takes $bloginfo_arg argument as reference to a blogInfo object .
* also takes the postid - the id of the entry to be removed
*/
2006-04-12 02:05:46 +00:00
function do_delete ( $postid ) {
2006-04-12 03:02:53 +00:00
global $CFG , $USER , $referrer ;
2006-03-10 06:53:01 +00:00
// make sure this user is authorized to delete this entry.
// cannot use $post->pid because it may not have been initialized yet. Also the pid may be in get format rather than post.
2006-04-12 02:05:46 +00:00
// check ownership
2006-04-12 08:58:49 +00:00
$blogEntry = get_record ( 'post' , 'id' , $postid );
2006-04-12 02:05:46 +00:00
2006-04-18 01:59:13 +00:00
if ( blog_user_can_edit_post ( $blogEntry )) {
2006-04-12 02:05:46 +00:00
if ( delete_records ( 'post' , 'id' , $postid )) {
//echo "bloginfo_arg:"; //debug
//print_object($bloginfo_arg); //debug
//echo "pid to delete:".$postid; //debug
delete_records ( 'blog_tag_instance' , 'entryid' , $postid );
print '<strong>' . get_string ( 'entrydeleted' , 'blog' ) . '</strong><p>' ;
//record a log message of this entry deletion
if ( $site = get_site ()) {
2006-04-20 02:14:17 +00:00
add_to_log ( $site -> id , 'blog' , 'delete' , 'index.php?userid=' . $blogEntry -> userid , 'deleted blog entry with entry id# ' . $postid );
2006-04-12 02:05:46 +00:00
}
2006-03-10 06:53:01 +00:00
}
2006-04-12 02:05:46 +00:00
}
else {
2006-03-10 06:53:01 +00:00
error ( get_string ( 'entryerrornotyours' , 'blog' ));
}
//comment out this redirect to debug the deletion of entries
2006-04-12 03:02:53 +00:00
2006-04-18 01:59:13 +00:00
redirect ( $CFG -> wwwroot . '/blog/index.php?userid=' . $blogEntry -> userid );
2006-03-10 06:53:01 +00:00
}
/**
* do_save
*
* @ param object $post argument is a reference to the post object which is used to store information for the form
* @ param object $bloginfo_arg argument is reference to a blogInfo object .
*/
2006-04-12 02:05:46 +00:00
function do_save ( $post ) {
2006-04-12 03:02:53 +00:00
global $USER , $CFG , $referrer ;
2006-03-10 06:53:01 +00:00
// echo 'Debug: Post object in do_save function of edit.php<br />'; //debug
// print_object($post); //debug
if ( $post -> body == '' ) {
$post -> error = get_string ( 'nomessagebodyerror' , 'blog' );
} else {
2006-04-12 02:05:46 +00:00
/// Write a blog entry into database
$blogEntry = new object ;
$blogEntry -> subject = addslashes ( $post -> etitle );
$blogEntry -> summary = addslashes ( $post -> body );
$blogEntry -> module = 'blog' ;
$blogEntry -> userid = $USER -> id ;
$blogEntry -> format = $post -> format ;
$blogEntry -> publishstate = $post -> publishstate ;
$blogEntry -> lastmodified = time ();
$blogEntry -> created = time ();
2006-03-10 06:53:01 +00:00
// Insert the new blog entry.
2006-04-12 02:05:46 +00:00
$entryID = insert_record ( 'post' , $blogEntry );
2006-03-10 06:53:01 +00:00
// print 'Debug: created a new entry - entryId = '.$entryID.'<br />'; //debug
// echo 'Debug: do_save() in edit.php calling blog_do_*back_pings<br />'."\n"; //debug
2006-04-12 02:05:46 +00:00
if ( $entryID ) {
/// Creates a unique hash. I don't know what this is for (Yu)
$dataobject = new object ;
$dataobject -> uniquehash = md5 ( $blogEntry -> userid . $CFG -> wwwroot . $entryID );
update_record ( 'post' , $dataobject );
/// Associate tags with entries
2006-04-21 02:47:01 +00:00
2006-04-12 02:05:46 +00:00
$tag = NULL ;
$tag -> entryid = $entryID ;
$tag -> userid = $USER -> id ;
$tag -> timemodified = time ();
/// Add tags information
2006-04-21 02:47:01 +00:00
if ( $otags = optional_param ( 'otags' , '' , PARAM_INT )) {
foreach ( $otags as $otag ) {
$tag -> tagid = $otag ;
insert_record ( 'blog_tag_instance' , $tag );
}
2006-04-12 02:05:46 +00:00
}
2006-04-21 02:47:01 +00:00
if ( $ptags = optional_param ( 'ptags' , '' , PARAM_INT )) {
foreach ( $ptags as $ptag ) {
$tag -> tagid = $ptag ;
insert_record ( 'blog_tag_instance' , $tag );
}
2006-04-12 02:05:46 +00:00
}
print '<strong>' . get_string ( 'entrysaved' , 'blog' ) . '</strong><br />' ;
2006-03-10 06:53:01 +00:00
}
//record a log message of this entry addition
if ( $site = get_site ()) {
2006-04-20 02:14:17 +00:00
add_to_log ( $site -> id , 'blog' , 'add' , 'index.php?userid=' . $blogEntry -> userid . '&postid=' . $entryID , 'created new blog entry with entry id# ' . $entryID );
2006-03-10 06:53:01 +00:00
}
2006-04-12 03:02:53 +00:00
redirect ( $referrer );
/*
2006-03-10 06:53:01 +00:00
//to debug this save function comment out the following redirect code
2006-03-13 06:05:44 +00:00
if ( $courseid == SITEID || $courseid == 0 || $courseid == '' ) {
2006-04-12 02:05:46 +00:00
redirect ( $CFG -> wwwroot . '/blog/index.php?userid=' . $blogEntry -> userid );
2006-03-10 06:53:01 +00:00
} else {
redirect ( $CFG -> wwwroot . '/course/view.php?id=' . $courseid );
2006-04-12 03:02:53 +00:00
} */
2006-03-10 06:53:01 +00:00
}
}
/**
* @ param . $post argument is a reference to the post object which is used to store information for the form
* @ param . $bloginfo_arg argument is reference to a blogInfo object .
* @ todo complete documenting this function . enable trackback and pingback between entries on the same server
*/
2006-04-12 02:05:46 +00:00
function do_update ( $post ) {
// here post = data_submitted();
2006-04-12 03:02:53 +00:00
global $CFG , $USER , $referrer ;
2006-04-12 02:05:46 +00:00
$blogEntry = get_record ( 'post' , 'id' , $post -> postid );
// echo "id id ".$post->postid;
2006-03-10 06:53:01 +00:00
// print_object($blogentry); //debug
2006-04-12 02:05:46 +00:00
$blogEntry -> subject = addslashes ( $post -> etitle );
$blogEntry -> summary = addslashes ( $post -> body );
if ( $blogEntry -> summary == '<br />' ) {
$blogEntry -> summary = '' ;
}
$blogEntry -> format = $post -> format ;
$blogEntry -> publishstate = $post -> publishstate ; //we don't care about the return value here
2006-03-10 06:53:01 +00:00
2006-04-12 02:05:46 +00:00
if ( update_record ( 'post' , $blogEntry )) {
delete_records ( 'blog_tag_instance' , 'entryid' , $blogEntry -> id );
2006-03-10 06:53:01 +00:00
2006-03-20 07:45:55 +00:00
$tag = NULL ;
2006-04-12 02:05:46 +00:00
$tag -> entryid = $blogEntry -> id ;
2006-03-20 07:45:55 +00:00
$tag -> userid = $USER -> id ;
$tag -> timemodified = time ();
2006-03-22 09:16:08 +00:00
2006-03-16 04:33:47 +00:00
/// Add tags information
2006-04-24 03:36:02 +00:00
if ( $otags = optional_param ( 'otags' , '' , PARAM_INT )) {
foreach ( $otags as $otag ) {
$tag -> tagid = $otag ;
insert_record ( 'blog_tag_instance' , $tag );
}
2006-03-10 06:53:01 +00:00
}
2006-04-24 03:36:02 +00:00
if ( $ptags = optional_param ( 'ptags' , '' , PARAM_INT )) {
foreach ( $ptags as $ptag ) {
$tag -> tagid = $ptag ;
insert_record ( 'blog_tag_instance' , $tag );
}
2006-03-10 06:53:01 +00:00
}
2006-03-22 09:16:08 +00:00
2006-03-10 06:53:01 +00:00
// only do pings if the entry is published to the world
// Daryl Hawes note - eventually should check if it's on the same server
// and if so allow pb/tb as well - especially now that moderation is in place
print '<strong>' . get_string ( 'entryupdated' , 'blog' ) . '</strong><p>' ;
//record a log message of this entry update action
if ( $site = get_site ()) {
2006-04-20 02:14:17 +00:00
add_to_log ( $site -> id , 'blog' , 'update' , 'index.php?userid=' . $blogEntry -> userid . '&postid=' . $post -> postid , 'updated existing blog entry with entry id# ' . $post -> postid );
2006-03-10 06:53:01 +00:00
}
2006-04-12 03:02:53 +00:00
redirect ( $referrer );
//to debug this save function comment out the following redirect code
/*
if ( $courseid == SITEID || $courseid == 0 || $courseid == '' ) {
redirect ( $CFG -> wwwroot . '/blog/index.php?userid=' . $blogEntry -> userid );
} else {
redirect ( $CFG -> wwwroot . '/course/view.php?id=' . $courseid );
} */
2006-03-10 06:53:01 +00:00
} else {
// get_string('', 'blog') //Daryl Hawes note: localize this line
2006-04-22 16:02:49 +00:00
$post -> error = 'There was an error updating this post in the database' ;
2006-03-10 06:53:01 +00:00
}
}
?>