* */ class Blog { /** * Get the title of a blog * @author Toon Keppens * * @param Integer $blog_id * * @return String Blog Title */ function get_blog_title($blog_id) { if(is_numeric($blog_id)) { // init $tbl_blogs = Database::get_course_table(BLOGS_TABLE); $sql = " SELECT `blog_name` FROM " . $tbl_blogs . " WHERE `blog_id` = " . $blog_id; $result = api_sql_query($sql, __FILE__, __LINE__); $blog = mysql_fetch_array($result); return stripslashes($blog['blog_name']); } } /** * Get the description of a blog * @author Toon Keppens * * @param Integer $blog_id * * @return String Blog description */ function get_blog_subtitle($blog_id) { // init $tbl_blogs = Database::get_course_table(BLOGS_TABLE); $sql = "SELECT `blog_subtitle` FROM $tbl_blogs WHERE `blog_id` =$blog_id"; $result = api_sql_query($sql, __FILE__, __LINE__); $blog = mysql_fetch_array($result); return stripslashes($blog['blog_subtitle']); } /** * Get the users of a blog * @author Toon Keppens * * @param Integer $blog_id * * @return Array Returns an array with [userid]=>[username] */ function get_blog_users($blog_id) { // Database table definitions $tbl_users = Database::get_main_table(TABLE_MAIN_USER); $tbl_blogs = Database::get_course_table(BLOGS_TABLE); $tbl_blogs_rel_user = Database::get_course_table(BLOGS_REL_USER_TABLE); // 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.blog_id = " . $blog_id; $result = api_sql_query($sql, __FILE__, __LINE__); $blog_members = array (); while($user = mysql_fetch_array($result)) { $blog_members[$user['user_id']] = $user['lastname']." " . $user['firstname']; } return $blog_members; } /** * Creates a new blog in the given course * @author Toon Keppens * * @param Integer $course_id Id * @param String $title * @param Text $description * * @return void */ function create_blog($title, $subtitle) { global $_user; // Tabel definitions $tbl_blogs = Database::get_course_table(BLOGS_TABLE); $tbl_tool = Database::get_course_table(TOOL_LIST_TABLE); $tbl_blogs_posts = Database::get_course_table(BLOGS_POSTS_TABLE); $tbl_blogs_tasks = Database::get_course_table(BLOGS_TASKS); // Create the blog $sql = "INSERT INTO $tbl_blogs (`blog_name`, `blog_subtitle`, `date_creation`, `visibility` ) VALUES ('$title', '$subtitle', NOW(), '1');"; api_sql_query($sql, __FILE__, __LINE__); $this_blog_id = Database::get_last_insert_id(); // Make first post. :) $sql = "INSERT INTO $tbl_blogs_posts (`title`, `full_text`, `date`, `blog_id`, `author` ) VALUES ('Welkom!', '" . get_lang('FirstPostText')."', NOW(), '$this_blog_id', '".$_user['user_id']."');"; api_sql_query($sql, __FILE__, __LINE__); // Put it on course homepage $sql = "INSERT INTO $tbl_tool (name, link, image, visibility, admin, address, added_tool) VALUES ('$title','blog/blog.php?blog_id=$this_blog_id','blog.gif','1','0','pastillegris.gif',0)"; api_sql_query($sql, __FILE__, __LINE__); // Subscribe the teacher to this blog Blog::set_user_subscribed($this_blog_id,$_user['user_id']); return void; } /** * Update title and subtitle of a blog in the given course * @author Toon Keppens * * @param Integer $course_id Id * @param String $title * @param Text $description * * @return void */ function edit_blog($blog_id, $title, $subtitle) { global $_user; // Table definitions $tbl_blogs = Database::get_course_table(BLOGS_TABLE); $tbl_tool = Database::get_course_table(TOOL_LIST_TABLE); // Update the blog $sql = "UPDATE $tbl_blogs SET blog_name = '$title', blog_subtitle = '$subtitle' WHERE blog_id =$blog_id LIMIT 1"; api_sql_query($sql, __FILE__, __LINE__); $this_blog_id = Database::get_last_insert_id(); // Update course homepage link $sql = "UPDATE $tbl_tool SET name = '$title' WHERE link = 'blog/blog.php?blog_id=$blog_id' LIMIT 1"; api_sql_query($sql, __FILE__, __LINE__); return void; } /** * Deletes a blog and it's posts from the course database * @author Toon Keppens * * @param Integer $blog_id * * @return void */ function delete_blog($blog_id) { // Init $tbl_blogs = Database::get_course_table(BLOGS_TABLE); $tbl_blogs_posts = Database::get_course_table(BLOGS_POSTS_TABLE); $tbl_blogs_tasks = Database::get_course_table(BLOGS_TASKS); $tbl_tool = Database::get_course_table(TOOL_LIST_TABLE); $tbl_blogs_rating = Database::get_course_table(BLOGS_RATING); // Delete posts $sql = "DELETE FROM $tbl_blogs_posts WHERE blog_id = $blog_id"; api_sql_query($sql, __FILE__, __LINE__); // Delete tasks $sql = "DELETE FROM $tbl_blogs_tasks WHERE blog_id = $blog_id"; api_sql_query($sql, __FILE__, __LINE__); // Delete ratings $sql = "DELETE FROM $tbl_blogs_rating WHERE blog_id = $blog_id"; api_sql_query($sql, __FILE__, __LINE__); // Delete blog $sql ="DELETE FROM $tbl_blogs WHERE blog_id = $blog_id"; api_sql_query($sql, __FILE__, __LINE__); // Delete from course homepage $sql = "DELETE FROM $tbl_tool WHERE link = 'blog/blog.php?blog_id=$blog_id'"; api_sql_query($sql, __FILE__, __LINE__); return void; } /** * Creates a new post in a given blog * @author Toon Keppens * * @param String $title * @param String $full_text * @param Integer $blog_id * * @return void */ function create_post($title, $full_text, $blog_id) { global $_user; // Table Definitions $tbl_blogs_posts = Database::get_course_table(BLOGS_POSTS_TABLE); // Create the post $sql = "INSERT INTO " . $tbl_blogs_posts." (`title`, `full_text`, `date`, `blog_id`, `author` ) VALUES ('$title', '$full_text', NOW(), '$blog_id', '".$_user['user_id']."');"; api_sql_query($sql, __FILE__, __LINE__); return void; } /** * Edits a post in a given blog * @author Toon Keppens * * @param Integer $blog_id * @param String $title * @param String $full_text * @param Integer $blog_id * * @return void */ function edit_post($post_id, $title, $full_text, $blog_id) { // Init $tbl_blogs_posts = Database::get_course_table(BLOGS_POSTS_TABLE); // Create the post $sql = "UPDATE $tbl_blogs_posts SET title = '" . $title."', full_text = '" . $full_text."' WHERE post_id =$post_id AND blog_id =$blog_id LIMIT 1 ;"; api_sql_query($sql, __FILE__, __LINE__); return void; } /** * Deletes an article and it's comments * @author Toon Keppens * * @param Integer $blog_id * @param Integer $post_id * * @return void */ function delete_post($blog_id, $post_id) { // Init $tbl_blogs_posts = Database::get_course_table(BLOGS_POSTS_TABLE); $tbl_blogs_comments = Database::get_course_table(BLOGS_COMMENTS_TABLE); $tbl_blogs_rating = Database::get_course_table(BLOGS_RATING); // Delete ratings on this comment $sql = "DELETE FROM $tbl_blogs_rating WHERE blog_id = $blog_id AND item_id = $post_id AND rating_type = 'post'"; api_sql_query($sql, __FILE__, __LINE__); // Delete the post $sql = "DELETE FROM $tbl_blogs_posts WHERE `post_id` = $post_id"; api_sql_query($sql, __FILE__, __LINE__); // Delete the comments $sql = "DELETE FROM $tbl_blogs_comments WHERE `post_id` = $post_id AND `blog_id` = $blog_id"; api_sql_query($sql, __FILE__, __LINE__); return void; } /** * Creates a comment on a post in a given blog * @author Toon Keppens * * @param String $title * @param String $full_text * @param Integer $blog_id * @param Integer $post_id * @param Integer $parent_id * * @return void */ function create_comment($title, $full_text, $blog_id, $post_id, $parent_id, $task_id = 'NULL') { global $_user; // Table Definition $tbl_blogs_comments = Database::get_course_table(BLOGS_COMMENTS_TABLE); // Create the comment $sql = "INSERT INTO $tbl_blogs_comments (`title`, `comment`, `author`, `date`, `blog_id`, `post_id`, `parent_comment_id`, `task_id` ) VALUES ('$title', '$full_text', '".$_user['user_id']."', NOW(), '$blog_id', '$post_id', '$parent_id', $task_id)"; api_sql_query($sql, __FILE__, __LINE__); // Empty post values, or they are shown on the page again $_POST['comment_title'] = ""; $_POST['comment_text'] = ""; return void; } /** * Deletes a comment from a blogpost * @author Toon Keppens * * @param Integer $blog_id * @param Integer $comment_id * * @return void */ function delete_comment($blog_id, $comment_id) { // Init $tbl_blogs_comments = Database::get_course_table(BLOGS_COMMENTS_TABLE); $tbl_blogs_rating = Database::get_course_table(BLOGS_RATING); // Delete ratings on this comment $sql = "DELETE FROM $tbl_blogs_rating WHERE blog_id = $blog_id AND item_id = $comment_id AND rating_type = 'comment'"; api_sql_query($sql, __FILE__, __LINE__); // select comments that have the selected comment as their parent $sql = "SELECT comment_id FROM $tbl_blogs_comments WHERE parent_comment_id = $comment_id"; $result = api_sql_query($sql, __FILE__, __LINE__); // Delete them recursively while($comment = mysql_fetch_array($result)) { Blog::delete_comment($blog_id, $comment['comment_id']); } // Finally, delete the selected comment to $sql = "DELETE FROM $tbl_blogs_comments WHERE `comment_id` = $comment_id"; api_sql_query($sql, __FILE__, __LINE__); return void; } /** * Creates a new task in a blog * @author Toon Keppens * * @param Integer $blog_id * @param String $title * @param String $description * @param String $color * * @return void */ function create_task($blog_id, $title, $description, $articleDelete, $articleEdit, $commentsDelete, $color) { // Init $tbl_blogs_tasks = Database::get_course_table(BLOGS_TASKS); $tbl_tasks_permissions = Database::get_course_table(BLOGS_TASKS_PERMISSIONS); // Create the task $sql = "INSERT INTO $tbl_blogs_tasks (`blog_id`, `title`, `description`, `color`, `system_task` ) VALUES ('$blog_id', '" . $title."', '" . $description."', '" . $color."', '0');"; api_sql_query($sql, __FILE__, __LINE__); $task_id = mysql_insert_id(); $tool = 'BLOG_' . $blog_id; if($articleDelete == 'on') { $sql = " INSERT INTO " . $tbl_tasks_permissions . " ( `task_id`, `tool`, `action` ) VALUES ( " . $task_id . ", '" . $tool . "', 'article_delete' )"; api_sql_query($sql, __FILE__, __LINE__); } if($articleEdit == 'on') { $sql = " INSERT INTO " . $tbl_tasks_permissions . " ( `task_id`, `tool`, `action` ) VALUES ( " . $task_id . ", '" . $tool . "', 'article_edit' )"; api_sql_query($sql, __FILE__, __LINE__); } if($commentsDelete == 'on') { $sql = " INSERT INTO " . $tbl_tasks_permissions . " ( `task_id`, `tool`, `action` ) VALUES ( " . $task_id . ", '" . $tool . "', 'article_comments_delete' )"; api_sql_query($sql, __FILE__, __LINE__); } return void; } /** * Edit a task in a blog * @author Toon Keppens * * @param Integer $task_id * @param String $title * @param String $description * @param String $color * * @return void */ function edit_task($blog_id, $task_id, $title, $description, $articleDelete, $articleEdit, $commentsDelete, $color) { // Init $tbl_blogs_tasks = Database::get_course_table(BLOGS_TASKS); $tbl_tasks_permissions = Database::get_course_table(BLOGS_TASKS_PERMISSIONS); // Create the task $sql = "UPDATE $tbl_blogs_tasks SET title = '$title', description = '$description', color = '$color' WHERE task_id =$task_id LIMIT 1"; api_sql_query($sql, __FILE__, __LINE__); $tool = 'BLOG_' . $blog_id; $sql = " DELETE FROM " . $tbl_tasks_permissions . " WHERE `task_id` = " . $task_id; api_sql_query($sql, __FILE__, __LINE__); if($articleDelete == 'on') { $sql = " INSERT INTO " . $tbl_tasks_permissions . " ( `task_id`, `tool`, `action` ) VALUES ( " . $task_id . ", '" . $tool . "', 'article_delete' )"; api_sql_query($sql, __FILE__, __LINE__); } if($articleEdit == 'on') { $sql = " INSERT INTO " . $tbl_tasks_permissions . " ( `task_id`, `tool`, `action` ) VALUES ( " . $task_id . ", '" . $tool . "', 'article_edit' )"; api_sql_query($sql, __FILE__, __LINE__); } if($commentsDelete == 'on') { $sql = " INSERT INTO " . $tbl_tasks_permissions . " ( `task_id`, `tool`, `action` ) VALUES ( " . $task_id . ", '" . $tool . "', 'article_comments_delete' )"; api_sql_query($sql, __FILE__, __LINE__); } return void; } /** * Deletes a task from a blog * * @param Integer $blog_id * @param Integer $task_id */ function delete_task($blog_id, $task_id) { // Init $tbl_blogs_tasks = Database::get_course_table(BLOGS_TASKS); // Delete posts $sql = "DELETE FROM $tbl_blogs_tasks WHERE `blog_id` = $blog_id AND `task_id` = $task_id"; api_sql_query($sql, __FILE__, __LINE__); return void; } /** * Deletes an assigned task from a blog * * @param Integer $blog_id * @param Integer $assignment_id */ function delete_assigned_task($blog_id, $assignment_id) { // Init $tbl_blogs_tasks_rel_user = Database::get_course_table(BLOGS_TASKS_REL_USER); $parameters = explode('|',$assignment_id); $task_id = $parameters[0]; $user_id = $parameters[1]; // Delete posts $sql = "DELETE FROM $tbl_blogs_tasks_rel_user WHERE `blog_id` = $blog_id AND `task_id` = $task_id AND `user_id` = $user_id"; api_sql_query($sql, __FILE__, __LINE__); return void; } /** * Get personal task list * @author Toon Keppens * * @return Returns an unsorted list (