diff --git a/main/inc/lib/social.lib.php b/main/inc/lib/social.lib.php index 0af81fa72a..40524df63e 100644 --- a/main/inc/lib/social.lib.php +++ b/main/inc/lib/social.lib.php @@ -2,10 +2,10 @@ /* For licensing terms, see /license.txt */ /** -* This class provides methods for the social network management. -* Include/require it in your code to use its features. +* This class provides methods for the social network management. +* Include/require it in your code to use its features. * -* @package chamilo.social +* @package chamilo.social */ /** * Code @@ -22,551 +22,551 @@ require_once api_get_path(LIBRARY_PATH).'message.lib.php'; /** * -* @package chamilo.social +* @package chamilo.social */ class SocialManager extends UserManager { - private function __construct() { - } - - /** - * Allow to see contacts list - * @author isaac flores paz - * @return array - */ - public static function show_list_type_friends () { - $friend_relation_list=array(); - $count_list=0; - $tbl_my_friend_relation_type = Database :: get_main_table(TABLE_MAIN_USER_FRIEND_RELATION_TYPE); - $sql='SELECT id,title FROM '.$tbl_my_friend_relation_type.' WHERE id<>6 ORDER BY id ASC'; - $result=Database::query($sql); - while ($row=Database::fetch_array($result,'ASSOC')) { - $friend_relation_list[]=$row; - } - $count_list=count($friend_relation_list); - if ($count_list==0) { - $friend_relation_list[]=get_lang('Unknown'); - } else { - return $friend_relation_list; - } - - } - /** - * Get relation type contact by name - * @param string names of the kind of relation - * @return int - * @author isaac flores paz - */ - public static function get_relation_type_by_name ($relation_type_name) { - $list_type_friend=array(); - $list_type_friend=self::show_list_type_friends(); - foreach ($list_type_friend as $value_type_friend) { - if (strtolower($value_type_friend['title'])==$relation_type_name) { - return $value_type_friend['id']; - } - } - } - /** - * Get the kind of relation between contacts - * @param int user id - * @param int user friend id - * @param string - * @author isaac flores paz - */ - public static function get_relation_between_contacts ($user_id,$user_friend) { - $tbl_my_friend_relation_type = Database :: get_main_table(TABLE_MAIN_USER_FRIEND_RELATION_TYPE); - $tbl_my_friend = Database :: get_main_table(TABLE_MAIN_USER_REL_USER); - $sql= 'SELECT rt.id as id FROM '.$tbl_my_friend_relation_type.' rt ' . - 'WHERE rt.id=(SELECT uf.relation_type FROM '.$tbl_my_friend.' uf WHERE user_id='.((int)$user_id).' AND friend_user_id='.((int)$user_friend).' AND uf.relation_type <> '.USER_RELATION_TYPE_RRHH.' )'; - $res=Database::query($sql); - if (Database::num_rows($res)>0) { - $row=Database::fetch_array($res,'ASSOC'); - return $row['id']; - } else { - return USER_UNKNOW; - } - } - - /** - * Gets friends id list - * @param int user id - * @param int group id - * @param string name to search - * @param bool true will load firstname, lastname, and image name - * @return array - * @author Julio Montoya Cleaning code, function renamed, $load_extra_info option added - * @author isaac flores paz - */ - public static function get_friends($user_id, $id_group = null, $search_name = null, $load_extra_info = true) { - $list_ids_friends=array(); - $tbl_my_friend = Database :: get_main_table(TABLE_MAIN_USER_REL_USER); - $tbl_my_user = Database :: get_main_table(TABLE_MAIN_USER); - $sql='SELECT friend_user_id FROM '.$tbl_my_friend.' WHERE relation_type NOT IN ('.USER_RELATION_TYPE_DELETED.', '.USER_RELATION_TYPE_RRHH.') AND friend_user_id<>'.((int)$user_id).' AND user_id='.((int)$user_id); - if (isset($id_group) && $id_group>0) { - $sql.=' AND relation_type='.$id_group; - } - if (isset($search_name)) { - $search_name = trim($search_name); - $search_name = str_replace(' ', '', $search_name); - $sql.=' AND friend_user_id IN (SELECT user_id FROM '.$tbl_my_user.' WHERE firstName LIKE "%'.Database::escape_string($search_name).'%" OR lastName LIKE "%'.Database::escape_string($search_name).'%" OR '.(api_is_western_name_order() ? 'concat(firstName, lastName)' : 'concat(lastName, firstName)').' like concat("%","'.Database::escape_string($search_name).'","%") ) '; - } - - $res = Database::query($sql); - while ($row = Database::fetch_array($res, 'ASSOC')) { - if ($load_extra_info) { - $path = UserManager::get_user_picture_path_by_id($row['friend_user_id'],'web',false,true); - $my_user_info = api_get_user_info($row['friend_user_id']); - $list_ids_friends[] = array('friend_user_id'=>$row['friend_user_id'],'firstName'=>$my_user_info['firstName'] , 'lastName'=>$my_user_info['lastName'], 'username'=>$my_user_info['username'], 'image'=>$path['file']); - } else { - $list_ids_friends[] = $row; - } - } - return $list_ids_friends; - } - - - /** - * get list web path of contacts by user id - * @param int user id - * @param int group id - * @param string name to search - * @param array - * @author isaac flores paz - */ - public static function get_list_path_web_by_user_id ($user_id,$id_group=null,$search_name=null) { - $combine_friend = array(); - $list_ids = self::get_friends($user_id,$id_group,$search_name); - if (is_array($list_ids)) { - foreach ($list_ids as $values_ids) { - $list_path_image_friend[] = UserManager::get_user_picture_path_by_id($values_ids['friend_user_id'],'web',false,true); - $combine_friend=array('id_friend'=>$list_ids,'path_friend'=>$list_path_image_friend); - } - } - return $combine_friend; - } - - /** - * get web path of user invitate - * @author isaac flores paz - * @author Julio Montoya setting variable array - * @param int user id - * @return array - */ - public static function get_list_web_path_user_invitation_by_user_id ($user_id) { - $list_ids = self::get_list_invitation_of_friends_by_user_id((int)$user_id); - $list_path_image_friend = array(); - foreach ($list_ids as $values_ids) { - $list_path_image_friend[] = UserManager::get_user_picture_path_by_id($values_ids['user_sender_id'],'web',false,true); - } - return $list_path_image_friend; - } - - /** - * Sends an invitation to contacts - * @param int user id - * @param int user friend id - * @param string title of the message - * @param string content of the message - * @return boolean - * @author isaac flores paz - * @author Julio Montoya Cleaning code - */ - public static function send_invitation_friend($user_id, $friend_id, $message_title, $message_content) { - $tbl_message = Database::get_main_table(TABLE_MAIN_MESSAGE); - $user_id = intval($user_id); - $friend_id = intval($friend_id); - - //Just in case we replace the and \n and \n\r while saving in the DB - $message_content = str_replace(array("\n", "\n\r"), '
', $message_content); - - $clean_message_title = Database::escape_string($message_title); - $clean_message_content = Database::escape_string($message_content); - - $now = api_get_utc_datetime(); - - $sql_exist='SELECT COUNT(*) AS count FROM '.$tbl_message.' WHERE user_sender_id='.$user_id.' AND user_receiver_id='.$friend_id.' AND msg_status IN(5,6,7);'; - - $res_exist = Database::query($sql_exist); - $row_exist = Database::fetch_array($res_exist,'ASSOC'); - - if ($row_exist['count']==0) { - - $sql=' INSERT INTO '.$tbl_message.'(user_sender_id,user_receiver_id,msg_status,send_date,title,content) - VALUES('.$user_id.','.$friend_id.','.MESSAGE_STATUS_INVITATION_PENDING.',"'.$now.'","'.$clean_message_title.'","'.$clean_message_content.'") '; - Database::query($sql); - - $sender_info = api_get_user_info($user_id); - $notification = new Notification(); - $notification->save_notification(NOTIFICATION_TYPE_INVITATION, array($friend_id), $message_title, $message_content, $sender_info); - - return true; - } else { - //invitation already exist - $sql_if_exist ='SELECT COUNT(*) AS count, id FROM '.$tbl_message.' WHERE user_sender_id='.$user_id.' AND user_receiver_id='.$friend_id.' AND msg_status = 7'; - $res_if_exist = Database::query($sql_if_exist); - $row_if_exist = Database::fetch_array($res_if_exist,'ASSOC'); - if ($row_if_exist['count']==1) { - $sql_if_exist_up='UPDATE '.$tbl_message.'SET msg_status=5, content = "'.$clean_message_content.'" WHERE user_sender_id='.$user_id.' AND user_receiver_id='.$friend_id.' AND msg_status = 7 '; - Database::query($sql_if_exist_up); - return true; - } else { - return false; - } - } - } - /** - * Get number messages of the inbox - * @author isaac flores paz - * @param int user receiver id - * @return int - */ - public static function get_message_number_invitation_by_user_id ($user_receiver_id) { - $tbl_message=Database::get_main_table(TABLE_MAIN_MESSAGE); - $sql='SELECT COUNT(*) as count_message_in_box FROM '.$tbl_message.' WHERE user_receiver_id='.intval($user_receiver_id).' AND msg_status='.MESSAGE_STATUS_INVITATION_PENDING; - $res=Database::query($sql); - $row=Database::fetch_array($res,'ASSOC'); - return $row['count_message_in_box']; - } - - /** - * Get invitation list received by user - * @author isaac flores paz - * @param int user id - * @return array() - */ - public static function get_list_invitation_of_friends_by_user_id ($user_id) { - $list_friend_invitation=array(); - $tbl_message = Database::get_main_table(TABLE_MAIN_MESSAGE); - $sql = 'SELECT user_sender_id,send_date,title,content FROM '.$tbl_message.' WHERE user_receiver_id='.intval($user_id).' AND msg_status = '.MESSAGE_STATUS_INVITATION_PENDING; - $res = Database::query($sql); - while ($row = Database::fetch_array($res,'ASSOC')) { - $list_friend_invitation[]=$row; - } - return $list_friend_invitation; - } - - /** - * Get invitation list sent by user - * @author Julio Montoya - * @param int user id - * @return array() - */ - - public static function get_list_invitation_sent_by_user_id ($user_id) { - $list_friend_invitation=array(); - $tbl_message=Database::get_main_table(TABLE_MAIN_MESSAGE); - $sql='SELECT user_receiver_id, send_date,title,content FROM '.$tbl_message.' WHERE user_sender_id = '.intval($user_id).' AND msg_status = '.MESSAGE_STATUS_INVITATION_PENDING; - $res=Database::query($sql); - while ($row=Database::fetch_array($res,'ASSOC')) { - $list_friend_invitation[$row['user_receiver_id']]=$row; - } - return $list_friend_invitation; - } - - /** - * Accepts invitation - * @param int user sender id - * @param int user receiver id - * @author isaac flores paz - * @author Julio Montoya Cleaning code - */ - public static function invitation_accepted ($user_send_id,$user_receiver_id) { - $tbl_message=Database::get_main_table(TABLE_MAIN_MESSAGE); - $sql='UPDATE '.$tbl_message.' SET msg_status='.MESSAGE_STATUS_INVITATION_ACCEPTED.' WHERE user_sender_id='.((int)$user_send_id).' AND user_receiver_id='.((int)$user_receiver_id).';'; - Database::query($sql); - } - /** - * Denies invitation - * @param int user sender id - * @param int user receiver id - * @author isaac flores paz - * @author Julio Montoya Cleaning code - */ - public static function invitation_denied ($user_send_id,$user_receiver_id) { - $tbl_message=Database::get_main_table(TABLE_MAIN_MESSAGE); - //$msg_status=7; - //$sql='UPDATE '.$tbl_message.' SET msg_status='.$msg_status.' WHERE user_sender_id='.((int)$user_send_id).' AND user_receiver_id='.((int)$user_receiver_id).';'; - $sql='DELETE FROM '.$tbl_message.' WHERE user_sender_id='.((int)$user_send_id).' AND user_receiver_id='.((int)$user_receiver_id).';'; - Database::query($sql); - } - /** - * allow attach to group - * @author isaac flores paz - * @param int user to qualify - * @param int kind of rating - * @return void() - */ - public static function qualify_friend ($id_friend_qualify,$type_qualify) { - $tbl_user_friend=Database::get_main_table(TABLE_MAIN_USER_REL_USER); - $user_id=api_get_user_id(); - $sql='UPDATE '.$tbl_user_friend.' SET relation_type='.((int)$type_qualify).' WHERE user_id='.((int)$user_id).' AND friend_user_id='.((int)$id_friend_qualify).';'; - Database::query($sql); - } - /** - * Sends invitations to friends - * @author Isaac Flores Paz - * @author Julio Montoya Cleaning code - * @param void - * @return string message invitation - */ - public static function send_invitation_friend_user($userfriend_id, $subject_message = '', $content_message = '') { - global $charset; - - $user_info = array(); - $user_info = api_get_user_info($userfriend_id); - $succes = get_lang('MessageSentTo'); - $succes.= ' : '.api_get_person_name($user_info['firstName'], $user_info['lastName']); - - if (isset($subject_message) && isset($content_message) && isset($userfriend_id)) { - $send_message = MessageManager::send_message($userfriend_id, $subject_message, $content_message); + private function __construct() { + } + + /** + * Allow to see contacts list + * @author isaac flores paz + * @return array + */ + public static function show_list_type_friends () { + $friend_relation_list=array(); + $count_list=0; + $tbl_my_friend_relation_type = Database :: get_main_table(TABLE_MAIN_USER_FRIEND_RELATION_TYPE); + $sql='SELECT id,title FROM '.$tbl_my_friend_relation_type.' WHERE id<>6 ORDER BY id ASC'; + $result=Database::query($sql); + while ($row=Database::fetch_array($result,'ASSOC')) { + $friend_relation_list[]=$row; + } + $count_list=count($friend_relation_list); + if ($count_list==0) { + $friend_relation_list[]=get_lang('Unknown'); + } else { + return $friend_relation_list; + } + + } + /** + * Get relation type contact by name + * @param string names of the kind of relation + * @return int + * @author isaac flores paz + */ + public static function get_relation_type_by_name ($relation_type_name) { + $list_type_friend=array(); + $list_type_friend=self::show_list_type_friends(); + foreach ($list_type_friend as $value_type_friend) { + if (strtolower($value_type_friend['title'])==$relation_type_name) { + return $value_type_friend['id']; + } + } + } + /** + * Get the kind of relation between contacts + * @param int user id + * @param int user friend id + * @param string + * @author isaac flores paz + */ + public static function get_relation_between_contacts ($user_id,$user_friend) { + $tbl_my_friend_relation_type = Database :: get_main_table(TABLE_MAIN_USER_FRIEND_RELATION_TYPE); + $tbl_my_friend = Database :: get_main_table(TABLE_MAIN_USER_REL_USER); + $sql= 'SELECT rt.id as id FROM '.$tbl_my_friend_relation_type.' rt ' . + 'WHERE rt.id=(SELECT uf.relation_type FROM '.$tbl_my_friend.' uf WHERE user_id='.((int)$user_id).' AND friend_user_id='.((int)$user_friend).' AND uf.relation_type <> '.USER_RELATION_TYPE_RRHH.' )'; + $res=Database::query($sql); + if (Database::num_rows($res)>0) { + $row=Database::fetch_array($res,'ASSOC'); + return $row['id']; + } else { + return USER_UNKNOW; + } + } + + /** + * Gets friends id list + * @param int user id + * @param int group id + * @param string name to search + * @param bool true will load firstname, lastname, and image name + * @return array + * @author Julio Montoya Cleaning code, function renamed, $load_extra_info option added + * @author isaac flores paz + */ + public static function get_friends($user_id, $id_group = null, $search_name = null, $load_extra_info = true) { + $list_ids_friends=array(); + $tbl_my_friend = Database :: get_main_table(TABLE_MAIN_USER_REL_USER); + $tbl_my_user = Database :: get_main_table(TABLE_MAIN_USER); + $sql='SELECT friend_user_id FROM '.$tbl_my_friend.' WHERE relation_type NOT IN ('.USER_RELATION_TYPE_DELETED.', '.USER_RELATION_TYPE_RRHH.') AND friend_user_id<>'.((int)$user_id).' AND user_id='.((int)$user_id); + if (isset($id_group) && $id_group>0) { + $sql.=' AND relation_type='.$id_group; + } + if (isset($search_name)) { + $search_name = trim($search_name); + $search_name = str_replace(' ', '', $search_name); + $sql.=' AND friend_user_id IN (SELECT user_id FROM '.$tbl_my_user.' WHERE firstName LIKE "%'.Database::escape_string($search_name).'%" OR lastName LIKE "%'.Database::escape_string($search_name).'%" OR '.(api_is_western_name_order() ? 'concat(firstName, lastName)' : 'concat(lastName, firstName)').' like concat("%","'.Database::escape_string($search_name).'","%") ) '; + } + + $res = Database::query($sql); + while ($row = Database::fetch_array($res, 'ASSOC')) { + if ($load_extra_info) { + $path = UserManager::get_user_picture_path_by_id($row['friend_user_id'],'web',false,true); + $my_user_info = api_get_user_info($row['friend_user_id']); + $list_ids_friends[] = array('friend_user_id'=>$row['friend_user_id'],'firstName'=>$my_user_info['firstName'] , 'lastName'=>$my_user_info['lastName'], 'username'=>$my_user_info['username'], 'image'=>$path['file']); + } else { + $list_ids_friends[] = $row; + } + } + return $list_ids_friends; + } + + + /** + * get list web path of contacts by user id + * @param int user id + * @param int group id + * @param string name to search + * @param array + * @author isaac flores paz + */ + public static function get_list_path_web_by_user_id ($user_id,$id_group=null,$search_name=null) { + $combine_friend = array(); + $list_ids = self::get_friends($user_id,$id_group,$search_name); + if (is_array($list_ids)) { + foreach ($list_ids as $values_ids) { + $list_path_image_friend[] = UserManager::get_user_picture_path_by_id($values_ids['friend_user_id'],'web',false,true); + $combine_friend=array('id_friend'=>$list_ids,'path_friend'=>$list_path_image_friend); + } + } + return $combine_friend; + } + + /** + * get web path of user invitate + * @author isaac flores paz + * @author Julio Montoya setting variable array + * @param int user id + * @return array + */ + public static function get_list_web_path_user_invitation_by_user_id ($user_id) { + $list_ids = self::get_list_invitation_of_friends_by_user_id((int)$user_id); + $list_path_image_friend = array(); + foreach ($list_ids as $values_ids) { + $list_path_image_friend[] = UserManager::get_user_picture_path_by_id($values_ids['user_sender_id'],'web',false,true); + } + return $list_path_image_friend; + } + + /** + * Sends an invitation to contacts + * @param int user id + * @param int user friend id + * @param string title of the message + * @param string content of the message + * @return boolean + * @author isaac flores paz + * @author Julio Montoya Cleaning code + */ + public static function send_invitation_friend($user_id, $friend_id, $message_title, $message_content) { + $tbl_message = Database::get_main_table(TABLE_MAIN_MESSAGE); + $user_id = intval($user_id); + $friend_id = intval($friend_id); + + //Just in case we replace the and \n and \n\r while saving in the DB + $message_content = str_replace(array("\n", "\n\r"), '
', $message_content); + + $clean_message_title = Database::escape_string($message_title); + $clean_message_content = Database::escape_string($message_content); + + $now = api_get_utc_datetime(); + + $sql_exist='SELECT COUNT(*) AS count FROM '.$tbl_message.' WHERE user_sender_id='.$user_id.' AND user_receiver_id='.$friend_id.' AND msg_status IN(5,6,7);'; + + $res_exist = Database::query($sql_exist); + $row_exist = Database::fetch_array($res_exist,'ASSOC'); + + if ($row_exist['count']==0) { + + $sql=' INSERT INTO '.$tbl_message.'(user_sender_id,user_receiver_id,msg_status,send_date,title,content) + VALUES('.$user_id.','.$friend_id.','.MESSAGE_STATUS_INVITATION_PENDING.',"'.$now.'","'.$clean_message_title.'","'.$clean_message_content.'") '; + Database::query($sql); + + $sender_info = api_get_user_info($user_id); + $notification = new Notification(); + $notification->save_notification(NOTIFICATION_TYPE_INVITATION, array($friend_id), $message_title, $message_content, $sender_info); + + return true; + } else { + //invitation already exist + $sql_if_exist ='SELECT COUNT(*) AS count, id FROM '.$tbl_message.' WHERE user_sender_id='.$user_id.' AND user_receiver_id='.$friend_id.' AND msg_status = 7'; + $res_if_exist = Database::query($sql_if_exist); + $row_if_exist = Database::fetch_array($res_if_exist,'ASSOC'); + if ($row_if_exist['count']==1) { + $sql_if_exist_up='UPDATE '.$tbl_message.'SET msg_status=5, content = "'.$clean_message_content.'" WHERE user_sender_id='.$user_id.' AND user_receiver_id='.$friend_id.' AND msg_status = 7 '; + Database::query($sql_if_exist_up); + return true; + } else { + return false; + } + } + } + /** + * Get number messages of the inbox + * @author isaac flores paz + * @param int user receiver id + * @return int + */ + public static function get_message_number_invitation_by_user_id ($user_receiver_id) { + $tbl_message=Database::get_main_table(TABLE_MAIN_MESSAGE); + $sql='SELECT COUNT(*) as count_message_in_box FROM '.$tbl_message.' WHERE user_receiver_id='.intval($user_receiver_id).' AND msg_status='.MESSAGE_STATUS_INVITATION_PENDING; + $res=Database::query($sql); + $row=Database::fetch_array($res,'ASSOC'); + return $row['count_message_in_box']; + } + + /** + * Get invitation list received by user + * @author isaac flores paz + * @param int user id + * @return array() + */ + public static function get_list_invitation_of_friends_by_user_id ($user_id) { + $list_friend_invitation=array(); + $tbl_message = Database::get_main_table(TABLE_MAIN_MESSAGE); + $sql = 'SELECT user_sender_id,send_date,title,content FROM '.$tbl_message.' WHERE user_receiver_id='.intval($user_id).' AND msg_status = '.MESSAGE_STATUS_INVITATION_PENDING; + $res = Database::query($sql); + while ($row = Database::fetch_array($res,'ASSOC')) { + $list_friend_invitation[]=$row; + } + return $list_friend_invitation; + } + + /** + * Get invitation list sent by user + * @author Julio Montoya + * @param int user id + * @return array() + */ + + public static function get_list_invitation_sent_by_user_id ($user_id) { + $list_friend_invitation=array(); + $tbl_message=Database::get_main_table(TABLE_MAIN_MESSAGE); + $sql='SELECT user_receiver_id, send_date,title,content FROM '.$tbl_message.' WHERE user_sender_id = '.intval($user_id).' AND msg_status = '.MESSAGE_STATUS_INVITATION_PENDING; + $res=Database::query($sql); + while ($row=Database::fetch_array($res,'ASSOC')) { + $list_friend_invitation[$row['user_receiver_id']]=$row; + } + return $list_friend_invitation; + } + + /** + * Accepts invitation + * @param int user sender id + * @param int user receiver id + * @author isaac flores paz + * @author Julio Montoya Cleaning code + */ + public static function invitation_accepted ($user_send_id,$user_receiver_id) { + $tbl_message=Database::get_main_table(TABLE_MAIN_MESSAGE); + $sql='UPDATE '.$tbl_message.' SET msg_status='.MESSAGE_STATUS_INVITATION_ACCEPTED.' WHERE user_sender_id='.((int)$user_send_id).' AND user_receiver_id='.((int)$user_receiver_id).';'; + Database::query($sql); + } + /** + * Denies invitation + * @param int user sender id + * @param int user receiver id + * @author isaac flores paz + * @author Julio Montoya Cleaning code + */ + public static function invitation_denied ($user_send_id,$user_receiver_id) { + $tbl_message=Database::get_main_table(TABLE_MAIN_MESSAGE); + //$msg_status=7; + //$sql='UPDATE '.$tbl_message.' SET msg_status='.$msg_status.' WHERE user_sender_id='.((int)$user_send_id).' AND user_receiver_id='.((int)$user_receiver_id).';'; + $sql='DELETE FROM '.$tbl_message.' WHERE user_sender_id='.((int)$user_send_id).' AND user_receiver_id='.((int)$user_receiver_id).';'; + Database::query($sql); + } + /** + * allow attach to group + * @author isaac flores paz + * @param int user to qualify + * @param int kind of rating + * @return void() + */ + public static function qualify_friend ($id_friend_qualify,$type_qualify) { + $tbl_user_friend=Database::get_main_table(TABLE_MAIN_USER_REL_USER); + $user_id=api_get_user_id(); + $sql='UPDATE '.$tbl_user_friend.' SET relation_type='.((int)$type_qualify).' WHERE user_id='.((int)$user_id).' AND friend_user_id='.((int)$id_friend_qualify).';'; + Database::query($sql); + } + /** + * Sends invitations to friends + * @author Isaac Flores Paz + * @author Julio Montoya Cleaning code + * @param void + * @return string message invitation + */ + public static function send_invitation_friend_user($userfriend_id, $subject_message = '', $content_message = '') { + global $charset; + + $user_info = array(); + $user_info = api_get_user_info($userfriend_id); + $succes = get_lang('MessageSentTo'); + $succes.= ' : '.api_get_person_name($user_info['firstName'], $user_info['lastName']); + + if (isset($subject_message) && isset($content_message) && isset($userfriend_id)) { + $send_message = MessageManager::send_message($userfriend_id, $subject_message, $content_message); - if ($send_message) { - echo Display::display_confirmation_message($succes,true); - } else { - echo Display::display_error_message(get_lang('ErrorSendingMessage'),true); - } - return false; - } elseif (isset($userfriend_id) && !isset($subject_message)) { - $count_is_true = false; - if (isset($userfriend_id) && $userfriend_id>0) { - $message_title = get_lang('Invitation'); - $count_is_true = self::send_invitation_friend(api_get_user_id(), $userfriend_id, $message_title, $content_message); - - if ($count_is_true) { - echo Display::display_confirmation_message(api_htmlentities(get_lang('InvitationHasBeenSent'), ENT_QUOTES,$charset),false); - } else { - echo Display::display_warning_message(api_htmlentities(get_lang('YouAlreadySentAnInvitation'), ENT_QUOTES,$charset),false); - } - } - } - } - - /** - * Get user's feeds - * @param int User ID - * @param int Limit of posts per feed - * @return string HTML section with all feeds included - * @author Yannick Warnier - * @since Dokeos 1.8.6.1 - */ - public static function get_user_feeds($user, $limit=5) { - if (!function_exists('fetch_rss')) { return '';} - $feeds = array(); - $feed = UserManager::get_extra_user_data_by_field($user,'rssfeeds'); - if(empty($feed)) { return ''; } - $feeds = explode(';',$feed['rssfeeds']); - if (count($feeds)==0) { return ''; } - $res = ''; - foreach ($feeds as $url) { - if (empty($url)) { continue; } - $rss = @fetch_rss($url); - $i = 1; - if (!empty($rss->items)) { - $icon_rss = ''; - if (!empty($feed)) { + if ($send_message) { + echo Display::display_confirmation_message($succes,true); + } else { + echo Display::display_error_message(get_lang('ErrorSendingMessage'),true); + } + return false; + } elseif (isset($userfriend_id) && !isset($subject_message)) { + $count_is_true = false; + if (isset($userfriend_id) && $userfriend_id>0) { + $message_title = get_lang('Invitation'); + $count_is_true = self::send_invitation_friend(api_get_user_id(), $userfriend_id, $message_title, $content_message); + + if ($count_is_true) { + echo Display::display_confirmation_message(api_htmlentities(get_lang('InvitationHasBeenSent'), ENT_QUOTES,$charset),false); + } else { + echo Display::display_warning_message(api_htmlentities(get_lang('YouAlreadySentAnInvitation'), ENT_QUOTES,$charset),false); + } + } + } + } + + /** + * Get user's feeds + * @param int User ID + * @param int Limit of posts per feed + * @return string HTML section with all feeds included + * @author Yannick Warnier + * @since Dokeos 1.8.6.1 + */ + public static function get_user_feeds($user, $limit=5) { + if (!function_exists('fetch_rss')) { return '';} + $feeds = array(); + $feed = UserManager::get_extra_user_data_by_field($user,'rssfeeds'); + if(empty($feed)) { return ''; } + $feeds = explode(';',$feed['rssfeeds']); + if (count($feeds)==0) { return ''; } + $res = ''; + foreach ($feeds as $url) { + if (empty($url)) { continue; } + $rss = @fetch_rss($url); + $i = 1; + if (!empty($rss->items)) { + $icon_rss = ''; + if (!empty($feed)) { $icon_rss = Display::url(Display::return_icon('rss.png', '', array(), 32), Security::remove_XSS($feed['rssfeeds']), array('target'=>'_blank')); } - $res .= '

'.$rss->channel['title'].''.$icon_rss.'

'; - $res .= ''; - } - } - return $res; - } - - /** - * Helper functions definition - */ - public static function get_logged_user_course_html($my_course, $count) { - global $nosession, $nbDigestEntries, $orderKey, $digest, $thisCourseSysCode; - if (!$nosession) { - global $now, $date_start, $date_end; - } - //initialise - $result = ''; - // Table definitions - $main_user_table = Database :: get_main_table(TABLE_MAIN_USER); - $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION); - - $course_code = $my_course['code']; - $course_visual_code = $my_course['course_info']['official_code']; - $course_title = $my_course['course_info']['title']; - - $course_info = Database :: get_course_info($course_code); - - $course_id = $course_info['real_id']; - - $course_access_settings = CourseManager :: get_access_settings($course_code); - - $course_visibility = $course_access_settings['visibility']; - - $user_in_course_status = CourseManager :: get_user_in_course_status(api_get_user_id(), $course_code); - //function logic - act on the data - $is_virtual_course = CourseManager :: is_virtual_course_from_system_code($course_code); - if ($is_virtual_course) { - // If the current user is also subscribed in the real course to which this - // virtual course is linked, we don't need to display the virtual course entry in - // the course list - it is combined with the real course entry. - $target_course_code = CourseManager :: get_target_of_linked_course($course_code); - $is_subscribed_in_target_course = CourseManager :: is_user_subscribed_in_course(api_get_user_id(), $target_course_code); - if ($is_subscribed_in_target_course) { - return; //do not display this course entry - } - } + $res .= '

'.$rss->channel['title'].''.$icon_rss.'

'; + $res .= ''; + } + } + return $res; + } + + /** + * Helper functions definition + */ + public static function get_logged_user_course_html($my_course, $count) { + global $nosession, $nbDigestEntries, $orderKey, $digest, $thisCourseSysCode; + if (!$nosession) { + global $now, $date_start, $date_end; + } + //initialise + $result = ''; + // Table definitions + $main_user_table = Database :: get_main_table(TABLE_MAIN_USER); + $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION); + + $course_code = $my_course['code']; + $course_visual_code = $my_course['course_info']['official_code']; + $course_title = $my_course['course_info']['title']; + + $course_info = Database :: get_course_info($course_code); + + $course_id = $course_info['real_id']; + + $course_access_settings = CourseManager :: get_access_settings($course_code); + + $course_visibility = $course_access_settings['visibility']; + + $user_in_course_status = CourseManager :: get_user_in_course_status(api_get_user_id(), $course_code); + //function logic - act on the data + $is_virtual_course = CourseManager :: is_virtual_course_from_system_code($course_code); + if ($is_virtual_course) { + // If the current user is also subscribed in the real course to which this + // virtual course is linked, we don't need to display the virtual course entry in + // the course list - it is combined with the real course entry. + $target_course_code = CourseManager :: get_target_of_linked_course($course_code); + $is_subscribed_in_target_course = CourseManager :: is_user_subscribed_in_course(api_get_user_id(), $target_course_code); + if ($is_subscribed_in_target_course) { + return; //do not display this course entry + } + } - $s_htlm_status_icon = Display::return_icon('course.gif', get_lang('Course')); - - //display course entry - $result .= '
'; - $result .= '

'; - $result .= $s_htlm_status_icon; - - //show a hyperlink to the course, unless the course is closed and user is not course admin - if ($course_visibility != COURSE_VISIBILITY_CLOSED || $user_in_course_status == COURSEMANAGER) { - $result .= ' '.$course_title.''; - } else { - $result .= $course_title." "." ".get_lang('CourseClosed').""; - } - $result .= '

'; - //$current_course_settings = CourseManager :: get_access_settings($my_course['k']); - // display the what's new icons - if ($nbDigestEntries > 0) { - reset($digest); - $result .= '
    '; - while (list ($key2) = each($digest[$thisCourseSysCode])) { - $result .= '
  • '; - if ($orderKey[1] == 'keyTools') { - $result .= ""; - $result .= "$toolsList[$key2][\"name\"]"; - } else { - $result .= api_convert_and_format_date($key2, DATE_FORMAT_LONG, date_default_timezone_get()); - } - $result .= '
  • '; - $result .= '
      '; - reset($digest[$thisCourseSysCode][$key2]); - while (list ($key3, $dataFromCourse) = each($digest[$thisCourseSysCode][$key2])) { - $result .= '
    • '; - if ($orderKey[2] == 'keyTools') { - $result .= ""; - $result .= "$toolsList[$key3][\"name\"]"; - } else { - $result .= api_convert_and_format_date($key3, DATE_FORMAT_LONG, date_default_timezone_get()); - } - $result .= '
        '; - reset($digest[$thisCourseSysCode][$key2][$key3]); - while (list ($key4, $dataFromCourse) = each($digest[$thisCourseSysCode][$key2][$key3])) { - $result .= '
      • '; - $result .= htmlspecialchars(substr(strip_tags($dataFromCourse), 0, CONFVAL_NB_CHAR_FROM_CONTENT)); - $result .= '
      • '; - } - $result .= '
      '; - $result .= '
    • '; - } - $result .= '
    '; - $result .= ''; - } - $result .= '
'; - } - $result .= ''; - $result .= '
'; - - if (!$nosession) { - $session = ''; - $active = false; - if (!empty($my_course['session_name'])) { - - // Request for the name of the general coach - $sql = 'SELECT lastname, firstname - FROM '.$tbl_session.' ts LEFT JOIN '.$main_user_table .' tu - ON ts.id_coach = tu.user_id - WHERE ts.id='.(int) $my_course['id_session']. ' LIMIT 1'; - $rs = Database::query($sql); - $sessioncoach = Database::store_result($rs); - $sessioncoach = $sessioncoach[0]; - - $session = array(); - $session['title'] = $my_course['session_name']; - if ($my_course['date_start']=='0000-00-00') { - $session['dates'] = get_lang('WithoutTimeLimits'); - if ( api_get_setting('show_session_coach') === 'true' ) { - $session['coach'] = get_lang('GeneralCoach').': '.api_get_person_name($sessioncoach['firstname'], $sessioncoach['lastname']); - } - $active = true; - } else { - $session ['dates'] = ' - '.get_lang('From').' '.$my_course['date_start'].' '.get_lang('To').' '.$my_course['date_end']; - if ( api_get_setting('show_session_coach') === 'true' ) { - $session['coach'] = get_lang('GeneralCoach').': '.api_get_person_name($sessioncoach['firstname'], $sessioncoach['lastname']); - } - $active = ($date_start <= $now && $date_end >= $now)?true:false; - } - } - $my_course['id_session'] = isset($my_course['id_session']) ? $my_course['id_session'] : 0; - $output = array ($my_course['user_course_cat'], $result, $my_course['id_session'], $session, 'active'=>$active); - } else { - $output = array ($my_course['user_course_cat'], $result); - } - //$my_course['creation_date']; - return $output; - } - - /** - * Shows the right menu of the Social Network tool - * - * @param string highlight link possible values: group_add, home, messages, messages_inbox, messages_compose ,messages_outbox ,invitations, shared_profile, friends, groups search - * @param int group id - * @param int user id - * @param bool show profile or not (show or hide the user image/information) - * - */ - public static function show_social_menu($show = '', $group_id = 0, $user_id = 0, $show_full_profile = false, $show_delete_account_button = false) { + $s_htlm_status_icon = Display::return_icon('course.gif', get_lang('Course')); + + //display course entry + $result .= '
'; + $result .= '

'; + $result .= $s_htlm_status_icon; + + //show a hyperlink to the course, unless the course is closed and user is not course admin + if ($course_visibility != COURSE_VISIBILITY_CLOSED || $user_in_course_status == COURSEMANAGER) { + $result .= ' '.$course_title.''; + } else { + $result .= $course_title." "." ".get_lang('CourseClosed').""; + } + $result .= '

'; + //$current_course_settings = CourseManager :: get_access_settings($my_course['k']); + // display the what's new icons + if ($nbDigestEntries > 0) { + reset($digest); + $result .= '
    '; + while (list ($key2) = each($digest[$thisCourseSysCode])) { + $result .= '
  • '; + if ($orderKey[1] == 'keyTools') { + $result .= ""; + $result .= "$toolsList[$key2][\"name\"]"; + } else { + $result .= api_convert_and_format_date($key2, DATE_FORMAT_LONG, date_default_timezone_get()); + } + $result .= '
  • '; + $result .= '
      '; + reset($digest[$thisCourseSysCode][$key2]); + while (list ($key3, $dataFromCourse) = each($digest[$thisCourseSysCode][$key2])) { + $result .= '
    • '; + if ($orderKey[2] == 'keyTools') { + $result .= ""; + $result .= "$toolsList[$key3][\"name\"]"; + } else { + $result .= api_convert_and_format_date($key3, DATE_FORMAT_LONG, date_default_timezone_get()); + } + $result .= '
        '; + reset($digest[$thisCourseSysCode][$key2][$key3]); + while (list ($key4, $dataFromCourse) = each($digest[$thisCourseSysCode][$key2][$key3])) { + $result .= '
      • '; + $result .= htmlspecialchars(substr(strip_tags($dataFromCourse), 0, CONFVAL_NB_CHAR_FROM_CONTENT)); + $result .= '
      • '; + } + $result .= '
      '; + $result .= '
    • '; + } + $result .= '
    '; + $result .= ''; + } + $result .= '
'; + } + $result .= ''; + $result .= '
'; + + if (!$nosession) { + $session = ''; + $active = false; + if (!empty($my_course['session_name'])) { + + // Request for the name of the general coach + $sql = 'SELECT lastname, firstname + FROM '.$tbl_session.' ts LEFT JOIN '.$main_user_table .' tu + ON ts.id_coach = tu.user_id + WHERE ts.id='.(int) $my_course['id_session']. ' LIMIT 1'; + $rs = Database::query($sql); + $sessioncoach = Database::store_result($rs); + $sessioncoach = $sessioncoach[0]; + + $session = array(); + $session['title'] = $my_course['session_name']; + if ($my_course['date_start']=='0000-00-00') { + $session['dates'] = get_lang('WithoutTimeLimits'); + if ( api_get_setting('show_session_coach') === 'true' ) { + $session['coach'] = get_lang('GeneralCoach').': '.api_get_person_name($sessioncoach['firstname'], $sessioncoach['lastname']); + } + $active = true; + } else { + $session ['dates'] = ' - '.get_lang('From').' '.$my_course['date_start'].' '.get_lang('To').' '.$my_course['date_end']; + if ( api_get_setting('show_session_coach') === 'true' ) { + $session['coach'] = get_lang('GeneralCoach').': '.api_get_person_name($sessioncoach['firstname'], $sessioncoach['lastname']); + } + $active = ($date_start <= $now && $date_end >= $now)?true:false; + } + } + $my_course['id_session'] = isset($my_course['id_session']) ? $my_course['id_session'] : 0; + $output = array ($my_course['user_course_cat'], $result, $my_course['id_session'], $session, 'active'=>$active); + } else { + $output = array ($my_course['user_course_cat'], $result); + } + //$my_course['creation_date']; + return $output; + } + + /** + * Shows the right menu of the Social Network tool + * + * @param string highlight link possible values: group_add, home, messages, messages_inbox, messages_compose ,messages_outbox ,invitations, shared_profile, friends, groups search + * @param int group id + * @param int user id + * @param bool show profile or not (show or hide the user image/information) + * + */ + public static function show_social_menu($show = '', $group_id = 0, $user_id = 0, $show_full_profile = false, $show_delete_account_button = false) { if (empty($user_id)) { - $user_id = api_get_user_id(); - } + $user_id = api_get_user_id(); + } $user_info = api_get_user_info($user_id, true); - $show_groups = array('groups', 'group_messages', 'messages_list', 'group_add', 'mygroups', 'group_edit', 'member_list', 'invite_friends', 'waiting_list', 'browse_groups'); - //$show_messages = array('messages', 'messages_inbox', 'messages_outbox', 'messages_compose'); - - // get count unread message and total invitations - $count_unread_message = MessageManager::get_number_of_messages(true); - $count_unread_message = (!empty($count_unread_message)? Display::badge($count_unread_message) :''); - - $number_of_new_messages_of_friend = SocialManager::get_message_number_invitation_by_user_id(api_get_user_id()); - $group_pending_invitations = GroupPortalManager::get_groups_by_user(api_get_user_id(), GROUP_USER_PERMISSION_PENDING_INVITATION,false); - $group_pending_invitations = count($group_pending_invitations); - $total_invitations = $number_of_new_messages_of_friend + $group_pending_invitations; - $total_invitations = (!empty($total_invitations) ? Display::badge($total_invitations) :''); - - $html = '