* @author Julio Montoya - Cleaning code
*/
class Blog
{
/**
* Get the title of a blog.
*
* @author Toon Keppens
*
* @param int $blog_id The internal ID of the blog
*
* @return string Blog Title
*/
public static function getBlogTitle($blog_id)
{
$course_id = api_get_course_int_id();
if (is_numeric($blog_id)) {
$table = Database::get_course_table(TABLE_BLOGS);
$sql = "SELECT blog_name
FROM $table
WHERE c_id = $course_id AND blog_id = ".intval($blog_id);
$result = Database::query($sql);
$blog = Database::fetch_array($result);
return stripslashes($blog['blog_name']);
}
}
/**
* Get the description of a blog.
*
* @author Toon Keppens
*
* @param int $blog_id The internal ID of the blog
*
* @return string Blog description
*/
public static function getBlogSubtitle($blog_id)
{
$table = Database::get_course_table(TABLE_BLOGS);
$course_id = api_get_course_int_id();
$sql = "SELECT blog_subtitle FROM $table
WHERE c_id = $course_id AND blog_id ='".intval($blog_id)."'";
$result = Database::query($sql);
$blog = Database::fetch_array($result);
return stripslashes($blog['blog_subtitle']);
}
/**
* Get the users of a blog.
*
* @author Toon Keppens
*
* @param int $blog_id The ID of the blog
*
* @return array Returns an array with [userid]=>[username]
*/
public static function getBlogUsers($blog_id)
{
// Database table definitions
$tbl_users = Database::get_main_table(TABLE_MAIN_USER);
$tbl_blogs_rel_user = Database::get_course_table(TABLE_BLOGS_REL_USER);
$course_id = api_get_course_int_id();
// Get blog members
$sql = "SELECT user.user_id, user.firstname, user.lastname
FROM $tbl_blogs_rel_user blogs_rel_user
INNER JOIN $tbl_users user
ON (blogs_rel_user.user_id = user.user_id)
WHERE
blogs_rel_user.c_id = $course_id AND
blogs_rel_user.blog_id = '".(int) $blog_id."'";
$result = Database::query($sql);
$blog_members = [];
while ($user = Database::fetch_array($result)) {
$blog_members[$user['user_id']] = api_get_person_name(
$user['firstname'],
$user['lastname']
);
}
return $blog_members;
}
/**
* Creates a new blog in the given course.
*
* @author Toon Keppens
*
* @param string $title The title of the new blog
* @param string $subtitle The description (or subtitle) of the new blog
*/
public static function addBlog($title, $subtitle)
{
$_user = api_get_user_info();
$course_id = api_get_course_int_id();
$current_date = api_get_utc_datetime();
$session_id = api_get_session_id();
$tbl_blogs = Database::get_course_table(TABLE_BLOGS);
$tbl_tool = Database::get_course_table(TABLE_TOOL_LIST);
$tbl_blogs_posts = Database::get_course_table(TABLE_BLOGS_POSTS);
//verified if exist blog
$sql = "SELECT COUNT(*) as count FROM $tbl_blogs
WHERE
c_id = $course_id AND
blog_name = '".Database::escape_string($title)."' AND
blog_subtitle = '".Database::escape_string($subtitle)."' ";
$res = Database::query($sql);
$info_count = Database::result($res, 0, 0);
if ($info_count == 0) {
// Create the blog
$params = [
'blog_id' => 0,
'c_id' => $course_id,
'blog_name' => $title,
'blog_subtitle' => $subtitle,
'date_creation' => $current_date,
'visibility' => 1,
'session_id' => $session_id,
];
$this_blog_id = Database::insert($tbl_blogs, $params);
if ($this_blog_id > 0) {
$sql = "UPDATE $tbl_blogs SET blog_id = iid WHERE iid = $this_blog_id";
Database::query($sql);
// insert into item_property
api_item_property_update(
api_get_course_info(),
TOOL_BLOGS,
$this_blog_id,
'BlogAdded',
api_get_user_id()
);
}
// Make first post. :)
$params = [
'post_id' => 0,
'c_id' => $course_id,
'title' => get_lang("Welcome"),
'full_text' => get_lang('FirstPostText'),
'date_creation' => $current_date,
'blog_id' => $this_blog_id,
'author_id' => $_user['user_id'],
];
$postId = Database::insert($tbl_blogs_posts, $params);
if ($postId) {
$sql = "UPDATE $tbl_blogs_posts SET post_id = iid WHERE iid = $postId";
Database::query($sql);
}
// Put it on course homepage
$params = [
'c_id' => $course_id,
'name' => $title,
'link' => 'blog/blog.php?blog_id='.$this_blog_id,
'image' => 'blog.gif',
'visibility' => '1',
'admin' => '0',
'address' => 'pastillegris.gif',
'added_tool' => 0,
'session_id' => $session_id,
'target' => '',
];
$toolId = Database::insert($tbl_tool, $params);
if ($toolId) {
$sql = "UPDATE $tbl_tool SET id = iid WHERE iid = $toolId";
Database::query($sql);
}
// Subscribe the teacher to this blog
self::subscribeUser($this_blog_id, $_user['user_id']);
}
}
/**
* Subscribes a user to a given blog.
*
* @author Toon Keppens
*
* @param int $blog_id The internal blog ID
* @param int $user_id The internal user ID (of the user to be subscribed)
*/
public static function subscribeUser($blog_id, $user_id)
{
$tbl_blogs_rel_user = Database::get_course_table(TABLE_BLOGS_REL_USER);
$tbl_user_permissions = Database::get_course_table(TABLE_PERMISSION_USER);
$course_id = api_get_course_int_id();
$blog_id = intval($blog_id);
$user_id = intval($user_id);
// Subscribe the user
$sql = "INSERT INTO $tbl_blogs_rel_user (c_id, blog_id, user_id )
VALUES ($course_id, $blog_id, $user_id)";
Database::query($sql);
// Give this user basic rights
$sql = "INSERT INTO $tbl_user_permissions (c_id, user_id, tool, action)
VALUES ($course_id, $user_id, 'BLOG_$blog_id', 'article_add')";
Database::query($sql);
$id = Database::insert_id();
if ($id) {
$sql = "UPDATE $tbl_user_permissions SET id = iid WHERE iid = $id";
Database::query($sql);
}
$sql = "INSERT INTO $tbl_user_permissions (c_id, user_id, tool, action)
VALUES ($course_id, $user_id,'BLOG_$blog_id', 'article_comments_add')";
Database::query($sql);
$id = Database::insert_id();
if ($id) {
$sql = "UPDATE $tbl_user_permissions SET id = iid WHERE iid = $id";
Database::query($sql);
}
}
/**
* Update title and subtitle of a blog in the given course.
*
* @author Toon Keppens
*
* @param int $blog_id The internal ID of the blog
* @param string $title The title to be set
* @param string $subtitle The subtitle (or description) to be set
*/
public static function editBlog($blog_id, $title, $subtitle = '')
{
// Table definitions
$tbl_blogs = Database::get_course_table(TABLE_BLOGS);
$tbl_tool = Database::get_course_table(TABLE_TOOL_LIST);
$course_id = api_get_course_int_id();
$blog_id = intval($blog_id);
$title = Database::escape_string($title);
$subtitle = Database::escape_string($subtitle);
// Update the blog
$sql = "UPDATE $tbl_blogs SET
blog_name = '$title',
blog_subtitle = '$subtitle'
WHERE
c_id = $course_id AND
blog_id = $blog_id
LIMIT 1";
Database::query($sql);
//update item_property (update)
api_item_property_update(
api_get_course_info(),
TOOL_BLOGS,
$blog_id,
'BlogUpdated',
api_get_user_id()
);
// Update course homepage link
$sql = "UPDATE $tbl_tool SET
name = '$title'
WHERE c_id = $course_id AND link = 'blog/blog.php?blog_id=$blog_id'
LIMIT 1";
Database::query($sql);
}
/**
* Deletes a blog and it's posts from the course database.
*
* @author Toon Keppens
*
* @param int $blog_id The internal blog ID
*/
public static function deleteBlog($blog_id)
{
$tbl_blogs = Database::get_course_table(TABLE_BLOGS);
$tbl_blogs_posts = Database::get_course_table(TABLE_BLOGS_POSTS);
$tbl_blogs_comment = Database::get_course_table(TABLE_BLOGS_COMMENTS);
$tbl_blogs_tasks = Database::get_course_table(TABLE_BLOGS_TASKS);
$tbl_tool = Database::get_course_table(TABLE_TOOL_LIST);
$tbl_blogs_rating = Database::get_course_table(TABLE_BLOGS_RATING);
$course_id = api_get_course_int_id();
$blog_id = intval($blog_id);
// Delete posts from DB and the attachments
self::deleteAllBlogAttachments($blog_id);
//Delete comments
$sql = "DELETE FROM $tbl_blogs_comment WHERE c_id = $course_id AND blog_id = $blog_id";
Database::query($sql);
// Delete posts
$sql = "DELETE FROM $tbl_blogs_posts WHERE c_id = $course_id AND blog_id = $blog_id";
Database::query($sql);
// Delete tasks
$sql = "DELETE FROM $tbl_blogs_tasks WHERE c_id = $course_id AND blog_id = $blog_id";
Database::query($sql);
// Delete ratings
$sql = "DELETE FROM $tbl_blogs_rating WHERE c_id = $course_id AND blog_id = $blog_id";
Database::query($sql);
// Delete blog
$sql = "DELETE FROM $tbl_blogs WHERE c_id = $course_id AND blog_id = $blog_id";
Database::query($sql);
// Delete from course homepage
$sql = "DELETE FROM $tbl_tool WHERE c_id = $course_id AND link = 'blog/blog.php?blog_id=".$blog_id."'";
Database::query($sql);
//update item_property (delete)
api_item_property_update(
api_get_course_info(),
TOOL_BLOGS,
$blog_id,
'delete',
api_get_user_id()
);
}
/**
* Creates a new post in a given blog.
*
* @author Toon Keppens
*
* @param string $title The title of the new post
* @param string $full_text The full text of the new post
* @param string $file_comment The text of the comment (if any)
* @param int $blog_id The internal blog ID
*
* @return int
*/
public static function createPost($title, $full_text, $file_comment, $blog_id)
{
$_user = api_get_user_info();
$_course = api_get_course_info();
$course_id = $_course['real_id'];
$blog_id = intval($blog_id);
$blog_table_attachment = Database::get_course_table(TABLE_BLOGS_ATTACHMENT);
$upload_ok = true;
$has_attachment = false;
$current_date = api_get_utc_datetime();
if (!empty($_FILES['user_upload']['name'])) {
$upload_ok = process_uploaded_file($_FILES['user_upload']);
$has_attachment = true;
}
if ($upload_ok) {
// Table Definitions
$tbl_blogs_posts = Database::get_course_table(TABLE_BLOGS_POSTS);
$title = Database::escape_string($title);
$full_text = Database::escape_string($full_text);
// Create the post
$sql = "INSERT INTO $tbl_blogs_posts (c_id, title, full_text, date_creation, blog_id, author_id )
VALUES ($course_id, '$title', '$full_text', '$current_date', '$blog_id', ".$_user['user_id'].")";
Database::query($sql);
$last_post_id = Database::insert_id();
if ($last_post_id) {
$sql = "UPDATE $tbl_blogs_posts SET post_id = iid WHERE iid = $last_post_id";
Database::query($sql);
}
if ($has_attachment) {
$courseDir = $_course['path'].'/upload/blog';
$sys_course_path = api_get_path(SYS_COURSE_PATH);
$updir = $sys_course_path.$courseDir;
// Try to add an extension to the file if it hasn't one
$new_file_name = add_ext_on_mime(
stripslashes($_FILES['user_upload']['name']),
$_FILES['user_upload']['type']
);
// user's file name
$file_name = $_FILES['user_upload']['name'];
if (!filter_extension($new_file_name)) {
echo Display::return_message(get_lang('UplUnableToSaveFileFilteredExtension'), 'error');
} else {
$new_file_name = uniqid('');
$new_path = $updir.'/'.$new_file_name;
$result = @move_uploaded_file($_FILES['user_upload']['tmp_name'], $new_path);
$comment = Database::escape_string($file_comment);
$file_name = Database::escape_string($file_name);
$size = intval($_FILES['user_upload']['size']);
// Storing the attachments if any
if ($result) {
$sql = "INSERT INTO $blog_table_attachment (c_id, filename,comment, path, post_id,size, blog_id,comment_id)
VALUES ($course_id, '$file_name', '$comment', '$new_file_name', $last_post_id, $size, $blog_id, 0)";
Database::query($sql);
$id = Database::insert_id();
if ($id) {
$sql = "UPDATE $blog_table_attachment SET id = iid WHERE iid = $id";
Database::query($sql);
}
}
}
}
return $last_post_id;
} else {
echo Display::return_message(get_lang('UplNoFileUploaded'), 'error');
return 0;
}
}
/**
* Edits a post in a given blog.
*
* @author Toon Keppens
*
* @param int $post_id The internal ID of the post to edit
* @param string $title The title
* @param string $full_text The full post text
* @param int $blog_id The internal ID of the blog in which the post is located
*/
public static function editPost($post_id, $title, $full_text, $blog_id)
{
$table = Database::get_course_table(TABLE_BLOGS_POSTS);
$course_id = api_get_course_int_id();
$title = Database::escape_string($title);
$full_text = Database::escape_string($full_text);
$post_id = intval($post_id);
$blog_id = intval($blog_id);
// Create the post
$sql = "UPDATE $table SET
title = '$title',
full_text = '$full_text'
WHERE c_id = $course_id AND post_id = $post_id AND blog_id = $blog_id
LIMIT 1";
Database::query($sql);
}
/**
* Deletes an article and its comments.
*
* @author Toon Keppens
*
* @param int $blog_id The internal blog ID
* @param int $post_id The internal post ID
*/
public static function deletePost($blog_id, $post_id)
{
$tbl_blogs_posts = Database::get_course_table(TABLE_BLOGS_POSTS);
$tbl_blogs_comments = Database::get_course_table(TABLE_BLOGS_COMMENTS);
$tbl_blogs_rating = Database::get_course_table(TABLE_BLOGS_RATING);
$blog_id = intval($blog_id);
$post_id = intval($post_id);
$course_id = api_get_course_int_id();
// Delete ratings on this comment
$sql = "DELETE FROM $tbl_blogs_rating
WHERE c_id = $course_id AND blog_id = $blog_id AND item_id = $post_id AND rating_type = 'post'";
Database::query($sql);
// Delete the post
$sql = "DELETE FROM $tbl_blogs_posts
WHERE c_id = $course_id AND post_id = $post_id";
Database::query($sql);
// Delete the comments
$sql = "DELETE FROM $tbl_blogs_comments
WHERE c_id = $course_id AND post_id = $post_id AND blog_id = $blog_id";
Database::query($sql);
// Delete posts and attachments
self::deleteAllBlogAttachments($blog_id, $post_id);
}
/**
* Creates a comment on a post in a given blog.
*
* @author Toon Keppens
*
* @param string $title The comment title
* @param string $full_text The full text of the comment
* @param string $file_comment A comment on a file, if any was uploaded
* @param int $blog_id The internal blog ID
* @param int $post_id The internal post ID
* @param int $parent_id The internal parent post ID
* @param int $task_id The internal task ID (if any)
*/
public static function createComment(
$title,
$full_text,
$file_comment,
$blog_id,
$post_id,
$parent_id,
$task_id = null
) {
$_user = api_get_user_info();
$_course = api_get_course_info();
$blog_table_attachment = Database::get_course_table(TABLE_BLOGS_ATTACHMENT);
$upload_ok = true;
$has_attachment = false;
$current_date = api_get_utc_datetime();
$course_id = api_get_course_int_id();
if (!empty($_FILES['user_upload']['name'])) {
$upload_ok = process_uploaded_file($_FILES['user_upload']);
$has_attachment = true;
}
if ($upload_ok) {
// Table Definition
$tbl_blogs_comments = Database::get_course_table(TABLE_BLOGS_COMMENTS);
$title = Database::escape_string($title);
$full_text = Database::escape_string($full_text);
$blog_id = intval($blog_id);
$post_id = intval($post_id);
$parent_id = intval($parent_id);
$task_id = !empty($task_id) ? intval($task_id) : 'null';
// Create the comment
$sql = "INSERT INTO $tbl_blogs_comments (c_id, title, comment, author_id, date_creation, blog_id, post_id, parent_comment_id, task_id )
VALUES ($course_id, '$title', '$full_text', ".$_user['user_id'].", '$current_date', $blog_id, $post_id, $parent_id, '$task_id')";
Database::query($sql);
// Empty post values, or they are shown on the page again
$last_id = Database::insert_id();
if ($last_id) {
$sql = "UPDATE $tbl_blogs_comments SET comment_id = iid WHERE iid = $last_id";
Database::query($sql);
if ($has_attachment) {
$courseDir = $_course['path'].'/upload/blog';
$sys_course_path = api_get_path(SYS_COURSE_PATH);
$updir = $sys_course_path.$courseDir;
// Try to add an extension to the file if it hasn't one
$new_file_name = add_ext_on_mime(
stripslashes($_FILES['user_upload']['name']),
$_FILES['user_upload']['type']
);
// user's file name
$file_name = Database::escape_string($_FILES['user_upload']['name']);
if (!filter_extension($new_file_name)) {
echo Display::return_message(get_lang('UplUnableToSaveFileFilteredExtension'), 'error');
} else {
$new_file_name = uniqid('');
$new_path = $updir.'/'.$new_file_name;
$result = @move_uploaded_file($_FILES['user_upload']['tmp_name'], $new_path);
$comment = Database::escape_string($file_comment);
$size = intval($_FILES['user_upload']['size']);
// Storing the attachments if any
if ($result) {
$sql = "INSERT INTO $blog_table_attachment (c_id, filename,comment, path, post_id,size,blog_id,comment_id)
VALUES ($course_id, '$file_name', '$comment', '$new_file_name', $post_id, $size, $blog_id, $last_id)";
Database::query($sql);
$id = Database::insert_id();
if ($id) {
$sql = "UPDATE $blog_table_attachment SET id = iid WHERE iid = $id";
Database::query($sql);
}
}
}
}
}
}
}
/**
* Deletes a comment from a blogpost.
*
* @author Toon Keppens
*
* @param int $blog_id The internal blog ID
* @param int $post_id The internal post ID
* @param int $comment_id The internal comment ID
*/
public static function deleteComment($blog_id, $post_id, $comment_id)
{
$tbl_blogs_comments = Database::get_course_table(TABLE_BLOGS_COMMENTS);
$tbl_blogs_rating = Database::get_course_table(TABLE_BLOGS_RATING);
$blog_id = intval($blog_id);
$post_id = intval($post_id);
$comment_id = intval($comment_id);
$course_id = api_get_course_int_id();
self::deleteAllBlogAttachments($blog_id, $post_id, $comment_id);
// Delete ratings on this comment
$sql = "DELETE FROM $tbl_blogs_rating
WHERE
c_id = $course_id AND
blog_id = $blog_id AND
item_id = $comment_id AND
rating_type = 'comment'";
Database::query($sql);
// select comments that have the selected comment as their parent
$sql = "SELECT comment_id FROM $tbl_blogs_comments
WHERE c_id = $course_id AND parent_comment_id = $comment_id";
$result = Database::query($sql);
// Delete them recursively
while ($comment = Database::fetch_array($result)) {
self::deleteComment($blog_id, $post_id, $comment['comment_id']);
}
// Finally, delete the selected comment to
$sql = "DELETE FROM $tbl_blogs_comments
WHERE c_id = $course_id AND comment_id = $comment_id";
Database::query($sql);
}
/**
* Creates a new task in a blog.
*
* @author Toon Keppens
*
* @param int $blog_id
* @param string $title
* @param string $description
* @param string $articleDelete Set to 'on' to register as 'article_delete' in tasks_permissions
* @param string $articleEdit Set to 'on' to register as 'article_edit' in tasks_permissions
* @param string $commentsDelete Set to 'on' to register as 'article_comments_delete' in tasks permissions
* @param string $color
*/
public static function addTask(
$blog_id,
$title,
$description,
$articleDelete,
$articleEdit,
$commentsDelete,
$color
) {
$tbl_blogs_tasks = Database::get_course_table(TABLE_BLOGS_TASKS);
$tbl_tasks_permissions = Database::get_course_table(TABLE_BLOGS_TASKS_PERMISSIONS);
$course_id = api_get_course_int_id();
$blog_id = intval($blog_id);
$title = Database::escape_string($title);
$description = Database::escape_string($description);
$color = Database::escape_string($color);
// Create the task
$sql = "INSERT INTO $tbl_blogs_tasks (c_id, blog_id, title, description, color, system_task)
VALUES ($course_id , $blog_id, '$title', '$description', '$color', '0');";
Database::query($sql);
$task_id = Database::insert_id();
if ($task_id) {
$sql = "UPDATE $tbl_blogs_tasks SET task_id = iid WHERE iid = $task_id";
Database::query($sql);
}
$tool = 'BLOG_'.$blog_id;
if ($articleDelete == 'on') {
$sql = "INSERT INTO $tbl_tasks_permissions ( c_id, task_id, tool, action)
VALUES ($course_id, $task_id, '$tool', 'article_delete')";
Database::query($sql);
$id = Database::insert_id();
if ($id) {
$sql = "UPDATE $tbl_tasks_permissions SET id = iid WHERE iid = $id";
Database::query($sql);
}
}
if ($articleEdit == 'on') {
$sql = "
INSERT INTO $tbl_tasks_permissions (c_id, task_id, tool, action )
VALUES ($course_id, $task_id, '$tool', 'article_edit')";
Database::query($sql);
$id = Database::insert_id();
if ($id) {
$sql = "UPDATE $tbl_tasks_permissions SET id = iid WHERE iid = $id";
Database::query($sql);
}
}
if ($commentsDelete == 'on') {
$sql = "
INSERT INTO $tbl_tasks_permissions (c_id, task_id, tool, action )
VALUES ($course_id, $task_id, '$tool', 'article_comments_delete')";
Database::query($sql);
$id = Database::insert_id();
if ($id) {
$sql = "UPDATE $tbl_tasks_permissions SET id = iid WHERE iid = $id";
Database::query($sql);
}
}
}
/**
* Edit a task in a blog.
*
* @author Toon Keppens
*
* @param int $blog_id The internal blog ID
* @param int $task_id The internal task ID
* @param string $title The task title
* @param string $description The task description
* @param string $articleDelete Set to 'on' to register as 'article_delete' in tasks_permissions
* @param string $articleEdit Set to 'on' to register as 'article_edit' in tasks_permissions
* @param string $commentsDelete Set to 'on' to register as 'article_comments_delete' in tasks permissions
* @param string $color The color code
*/
public static function editTask(
$blog_id,
$task_id,
$title,
$description,
$articleDelete,
$articleEdit,
$commentsDelete,
$color
) {
$tbl_blogs_tasks = Database::get_course_table(TABLE_BLOGS_TASKS);
$tbl_tasks_permissions = Database::get_course_table(TABLE_BLOGS_TASKS_PERMISSIONS);
$course_id = api_get_course_int_id();
$blog_id = intval($blog_id);
$task_id = intval($task_id);
$title = Database::escape_string($title);
$description = Database::escape_string($description);
$color = Database::escape_string($color);
// Create the task
$sql = "UPDATE $tbl_blogs_tasks SET
title = '$title',
description = '$description',
color = '$color'
WHERE c_id = $course_id AND task_id = task_id LIMIT 1";
Database::query($sql);
$tool = 'BLOG_'.$blog_id;
$sql = "DELETE FROM $tbl_tasks_permissions
WHERE c_id = $course_id AND task_id = $task_id";
Database::query($sql);
if ($articleDelete == 'on') {
$sql = "INSERT INTO $tbl_tasks_permissions ( c_id, task_id, tool, action)
VALUES ($course_id, $task_id, '$tool', 'article_delete')";
Database::query($sql);
$id = Database::insert_id();
if ($id) {
$sql = "UPDATE $tbl_tasks_permissions SET id = iid WHERE iid = $id";
Database::query($sql);
}
}
if ($articleEdit == 'on') {
$sql = "INSERT INTO $tbl_tasks_permissions (c_id, task_id, tool, action)
VALUES ($course_id, $task_id, '$tool', 'article_edit')";
Database::query($sql);
$id = Database::insert_id();
if ($id) {
$sql = "UPDATE $tbl_tasks_permissions SET id = iid WHERE iid = $id";
Database::query($sql);
}
}
if ($commentsDelete == 'on') {
$sql = "INSERT INTO $tbl_tasks_permissions (c_id, task_id, tool, action)
VALUES ($course_id, $task_id, '$tool', 'article_comments_delete')";
Database::query($sql);
$id = Database::insert_id();
if ($id) {
$sql = "UPDATE $tbl_tasks_permissions SET id = iid WHERE iid = $id";
Database::query($sql);
}
}
}
/**
* Deletes a task from a blog.
*
* @param int $blog_id
* @param int $task_id
*/
public static function deleteTask($blog_id, $task_id)
{
$table = Database::get_course_table(TABLE_BLOGS_TASKS);
$course_id = api_get_course_int_id();
$blog_id = intval($blog_id);
$task_id = intval($task_id);
// Delete posts
$sql = "DELETE FROM $table
WHERE c_id = $course_id AND blog_id = $blog_id AND task_id = $task_id";
Database::query($sql);
}
/**
* Deletes an assigned task from a blog.
*
* @param int $blog_id
* @param int $task_id
* @param int $user_id
*/
public static function deleteAssignedTask($blog_id, $task_id, $user_id)
{
$table = Database::get_course_table(TABLE_BLOGS_TASKS_REL_USER);
$course_id = api_get_course_int_id();
$blog_id = intval($blog_id);
$task_id = intval($task_id);
$user_id = intval($user_id);
// Delete posts
$sql = "DELETE FROM $table
WHERE
c_id = $course_id AND
blog_id = $blog_id AND
task_id = $task_id AND
user_id = $user_id";
Database::query($sql);
}
/**
* Get personal task list.
*
* @author Toon Keppens
*
* @return string Returns an unsorted list (
) with the users' tasks
*/
public static function getPersonalTasksList()
{
$_user = api_get_user_info();
$html = null;
$tbl_blogs = Database::get_course_table(TABLE_BLOGS);
$tbl_blogs_tasks_rel_user = Database::get_course_table(TABLE_BLOGS_TASKS_REL_USER);
$tbl_blogs_tasks = Database::get_course_table(TABLE_BLOGS_TASKS);
$course_id = api_get_course_int_id();
$blog_id = intval($_GET['blog_id']);
if ($_user['user_id']) {
$sql = "SELECT task_rel_user.*, task.title, blog.blog_name
FROM $tbl_blogs_tasks_rel_user task_rel_user
INNER JOIN $tbl_blogs_tasks task
ON task_rel_user.task_id = task.task_id
INNER JOIN $tbl_blogs blog
ON task_rel_user.blog_id = blog.blog_id
AND blog.blog_id = $blog_id
WHERE
task.c_id = $course_id AND
blog.c_id = $course_id AND
task_rel_user.c_id = $course_id AND
task_rel_user.user_id = ".$_user['user_id']."
ORDER BY target_date ASC";
$result = Database::query($sql);
if (Database::num_rows($result) > 0) {
$html .= '
';
while ($mytask = Database::fetch_array($result)) {
$html .= '
';
$return_data .= ' ';
$return_data .= $row['comment'];
$return_data .= ' ';
}
}
return $return_data;
}
/**
* Filter the post $fullText to get a extract of $length characters.
*
* @param string $fullText
* @param int $length
*
* @return string|null
*/
private static function getPostExtract($fullText, $length = BLOG_MAX_PREVIEW_CHARS)
{
$parts = explode(BLOG_PAGE_BREAK, $fullText);
if (count($parts) > 1) {
return $parts[0];
}
// Remove any HTML from the string
$text = strip_tags($fullText);
$text = api_html_entity_decode($text);
// Replace end of lines with spaces
$text = preg_replace('/\s+/', ' ', $text);
// Count whitespaces to add to the cut() call below
$countBlanks = substr_count($text, ' ');
// Get a version of the string without spaces for comparison purposes
$textWithoutBlanks = str_replace(' ', '', $text);
// utf8_decode replaces non-ISO chars by '?' which avoids counting
// multi-byte characters as more than one character
$stringLength = strlen(utf8_decode($textWithoutBlanks));
if ($stringLength <= $length) {
return null;
}
// Cut the string to the BLOG_MAX_PREVIEX_CHARS limit, adding
// whitespaces
$extract = cut($text, $length + $countBlanks);
// Return an HTML string for printing
return api_htmlentities($extract);
}
}