2006-03-10 06:53:01 +00:00
|
|
|
<?php //$Id$
|
|
|
|
|
|
|
|
require_once('../config.php');
|
|
|
|
include_once('lib.php');
|
2007-08-27 08:46:00 +00:00
|
|
|
include_once($CFG->dirroot.'/tag/lib.php');
|
2006-04-10 07:27:03 +00:00
|
|
|
|
2006-10-06 10:11:52 +00:00
|
|
|
$action = required_param('action', PARAM_ALPHA);
|
|
|
|
$id = optional_param('id', 0, PARAM_INT);
|
|
|
|
$confirm = optional_param('confirm', 0, PARAM_BOOL);
|
|
|
|
$courseid = optional_param('courseid', 0, PARAM_INT); // needed for user tab - does nothing here
|
|
|
|
|
2007-10-07 09:27:13 +00:00
|
|
|
require_login($courseid);
|
2006-04-10 07:27:03 +00:00
|
|
|
|
2006-10-03 19:21:35 +00:00
|
|
|
if (empty($CFG->bloglevel)) {
|
|
|
|
error('Blogging is disabled!');
|
|
|
|
}
|
|
|
|
|
2006-10-06 10:11:52 +00:00
|
|
|
if (isguest()) {
|
|
|
|
error(get_string('noguestpost', 'blog'));
|
2006-03-10 06:53:01 +00:00
|
|
|
}
|
|
|
|
|
2006-10-06 10:11:52 +00:00
|
|
|
$sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
|
|
|
|
if (!has_capability('moodle/blog:create', $sitecontext) and !has_capability('moodle/blog:manageentries', $sitecontext)) {
|
|
|
|
error('You can not post or edit blogs.');
|
2006-03-10 06:53:01 +00:00
|
|
|
}
|
|
|
|
|
2006-08-08 05:13:06 +00:00
|
|
|
// Make sure that the person trying to edit have access right
|
2006-10-06 10:11:52 +00:00
|
|
|
if ($id) {
|
|
|
|
if (!$existing = get_record('post', 'id', $id)) {
|
|
|
|
error('Wrong blog post id');
|
2006-03-10 06:53:01 +00:00
|
|
|
}
|
2006-04-12 08:58:49 +00:00
|
|
|
|
2006-10-06 10:11:52 +00:00
|
|
|
if (!blog_user_can_edit_post($existing)) {
|
|
|
|
error(get_string('notallowedtoedit', 'blog'));
|
|
|
|
}
|
|
|
|
$userid = $existing->userid;
|
|
|
|
$returnurl = $CFG->wwwroot.'/blog/index.php?userid='.$existing->userid;
|
2006-03-10 06:53:01 +00:00
|
|
|
} else {
|
2006-10-06 10:11:52 +00:00
|
|
|
if (!has_capability('moodle/blog:create', $sitecontext)) {
|
|
|
|
error(get_string('nopost', 'blog')); // manageentries is not enough for adding
|
|
|
|
}
|
|
|
|
$existing = false;
|
|
|
|
$userid = $USER->id;
|
|
|
|
$returnurl = 'index.php?userid='.$USER->id;
|
2006-03-10 06:53:01 +00:00
|
|
|
}
|
2006-10-06 10:11:52 +00:00
|
|
|
if (!empty($courseid)) {
|
|
|
|
$returnurl .= '&courseid='.$courseid;
|
2006-04-20 02:14:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-06 10:11:52 +00:00
|
|
|
$strblogs = get_string('blogs','blog');
|
2006-03-10 06:53:01 +00:00
|
|
|
|
2006-12-28 09:32:45 +00:00
|
|
|
if ($action=='delete'){
|
|
|
|
if (!$existing) {
|
|
|
|
error('Incorrect blog post id');
|
|
|
|
}
|
|
|
|
if (data_submitted() and $confirm and confirm_sesskey()) {
|
|
|
|
do_delete($existing);
|
|
|
|
redirect($returnurl);
|
|
|
|
} else {
|
|
|
|
$optionsyes = array('id'=>$id, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'courseid'=>$courseid);
|
|
|
|
$optionsno = array('userid'=>$existing->userid, 'courseid'=>$courseid);
|
|
|
|
print_header("$SITE->shortname: $strblogs", $SITE->fullname);
|
|
|
|
blog_print_entry($existing);
|
|
|
|
echo '<br />';
|
|
|
|
notice_yesno(get_string('blogdeleteconfirm', 'blog'), 'edit.php', 'index.php', $optionsyes, $optionsno, 'post', 'get');
|
|
|
|
print_footer();
|
|
|
|
die;
|
|
|
|
}
|
|
|
|
}
|
2006-04-12 08:58:49 +00:00
|
|
|
|
2006-12-28 09:32:45 +00:00
|
|
|
require_once('edit_form.php');
|
2006-12-28 15:43:47 +00:00
|
|
|
$blogeditform = new blog_edit_form(null, compact('existing', 'sitecontext'));
|
2006-12-28 09:32:45 +00:00
|
|
|
|
2006-12-28 15:43:47 +00:00
|
|
|
if ($blogeditform->is_cancelled()){
|
2006-12-28 09:32:45 +00:00
|
|
|
redirect($returnurl);
|
2007-01-12 18:52:09 +00:00
|
|
|
} else if ($fromform = $blogeditform->get_data()){
|
2006-12-28 09:32:45 +00:00
|
|
|
//save stuff in db
|
|
|
|
switch ($action) {
|
|
|
|
case 'add':
|
2006-12-28 21:21:44 +00:00
|
|
|
do_add($fromform, $blogeditform);
|
2006-12-28 09:32:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'edit':
|
|
|
|
if (!$existing) {
|
|
|
|
error('Incorrect blog post id');
|
2006-10-06 10:11:52 +00:00
|
|
|
}
|
2006-12-28 21:21:44 +00:00
|
|
|
do_edit($fromform, $blogeditform);
|
2006-12-28 09:32:45 +00:00
|
|
|
break;
|
|
|
|
default :
|
|
|
|
error('Unknown action!');
|
|
|
|
}
|
|
|
|
redirect($returnurl);
|
|
|
|
}
|
2006-04-20 02:14:17 +00:00
|
|
|
|
2006-12-28 09:32:45 +00:00
|
|
|
|
|
|
|
// gui setup
|
|
|
|
switch ($action) {
|
|
|
|
case 'add':
|
|
|
|
// prepare new empty form
|
2007-09-03 09:13:56 +00:00
|
|
|
$post->publishstate = 'site';
|
2006-10-06 10:11:52 +00:00
|
|
|
$strformheading = get_string('addnewentry', 'blog');
|
2006-12-28 09:32:45 +00:00
|
|
|
$post->action = $action;
|
2006-10-06 10:11:52 +00:00
|
|
|
break;
|
2006-03-10 06:53:01 +00:00
|
|
|
|
2006-10-06 10:11:52 +00:00
|
|
|
case 'edit':
|
|
|
|
if (!$existing) {
|
|
|
|
error('Incorrect blog post id');
|
|
|
|
}
|
2006-12-28 09:32:45 +00:00
|
|
|
$post->id = $existing->id;
|
|
|
|
$post->subject = $existing->subject;
|
|
|
|
$post->summary = $existing->summary;
|
|
|
|
$post->publishstate = $existing->publishstate;
|
|
|
|
$post->format = $existing->format;
|
|
|
|
$post->action = $action;
|
2006-10-06 10:11:52 +00:00
|
|
|
$strformheading = get_string('updateentrywithid', 'blog');
|
2006-03-10 06:53:01 +00:00
|
|
|
|
2008-02-26 06:43:54 +00:00
|
|
|
if ($itemptags = html_entity_decode(tag_get_tags_csv('post', $post->id, TAG_RETURN_TEXT, 'default'))) {
|
2008-02-25 01:58:17 +00:00
|
|
|
$post->ptags = $itemptags;
|
2006-12-28 09:32:45 +00:00
|
|
|
}
|
2007-08-28 08:32:38 +00:00
|
|
|
|
2008-02-26 06:43:54 +00:00
|
|
|
if ($itemotags = tag_get_tags_array('post', $post->id, 'official')) {
|
2008-02-25 01:58:17 +00:00
|
|
|
$post->otags = array_keys($itemotags);
|
2006-03-10 06:53:01 +00:00
|
|
|
}
|
2006-10-06 10:11:52 +00:00
|
|
|
break;
|
2006-12-28 09:32:45 +00:00
|
|
|
default :
|
2006-10-06 10:11:52 +00:00
|
|
|
error('Unknown action!');
|
2006-03-10 06:53:01 +00:00
|
|
|
}
|
|
|
|
|
2006-10-06 10:11:52 +00:00
|
|
|
// done here in order to allow deleting of posts with wrong user id above
|
|
|
|
if (!$user = get_record('user', 'id', $userid)) {
|
|
|
|
error('Incorrect user id');
|
2006-03-10 06:53:01 +00:00
|
|
|
}
|
2007-08-17 11:18:58 +00:00
|
|
|
$navlinks = array();
|
|
|
|
$navlinks[] = array('name' => fullname($user), 'link' => "$CFG->wwwroot/user/view.php?id=$userid", 'type' => 'misc');
|
|
|
|
$navlinks[] = array('name' => $strblogs, 'link' => "$CFG->wwwroot/blog/index.php?userid=$userid", 'type' => 'misc');
|
|
|
|
$navlinks[] = array('name' => $strformheading, 'link' => null, 'type' => 'misc');
|
|
|
|
$navigation = build_navigation($navlinks);
|
2006-03-10 06:53:01 +00:00
|
|
|
|
2007-08-17 11:18:58 +00:00
|
|
|
print_header("$SITE->shortname: $strblogs", $SITE->fullname, $navigation,'','',true);
|
2007-01-12 18:52:09 +00:00
|
|
|
$blogeditform->set_data($post);
|
2006-12-28 15:43:47 +00:00
|
|
|
$blogeditform->display();
|
2006-03-10 06:53:01 +00:00
|
|
|
|
2006-10-06 10:11:52 +00:00
|
|
|
|
|
|
|
print_footer();
|
|
|
|
|
2006-12-28 09:32:45 +00:00
|
|
|
|
2006-10-06 10:11:52 +00:00
|
|
|
die;
|
2006-03-10 06:53:01 +00:00
|
|
|
|
|
|
|
/***************************** edit.php functions ***************************/
|
2006-12-28 21:21:44 +00:00
|
|
|
|
2006-03-10 06:53:01 +00:00
|
|
|
/*
|
2006-10-06 10:11:52 +00:00
|
|
|
* Delete blog post from database
|
2006-03-10 06:53:01 +00:00
|
|
|
*/
|
2006-10-06 10:11:52 +00:00
|
|
|
function do_delete($post) {
|
|
|
|
global $returnurl;
|
2006-03-10 06:53:01 +00:00
|
|
|
|
2006-10-06 10:11:52 +00:00
|
|
|
$status = delete_records('post', 'id', $post->id);
|
2007-08-27 08:46:00 +00:00
|
|
|
//$status = delete_records('blog_tag_instance', 'entryid', $post->id) and $status;
|
2008-02-25 01:58:17 +00:00
|
|
|
tag_set('post', $post->id, array());
|
2007-08-27 08:46:00 +00:00
|
|
|
|
2007-03-19 07:52:29 +00:00
|
|
|
blog_delete_old_attachments($post);
|
2007-08-17 11:18:58 +00:00
|
|
|
|
2006-10-06 10:11:52 +00:00
|
|
|
add_to_log(SITEID, 'blog', 'delete', 'index.php?userid='. $post->userid, 'deleted blog entry with entry id# '. $post->id);
|
|
|
|
|
|
|
|
if (!$status) {
|
|
|
|
error('Error occured while deleting post', $returnurl);
|
|
|
|
}
|
2006-03-10 06:53:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-10-06 10:11:52 +00:00
|
|
|
* Write a new blog entry into database
|
|
|
|
*/
|
2006-12-28 21:21:44 +00:00
|
|
|
function do_add($post, $blogeditform) {
|
2006-10-06 10:11:52 +00:00
|
|
|
global $CFG, $USER, $returnurl;
|
2006-03-10 06:53:01 +00:00
|
|
|
|
2006-10-06 10:11:52 +00:00
|
|
|
$post->module = 'blog';
|
|
|
|
$post->userid = $USER->id;
|
|
|
|
$post->lastmodified = time();
|
|
|
|
$post->created = time();
|
2006-12-28 09:32:45 +00:00
|
|
|
|
2006-10-06 10:11:52 +00:00
|
|
|
// Insert the new blog entry.
|
|
|
|
if ($id = insert_record('post', $post)) {
|
|
|
|
$post->id = $id;
|
2006-11-20 08:31:48 +00:00
|
|
|
// add blog attachment
|
2006-12-28 21:21:44 +00:00
|
|
|
$dir = blog_file_area_name($post);
|
|
|
|
if ($blogeditform->save_files($dir) and $newfilename = $blogeditform->get_new_filename()) {
|
|
|
|
set_field("post", "attachment", $newfilename, "id", $post->id);
|
2006-12-28 09:32:45 +00:00
|
|
|
}
|
2006-10-06 10:11:52 +00:00
|
|
|
add_tags_info($post->id);
|
2006-12-28 09:32:45 +00:00
|
|
|
add_to_log(SITEID, 'blog', 'add', 'index.php?userid='.$post->userid.'&postid='.$post->id, $post->subject);
|
2006-10-06 10:11:52 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
error('There was an error adding this post in the database', $returnurl);
|
|
|
|
}
|
|
|
|
|
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-12-28 21:21:44 +00:00
|
|
|
function do_edit($post, $blogeditform) {
|
2006-10-06 10:11:52 +00:00
|
|
|
|
|
|
|
global $CFG, $USER, $returnurl;
|
|
|
|
|
|
|
|
|
|
|
|
$post->lastmodified = time();
|
2006-12-28 09:32:45 +00:00
|
|
|
|
2006-12-28 21:21:44 +00:00
|
|
|
$dir = blog_file_area_name($post);
|
|
|
|
if ($blogeditform->save_files($dir) and $newfilename = $blogeditform->get_new_filename()) {
|
2006-11-20 08:31:48 +00:00
|
|
|
$post->attachment = $newfilename;
|
2006-12-28 21:21:44 +00:00
|
|
|
}
|
|
|
|
|
2006-10-06 10:11:52 +00:00
|
|
|
// update record
|
|
|
|
if (update_record('post', $post)) {
|
2006-11-16 03:37:04 +00:00
|
|
|
// delete all tags associated with this entry
|
2007-08-27 08:46:00 +00:00
|
|
|
|
|
|
|
//delete_records('blog_tag_instance', 'entryid', $post->id);
|
|
|
|
//delete_records('tag_instance', 'itemid', $post->id, 'itemtype', 'blog');
|
2008-02-25 01:58:17 +00:00
|
|
|
//untag_an_item('post', $post->id);
|
2006-11-16 03:37:04 +00:00
|
|
|
// add them back
|
2006-10-06 10:11:52 +00:00
|
|
|
add_tags_info($post->id);
|
2006-12-28 09:32:45 +00:00
|
|
|
|
2007-10-17 03:02:17 +00:00
|
|
|
add_to_log(SITEID, 'blog', 'update', 'index.php?userid='.$USER->id.'&postid='.$post->id, $post->subject);
|
2006-10-06 10:11:52 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
error('There was an error updating this post in the database', $returnurl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-16 03:37:04 +00:00
|
|
|
/**
|
|
|
|
* function to attach tags into a post
|
|
|
|
* @param int postid - id of the blog
|
|
|
|
*/
|
2006-10-06 10:11:52 +00:00
|
|
|
function add_tags_info($postid) {
|
2008-02-25 01:58:17 +00:00
|
|
|
|
|
|
|
$tags = array();
|
2006-12-28 09:32:45 +00:00
|
|
|
if ($otags = optional_param('otags', '', PARAM_INT)) {
|
2008-02-25 01:58:17 +00:00
|
|
|
foreach ($otags as $tagid) {
|
|
|
|
// TODO : make this use the tag name in the form
|
2008-02-29 06:26:21 +00:00
|
|
|
if ($tag = tag_get('id', $tagid)) {
|
|
|
|
$tags[] = $tag->name;
|
|
|
|
}
|
2006-03-10 06:53:01 +00:00
|
|
|
}
|
2006-10-06 10:11:52 +00:00
|
|
|
}
|
|
|
|
|
2008-02-25 01:58:17 +00:00
|
|
|
$manual_tags = optional_param('ptags', '', PARAM_NOTAGS);
|
|
|
|
$tags = array_merge($tags, explode(',', $manual_tags));
|
|
|
|
|
|
|
|
tag_set('post', $postid, $tags);
|
2006-03-10 06:53:01 +00:00
|
|
|
}
|
2007-08-17 11:18:58 +00:00
|
|
|
?>
|