message.lib.php 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\Message;
  4. use Chamilo\UserBundle\Entity\User;
  5. use ChamiloSession as Session;
  6. /**
  7. * Class MessageManager.
  8. *
  9. * This class provides methods for messages management.
  10. * Include/require it in your code to use its features.
  11. *
  12. * @package chamilo.library
  13. */
  14. class MessageManager
  15. {
  16. /**
  17. * Get count new messages for the current user from the database.
  18. *
  19. * @return int
  20. */
  21. public static function getCountNewMessages()
  22. {
  23. $userId = api_get_user_id();
  24. if (empty($userId)) {
  25. return false;
  26. }
  27. static $count;
  28. if (!isset($count)) {
  29. $cacheAvailable = api_get_configuration_value('apc');
  30. if ($cacheAvailable === true) {
  31. $var = api_get_configuration_value('apc_prefix').'social_messages_unread_u_'.$userId;
  32. if (apcu_exists($var)) {
  33. $count = apcu_fetch($var);
  34. } else {
  35. $count = self::getCountNewMessagesFromDB($userId);
  36. apcu_store($var, $count, 60);
  37. }
  38. } else {
  39. $count = self::getCountNewMessagesFromDB($userId);
  40. }
  41. }
  42. return $count;
  43. }
  44. /**
  45. * Gets the total number of messages, used for the inbox sortable table.
  46. *
  47. * @param bool $unread
  48. *
  49. * @return int
  50. */
  51. public static function getNumberOfMessages($unread = false)
  52. {
  53. $table = Database::get_main_table(TABLE_MESSAGE);
  54. if ($unread) {
  55. $condition_msg_status = ' msg_status = '.MESSAGE_STATUS_UNREAD.' ';
  56. } else {
  57. $condition_msg_status = ' msg_status IN('.MESSAGE_STATUS_NEW.','.MESSAGE_STATUS_UNREAD.') ';
  58. }
  59. $keyword = Session::read('message_search_keyword');
  60. $keywordCondition = '';
  61. if (!empty($keyword)) {
  62. $keyword = Database::escape_string($keyword);
  63. $keywordCondition = " AND (title like '%$keyword%' OR content LIKE '%$keyword%') ";
  64. }
  65. $sql = "SELECT COUNT(id) as number_messages
  66. FROM $table
  67. WHERE $condition_msg_status AND
  68. user_receiver_id=".api_get_user_id()."
  69. $keywordCondition
  70. ";
  71. $result = Database::query($sql);
  72. $result = Database::fetch_array($result);
  73. if ($result) {
  74. return (int) $result['number_messages'];
  75. }
  76. return 0;
  77. }
  78. /**
  79. * Gets information about some messages, used for the inbox sortable table.
  80. *
  81. * @param int $from
  82. * @param int $number_of_items
  83. * @param string $column
  84. * @param string $direction
  85. * @param int $userId
  86. *
  87. * @return array
  88. */
  89. public static function get_message_data(
  90. $from,
  91. $number_of_items,
  92. $column,
  93. $direction,
  94. $userId = 0
  95. ) {
  96. $from = (int) $from;
  97. $number_of_items = (int) $number_of_items;
  98. $userId = empty($userId) ? api_get_user_id() : (int) $userId;
  99. // Forcing this order.
  100. if (!isset($direction)) {
  101. $column = 2;
  102. $direction = 'DESC';
  103. } else {
  104. $column = (int) $column;
  105. if (!in_array($direction, ['ASC', 'DESC'])) {
  106. $direction = 'ASC';
  107. }
  108. }
  109. if (!in_array($column, [0, 1, 2])) {
  110. $column = 2;
  111. }
  112. $keyword = Session::read('message_search_keyword');
  113. $keywordCondition = '';
  114. if (!empty($keyword)) {
  115. $keyword = Database::escape_string($keyword);
  116. $keywordCondition = " AND (title like '%$keyword%' OR content LIKE '%$keyword%') ";
  117. }
  118. $table = Database::get_main_table(TABLE_MESSAGE);
  119. $sql = "SELECT
  120. id as col0,
  121. title as col1,
  122. send_date as col2,
  123. msg_status as col3,
  124. user_sender_id
  125. FROM $table
  126. WHERE
  127. user_receiver_id=".$userId." AND
  128. msg_status IN (".MESSAGE_STATUS_NEW.", ".MESSAGE_STATUS_UNREAD.")
  129. $keywordCondition
  130. ORDER BY col$column $direction
  131. LIMIT $from, $number_of_items";
  132. $result = Database::query($sql);
  133. $messageList = [];
  134. $newMessageLink = api_get_path(WEB_CODE_PATH).'messages/new_message.php';
  135. while ($row = Database::fetch_array($result, 'ASSOC')) {
  136. $messageId = $row['col0'];
  137. $title = $row['col1'];
  138. $sendDate = $row['col2'];
  139. $status = $row['col3'];
  140. $senderId = $row['user_sender_id'];
  141. $title = Security::remove_XSS($title, STUDENT, true);
  142. $title = cut($title, 80, true);
  143. $class = 'class = "read"';
  144. if ($status == 1) {
  145. $class = 'class = "unread"';
  146. }
  147. $userInfo = api_get_user_info($senderId);
  148. if (!empty($senderId) && !empty($userInfo)) {
  149. $message[1] = '<a '.$class.' href="view_message.php?id='.$messageId.'">'.$title.'</a><br />';
  150. $message[1] .= $userInfo['complete_name_with_username'];
  151. $message[3] =
  152. Display::url(
  153. Display::returnFontAwesomeIcon('reply', 2),
  154. $newMessageLink.'?re_id='.$messageId,
  155. ['title' => get_lang('ReplyToMessage')]
  156. );
  157. } else {
  158. $message[1] = '<a '.$class.' href="view_message.php?id='.$messageId.'">'.$title.'</a><br />';
  159. $message[1] .= get_lang('UnknownUser');
  160. $message[3] =
  161. Display::url(
  162. Display::returnFontAwesomeIcon('reply', 2),
  163. '#',
  164. ['title' => get_lang('ReplyToMessage')]
  165. );
  166. }
  167. $message[0] = $messageId;
  168. $message[2] = api_convert_and_format_date($sendDate, DATE_TIME_FORMAT_LONG);
  169. $message[3] .=
  170. '&nbsp;&nbsp;'.
  171. Display::url(
  172. Display::returnFontAwesomeIcon('share', 2),
  173. $newMessageLink.'?forward_id='.$messageId,
  174. ['title' => get_lang('ForwardMessage')]
  175. ).
  176. '&nbsp;&nbsp;<a title="'.addslashes(
  177. get_lang('DeleteMessage')
  178. ).'" onclick="javascript:if(!confirm('."'".addslashes(
  179. api_htmlentities(get_lang('ConfirmDeleteMessage'))
  180. )."'".')) return false;" href="inbox.php?action=deleteone&id='.$messageId.'">'.
  181. Display::returnFontAwesomeIcon('trash', 2).'</a>';
  182. foreach ($message as $key => $value) {
  183. $message[$key] = api_xml_http_response_encode($value);
  184. }
  185. $messageList[] = $message;
  186. }
  187. return $messageList;
  188. }
  189. /**
  190. * @param array $aboutUserInfo
  191. * @param array $fromUserInfo
  192. * @param string $subject
  193. * @param string $content
  194. *
  195. * @return bool
  196. */
  197. public static function sendMessageAboutUser(
  198. $aboutUserInfo,
  199. $fromUserInfo,
  200. $subject,
  201. $content
  202. ) {
  203. if (empty($aboutUserInfo) || empty($fromUserInfo)) {
  204. return false;
  205. }
  206. if (empty($fromUserInfo['id']) || empty($aboutUserInfo['id'])) {
  207. return false;
  208. }
  209. $table = Database::get_main_table(TABLE_MESSAGE);
  210. $now = api_get_utc_datetime();
  211. $params = [
  212. 'user_sender_id' => $fromUserInfo['id'],
  213. 'user_receiver_id' => $aboutUserInfo['id'],
  214. 'msg_status' => MESSAGE_STATUS_CONVERSATION,
  215. 'send_date' => $now,
  216. 'title' => $subject,
  217. 'content' => $content,
  218. 'group_id' => 0,
  219. 'parent_id' => 0,
  220. 'update_date' => $now,
  221. ];
  222. $id = Database::insert($table, $params);
  223. if ($id) {
  224. return true;
  225. }
  226. return false;
  227. }
  228. /**
  229. * @param array $aboutUserInfo
  230. *
  231. * @return array
  232. */
  233. public static function getMessagesAboutUser($aboutUserInfo)
  234. {
  235. if (!empty($aboutUserInfo)) {
  236. $table = Database::get_main_table(TABLE_MESSAGE);
  237. $sql = 'SELECT id FROM '.$table.'
  238. WHERE
  239. user_receiver_id = '.$aboutUserInfo['id'].' AND
  240. msg_status = '.MESSAGE_STATUS_CONVERSATION.'
  241. ';
  242. $result = Database::query($sql);
  243. $messages = [];
  244. $repo = Database::getManager()->getRepository('ChamiloCoreBundle:Message');
  245. while ($row = Database::fetch_array($result)) {
  246. $message = $repo->find($row['id']);
  247. $messages[] = $message;
  248. }
  249. /*$criteria = [
  250. 'userReceiverId' => $aboutUserInfo['id'],
  251. 'msgStatus' => MESSAGE_STATUS_CONVERSATION,
  252. ];
  253. $repo = Database::getManager()->getRepository('ChamiloCoreBundle:Message');
  254. $messages = $repo->findBy($criteria, ['sendDate' => 'DESC']);*/
  255. return $messages;
  256. }
  257. return [];
  258. }
  259. /**
  260. * @param array $userInfo
  261. *
  262. * @return string
  263. */
  264. public static function getMessagesAboutUserToString($userInfo)
  265. {
  266. $messages = self::getMessagesAboutUser($userInfo);
  267. $html = '';
  268. if (!empty($messages)) {
  269. /** @var Message $message */
  270. foreach ($messages as $message) {
  271. $tag = 'message_'.$message->getId();
  272. $tagAccordion = 'accordion_'.$message->getId();
  273. $tagCollapse = 'collapse_'.$message->getId();
  274. $date = Display::dateToStringAgoAndLongDate(
  275. $message->getSendDate()
  276. );
  277. $localTime = api_get_local_time(
  278. $message->getSendDate(),
  279. null,
  280. null,
  281. false,
  282. false
  283. );
  284. $senderId = $message->getUserSenderId();
  285. $senderInfo = api_get_user_info($senderId);
  286. $html .= Display::panelCollapse(
  287. $localTime.' '.$senderInfo['complete_name'].' '.$message->getTitle(),
  288. $message->getContent().'<br />'.$date.'<br />'.get_lang(
  289. 'Author'
  290. ).': '.$senderInfo['complete_name_with_message_link'],
  291. $tag,
  292. null,
  293. $tagAccordion,
  294. $tagCollapse,
  295. false
  296. );
  297. }
  298. }
  299. return $html;
  300. }
  301. /**
  302. * @param int $senderId
  303. * @param int $receiverId
  304. * @param string $subject
  305. * @param string $message
  306. *
  307. * @return bool
  308. */
  309. public static function messageWasAlreadySent($senderId, $receiverId, $subject, $message)
  310. {
  311. $table = Database::get_main_table(TABLE_MESSAGE);
  312. $senderId = (int) $senderId;
  313. $receiverId = (int) $receiverId;
  314. $subject = Database::escape_string($subject);
  315. $message = Database::escape_string($message);
  316. $sql = "SELECT * FROM $table
  317. WHERE
  318. user_sender_id = $senderId AND
  319. user_receiver_id = $receiverId AND
  320. title = '$subject' AND
  321. content = '$message' AND
  322. (msg_status = ".MESSAGE_STATUS_UNREAD." OR msg_status = ".MESSAGE_STATUS_NEW.")
  323. ";
  324. $result = Database::query($sql);
  325. return Database::num_rows($result) > 0;
  326. }
  327. /**
  328. * Sends a message to a user/group.
  329. *
  330. * @param int $receiver_user_id
  331. * @param string $subject
  332. * @param string $content
  333. * @param array $attachments files array($_FILES) (optional)
  334. * @param array $fileCommentList about attachment files (optional)
  335. * @param int $group_id (optional)
  336. * @param int $parent_id (optional)
  337. * @param int $editMessageId id for updating the message (optional)
  338. * @param int $topic_id (optional) the default value is the current user_id
  339. * @param int $sender_id
  340. * @param bool $directMessage
  341. * @param int $forwardId
  342. * @param array $smsParameters
  343. * @param bool $checkCurrentAudioId
  344. * @param bool $forceTitleWhenSendingEmail force the use of $title as subject instead of "You have a new message"
  345. *
  346. * @return bool
  347. */
  348. public static function send_message(
  349. $receiver_user_id,
  350. $subject,
  351. $content,
  352. array $attachments = [],
  353. array $fileCommentList = [],
  354. $group_id = 0,
  355. $parent_id = 0,
  356. $editMessageId = 0,
  357. $topic_id = 0,
  358. $sender_id = 0,
  359. $directMessage = false,
  360. $forwardId = 0,
  361. $smsParameters = [],
  362. $checkCurrentAudioId = false,
  363. $forceTitleWhenSendingEmail = false
  364. ) {
  365. $table = Database::get_main_table(TABLE_MESSAGE);
  366. $group_id = (int) $group_id;
  367. $receiver_user_id = (int) $receiver_user_id;
  368. $parent_id = (int) $parent_id;
  369. $editMessageId = (int) $editMessageId;
  370. $topic_id = (int) $topic_id;
  371. if (!empty($receiver_user_id)) {
  372. $receiverUserInfo = api_get_user_info($receiver_user_id);
  373. // Disabling messages for inactive users.
  374. if ($receiverUserInfo['active'] == 0) {
  375. return false;
  376. }
  377. }
  378. $user_sender_id = empty($sender_id) ? api_get_user_id() : (int) $sender_id;
  379. if (empty($user_sender_id)) {
  380. Display::addFlash(Display::return_message(get_lang('UserDoesNotExist'), 'warning'));
  381. return false;
  382. }
  383. $totalFileSize = 0;
  384. $attachmentList = [];
  385. if (is_array($attachments)) {
  386. $counter = 0;
  387. foreach ($attachments as $attachment) {
  388. $attachment['comment'] = isset($fileCommentList[$counter]) ? $fileCommentList[$counter] : '';
  389. $fileSize = isset($attachment['size']) ? $attachment['size'] : 0;
  390. if (is_array($fileSize)) {
  391. foreach ($fileSize as $size) {
  392. $totalFileSize += $size;
  393. }
  394. } else {
  395. $totalFileSize += $fileSize;
  396. }
  397. $attachmentList[] = $attachment;
  398. $counter++;
  399. }
  400. }
  401. if ($checkCurrentAudioId) {
  402. // Add the audio file as an attachment
  403. $audioId = Session::read('current_audio_id');
  404. if (!empty($audioId)) {
  405. $file = api_get_uploaded_file('audio_message', api_get_user_id(), $audioId);
  406. if (!empty($file)) {
  407. $audioAttachment = [
  408. 'name' => basename($file),
  409. 'comment' => 'audio_message',
  410. 'size' => filesize($file),
  411. 'tmp_name' => $file,
  412. 'error' => 0,
  413. 'type' => DocumentManager::file_get_mime_type(basename($file)),
  414. ];
  415. // create attachment from audio message
  416. $attachmentList[] = $audioAttachment;
  417. }
  418. }
  419. }
  420. // Validating fields
  421. if (empty($subject) && empty($group_id)) {
  422. Display::addFlash(
  423. Display::return_message(
  424. get_lang('YouShouldWriteASubject'),
  425. 'warning'
  426. )
  427. );
  428. return false;
  429. } elseif ($totalFileSize > intval(api_get_setting('message_max_upload_filesize'))) {
  430. $warning = sprintf(
  431. get_lang('FilesSizeExceedsX'),
  432. format_file_size(api_get_setting('message_max_upload_filesize'))
  433. );
  434. Display::addFlash(Display::return_message($warning, 'warning'));
  435. return false;
  436. }
  437. // Just in case we replace the and \n and \n\r while saving in the DB
  438. // $content = str_replace(array("\n", "\n\r"), '<br />', $content);
  439. $now = api_get_utc_datetime();
  440. if (!empty($receiver_user_id) || !empty($group_id)) {
  441. // message for user friend
  442. //@todo it's possible to edit a message? yes, only for groups
  443. if (!empty($editMessageId)) {
  444. $query = " UPDATE $table SET
  445. update_date = '".$now."',
  446. content = '".Database::escape_string($content)."'
  447. WHERE id = '$editMessageId' ";
  448. Database::query($query);
  449. $messageId = $editMessageId;
  450. } else {
  451. $params = [
  452. 'user_sender_id' => $user_sender_id,
  453. 'user_receiver_id' => $receiver_user_id,
  454. 'msg_status' => MESSAGE_STATUS_UNREAD,
  455. 'send_date' => $now,
  456. 'title' => $subject,
  457. 'content' => $content,
  458. 'group_id' => $group_id,
  459. 'parent_id' => $parent_id,
  460. 'update_date' => $now,
  461. ];
  462. $messageId = Database::insert($table, $params);
  463. }
  464. // Forward also message attachments
  465. if (!empty($forwardId)) {
  466. $attachments = self::getAttachmentList($forwardId);
  467. foreach ($attachments as $attachment) {
  468. if (!empty($attachment['file_source'])) {
  469. $file = [
  470. 'name' => $attachment['filename'],
  471. 'tmp_name' => $attachment['file_source'],
  472. 'size' => $attachment['size'],
  473. 'error' => 0,
  474. 'comment' => $attachment['comment'],
  475. ];
  476. // Inject this array so files can be added when sending and email with the mailer
  477. $attachmentList[] = $file;
  478. }
  479. }
  480. }
  481. // Save attachment file for inbox messages
  482. if (is_array($attachmentList)) {
  483. foreach ($attachmentList as $attachment) {
  484. if ($attachment['error'] == 0) {
  485. $comment = $attachment['comment'];
  486. self::saveMessageAttachmentFile(
  487. $attachment,
  488. $comment,
  489. $messageId,
  490. null,
  491. $receiver_user_id,
  492. $group_id
  493. );
  494. }
  495. }
  496. }
  497. if (empty($group_id)) {
  498. // message in outbox for user friend or group
  499. $params = [
  500. 'user_sender_id' => $user_sender_id,
  501. 'user_receiver_id' => $receiver_user_id,
  502. 'msg_status' => MESSAGE_STATUS_OUTBOX,
  503. 'send_date' => $now,
  504. 'title' => $subject,
  505. 'content' => $content,
  506. 'group_id' => $group_id,
  507. 'parent_id' => $parent_id,
  508. 'update_date' => $now,
  509. ];
  510. $outbox_last_id = Database::insert($table, $params);
  511. // save attachment file for outbox messages
  512. if (is_array($attachmentList)) {
  513. foreach ($attachmentList as $attachment) {
  514. if ($attachment['error'] == 0) {
  515. $comment = $attachment['comment'];
  516. self::saveMessageAttachmentFile(
  517. $attachment,
  518. $comment,
  519. $outbox_last_id,
  520. $user_sender_id
  521. );
  522. }
  523. }
  524. }
  525. }
  526. // Load user settings.
  527. $notification = new Notification();
  528. $sender_info = api_get_user_info($user_sender_id);
  529. // add file attachment additional attributes
  530. $attachmentAddedByMail = [];
  531. foreach ($attachmentList as $attachment) {
  532. $attachmentAddedByMail[] = [
  533. 'path' => $attachment['tmp_name'],
  534. 'filename' => $attachment['name'],
  535. ];
  536. }
  537. if (empty($group_id)) {
  538. $type = Notification::NOTIFICATION_TYPE_MESSAGE;
  539. if ($directMessage) {
  540. $type = Notification::NOTIFICATION_TYPE_DIRECT_MESSAGE;
  541. }
  542. $notification->saveNotification(
  543. $messageId,
  544. $type,
  545. [$receiver_user_id],
  546. $subject,
  547. $content,
  548. $sender_info,
  549. $attachmentAddedByMail,
  550. $smsParameters,
  551. $forceTitleWhenSendingEmail
  552. );
  553. } else {
  554. $usergroup = new UserGroup();
  555. $group_info = $usergroup->get($group_id);
  556. $group_info['topic_id'] = $topic_id;
  557. $group_info['msg_id'] = $messageId;
  558. $user_list = $usergroup->get_users_by_group(
  559. $group_id,
  560. false,
  561. [],
  562. 0,
  563. 1000
  564. );
  565. // Adding more sense to the message group
  566. $subject = sprintf(get_lang('ThereIsANewMessageInTheGroupX'), $group_info['name']);
  567. $new_user_list = [];
  568. foreach ($user_list as $user_data) {
  569. $new_user_list[] = $user_data['id'];
  570. }
  571. $group_info = [
  572. 'group_info' => $group_info,
  573. 'user_info' => $sender_info,
  574. ];
  575. $notification->saveNotification(
  576. $messageId,
  577. Notification::NOTIFICATION_TYPE_GROUP,
  578. $new_user_list,
  579. $subject,
  580. $content,
  581. $group_info,
  582. $attachmentAddedByMail,
  583. $smsParameters
  584. );
  585. }
  586. return $messageId;
  587. }
  588. return false;
  589. }
  590. /**
  591. * @param int $receiver_user_id
  592. * @param int $subject
  593. * @param string $message
  594. * @param int $sender_id
  595. * @param bool $sendCopyToDrhUsers send copy to related DRH users
  596. * @param bool $directMessage
  597. * @param array $smsParameters
  598. * @param bool $uploadFiles Do not upload files using the MessageManager class
  599. * @param array $attachmentList
  600. *
  601. * @return bool
  602. */
  603. public static function send_message_simple(
  604. $receiver_user_id,
  605. $subject,
  606. $message,
  607. $sender_id = 0,
  608. $sendCopyToDrhUsers = false,
  609. $directMessage = false,
  610. $smsParameters = [],
  611. $uploadFiles = true,
  612. $attachmentList = []
  613. ) {
  614. $files = $_FILES ? $_FILES : [];
  615. if ($uploadFiles === false) {
  616. $files = [];
  617. }
  618. // $attachmentList must have: tmp_name, name, size keys
  619. if (!empty($attachmentList)) {
  620. $files = $attachmentList;
  621. }
  622. $result = self::send_message(
  623. $receiver_user_id,
  624. $subject,
  625. $message,
  626. $files,
  627. [],
  628. null,
  629. null,
  630. null,
  631. null,
  632. $sender_id,
  633. $directMessage,
  634. 0,
  635. $smsParameters
  636. );
  637. if ($sendCopyToDrhUsers) {
  638. $userInfo = api_get_user_info($receiver_user_id);
  639. $drhList = UserManager::getDrhListFromUser($receiver_user_id);
  640. if (!empty($drhList)) {
  641. foreach ($drhList as $drhInfo) {
  642. $message = sprintf(
  643. get_lang('CopyOfMessageSentToXUser'),
  644. $userInfo['complete_name']
  645. ).' <br />'.$message;
  646. self::send_message_simple(
  647. $drhInfo['user_id'],
  648. $subject,
  649. $message,
  650. $sender_id,
  651. false,
  652. $directMessage
  653. );
  654. }
  655. }
  656. }
  657. return $result;
  658. }
  659. /**
  660. * Update parent ids for other receiver user from current message in groups.
  661. *
  662. * @author Christian Fasanando Flores
  663. *
  664. * @param int $parent_id
  665. * @param int $receiver_user_id
  666. * @param int $messageId
  667. */
  668. public static function update_parent_ids_from_reply(
  669. $parent_id,
  670. $receiver_user_id,
  671. $messageId
  672. ) {
  673. $table = Database::get_main_table(TABLE_MESSAGE);
  674. $parent_id = intval($parent_id);
  675. $receiver_user_id = intval($receiver_user_id);
  676. $messageId = intval($messageId);
  677. // first get data from message id (parent)
  678. $sql = "SELECT * FROM $table WHERE id = '$parent_id'";
  679. $rs_message = Database::query($sql);
  680. $row_message = Database::fetch_array($rs_message);
  681. // get message id from data found early for other receiver user
  682. $sql = "SELECT id FROM $table
  683. WHERE
  684. user_sender_id ='{$row_message['user_sender_id']}' AND
  685. title='{$row_message['title']}' AND
  686. content='{$row_message['content']}' AND
  687. group_id='{$row_message['group_id']}' AND
  688. user_receiver_id='$receiver_user_id'";
  689. $result = Database::query($sql);
  690. $row = Database::fetch_array($result);
  691. // update parent_id for other user receiver
  692. $sql = "UPDATE $table SET parent_id = ".$row['id']."
  693. WHERE id = $messageId";
  694. Database::query($sql);
  695. }
  696. /**
  697. * @param int $user_receiver_id
  698. * @param int $id
  699. *
  700. * @return bool
  701. */
  702. public static function delete_message_by_user_receiver($user_receiver_id, $id)
  703. {
  704. $table = Database::get_main_table(TABLE_MESSAGE);
  705. if ($id != strval(intval($id))) {
  706. return false;
  707. }
  708. $user_receiver_id = intval($user_receiver_id);
  709. $id = intval($id);
  710. $sql = "SELECT * FROM $table
  711. WHERE id = ".$id." AND msg_status <>".MESSAGE_STATUS_OUTBOX;
  712. $rs = Database::query($sql);
  713. if (Database::num_rows($rs) > 0) {
  714. // delete attachment file
  715. self::delete_message_attachment_file($id, $user_receiver_id);
  716. // delete message
  717. $query = "UPDATE $table
  718. SET msg_status = ".MESSAGE_STATUS_DELETED."
  719. WHERE
  720. user_receiver_id=".$user_receiver_id." AND
  721. id = ".$id;
  722. Database::query($query);
  723. return true;
  724. } else {
  725. return false;
  726. }
  727. }
  728. /**
  729. * Set status deleted.
  730. *
  731. * @author Isaac FLores Paz <isaac.flores@dokeos.com>
  732. *
  733. * @param int
  734. * @param int
  735. *
  736. * @return bool
  737. */
  738. public static function delete_message_by_user_sender($user_sender_id, $id)
  739. {
  740. if ($id != strval(intval($id))) {
  741. return false;
  742. }
  743. $table = Database::get_main_table(TABLE_MESSAGE);
  744. $id = intval($id);
  745. $user_sender_id = intval($user_sender_id);
  746. $sql = "SELECT * FROM $table WHERE id='$id'";
  747. $rs = Database::query($sql);
  748. if (Database::num_rows($rs) > 0) {
  749. // delete attachment file
  750. self::delete_message_attachment_file($id, $user_sender_id);
  751. // delete message
  752. $sql = "UPDATE $table
  753. SET msg_status = ".MESSAGE_STATUS_DELETED."
  754. WHERE user_sender_id='$user_sender_id' AND id='$id'";
  755. Database::query($sql);
  756. return true;
  757. }
  758. return false;
  759. }
  760. /**
  761. * Saves a message attachment files.
  762. *
  763. * @param array $file_attach $_FILES['name']
  764. * @param string a comment about the uploaded file
  765. * @param int message id
  766. * @param int receiver user id (optional)
  767. * @param int sender user id (optional)
  768. * @param int group id (optional)
  769. */
  770. public static function saveMessageAttachmentFile(
  771. $file_attach,
  772. $file_comment,
  773. $message_id,
  774. $receiver_user_id = 0,
  775. $sender_user_id = 0,
  776. $group_id = 0
  777. ) {
  778. $table = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
  779. // Try to add an extension to the file if it hasn't one
  780. $type = isset($file_attach['type']) ? $file_attach['type'] : '';
  781. if (empty($type)) {
  782. $type = DocumentManager::file_get_mime_type($file_attach['name']);
  783. }
  784. $new_file_name = add_ext_on_mime(stripslashes($file_attach['name']), $type);
  785. // user's file name
  786. $file_name = $file_attach['name'];
  787. if (!filter_extension($new_file_name)) {
  788. Display::addFlash(Display::return_message(get_lang('UplUnableToSaveFileFilteredExtension'), 'error'));
  789. } else {
  790. $new_file_name = uniqid('');
  791. if (!empty($receiver_user_id)) {
  792. $message_user_id = $receiver_user_id;
  793. } else {
  794. $message_user_id = $sender_user_id;
  795. }
  796. // User-reserved directory where photos have to be placed.*
  797. $userGroup = new UserGroup();
  798. if (!empty($group_id)) {
  799. $path_user_info = $userGroup->get_group_picture_path_by_id(
  800. $group_id,
  801. 'system',
  802. true
  803. );
  804. } else {
  805. $path_user_info['dir'] = UserManager::getUserPathById($message_user_id, 'system');
  806. }
  807. $path_message_attach = $path_user_info['dir'].'message_attachments/';
  808. // If this directory does not exist - we create it.
  809. if (!file_exists($path_message_attach)) {
  810. @mkdir($path_message_attach, api_get_permissions_for_new_directories(), true);
  811. }
  812. $new_path = $path_message_attach.$new_file_name;
  813. $fileCopied = false;
  814. if (isset($file_attach['tmp_name']) && !empty($file_attach['tmp_name'])) {
  815. if (is_uploaded_file($file_attach['tmp_name'])) {
  816. @copy($file_attach['tmp_name'], $new_path);
  817. $fileCopied = true;
  818. } else {
  819. // 'tmp_name' can be set by the ticket or when forwarding a message
  820. if (file_exists($file_attach['tmp_name'])) {
  821. @copy($file_attach['tmp_name'], $new_path);
  822. $fileCopied = true;
  823. }
  824. }
  825. }
  826. if ($fileCopied) {
  827. // Storing the attachments if any
  828. $params = [
  829. 'filename' => $file_name,
  830. 'comment' => $file_comment,
  831. 'path' => $new_file_name,
  832. 'message_id' => $message_id,
  833. 'size' => $file_attach['size'],
  834. ];
  835. return Database::insert($table, $params);
  836. }
  837. }
  838. return false;
  839. }
  840. /**
  841. * Delete message attachment files (logically updating the row with a suffix _DELETE_id).
  842. *
  843. * @param int message id
  844. * @param int message user id (receiver user id or sender user id)
  845. * @param int group id (optional)
  846. */
  847. public static function delete_message_attachment_file(
  848. $message_id,
  849. $message_uid,
  850. $group_id = 0
  851. ) {
  852. $message_id = (int) $message_id;
  853. $message_uid = (int) $message_uid;
  854. $table_message_attach = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
  855. $sql = "SELECT * FROM $table_message_attach
  856. WHERE message_id = '$message_id'";
  857. $rs = Database::query($sql);
  858. while ($row = Database::fetch_array($rs)) {
  859. $path = $row['path'];
  860. $attach_id = $row['id'];
  861. $new_path = $path.'_DELETED_'.$attach_id;
  862. if (!empty($group_id)) {
  863. $userGroup = new UserGroup();
  864. $path_user_info = $userGroup->get_group_picture_path_by_id(
  865. $group_id,
  866. 'system',
  867. true
  868. );
  869. } else {
  870. $path_user_info['dir'] = UserManager::getUserPathById(
  871. $message_uid,
  872. 'system'
  873. );
  874. }
  875. $path_message_attach = $path_user_info['dir'].'message_attachments/';
  876. if (is_file($path_message_attach.$path)) {
  877. if (rename($path_message_attach.$path, $path_message_attach.$new_path)) {
  878. $sql = "UPDATE $table_message_attach set path='$new_path'
  879. WHERE id ='$attach_id'";
  880. Database::query($sql);
  881. }
  882. }
  883. }
  884. }
  885. /**
  886. * @param int $user_id
  887. * @param int $message_id
  888. * @param int $type
  889. *
  890. * @return bool
  891. */
  892. public static function update_message_status($user_id, $message_id, $type)
  893. {
  894. $user_id = (int) $user_id;
  895. $message_id = (int) $message_id;
  896. $type = (int) $type;
  897. if (empty($user_id) || empty($message_id)) {
  898. return false;
  899. }
  900. $table_message = Database::get_main_table(TABLE_MESSAGE);
  901. $sql = "UPDATE $table_message SET
  902. msg_status = '$type'
  903. WHERE
  904. user_receiver_id = ".$user_id." AND
  905. id = '".$message_id."'";
  906. Database::query($sql);
  907. }
  908. /**
  909. * get messages by group id.
  910. *
  911. * @param int $group_id group id
  912. *
  913. * @return array
  914. */
  915. public static function get_messages_by_group($group_id)
  916. {
  917. if ($group_id != strval(intval($group_id))) {
  918. return false;
  919. }
  920. $table = Database::get_main_table(TABLE_MESSAGE);
  921. $group_id = intval($group_id);
  922. $sql = "SELECT * FROM $table
  923. WHERE
  924. group_id= $group_id AND
  925. msg_status NOT IN ('".MESSAGE_STATUS_OUTBOX."', '".MESSAGE_STATUS_DELETED."')
  926. ORDER BY id";
  927. $rs = Database::query($sql);
  928. $data = [];
  929. if (Database::num_rows($rs) > 0) {
  930. while ($row = Database::fetch_array($rs, 'ASSOC')) {
  931. $data[] = $row;
  932. }
  933. }
  934. return $data;
  935. }
  936. /**
  937. * get messages by group id.
  938. *
  939. * @param int $group_id
  940. * @param int $message_id
  941. *
  942. * @return array
  943. */
  944. public static function get_messages_by_group_by_message($group_id, $message_id)
  945. {
  946. if ($group_id != strval(intval($group_id))) {
  947. return false;
  948. }
  949. $table = Database::get_main_table(TABLE_MESSAGE);
  950. $group_id = intval($group_id);
  951. $sql = "SELECT * FROM $table
  952. WHERE
  953. group_id = $group_id AND
  954. msg_status NOT IN ('".MESSAGE_STATUS_OUTBOX."', '".MESSAGE_STATUS_DELETED."')
  955. ORDER BY id ";
  956. $rs = Database::query($sql);
  957. $data = [];
  958. $parents = [];
  959. if (Database::num_rows($rs) > 0) {
  960. while ($row = Database::fetch_array($rs, 'ASSOC')) {
  961. if ($message_id == $row['parent_id'] || in_array($row['parent_id'], $parents)) {
  962. $parents[] = $row['id'];
  963. $data[] = $row;
  964. }
  965. }
  966. }
  967. return $data;
  968. }
  969. /**
  970. * Get messages by parent id optionally with limit.
  971. *
  972. * @param int parent id
  973. * @param int group id (optional)
  974. * @param int offset (optional)
  975. * @param int limit (optional)
  976. *
  977. * @return array
  978. */
  979. public static function getMessagesByParent($parentId, $groupId = 0, $offset = 0, $limit = 0)
  980. {
  981. $table = Database::get_main_table(TABLE_MESSAGE);
  982. $parentId = (int) $parentId;
  983. if (empty($parentId)) {
  984. return [];
  985. }
  986. $condition_group_id = '';
  987. if (!empty($groupId)) {
  988. $groupId = (int) $groupId;
  989. $condition_group_id = " AND group_id = '$groupId' ";
  990. }
  991. $condition_limit = '';
  992. if ($offset && $limit) {
  993. $offset = (int) $offset;
  994. $limit = (int) $limit;
  995. $offset = ($offset - 1) * $limit;
  996. $condition_limit = " LIMIT $offset,$limit ";
  997. }
  998. $sql = "SELECT * FROM $table
  999. WHERE
  1000. parent_id='$parentId' AND
  1001. msg_status NOT IN (".MESSAGE_STATUS_OUTBOX.", ".MESSAGE_STATUS_WALL_DELETE.")
  1002. $condition_group_id
  1003. ORDER BY send_date DESC $condition_limit ";
  1004. $rs = Database::query($sql);
  1005. $data = [];
  1006. if (Database::num_rows($rs) > 0) {
  1007. while ($row = Database::fetch_array($rs)) {
  1008. $data[$row['id']] = $row;
  1009. }
  1010. }
  1011. return $data;
  1012. }
  1013. /**
  1014. * Gets information about messages sent.
  1015. *
  1016. * @param int
  1017. * @param int
  1018. * @param string
  1019. * @param string
  1020. *
  1021. * @return array
  1022. */
  1023. public static function get_message_data_sent(
  1024. $from,
  1025. $number_of_items,
  1026. $column,
  1027. $direction
  1028. ) {
  1029. $from = (int) $from;
  1030. $number_of_items = (int) $number_of_items;
  1031. if (!isset($direction)) {
  1032. $column = 2;
  1033. $direction = 'DESC';
  1034. } else {
  1035. $column = (int) $column;
  1036. if (!in_array($direction, ['ASC', 'DESC'])) {
  1037. $direction = 'ASC';
  1038. }
  1039. }
  1040. if (!in_array($column, [0, 1, 2])) {
  1041. $column = 2;
  1042. }
  1043. $table = Database::get_main_table(TABLE_MESSAGE);
  1044. $request = api_is_xml_http_request();
  1045. $keyword = Session::read('message_sent_search_keyword');
  1046. $keywordCondition = '';
  1047. if (!empty($keyword)) {
  1048. $keyword = Database::escape_string($keyword);
  1049. $keywordCondition = " AND (title like '%$keyword%' OR content LIKE '%$keyword%') ";
  1050. }
  1051. $sql = "SELECT
  1052. id as col0,
  1053. title as col1,
  1054. send_date as col2,
  1055. user_receiver_id,
  1056. msg_status,
  1057. user_sender_id
  1058. FROM $table
  1059. WHERE
  1060. user_sender_id = ".api_get_user_id()." AND
  1061. msg_status = ".MESSAGE_STATUS_OUTBOX."
  1062. $keywordCondition
  1063. ORDER BY col$column $direction
  1064. LIMIT $from, $number_of_items";
  1065. $result = Database::query($sql);
  1066. $message_list = [];
  1067. while ($row = Database::fetch_array($result, 'ASSOC')) {
  1068. $messageId = $row['col0'];
  1069. $title = $row['col1'];
  1070. $sendDate = $row['col2'];
  1071. $senderId = $row['user_sender_id'];
  1072. if ($request === true) {
  1073. $message[0] = '<input type="checkbox" value='.$messageId.' name="out[]">';
  1074. } else {
  1075. $message[0] = $messageId;
  1076. }
  1077. $class = 'class = "read"';
  1078. $title = Security::remove_XSS($title);
  1079. $userInfo = api_get_user_info($senderId);
  1080. if ($request === true) {
  1081. $message[1] = '<a onclick="show_sent_message('.$messageId.')" href="javascript:void(0)">'.
  1082. $userInfo['complete_name_with_username'].'</a>';
  1083. $message[2] = '<a onclick="show_sent_message('.$messageId.')" href="javascript:void(0)">'.str_replace(
  1084. "\\",
  1085. "",
  1086. $title
  1087. ).'</a>';
  1088. //date stays the same
  1089. $message[3] = api_convert_and_format_date($sendDate, DATE_TIME_FORMAT_LONG);
  1090. $message[4] = '&nbsp;&nbsp;<a title="'.addslashes(
  1091. get_lang('DeleteMessage')
  1092. ).'" onclick="delete_one_message_outbox('.$messageId.')" href="javascript:void(0)" >'.
  1093. Display::returnFontAwesomeIcon('trash', 2).'</a>';
  1094. } else {
  1095. $message[1] = '<a '.$class.' onclick="show_sent_message('.$messageId.')" href="../messages/view_message.php?id_send='.$messageId.'">'.$title.'</a><br />'.$userInfo['complete_name_with_username'];
  1096. $message[2] = api_convert_and_format_date($sendDate, DATE_TIME_FORMAT_LONG);
  1097. $message[3] = '<a title="'.addslashes(
  1098. get_lang('DeleteMessage')
  1099. ).'" href="outbox.php?action=deleteone&id='.$messageId.'" onclick="javascript:if(!confirm('."'".addslashes(
  1100. api_htmlentities(get_lang('ConfirmDeleteMessage'))
  1101. )."'".')) return false;" >'.
  1102. Display::returnFontAwesomeIcon('trash', 2).'</a>';
  1103. }
  1104. $message_list[] = $message;
  1105. }
  1106. return $message_list;
  1107. }
  1108. /**
  1109. * Gets information about number messages sent.
  1110. *
  1111. * @author Isaac FLores Paz <isaac.flores@dokeos.com>
  1112. *
  1113. * @param void
  1114. *
  1115. * @return int
  1116. */
  1117. public static function getNumberOfMessagesSent()
  1118. {
  1119. $table = Database::get_main_table(TABLE_MESSAGE);
  1120. $keyword = Session::read('message_sent_search_keyword');
  1121. $keywordCondition = '';
  1122. if (!empty($keyword)) {
  1123. $keyword = Database::escape_string($keyword);
  1124. $keywordCondition = " AND (title like '%$keyword%' OR content LIKE '%$keyword%') ";
  1125. }
  1126. $sql = "SELECT COUNT(id) as number_messages
  1127. FROM $table
  1128. WHERE
  1129. msg_status = ".MESSAGE_STATUS_OUTBOX." AND
  1130. user_sender_id = ".api_get_user_id()."
  1131. $keywordCondition
  1132. ";
  1133. $result = Database::query($sql);
  1134. $result = Database::fetch_array($result);
  1135. return $result['number_messages'];
  1136. }
  1137. /**
  1138. * display message box in the inbox.
  1139. *
  1140. * @param int the message id
  1141. * @param string inbox or outbox strings are available
  1142. *
  1143. * @todo replace numbers with letters in the $row array pff...
  1144. *
  1145. * @return string html with the message content
  1146. */
  1147. public static function showMessageBox($messageId, $source = 'inbox')
  1148. {
  1149. $table = Database::get_main_table(TABLE_MESSAGE);
  1150. $messageId = (int) $messageId;
  1151. $currentUserId = api_get_user_id();
  1152. if ($source == 'outbox') {
  1153. if (isset($messageId) && is_numeric($messageId)) {
  1154. $query = "SELECT * FROM $table
  1155. WHERE
  1156. user_sender_id = ".$currentUserId." AND
  1157. id = $messageId AND
  1158. msg_status = ".MESSAGE_STATUS_OUTBOX;
  1159. $result = Database::query($query);
  1160. }
  1161. } else {
  1162. if (is_numeric($messageId) && !empty($messageId)) {
  1163. $query = "UPDATE $table SET
  1164. msg_status = '".MESSAGE_STATUS_NEW."'
  1165. WHERE
  1166. user_receiver_id=".$currentUserId." AND
  1167. id = '".$messageId."'";
  1168. Database::query($query);
  1169. $query = "SELECT * FROM $table
  1170. WHERE
  1171. msg_status<> ".MESSAGE_STATUS_OUTBOX." AND
  1172. user_receiver_id = ".$currentUserId." AND
  1173. id = '".$messageId."'";
  1174. $result = Database::query($query);
  1175. }
  1176. }
  1177. $row = Database::fetch_array($result, 'ASSOC');
  1178. if (empty($row)) {
  1179. return '';
  1180. }
  1181. $user_sender_id = $row['user_sender_id'];
  1182. // get file attachments by message id
  1183. $files_attachments = self::getAttachmentLinkList(
  1184. $messageId,
  1185. $source
  1186. );
  1187. $row['content'] = str_replace('</br>', '<br />', $row['content']);
  1188. $title = Security::remove_XSS($row['title'], STUDENT, true);
  1189. $content = Security::remove_XSS($row['content'], STUDENT, true);
  1190. $name = get_lang('UnknownUser');
  1191. $fromUser = api_get_user_info($user_sender_id);
  1192. $userImage = '';
  1193. if (!empty($user_sender_id) && !empty($fromUser)) {
  1194. $name = $fromUser['complete_name_with_username'];
  1195. $userImage = Display::img(
  1196. $fromUser['avatar_small'],
  1197. $name,
  1198. ['title' => $name, 'class' => 'img-responsive img-circle', 'style' => 'max-width:35px'],
  1199. false
  1200. );
  1201. }
  1202. $message_content = Display::page_subheader(str_replace("\\", "", $title));
  1203. $receiverUserInfo = [];
  1204. if (!empty($row['user_receiver_id'])) {
  1205. $receiverUserInfo = api_get_user_info($row['user_receiver_id']);
  1206. }
  1207. $message_content .= '<tr>';
  1208. if (api_get_setting('allow_social_tool') === 'true') {
  1209. $message_content .= '<div class="row">';
  1210. if ($source == 'outbox') {
  1211. $message_content .= '<div class="col-md-12">';
  1212. $message_content .= '<ul class="list-message">';
  1213. $message_content .= '<li>'.$userImage.'</li>';
  1214. $message_content .= '<li>'.$name.'&nbsp;';
  1215. if (!empty($receiverUserInfo)) {
  1216. $message_content .= api_strtolower(
  1217. get_lang('To')
  1218. ).'&nbsp;<b>'.$receiverUserInfo['complete_name_with_username'].'</b></li>';
  1219. } else {
  1220. $message_content .= api_strtolower(get_lang('To')).'&nbsp;<b>-</b></li>';
  1221. }
  1222. $message_content .= '<li>'.Display::dateToStringAgoAndLongDate($row['send_date']).'</li>';
  1223. $message_content .= '</ul>';
  1224. $message_content .= '</div>';
  1225. } else {
  1226. $message_content .= '<div class="col-md-12">';
  1227. $message_content .= '<ul class="list-message">';
  1228. if (!empty($user_sender_id)) {
  1229. $message_content .= '<li>'.$userImage.'</li>';
  1230. $message_content .= '<li><a href="'.api_get_path(
  1231. WEB_PATH
  1232. ).'main/social/profile.php?u='.$user_sender_id.'">'.$name.'</a>';
  1233. } else {
  1234. $message_content .= '<li>'.$name;
  1235. }
  1236. $message_content .= '&nbsp;'.api_strtolower(get_lang('To')).'&nbsp;'.get_lang('Me');
  1237. $message_content .= '<li>'.Display::dateToStringAgoAndLongDate($row['send_date']).'</li>';
  1238. $message_content .= '</ul>';
  1239. $message_content .= '</div>';
  1240. }
  1241. $message_content .= '</div>';
  1242. } else {
  1243. if ($source == 'outbox') {
  1244. $message_content .= get_lang('From').':&nbsp;'.$name.'</b> '.api_strtolower(get_lang('To')).' <b>'.
  1245. $receiverUserInfo['complete_name_with_username'].'</b>';
  1246. } else {
  1247. $message_content .= get_lang('From').':&nbsp;'.$name.'</b> '.api_strtolower(get_lang('To')).' <b>'.
  1248. get_lang('Me').'</b>';
  1249. }
  1250. }
  1251. $message_content .= '
  1252. <hr style="color:#ddd" />
  1253. <table width="100%">
  1254. <tr>
  1255. <td valign=top class="view-message-content">'.str_replace("\\", "", $content).'</td>
  1256. </tr>
  1257. </table>
  1258. <div id="message-attach">'.(!empty($files_attachments) ? implode('<br />', $files_attachments) : '').'</div>
  1259. <div style="padding: 15px 0px 5px 0px">';
  1260. $social_link = '';
  1261. if (isset($_GET['f']) && $_GET['f'] == 'social') {
  1262. $social_link = 'f=social';
  1263. }
  1264. if ($source == 'outbox') {
  1265. $message_content .= '<a href="outbox.php?'.$social_link.'">'.
  1266. Display::return_icon('back.png', get_lang('ReturnToOutbox')).'</a> &nbsp';
  1267. } else {
  1268. $message_content .= '<a href="inbox.php?'.$social_link.'">'.
  1269. Display::return_icon('back.png', get_lang('ReturnToInbox')).'</a> &nbsp';
  1270. $message_content .= '<a href="new_message.php?re_id='.$messageId.'&'.$social_link.'">'.
  1271. Display::return_icon('message_reply.png', get_lang('ReplyToMessage')).'</a> &nbsp';
  1272. }
  1273. $message_content .= '<a href="inbox.php?action=deleteone&id='.$messageId.'&'.$social_link.'" >'.
  1274. Display::return_icon('delete.png', get_lang('DeleteMessage')).'</a>&nbsp';
  1275. $message_content .= '</div></td>
  1276. <td width=10></td>
  1277. </tr>
  1278. </table>';
  1279. return $message_content;
  1280. }
  1281. /**
  1282. * get user id by user email.
  1283. *
  1284. * @param string $user_email
  1285. *
  1286. * @return int user id
  1287. */
  1288. public static function get_user_id_by_email($user_email)
  1289. {
  1290. $table = Database::get_main_table(TABLE_MAIN_USER);
  1291. $sql = 'SELECT user_id
  1292. FROM '.$table.'
  1293. WHERE email="'.Database::escape_string($user_email).'";';
  1294. $rs = Database::query($sql);
  1295. $row = Database::fetch_array($rs, 'ASSOC');
  1296. if (isset($row['user_id'])) {
  1297. return $row['user_id'];
  1298. }
  1299. return null;
  1300. }
  1301. /**
  1302. * Displays messages of a group with nested view.
  1303. *
  1304. * @param int $groupId
  1305. *
  1306. * @return string
  1307. */
  1308. public static function display_messages_for_group($groupId)
  1309. {
  1310. global $my_group_role;
  1311. $rows = self::get_messages_by_group($groupId);
  1312. $topics_per_page = 10;
  1313. $html_messages = '';
  1314. $query_vars = ['id' => $groupId, 'topics_page_nr' => 0];
  1315. if (is_array($rows) && count($rows) > 0) {
  1316. // prepare array for topics with its items
  1317. $topics = [];
  1318. $x = 0;
  1319. foreach ($rows as $index => $value) {
  1320. if (empty($value['parent_id'])) {
  1321. $topics[$value['id']] = $value;
  1322. }
  1323. }
  1324. $new_topics = [];
  1325. foreach ($topics as $id => $value) {
  1326. $rows = null;
  1327. $rows = self::get_messages_by_group_by_message($groupId, $value['id']);
  1328. if (!empty($rows)) {
  1329. $count = count(self::calculate_children($rows, $value['id']));
  1330. } else {
  1331. $count = 0;
  1332. }
  1333. $value['count'] = $count;
  1334. $new_topics[$id] = $value;
  1335. }
  1336. $array_html = [];
  1337. foreach ($new_topics as $index => $topic) {
  1338. $html = '';
  1339. // topics
  1340. $user_sender_info = api_get_user_info($topic['user_sender_id']);
  1341. $name = $user_sender_info['complete_name'];
  1342. $html .= '<div class="groups-messages">';
  1343. $html .= '<div class="row">';
  1344. $items = $topic['count'];
  1345. $reply_label = ($items == 1) ? get_lang('GroupReply') : get_lang('GroupReplies');
  1346. $label = '<i class="fa fa-envelope"></i> '.$items.' '.$reply_label;
  1347. $topic['title'] = trim($topic['title']);
  1348. if (empty($topic['title'])) {
  1349. $topic['title'] = get_lang('Untitled');
  1350. }
  1351. $html .= '<div class="col-xs-8 col-md-10">';
  1352. $html .= Display::tag(
  1353. 'h4',
  1354. Display::url(
  1355. Security::remove_XSS($topic['title'], STUDENT, true),
  1356. api_get_path(WEB_CODE_PATH).'social/group_topics.php?id='.$groupId.'&topic_id='.$topic['id']
  1357. ),
  1358. ['class' => 'title']
  1359. );
  1360. $actions = '';
  1361. if ($my_group_role == GROUP_USER_PERMISSION_ADMIN ||
  1362. $my_group_role == GROUP_USER_PERMISSION_MODERATOR
  1363. ) {
  1364. $actions = '<br />'.Display::url(
  1365. get_lang('Delete'),
  1366. api_get_path(
  1367. WEB_CODE_PATH
  1368. ).'social/group_topics.php?action=delete&id='.$groupId.'&topic_id='.$topic['id'],
  1369. ['class' => 'btn btn-default']
  1370. );
  1371. }
  1372. $date = '';
  1373. if ($topic['send_date'] != $topic['update_date']) {
  1374. if (!empty($topic['update_date'])) {
  1375. $date .= '<i class="fa fa-calendar"></i> '.get_lang(
  1376. 'LastUpdate'
  1377. ).' '.Display::dateToStringAgoAndLongDate($topic['update_date']);
  1378. }
  1379. } else {
  1380. $date .= '<i class="fa fa-calendar"></i> '.get_lang(
  1381. 'Created'
  1382. ).' '.Display::dateToStringAgoAndLongDate($topic['send_date']);
  1383. }
  1384. $html .= '<div class="date">'.$label.' - '.$date.$actions.'</div>';
  1385. $html .= '</div>';
  1386. $image = $user_sender_info['avatar'];
  1387. $user_info = '<div class="author"><img class="img-responsive img-circle" src="'.$image.'" alt="'.$name.'" width="64" height="64" title="'.$name.'" /></div>';
  1388. $user_info .= '<div class="name"><a href="'.api_get_path(
  1389. WEB_PATH
  1390. ).'main/social/profile.php?u='.$topic['user_sender_id'].'">'.$name.'&nbsp;</a></div>';
  1391. $html .= '<div class="col-xs-4 col-md-2">';
  1392. $html .= $user_info;
  1393. $html .= '</div>';
  1394. $html .= '</div>';
  1395. $html .= '</div>';
  1396. $array_html[] = [$html];
  1397. }
  1398. // grids for items and topics with paginations
  1399. $html_messages .= Display::return_sortable_grid(
  1400. 'topics',
  1401. [],
  1402. $array_html,
  1403. [
  1404. 'hide_navigation' => false,
  1405. 'per_page' => $topics_per_page,
  1406. ],
  1407. $query_vars,
  1408. false,
  1409. [true, true, true, false],
  1410. false
  1411. );
  1412. }
  1413. return $html_messages;
  1414. }
  1415. /**
  1416. * Displays messages of a group with nested view.
  1417. *
  1418. * @param $groupId
  1419. * @param $topic_id
  1420. *
  1421. * @return string
  1422. */
  1423. public static function display_message_for_group($groupId, $topic_id)
  1424. {
  1425. global $my_group_role;
  1426. $main_message = self::get_message_by_id($topic_id);
  1427. if (empty($main_message)) {
  1428. return false;
  1429. }
  1430. $webCodePath = api_get_path(WEB_CODE_PATH);
  1431. $iconCalendar = Display::returnFontAwesomeIcon('calendar');
  1432. $langEdit = get_lang('Edit');
  1433. $langReply = get_lang('Reply');
  1434. $langLastUpdated = get_lang('LastUpdated');
  1435. $langCreated = get_lang('Created');
  1436. $rows = self::get_messages_by_group_by_message($groupId, $topic_id);
  1437. $rows = self::calculate_children($rows, $topic_id);
  1438. $current_user_id = api_get_user_id();
  1439. $items_per_page = 50;
  1440. $query_vars = ['id' => $groupId, 'topic_id' => $topic_id, 'topics_page_nr' => 0];
  1441. // Main message
  1442. $links = '';
  1443. $main_content = '';
  1444. $html = '';
  1445. $items_page_nr = null;
  1446. $user_sender_info = api_get_user_info($main_message['user_sender_id']);
  1447. $files_attachments = self::getAttachmentLinkList($main_message['id']);
  1448. $name = $user_sender_info['complete_name'];
  1449. $topic_page_nr = isset($_GET['topics_page_nr']) ? (int) $_GET['topics_page_nr'] : null;
  1450. $links .= '<div class="pull-right">';
  1451. $links .= '<div class="btn-group btn-group-sm">';
  1452. if (($my_group_role == GROUP_USER_PERMISSION_ADMIN || $my_group_role == GROUP_USER_PERMISSION_MODERATOR) ||
  1453. $main_message['user_sender_id'] == $current_user_id
  1454. ) {
  1455. $urlEdit = $webCodePath.'social/message_for_group_form.inc.php?'
  1456. .http_build_query(
  1457. [
  1458. 'user_friend' => $current_user_id,
  1459. 'group_id' => $groupId,
  1460. 'message_id' => $main_message['id'],
  1461. 'action' => 'edit_message_group',
  1462. 'anchor_topic' => 'topic_'.$main_message['id'],
  1463. 'topics_page_nr' => $topic_page_nr,
  1464. 'items_page_nr' => $items_page_nr,
  1465. 'topic_id' => $main_message['id'],
  1466. ]
  1467. );
  1468. $links .= Display::toolbarButton(
  1469. $langEdit,
  1470. $urlEdit,
  1471. 'pencil',
  1472. 'default',
  1473. ['class' => 'ajax', 'data-title' => $langEdit, 'data-size' => 'lg'],
  1474. false
  1475. );
  1476. }
  1477. $links .= self::getLikesButton($main_message['id'], $current_user_id, $groupId);
  1478. $urlReply = $webCodePath.'social/message_for_group_form.inc.php?'
  1479. .http_build_query(
  1480. [
  1481. 'user_friend' => $current_user_id,
  1482. 'group_id' => $groupId,
  1483. 'message_id' => $main_message['id'],
  1484. 'action' => 'reply_message_group',
  1485. 'anchor_topic' => 'topic_'.$main_message['id'],
  1486. 'topics_page_nr' => $topic_page_nr,
  1487. 'topic_id' => $main_message['id'],
  1488. ]
  1489. );
  1490. $links .= Display::toolbarButton(
  1491. $langReply,
  1492. $urlReply,
  1493. 'commenting',
  1494. 'default',
  1495. ['class' => 'ajax', 'data-title' => $langReply, 'data-size' => 'lg'],
  1496. false
  1497. );
  1498. if (api_is_platform_admin()) {
  1499. $links .= Display::toolbarButton(
  1500. get_lang('Delete'),
  1501. 'group_topics.php?action=delete&id='.$groupId.'&topic_id='.$topic_id,
  1502. 'trash',
  1503. 'default',
  1504. [],
  1505. false
  1506. );
  1507. }
  1508. $links .= '</div>';
  1509. $links .= '</div>';
  1510. $title = '<h4>'.Security::remove_XSS($main_message['title'], STUDENT, true).$links.'</h4>';
  1511. $userPicture = $user_sender_info['avatar'];
  1512. $main_content .= '<div class="row">';
  1513. $main_content .= '<div class="col-md-2">';
  1514. $main_content .= '<div class="avatar-author">';
  1515. $main_content .= Display::img(
  1516. $userPicture,
  1517. $name,
  1518. ['width' => '60px', 'class' => 'img-responsive img-circle'],
  1519. false
  1520. );
  1521. $main_content .= '</div>';
  1522. $main_content .= '</div>';
  1523. $date = '';
  1524. if ($main_message['send_date'] != $main_message['update_date']) {
  1525. if (!empty($main_message['update_date'])) {
  1526. $date = '<div class="date"> '
  1527. ."$iconCalendar $langLastUpdated "
  1528. .Display::dateToStringAgoAndLongDate($main_message['update_date'])
  1529. .'</div>';
  1530. }
  1531. } else {
  1532. $date = '<div class="date"> '
  1533. ."$iconCalendar $langCreated "
  1534. .Display::dateToStringAgoAndLongDate($main_message['send_date'])
  1535. .'</div>';
  1536. }
  1537. $attachment = '<div class="message-attach">'
  1538. .(!empty($files_attachments) ? implode('<br />', $files_attachments) : '')
  1539. .'</div>';
  1540. $main_content .= '<div class="col-md-10">';
  1541. $user_link = Display::url(
  1542. $name,
  1543. $webCodePath.'social/profile.php?u='.$main_message['user_sender_id']
  1544. );
  1545. $main_content .= '<div class="message-content"> ';
  1546. $main_content .= '<div class="username">'.$user_link.'</div>';
  1547. $main_content .= $date;
  1548. $main_content .= '<div class="message">'.$main_message['content'].$attachment.'</div></div>';
  1549. $main_content .= '</div>';
  1550. $main_content .= '</div>';
  1551. $html .= Display::div(
  1552. Display::div(
  1553. $title.$main_content,
  1554. ['class' => 'message-topic']
  1555. ),
  1556. ['class' => 'sm-groups-message']
  1557. );
  1558. $topic_id = $main_message['id'];
  1559. if (is_array($rows) && count($rows) > 0) {
  1560. $topics = $rows;
  1561. $array_html_items = [];
  1562. foreach ($topics as $index => $topic) {
  1563. if (empty($topic['id'])) {
  1564. continue;
  1565. }
  1566. $items_page_nr = isset($_GET['items_'.$topic['id'].'_page_nr'])
  1567. ? (int) $_GET['items_'.$topic['id'].'_page_nr']
  1568. : null;
  1569. $links = '';
  1570. $links .= '<div class="pull-right">';
  1571. $html_items = '';
  1572. $user_sender_info = api_get_user_info($topic['user_sender_id']);
  1573. $files_attachments = self::getAttachmentLinkList($topic['id']);
  1574. $name = $user_sender_info['complete_name'];
  1575. $links .= '<div class="btn-group btn-group-sm">';
  1576. if (
  1577. ($my_group_role == GROUP_USER_PERMISSION_ADMIN ||
  1578. $my_group_role == GROUP_USER_PERMISSION_MODERATOR
  1579. ) ||
  1580. $topic['user_sender_id'] == $current_user_id
  1581. ) {
  1582. $links .= Display::toolbarButton(
  1583. $langEdit,
  1584. $webCodePath.'social/message_for_group_form.inc.php?'
  1585. .http_build_query(
  1586. [
  1587. 'user_friend' => $current_user_id,
  1588. 'group_id' => $groupId,
  1589. 'message_id' => $topic['id'],
  1590. 'action' => 'edit_message_group',
  1591. 'anchor_topic' => 'topic_'.$topic_id,
  1592. 'topics_page_nr' => $topic_page_nr,
  1593. 'items_page_nr' => $items_page_nr,
  1594. 'topic_id' => $topic_id,
  1595. ]
  1596. ),
  1597. 'pencil',
  1598. 'default',
  1599. ['class' => 'ajax', 'data-title' => $langEdit, 'data-size' => 'lg'],
  1600. false
  1601. );
  1602. }
  1603. $links .= self::getLikesButton($topic['id'], $current_user_id, $groupId);
  1604. $links .= Display::toolbarButton(
  1605. $langReply,
  1606. $webCodePath.'social/message_for_group_form.inc.php?'
  1607. .http_build_query(
  1608. [
  1609. 'user_friend' => $current_user_id,
  1610. 'group_id' => $groupId,
  1611. 'message_id' => $topic['id'],
  1612. 'action' => 'reply_message_group',
  1613. 'anchor_topic' => 'topic_'.$topic_id,
  1614. 'topics_page_nr' => $topic_page_nr,
  1615. 'items_page_nr' => $items_page_nr,
  1616. 'topic_id' => $topic_id,
  1617. ]
  1618. ),
  1619. 'commenting',
  1620. 'default',
  1621. ['class' => 'ajax', 'data-title' => $langReply, 'data-size' => 'lg'],
  1622. false
  1623. );
  1624. $links .= '</div>';
  1625. $links .= '</div>';
  1626. $userPicture = $user_sender_info['avatar'];
  1627. $user_link = Display::url(
  1628. $name,
  1629. $webCodePath.'social/profile.php?u='.$topic['user_sender_id']
  1630. );
  1631. $html_items .= '<div class="row">';
  1632. $html_items .= '<div class="col-md-2">';
  1633. $html_items .= '<div class="avatar-author">';
  1634. $html_items .= Display::img(
  1635. $userPicture,
  1636. $name,
  1637. ['width' => '60px', 'class' => 'img-responsive img-circle'],
  1638. false
  1639. );
  1640. $html_items .= '</div>';
  1641. $html_items .= '</div>';
  1642. $date = '';
  1643. if ($topic['send_date'] != $topic['update_date']) {
  1644. if (!empty($topic['update_date'])) {
  1645. $date = '<div class="date"> '
  1646. ."$iconCalendar $langLastUpdated "
  1647. .Display::dateToStringAgoAndLongDate($topic['update_date'])
  1648. .'</div>';
  1649. }
  1650. } else {
  1651. $date = '<div class="date"> '
  1652. ."$iconCalendar $langCreated "
  1653. .Display::dateToStringAgoAndLongDate($topic['send_date'])
  1654. .'</div>';
  1655. }
  1656. $attachment = '<div class="message-attach">'
  1657. .(!empty($files_attachments) ? implode('<br />', $files_attachments) : '')
  1658. .'</div>';
  1659. $html_items .= '<div class="col-md-10">'
  1660. .'<div class="message-content">'
  1661. .$links
  1662. .'<div class="username">'.$user_link.'</div>'
  1663. .$date
  1664. .'<div class="message">'
  1665. .Security::remove_XSS($topic['content'], STUDENT, true)
  1666. .'</div>'.$attachment.'</div>'
  1667. .'</div>'
  1668. .'</div>';
  1669. $base_padding = 20;
  1670. if ($topic['indent_cnt'] == 0) {
  1671. $indent = $base_padding;
  1672. } else {
  1673. $indent = (int) $topic['indent_cnt'] * $base_padding + $base_padding;
  1674. }
  1675. $html_items = Display::div($html_items, ['class' => 'message-post', 'id' => 'msg_'.$topic['id']]);
  1676. $html_items = Display::div($html_items, ['class' => '', 'style' => 'margin-left:'.$indent.'px']);
  1677. $array_html_items[] = [$html_items];
  1678. }
  1679. // grids for items with paginations
  1680. $options = ['hide_navigation' => false, 'per_page' => $items_per_page];
  1681. $visibility = [true, true, true, false];
  1682. $style_class = [
  1683. 'item' => ['class' => 'user-post'],
  1684. 'main' => ['class' => 'user-list'],
  1685. ];
  1686. if (!empty($array_html_items)) {
  1687. $html .= Display::return_sortable_grid(
  1688. 'items_'.$topic['id'],
  1689. [],
  1690. $array_html_items,
  1691. $options,
  1692. $query_vars,
  1693. null,
  1694. $visibility,
  1695. false,
  1696. $style_class
  1697. );
  1698. }
  1699. }
  1700. return $html;
  1701. }
  1702. /**
  1703. * Add children to messages by id is used for nested view messages.
  1704. *
  1705. * @param array $rows rows of messages
  1706. *
  1707. * @return array $first_seed new list adding the item children
  1708. */
  1709. public static function calculate_children($rows, $first_seed)
  1710. {
  1711. $rows_with_children = [];
  1712. foreach ($rows as $row) {
  1713. $rows_with_children[$row["id"]] = $row;
  1714. $rows_with_children[$row["parent_id"]]["children"][] = $row["id"];
  1715. }
  1716. $rows = $rows_with_children;
  1717. $sorted_rows = [0 => []];
  1718. self::message_recursive_sort($rows, $sorted_rows, $first_seed);
  1719. unset($sorted_rows[0]);
  1720. return $sorted_rows;
  1721. }
  1722. /**
  1723. * Sort recursively the messages, is used for for nested view messages.
  1724. *
  1725. * @param array original rows of messages
  1726. * @param array list recursive of messages
  1727. * @param int seed for calculate the indent
  1728. * @param int indent for nested view
  1729. */
  1730. public static function message_recursive_sort(
  1731. $rows,
  1732. &$messages,
  1733. $seed = 0,
  1734. $indent = 0
  1735. ) {
  1736. if ($seed > 0 && isset($rows[$seed]["id"])) {
  1737. $messages[$rows[$seed]["id"]] = $rows[$seed];
  1738. $messages[$rows[$seed]["id"]]["indent_cnt"] = $indent;
  1739. $indent++;
  1740. }
  1741. if (isset($rows[$seed]["children"])) {
  1742. foreach ($rows[$seed]["children"] as $child) {
  1743. self::message_recursive_sort($rows, $messages, $child, $indent);
  1744. }
  1745. }
  1746. }
  1747. /**
  1748. * @param int $messageId
  1749. *
  1750. * @return array
  1751. */
  1752. public static function getAttachmentList($messageId)
  1753. {
  1754. $table = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
  1755. $messageId = (int) $messageId;
  1756. if (empty($messageId)) {
  1757. return [];
  1758. }
  1759. $messageInfo = self::get_message_by_id($messageId);
  1760. if (empty($messageInfo)) {
  1761. return [];
  1762. }
  1763. $attachmentDir = UserManager::getUserPathById($messageInfo['user_receiver_id'], 'system');
  1764. $attachmentDir .= 'message_attachments/';
  1765. $sql = "SELECT * FROM $table
  1766. WHERE message_id = '$messageId'";
  1767. $result = Database::query($sql);
  1768. $files = [];
  1769. while ($row = Database::fetch_array($result, 'ASSOC')) {
  1770. $row['file_source'] = '';
  1771. if (file_exists($attachmentDir.$row['path'])) {
  1772. $row['file_source'] = $attachmentDir.$row['path'];
  1773. }
  1774. $files[] = $row;
  1775. }
  1776. return $files;
  1777. }
  1778. /**
  1779. * Get array of links (download) for message attachment files.
  1780. *
  1781. * @param int $messageId
  1782. * @param string $type message list (inbox/outbox)
  1783. *
  1784. * @return array
  1785. */
  1786. public static function getAttachmentLinkList($messageId, $type = '')
  1787. {
  1788. $files = self::getAttachmentList($messageId);
  1789. // get file attachments by message id
  1790. $list = [];
  1791. if ($files) {
  1792. $attachIcon = Display::return_icon('attachment.gif', '');
  1793. $archiveURL = api_get_path(WEB_CODE_PATH).'messages/download.php?type='.$type.'&file=';
  1794. foreach ($files as $row_file) {
  1795. $archiveFile = $row_file['path'];
  1796. $filename = $row_file['filename'];
  1797. $size = format_file_size($row_file['size']);
  1798. $comment = Security::remove_XSS($row_file['comment']);
  1799. $filename = Security::remove_XSS($filename);
  1800. $link = Display::url($filename, $archiveURL.$archiveFile);
  1801. $comment = !empty($comment) ? '&nbsp;-&nbsp;<i>'.$comment.'</i>' : '';
  1802. $attachmentLine = $attachIcon.'&nbsp;'.$link.'&nbsp;('.$size.')'.$comment;
  1803. if ($row_file['comment'] == 'audio_message') {
  1804. $attachmentLine = '<audio src="'.$archiveURL.$archiveFile.'"/>';
  1805. }
  1806. $list[] = $attachmentLine;
  1807. }
  1808. }
  1809. return $list;
  1810. }
  1811. /**
  1812. * Get message list by id.
  1813. *
  1814. * @param int $messageId
  1815. *
  1816. * @return array
  1817. */
  1818. public static function get_message_by_id($messageId)
  1819. {
  1820. $table = Database::get_main_table(TABLE_MESSAGE);
  1821. $messageId = (int) $messageId;
  1822. $sql = "SELECT * FROM $table
  1823. WHERE
  1824. id = '$messageId' AND
  1825. msg_status <> '".MESSAGE_STATUS_DELETED."' ";
  1826. $res = Database::query($sql);
  1827. $item = [];
  1828. if (Database::num_rows($res) > 0) {
  1829. $item = Database::fetch_array($res, 'ASSOC');
  1830. }
  1831. return $item;
  1832. }
  1833. /**
  1834. * @return string
  1835. */
  1836. public static function generate_message_form()
  1837. {
  1838. $form = new FormValidator('send_message');
  1839. $form->addText(
  1840. 'subject',
  1841. get_lang('Subject'),
  1842. false,
  1843. ['id' => 'subject_id']
  1844. );
  1845. $form->addTextarea(
  1846. 'content',
  1847. get_lang('Message'),
  1848. ['id' => 'content_id', 'rows' => '5']
  1849. );
  1850. return $form->returnForm();
  1851. }
  1852. /**
  1853. * @return string
  1854. */
  1855. public static function generate_invitation_form()
  1856. {
  1857. $form = new FormValidator('send_invitation');
  1858. $form->addTextarea(
  1859. 'content',
  1860. get_lang('AddPersonalMessage'),
  1861. ['id' => 'content_invitation_id', 'rows' => 5]
  1862. );
  1863. return $form->returnForm();
  1864. }
  1865. /**
  1866. * @param string $keyword
  1867. *
  1868. * @return string
  1869. */
  1870. public static function inbox_display($keyword = '')
  1871. {
  1872. $success = get_lang('SelectedMessagesDeleted');
  1873. $success_read = get_lang('SelectedMessagesRead');
  1874. $success_unread = get_lang('SelectedMessagesUnRead');
  1875. $html = '';
  1876. Session::write('message_search_keyword', $keyword);
  1877. if (isset($_REQUEST['action'])) {
  1878. switch ($_REQUEST['action']) {
  1879. case 'mark_as_unread':
  1880. if (is_array($_POST['id'])) {
  1881. foreach ($_POST['id'] as $index => $message_id) {
  1882. self::update_message_status(
  1883. api_get_user_id(),
  1884. $message_id,
  1885. MESSAGE_STATUS_UNREAD
  1886. );
  1887. }
  1888. }
  1889. $html .= Display::return_message(
  1890. api_xml_http_response_encode($success_unread),
  1891. 'normal',
  1892. false
  1893. );
  1894. break;
  1895. case 'mark_as_read':
  1896. if (is_array($_POST['id'])) {
  1897. foreach ($_POST['id'] as $index => $message_id) {
  1898. self::update_message_status(
  1899. api_get_user_id(),
  1900. $message_id,
  1901. MESSAGE_STATUS_NEW
  1902. );
  1903. }
  1904. }
  1905. $html .= Display::return_message(
  1906. api_xml_http_response_encode($success_read),
  1907. 'normal',
  1908. false
  1909. );
  1910. break;
  1911. case 'delete':
  1912. foreach ($_POST['id'] as $index => $message_id) {
  1913. self::delete_message_by_user_receiver(api_get_user_id(), $message_id);
  1914. }
  1915. $html .= Display::return_message(
  1916. api_xml_http_response_encode($success),
  1917. 'normal',
  1918. false
  1919. );
  1920. break;
  1921. case 'deleteone':
  1922. self::delete_message_by_user_receiver(api_get_user_id(), $_GET['id']);
  1923. $html .= Display::return_message(
  1924. api_xml_http_response_encode($success),
  1925. 'confirmation',
  1926. false
  1927. );
  1928. break;
  1929. }
  1930. }
  1931. // display sortable table with messages of the current user
  1932. $table = new SortableTable(
  1933. 'message_inbox',
  1934. ['MessageManager', 'getNumberOfMessages'],
  1935. ['MessageManager', 'get_message_data'],
  1936. 2,
  1937. 20,
  1938. 'DESC'
  1939. );
  1940. $table->set_header(0, '', false, ['style' => 'width:15px;']);
  1941. $table->set_header(1, get_lang('Messages'), false);
  1942. $table->set_header(2, get_lang('Date'), true, ['style' => 'width:180px;']);
  1943. $table->set_header(3, get_lang('Modify'), false, ['style' => 'width:120px;']);
  1944. if (isset($_REQUEST['f']) && $_REQUEST['f'] == 'social') {
  1945. $parameters['f'] = 'social';
  1946. $table->set_additional_parameters($parameters);
  1947. }
  1948. $table->set_form_actions(
  1949. [
  1950. 'delete' => get_lang('DeleteSelectedMessages'),
  1951. 'mark_as_unread' => get_lang('MailMarkSelectedAsUnread'),
  1952. 'mark_as_read' => get_lang('MailMarkSelectedAsRead'),
  1953. ]
  1954. );
  1955. $html .= $table->return_table();
  1956. Session::erase('message_search_keyword');
  1957. return $html;
  1958. }
  1959. /**
  1960. * @param string $keyword
  1961. *
  1962. * @return string
  1963. */
  1964. public static function outbox_display($keyword = '')
  1965. {
  1966. Session::write('message_sent_search_keyword', $keyword);
  1967. $success = get_lang('SelectedMessagesDeleted').'&nbsp</b><br />
  1968. <a href="outbox.php">'.get_lang('BackToOutbox').'</a>';
  1969. $html = '';
  1970. if (isset($_REQUEST['action'])) {
  1971. switch ($_REQUEST['action']) {
  1972. case 'delete':
  1973. $number_of_selected_messages = count($_POST['id']);
  1974. if ($number_of_selected_messages != 0) {
  1975. foreach ($_POST['id'] as $index => $message_id) {
  1976. self::delete_message_by_user_receiver(
  1977. api_get_user_id(),
  1978. $message_id
  1979. );
  1980. }
  1981. }
  1982. $html .= Display::return_message(api_xml_http_response_encode($success), 'normal', false);
  1983. break;
  1984. case 'deleteone':
  1985. self::delete_message_by_user_receiver(api_get_user_id(), $_GET['id']);
  1986. $html .= Display::return_message(api_xml_http_response_encode($success), 'normal', false);
  1987. $html .= '<br/>';
  1988. break;
  1989. }
  1990. }
  1991. // display sortable table with messages of the current user
  1992. $table = new SortableTable(
  1993. 'message_outbox',
  1994. ['MessageManager', 'getNumberOfMessagesSent'],
  1995. ['MessageManager', 'get_message_data_sent'],
  1996. 2,
  1997. 20,
  1998. 'DESC'
  1999. );
  2000. $table->set_header(0, '', false, ['style' => 'width:15px;']);
  2001. $table->set_header(1, get_lang('Messages'), false);
  2002. $table->set_header(2, get_lang('Date'), true, ['style' => 'width:180px;']);
  2003. $table->set_header(3, get_lang('Modify'), false, ['style' => 'width:70px;']);
  2004. $table->set_form_actions(['delete' => get_lang('DeleteSelectedMessages')]);
  2005. $html .= $table->return_table();
  2006. Session::erase('message_sent_search_keyword');
  2007. return $html;
  2008. }
  2009. /**
  2010. * Get the data of the last received messages for a user.
  2011. *
  2012. * @param int $userId The user id
  2013. * @param int $lastId The id of the last received message
  2014. *
  2015. * @return array
  2016. */
  2017. public static function getMessagesFromLastReceivedMessage($userId, $lastId = 0)
  2018. {
  2019. $userId = (int) $userId;
  2020. $lastId = (int) $lastId;
  2021. if (empty($userId)) {
  2022. return [];
  2023. }
  2024. $messagesTable = Database::get_main_table(TABLE_MESSAGE);
  2025. $userTable = Database::get_main_table(TABLE_MAIN_USER);
  2026. $sql = "SELECT m.*, u.user_id, u.lastname, u.firstname
  2027. FROM $messagesTable as m
  2028. INNER JOIN $userTable as u
  2029. ON m.user_sender_id = u.user_id
  2030. WHERE
  2031. m.user_receiver_id = $userId AND
  2032. m.msg_status = ".MESSAGE_STATUS_UNREAD."
  2033. AND m.id > $lastId
  2034. ORDER BY m.send_date DESC";
  2035. $result = Database::query($sql);
  2036. $messages = [];
  2037. if ($result !== false) {
  2038. while ($row = Database::fetch_assoc($result)) {
  2039. $messages[] = $row;
  2040. }
  2041. }
  2042. return $messages;
  2043. }
  2044. /**
  2045. * Check whether a message has attachments.
  2046. *
  2047. * @param int $messageId The message id
  2048. *
  2049. * @return bool Whether the message has attachments return true. Otherwise return false
  2050. */
  2051. public static function hasAttachments($messageId)
  2052. {
  2053. $messageId = (int) $messageId;
  2054. if (empty($messageId)) {
  2055. return false;
  2056. }
  2057. $messageAttachmentTable = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
  2058. $conditions = [
  2059. 'where' => [
  2060. 'message_id = ?' => $messageId,
  2061. ],
  2062. ];
  2063. $result = Database::select(
  2064. 'COUNT(1) AS qty',
  2065. $messageAttachmentTable,
  2066. $conditions,
  2067. 'first'
  2068. );
  2069. if (!empty($result)) {
  2070. if ($result['qty'] > 0) {
  2071. return true;
  2072. }
  2073. }
  2074. return false;
  2075. }
  2076. /**
  2077. * @param int $messageId
  2078. *
  2079. * @return array|bool
  2080. */
  2081. public static function getAttachment($messageId)
  2082. {
  2083. $messageId = (int) $messageId;
  2084. if (empty($messageId)) {
  2085. return false;
  2086. }
  2087. $table = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
  2088. $conditions = [
  2089. 'where' => [
  2090. 'id = ?' => $messageId,
  2091. ],
  2092. ];
  2093. $result = Database::select(
  2094. '*',
  2095. $table,
  2096. $conditions,
  2097. 'first'
  2098. );
  2099. if (!empty($result)) {
  2100. return $result;
  2101. }
  2102. return false;
  2103. }
  2104. /**
  2105. * @param string $url
  2106. *
  2107. * @return FormValidator
  2108. */
  2109. public static function getSearchForm($url)
  2110. {
  2111. $form = new FormValidator(
  2112. 'search',
  2113. 'post',
  2114. $url,
  2115. null,
  2116. [],
  2117. FormValidator::LAYOUT_INLINE
  2118. );
  2119. $form->addElement(
  2120. 'text',
  2121. 'keyword',
  2122. false,
  2123. [
  2124. 'aria-label' => get_lang('Search'),
  2125. ]
  2126. );
  2127. $form->addButtonSearch(get_lang('Search'));
  2128. return $form;
  2129. }
  2130. /**
  2131. * Send a notification to all admins when a new user is registered.
  2132. *
  2133. * @param User $user
  2134. */
  2135. public static function sendNotificationByRegisteredUser(User $user)
  2136. {
  2137. $tplMailBody = new Template(
  2138. null,
  2139. false,
  2140. false,
  2141. false,
  2142. false,
  2143. false,
  2144. false
  2145. );
  2146. $tplMailBody->assign('user', $user);
  2147. $tplMailBody->assign('is_western_name_order', api_is_western_name_order());
  2148. $tplMailBody->assign(
  2149. 'manageUrl',
  2150. api_get_path(WEB_CODE_PATH).'admin/user_edit.php?user_id='.$user->getId()
  2151. );
  2152. $layoutContent = $tplMailBody->get_template('mail/new_user_mail_to_admin.tpl');
  2153. $emailsubject = '['.get_lang('UserRegistered').'] '.$user->getUsername();
  2154. $emailbody = $tplMailBody->fetch($layoutContent);
  2155. $admins = UserManager::get_all_administrators();
  2156. foreach ($admins as $admin_info) {
  2157. self::send_message(
  2158. $admin_info['user_id'],
  2159. $emailsubject,
  2160. $emailbody,
  2161. [],
  2162. [],
  2163. null,
  2164. null,
  2165. null,
  2166. null,
  2167. $user->getId()
  2168. );
  2169. }
  2170. }
  2171. /**
  2172. * Get the error log from failed mailing
  2173. * This assumes a complex setup where you have a cron script regularly copying the mail queue log
  2174. * into app/cache/mail/mailq.
  2175. * This can be done with a cron command like (check the location of your mail log file first):.
  2176. *
  2177. * @example 0,30 * * * * root cp /var/log/exim4/mainlog /var/www/chamilo/app/cache/mail/mailq
  2178. *
  2179. * @return array|bool
  2180. */
  2181. public static function failedSentMailErrors()
  2182. {
  2183. $base = api_get_path(SYS_ARCHIVE_PATH).'mail/';
  2184. $mailq = $base.'mailq';
  2185. if (!file_exists($mailq) || !is_readable($mailq)) {
  2186. return false;
  2187. }
  2188. $file = fopen($mailq, 'r');
  2189. $i = 1;
  2190. while (!feof($file)) {
  2191. $line = fgets($file);
  2192. // $line = trim($line);
  2193. if (trim($line) == '') {
  2194. continue;
  2195. }
  2196. // Get the mail code, something like 1WBumL-0002xg-FF
  2197. if (preg_match('/(.*)\s((.*)-(.*)-(.*))\s<(.*)$/', $line, $codeMatches)) {
  2198. $mail_queue[$i]['code'] = $codeMatches[2];
  2199. }
  2200. $fullMail = $base.$mail_queue[$i]['code'];
  2201. $mailFile = fopen($fullMail, 'r');
  2202. // Get the reason of mail fail
  2203. $iX = 1;
  2204. while (!feof($mailFile)) {
  2205. $mailLine = fgets($mailFile);
  2206. //if ($iX == 4 && preg_match('/(.*):\s(.*)$/', $mailLine, $matches)) {
  2207. if ($iX == 2 &&
  2208. preg_match('/(.*)(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\s(.*)/', $mailLine, $detailsMatches)
  2209. ) {
  2210. $mail_queue[$i]['reason'] = $detailsMatches[3];
  2211. }
  2212. $iX++;
  2213. }
  2214. fclose($mailFile);
  2215. // Get the time of mail fail
  2216. if (preg_match('/^\s?(\d+)(\D+)\s+(.*)$/', $line, $timeMatches)) {
  2217. $mail_queue[$i]['time'] = $timeMatches[1].$timeMatches[2];
  2218. } elseif (preg_match('/^(\s+)((.*)@(.*))\s+(.*)$/', $line, $emailMatches)) {
  2219. $mail_queue[$i]['mail'] = $emailMatches[2];
  2220. $i++;
  2221. }
  2222. }
  2223. fclose($file);
  2224. return array_reverse($mail_queue);
  2225. }
  2226. /**
  2227. * @param int $userId
  2228. *
  2229. * @return array
  2230. */
  2231. public static function getUsersThatHadConversationWithUser($userId)
  2232. {
  2233. $messagesTable = Database::get_main_table(TABLE_MESSAGE);
  2234. $userId = (int) $userId;
  2235. $sql = "SELECT DISTINCT
  2236. user_sender_id
  2237. FROM $messagesTable
  2238. WHERE
  2239. user_receiver_id = ".$userId;
  2240. $result = Database::query($sql);
  2241. $users = Database::store_result($result);
  2242. $userList = [];
  2243. foreach ($users as $userData) {
  2244. $userId = $userData['user_sender_id'];
  2245. if (empty($userId)) {
  2246. continue;
  2247. }
  2248. $userInfo = api_get_user_info($userId);
  2249. if ($userInfo) {
  2250. $userList[$userId] = $userInfo;
  2251. }
  2252. }
  2253. return $userList;
  2254. }
  2255. /**
  2256. * @param int $userId
  2257. * @param int $otherUserId
  2258. *
  2259. * @return array
  2260. */
  2261. public static function getAllMessagesBetweenStudents($userId, $otherUserId)
  2262. {
  2263. $messagesTable = Database::get_main_table(TABLE_MESSAGE);
  2264. $userId = (int) $userId;
  2265. $otherUserId = (int) $otherUserId;
  2266. if (empty($otherUserId) || empty($userId)) {
  2267. return [];
  2268. }
  2269. $sql = "SELECT DISTINCT *
  2270. FROM $messagesTable
  2271. WHERE
  2272. (user_receiver_id = $userId AND user_sender_id = $otherUserId) OR
  2273. (user_receiver_id = $otherUserId AND user_sender_id = $userId)
  2274. ORDER BY send_date DESC
  2275. ";
  2276. $result = Database::query($sql);
  2277. $messages = Database::store_result($result);
  2278. $list = [];
  2279. foreach ($messages as $message) {
  2280. $list[] = $message;
  2281. }
  2282. return $list;
  2283. }
  2284. /**
  2285. * @param string $subject
  2286. * @param string $message
  2287. * @param array $courseInfo
  2288. * @param int $sessionId
  2289. *
  2290. * @return bool
  2291. */
  2292. public static function sendMessageToAllUsersInCourse($subject, $message, $courseInfo, $sessionId = 0)
  2293. {
  2294. if (empty($courseInfo)) {
  2295. return false;
  2296. }
  2297. $senderId = api_get_user_id();
  2298. if (empty($senderId)) {
  2299. return false;
  2300. }
  2301. if (empty($sessionId)) {
  2302. // Course students and teachers
  2303. $users = CourseManager::get_user_list_from_course_code($courseInfo['code']);
  2304. } else {
  2305. // Course-session students and course session coaches
  2306. $users = CourseManager::get_user_list_from_course_code($courseInfo['code'], $sessionId);
  2307. }
  2308. if (empty($users)) {
  2309. return false;
  2310. }
  2311. foreach ($users as $userInfo) {
  2312. self::send_message_simple(
  2313. $userInfo['user_id'],
  2314. $subject,
  2315. $message,
  2316. $senderId,
  2317. false,
  2318. false,
  2319. [],
  2320. false
  2321. );
  2322. }
  2323. }
  2324. /**
  2325. * Clean audio messages already added in the message tool.
  2326. */
  2327. public static function cleanAudioMessage()
  2328. {
  2329. $audioId = Session::read('current_audio_id');
  2330. if (!empty($audioId)) {
  2331. api_remove_uploaded_file_by_id('audio_message', api_get_user_id(), $audioId);
  2332. Session::erase('current_audio_id');
  2333. }
  2334. }
  2335. /**
  2336. * @param int $senderId
  2337. * @param string $subject
  2338. * @param string $message
  2339. */
  2340. public static function sendMessageToAllAdminUsers(
  2341. $senderId,
  2342. $subject,
  2343. $message
  2344. ) {
  2345. $admins = UserManager::get_all_administrators();
  2346. foreach ($admins as $admin) {
  2347. self::send_message_simple($admin['user_id'], $subject, $message, $senderId);
  2348. }
  2349. }
  2350. /**
  2351. * @param int $messageId
  2352. * @param int $userId
  2353. *
  2354. * @return array
  2355. */
  2356. public static function countLikesAndDislikes($messageId, $userId)
  2357. {
  2358. if (!api_get_configuration_value('social_enable_messages_feedback')) {
  2359. return [];
  2360. }
  2361. $messageId = (int) $messageId;
  2362. $userId = (int) $userId;
  2363. $em = Database::getManager();
  2364. $query = $em
  2365. ->createQuery('
  2366. SELECT SUM(l.liked) AS likes, SUM(l.disliked) AS dislikes FROM ChamiloCoreBundle:MessageFeedback l
  2367. WHERE l.message = :message
  2368. ')
  2369. ->setParameters(['message' => $messageId]);
  2370. try {
  2371. $counts = $query->getSingleResult();
  2372. } catch (Exception $e) {
  2373. $counts = ['likes' => 0, 'dislikes' => 0];
  2374. }
  2375. $userLike = $em
  2376. ->getRepository('ChamiloCoreBundle:MessageFeedback')
  2377. ->findOneBy(['message' => $messageId, 'user' => $userId]);
  2378. return [
  2379. 'likes' => (int) $counts['likes'],
  2380. 'dislikes' => (int) $counts['dislikes'],
  2381. 'user_liked' => $userLike ? $userLike->isLiked() : false,
  2382. 'user_disliked' => $userLike ? $userLike->isDisliked() : false,
  2383. ];
  2384. }
  2385. /**
  2386. * @param int $messageId
  2387. * @param int $userId
  2388. * @param int $groupId Optional.
  2389. *
  2390. * @return string
  2391. */
  2392. public static function getLikesButton($messageId, $userId, $groupId = 0)
  2393. {
  2394. if (!api_get_configuration_value('social_enable_messages_feedback')) {
  2395. return '';
  2396. }
  2397. $countLikes = self::countLikesAndDislikes($messageId, $userId);
  2398. $btnLike = Display::button(
  2399. 'like',
  2400. Display::returnFontAwesomeIcon('thumbs-up', '', true)
  2401. .PHP_EOL.'<span>'.$countLikes['likes'].'</span>',
  2402. [
  2403. 'title' => get_lang('VoteLike'),
  2404. 'class' => 'btn btn-default social-like '.($countLikes['user_liked'] ? 'disabled' : ''),
  2405. 'data-status' => 'like',
  2406. 'data-message' => $messageId,
  2407. 'data-group' => $groupId,
  2408. ]
  2409. );
  2410. $btnDislike = Display::button(
  2411. 'like',
  2412. Display::returnFontAwesomeIcon('thumbs-down', '', true)
  2413. .PHP_EOL.'<span>'.$countLikes['dislikes'].'</span>',
  2414. [
  2415. 'title' => get_lang('VoteDislike'),
  2416. 'class' => 'btn btn-default social-like '.($countLikes['user_disliked'] ? 'disabled' : ''),
  2417. 'data-status' => 'dislike',
  2418. 'data-message' => $messageId,
  2419. 'data-group' => $groupId,
  2420. ]
  2421. );
  2422. return $btnLike.PHP_EOL.$btnDislike;
  2423. }
  2424. /**
  2425. * Execute the SQL necessary to know the number of messages in the database.
  2426. *
  2427. * @param int $userId The user for which we need the unread messages count
  2428. *
  2429. * @return int The number of unread messages in the database for the given user
  2430. */
  2431. public static function getCountNewMessagesFromDB($userId)
  2432. {
  2433. $userId = (int) $userId;
  2434. if (empty($userId)) {
  2435. return 0;
  2436. }
  2437. $table = Database::get_main_table(TABLE_MESSAGE);
  2438. $sql = "SELECT COUNT(id) as count
  2439. FROM $table
  2440. WHERE
  2441. user_receiver_id=".$userId." AND
  2442. msg_status = ".MESSAGE_STATUS_UNREAD;
  2443. $result = Database::query($sql);
  2444. $row = Database::fetch_assoc($result);
  2445. return $row['count'];
  2446. }
  2447. }