From ef7aefbfa7a9c4ed8a06d9cdc82b0e4edad4c429 Mon Sep 17 00:00:00 2001 From: Patrick Cool Date: Tue, 6 May 2008 14:32:09 +0200 Subject: [PATCH] [svn r15212] improved notification --- main/forum/forumfunction.inc.php | 234 ++++++++++++++++++++++++++++++- main/forum/index.php | 28 +++- main/forum/viewforum.php | 30 ++-- main/forum/viewforumcategory.php | 26 +++- 4 files changed, 295 insertions(+), 23 deletions(-) diff --git a/main/forum/forumfunction.inc.php b/main/forum/forumfunction.inc.php index 80fefb4cc1..e629b674f6 100644 --- a/main/forum/forumfunction.inc.php +++ b/main/forum/forumfunction.inc.php @@ -71,7 +71,7 @@ require_once(api_get_path(INCLUDE_PATH).'/lib/mail.lib.inc.php'); require_once(api_get_path(INCLUDE_PATH).'/conf/mail.conf.php'); require_once(api_get_path(INCLUDE_PATH).'/lib/usermanager.lib.php'); - +get_notifications_of_user(); /** * This function handles all the forum and forumcategories actions. This is a wrapper for the * forum and forum categories. All this code code could go into the section where this function is @@ -1694,6 +1694,14 @@ function store_thread($values) $message.=get_lang('ReturnTo').' '.get_lang('Forum').'
'; $message.=get_lang('ReturnTo').' '.get_lang('Message').''; } + $reply_info['new_post_id'] = $last_post_id; + + if ($values['post_notification'] == 1) + { + set_notification('thread',$last_thread_id, true); + } + + send_notification_mails($last_thread_id,$reply_info); session_unregister('formelements'); session_unregister('origin'); @@ -1929,6 +1937,12 @@ function store_reply($values) $message.='
'.get_lang('ReturnTo').' '.get_lang('Forum').'
'; $message.=get_lang('ReturnTo').' '.get_lang('Message').''; + // setting the notification correctly + if ($values['post_notification'] == 1) + { + set_notification('thread',$values['thread_id'], true); + } + send_notification_mails($values['thread_id'], $values); session_unregister('formelements'); @@ -2411,6 +2425,8 @@ function send_notification_mails($thread_id, $reply_info) // the forum category, the forum, the thread and the reply are visible to the user if ($send_mails==true) { + send_notifications($current_thread['forum_id'],$thread_id); + /* $sql="SELECT DISTINCT user.firstname, user.lastname, user.email, user.user_id FROM $table_posts post, $table_user user WHERE post.thread_id='".Database::escape_string($thread_id)."' @@ -2421,11 +2437,17 @@ function send_notification_mails($thread_id, $reply_info) { send_mail($row, $current_thread); } + */ } else { + /* $sql="SELECT * FROM $table_posts WHERE thread_id='".Database::escape_string($thread_id)."' AND post_notification='1'"; $result=api_sql_query($sql, __LINE__, __FILE__); + */ + $table_notification = Database::get_course_table('forum_notification'); + $sql = "SELECT * FROM $table_notification WHERE forum_id = '".Database::escape_string($current_forum['forum_id'])."' OR thread_id = '".Database::escape_string($thread_id)."'"; + $result=api_sql_query($sql, __LINE__, __FILE__); while ($row=Database::fetch_array($result)) { $sql_mailcue="INSERT INTO $table_mailcue (thread_id, post_id) VALUES ('".Database::escape_string($thread_id)."', '".Database::escape_string($reply_info['new_post_id'])."')"; @@ -3116,4 +3138,214 @@ function get_forums_of_group($group_id) } return $forum_list; } + +/** + * This function stores which users have to be notified of which forums or threads + * + * @param string $content does the user want to be notified about a forum or about a thread + * @param integer $id the id of the forum or thread + * + * @author Patrick Cool , Ghent University, Belgium + * @version May 2008, dokeos 1.8.5 + * @since May 2008, dokeos 1.8.5 + */ +function set_notification($content,$id, $add_only = false) +{ + global $_user; + + // which database field do we have to store the id in? + if ($content == 'forum') + { + $database_field = 'forum_id'; + } + else + { + $database_field = 'thread_id'; + } + + // database table definition + $table_notification = Database::get_course_table('forum_notification'); + + // first we check if the notification is already set for this + $sql = "SELECT * FROM $table_notification WHERE $database_field = '".Database::escape_string($id)."' AND user_id = '".Database::escape_string($_user['user_id'])."'"; + $result=api_sql_query($sql, __FILE__, __LINE__); + $total = mysql_num_rows($result); + + // if the user did not indicate that (s)he wanted to be notified already then we store the notification request (to prevent double notification requests) + if ($total <= 0) + { + $sql = "INSERT INTO $table_notification ($database_field, user_id) VALUES ('".Database::escape_string($id)."','".Database::escape_string($_user['user_id'])."')"; + $result=api_sql_query($sql, __FILE__, __LINE__); + api_session_unregister('forum_notification'); + get_notifications_of_user(0,true); + return get_lang('YouWillBeNotifiedOfNewPosts'); + } + else + { + if (!$add_only) + { + $sql = "DELETE FROM $table_notification WHERE $database_field = '".Database::escape_string($id)."' AND user_id = '".Database::escape_string($_user['user_id'])."'"; + $result=api_sql_query($sql, __FILE__, __LINE__); + api_session_unregister('forum_notification'); + get_notifications_of_user(0,true); + return get_lang('YouWillNoLongerBeNotifiedOfNewPosts'); + } + + } +} + +/** + * This function retrieves all the email adresses of the users who wanted to be notified + * about a new post in a certain forum or thread + * + * @param string $content does the user want to be notified about a forum or about a thread + * @param integer $id the id of the forum or thread + * + * @author Patrick Cool , Ghent University, Belgium + * @version May 2008, dokeos 1.8.5 + * @since May 2008, dokeos 1.8.5 + */ +function get_notifications($content,$id) +{ + global $table_users; + + // which database field contains the notification? + if ($content == 'forum') + { + $database_field = 'forum_id'; + } + else + { + $database_field = 'thread_id'; + } + + // database table definition + $table_notification = Database::get_course_table('forum_notification'); + + + $sql = "SELECT user.user_id, user.firstname, user.lastname, user.email, user.user_id user FROM $table_users user, $table_notification notification + WHERE user.user_id = notification.user_id + AND notification.$database_field= '".Database::escape_string($id)."'"; + $result=api_sql_query($sql, __FILE__, __LINE__); + while ($row=Database::fetch_array($result)) + { + $return['user'.$row['user_id']]=array('email' => $row['email'], 'user_id' => $row['user_id']); + } + return $return; +} + +/** + * Get all the users who need to receive a notification of a new post (those subscribed to + * the forum or the thread) + * + * @param integer $forum_id the id of the forum + * @param integer $thread_id the id of the thread + * @param integer $post_id the id of the post + * @return unknown + * + * @author Patrick Cool , Ghent University, Belgium + * @version May 2008, dokeos 1.8.5 + * @since May 2008, dokeos 1.8.5 + */ +function send_notifications($forum_id=0, $thread_id=0, $post_id=0) +{ + global $_course; + + // the content of the mail + $email_subject = get_lang('NewForumPost')." - ".$_course['official_code']; + $thread_link= api_get_path('WEB_CODE_PATH').'forum/viewthread.php?'.api_get_cidreq().'&forum='.$forum_id.'&thread='.$thread_id; + $message .= $link; + + // users who subscribed to the forum + if ($forum_id<>0) + { + $users_to_be_notified_by_forum = get_notifications('forum',$forum_id); + } + else + { + return false; + } + + // user who subscribed to the thread + if ($thread_id<>0) + { + $users_to_be_notified_by_thread = get_notifications('thread',$thread_id); + } + + // merging the two + $users_to_be_notified = array_merge($users_to_be_notified_by_forum, $users_to_be_notified_by_thread); + + if (is_array($users_to_be_notified)) + { + foreach ($users_to_be_notified as $key=>$value) + { + if ($value['email'] <> $_user['email']) + { + $email_body= $value['firstname']." ".$value['lastname']."\n\r"; + $email_body .= '['.$_course['official_code'].'] - ['.$_course['name']."]
\n"; + $email_body .= get_lang('NewForumPost')."\n"; + $email_body .= get_lang('YouWantedToStayInformed')."

\n"; + $email_body .= get_lang('ThreadCanBeFoundHere')." : ".$thread_link."\n"; + + //set the charset and use it for the encoding of the email - small fix, not really clean (should check the content encoding origin first) + //here we use the encoding used for the webpage where the text is encoded (ISO-8859-1 in this case) + if(empty($charset)) + { + $charset='ISO-8859-1'; + } + + if ($user_info['user_id']<>$_user['user_id']) + { + $newmail = api_mail_html($value['lastname'].' '.$value['firstname'], $value['email'], $email_subject, $email_body, $_SESSION['_user']['lastName'].' '.$_SESSION['_user']['firstName'], $_SESSION['_user']['mail']); + } + } + } + } +} + +/** + * Get all the notification subscriptions of the user + * = which forums and which threads does the user wants to be informed of when a new + * post is added to this thread + * + * @param integer $user_id the user_id of a user (default = 0 => the current user) + * @param boolean $force force get the notification subscriptions (even if the information is already in the session + * + * @author Patrick Cool , Ghent University, Belgium + * @version May 2008, dokeos 1.8.5 + * @since May 2008, dokeos 1.8.5 + */ +function get_notifications_of_user($user_id = 0, $force = false) +{ + global $_course; + + if ($user_id == 0) + { + global $_user; + $user_id = $_user['user_id']; + } + + // database table definition + $table_notification = Database::get_course_table('forum_notification'); + + if (!$_SESSION['forum_notification'] OR $_SESSION['forum_notification']['course'] <> $_course['code'] OR $force=true) + { + $_SESSION['forum_notification']['course'] = $_course['code']; + + + $sql = "SELECT * FROM $table_notification WHERE user_id='".Database::escape_string($user_id)."'"; + $result=api_sql_query($sql, __FILE__, __LINE__); + while ($row=Database::fetch_array($result)) + { + if (!is_null($row['forum_id'])) + { + $_SESSION['forum_notification']['forum'][] = $row['forum_id']; + } + if (!is_null($row['thread_id'])) + { + $_SESSION['forum_notification']['thread'][] = $row['thread_id']; + } + } + } +} ?> \ No newline at end of file diff --git a/main/forum/index.php b/main/forum/index.php index 87a1dfad67..3ef411f6f6 100644 --- a/main/forum/index.php +++ b/main/forum/index.php @@ -115,6 +115,13 @@ if (api_is_allowed_to_edit()) handle_forum_and_forumcategories(); } +// notification +if ($_GET['action'] == 'notify' AND isset($_GET['content']) AND isset($_GET['id'])) +{ + $return_message = set_notification($_GET['content'],$_GET['id']); + Display :: display_confirmation_message($return_message,false); +} + if ($_GET['action']!='add' && $_GET['action']!='edit' ) { get_whats_new(); @@ -201,16 +208,16 @@ if ($_GET['action']!='add' && $_GET['action']!='edit' ) } echo "\n"; + echo ''; if (api_is_allowed_to_edit()) { - echo ''; echo "".icon('../img/edit.gif',get_lang('Edit')).""; echo "".icon('../img/delete.gif',get_lang('Delete')).""; display_visible_invisible_icon('forumcategory', prepare4display($forum_category['cat_id']), prepare4display($forum_category['visibility'])); display_lock_unlock_icon('forumcategory',prepare4display($forum_category['cat_id']), prepare4display($forum_category['locked'])); display_up_down_icon('forumcategory',prepare4display($forum_category['cat_id']), $forum_categories_list); - echo ''; } + echo ''; echo "\t\n"; // step 4: the interim headers (for the forum) @@ -219,10 +226,7 @@ if ($_GET['action']!='add' && $_GET['action']!='edit' ) echo "\t\t".get_lang('Topics')."\n"; echo "\t\t".get_lang('Posts')."\n"; echo "\t\t".get_lang('LastPosts')."\n"; - if (api_is_allowed_to_edit()) - { echo "\t\t".get_lang('Actions')."\n"; - } echo "\t\n"; // the forums in this category @@ -374,16 +378,26 @@ if ($_GET['action']!='add' && $_GET['action']!='edit' ) echo "\n"; + echo "\t\t"; if (api_is_allowed_to_edit()) { - echo "\t\t"; echo "".icon('../img/edit.gif',get_lang('Edit')).""; echo "".icon('../img/delete.gif',get_lang('Delete')).""; display_visible_invisible_icon('forum',$forum['forum_id'], $forum['visibility']); display_lock_unlock_icon('forum',$forum['forum_id'], $forum['locked']); display_up_down_icon('forum',$forum['forum_id'], $forums_in_category); - echo "\n"; + } + $iconnotify = 'send_mail.gif'; + if (is_array($_SESSION['forum_notification']['forum'])) + { + if (in_array($forum['forum_id'],$_SESSION['forum_notification']['forum'])) + { + $iconnotify = 'send_mail_checked.gif'; + } + } + echo "".icon('../img/'.$iconnotify,get_lang('NotifyMe')).""; + echo "\n"; echo "\t"; } } diff --git a/main/forum/viewforum.php b/main/forum/viewforum.php index 44a5c3f4c7..f8d0d3b459 100644 --- a/main/forum/viewforum.php +++ b/main/forum/viewforum.php @@ -127,19 +127,27 @@ if (($_GET['action']=='invisible' OR $_GET['action']=='visible') AND isset($_GET { $message=change_visibility($_GET['content'], $_GET['id'],$_GET['action']);// note: this has to be cleaned first } +// locking and unlocking if (($_GET['action']=='lock' OR $_GET['action']=='unlock') AND isset($_GET['content']) AND isset($_GET['id']) AND api_is_allowed_to_edit()) { $message=change_lock_status($_GET['content'], $_GET['id'],$_GET['action']);// note: this has to be cleaned first } +// deleting if ($_GET['action']=='delete' AND isset($_GET['content']) AND isset($_GET['id']) AND api_is_allowed_to_edit()) { $message=delete_forum_forumcategory_thread($_GET['content'],$_GET['id']); // note: this has to be cleaned first } -if ($_GET['action']=='move' and isset($_GET['thread'])) +// moving +if ($_GET['action']=='move' and isset($_GET['thread']) AND api_is_allowed_to_edit()) { $message=move_thread_form(); } - +// notification +if ($_GET['action'] == 'notify' AND isset($_GET['content']) AND isset($_GET['id'])) +{ + $return_message = set_notification($_GET['content'],$_GET['id']); + Display :: display_confirmation_message($return_message,false); +} /* ----------------------------------------------------------- @@ -225,10 +233,7 @@ echo "\t\t".get_lang('Replies')."\n"; echo "\t\t".get_lang('Views')."\n"; echo "\t\t".get_lang('Author')."\n"; echo "\t\t".get_lang('LastPost')."\n"; -if (api_is_allowed_to_edit()) -{ - echo "\t\t".get_lang('Actions')."\n"; -} +echo "\t\t".get_lang('Actions')."\n"; echo "\t\n"; // getting al the threads @@ -321,15 +326,24 @@ if(is_array($threads)) $last_post=$last_post_row['post_date']." ".get_lang('By').' '.$name; } echo "\t\t".$last_post."\n"; + echo "\t\t"; if (api_is_allowed_to_edit()) { - echo "\t\t"; echo "".icon('../img/delete.gif',get_lang('Delete')).""; display_visible_invisible_icon('thread', $row['thread_id'], $row['visibility'], array("forum"=>$_GET['forum'],'origin'=>$origin)); display_lock_unlock_icon('thread',$row['thread_id'], $row['locked'], array("forum"=>$_GET['forum'],'origin'=>$origin)); echo "".icon('../img/deplacer_fichier.gif',get_lang('MoveThread')).""; - echo "\n"; } + $iconnotify = 'send_mail.gif'; + if (is_array($_SESSION['forum_notification']['thread'])) + { + if (in_array($row['thread_id'],$_SESSION['forum_notification']['thread'])) + { + $iconnotify = 'send_mail_checked.gif'; + } + } + echo "".icon('../img/'.$iconnotify,get_lang('NotifyMe')).""; + echo "\n"; echo "\t\n"; } $counter++; diff --git a/main/forum/viewforumcategory.php b/main/forum/viewforumcategory.php index 7e7f838ba0..432f7e171a 100644 --- a/main/forum/viewforumcategory.php +++ b/main/forum/viewforumcategory.php @@ -134,6 +134,12 @@ if (api_is_allowed_to_edit()) handle_forum_and_forumcategories(); } +// notification +if ($_GET['action'] == 'notify' AND isset($_GET['content']) AND isset($_GET['id'])) +{ + $return_message = set_notification($_GET['content'],$_GET['id']); + Display :: display_confirmation_message($return_message,false); +} if ($_GET['action']!='add') { /* @@ -180,7 +186,7 @@ if (api_is_allowed_to_edit()) */ echo "\n"; - echo "\t\n\t\t\n\t\t\n"; @@ -203,10 +209,7 @@ echo "\t\t\n"; echo "\t\t\n"; echo "\t\t\n"; echo "\t\t\n"; -if (api_is_allowed_to_edit()) -{ - echo "\t\t\n"; -} +echo "\t\t\n"; echo "\t\n"; // the forums in this category @@ -325,16 +328,25 @@ foreach ($forum_list as $key=>$forum) echo $forum['last_post_date']." ".get_lang('By').' '.display_user_link($poster_id, $name); } echo "\n"; + echo "\t\t\n"; } + $iconnotify = 'send_mail.gif'; + if (is_array($_SESSION['forum_notification']['forum'])) + { + if (in_array($forum['forum_id'],$_SESSION['forum_notification']['forum'])) + { + $iconnotify = 'send_mail_checked.gif'; + } + } + echo "".icon('../img/'.$iconnotify,get_lang('NotifyMe')).""; + echo "\n"; echo "\t"; } }
"; + echo "\t
"; echo ''.prepare4display($forum_category['cat_title']).'
'; echo ''.prepare4display($forum_category['cat_comment']).''; echo "
".get_lang('Forum')."".get_lang('Topics')."".get_lang('Posts')."".get_lang('LastPosts')."".get_lang('Actions')."".get_lang('Actions')."
"; if (api_is_allowed_to_edit()) { - echo "\t\t"; echo "".icon('../img/edit.gif',get_lang('Edit')).""; echo "".icon('../img/delete.gif',get_lang('Delete')).""; display_visible_invisible_icon('forum',$forum['forum_id'], $forum['visibility'], array("forumcategory"=>$_GET['forumcategory'])); display_lock_unlock_icon('forum',$forum['forum_id'], $forum['locked'], array("forumcategory"=>$_GET['forumcategory'])); display_up_down_icon('forum',$forum['forum_id'], $forums_in_category); - echo "