message.lib.php 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once api_get_path(LIBRARY_PATH).'online.inc.php';
  4. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  5. define('MESSAGE_STATUS_NEW', '0');
  6. define('MESSAGE_STATUS_UNREAD', '1');
  7. //2 ??
  8. define('MESSAGE_STATUS_DELETED', '3');
  9. define('MESSAGE_STATUS_OUTBOX', '4');
  10. define('MESSAGE_STATUS_INVITATION_PENDING', '5');
  11. define('MESSAGE_STATUS_INVITATION_ACCEPTED', '6');
  12. define('MESSAGE_STATUS_INVITATION_DENIED', '7');
  13. define('MESSAGE_STATUS_WALL', '8');
  14. define('MESSAGE_STATUS_WALL_DELETE', '9');
  15. define('MESSAGE_STATUS_WALL_POST', '10');
  16. // Images
  17. define('IMAGE_WALL_SMALL_SIZE', 200);
  18. define('IMAGE_WALL_MEDIUM_SIZE', 500);
  19. define('IMAGE_WALL_BIG_SIZE', 2000);
  20. define('IMAGE_WALL_SMALL', 'small');
  21. define('IMAGE_WALL_MEDIUM', 'medium');
  22. define('IMAGE_WALL_BIG', 'big');
  23. /**
  24. * Class MessageManager
  25. *
  26. * This class provides methods for messages management.
  27. * Include/require it in your code to use its features.
  28. *
  29. * @package chamilo.library
  30. */
  31. class MessageManager
  32. {
  33. /**
  34. * @param int $current_user_id
  35. * @return array
  36. */
  37. public static function get_online_user_list($current_user_id)
  38. {
  39. //@todo this is a bad idea to parse all users online
  40. $count = who_is_online_count();
  41. $userlist = who_is_online(0, $count, null, null, 30, true);
  42. $online_user_list = array();
  43. foreach ($userlist as $user_id) {
  44. $online_user_list[$user_id] = GetFullUserName($user_id).($current_user_id == $user_id ? ("&nbsp;(".get_lang('Myself').")") : (""));
  45. }
  46. return $online_user_list;
  47. }
  48. /**
  49. * Displays info stating that the message is sent successfully.
  50. * @deprecated
  51. */
  52. public static function display_success_message($uid)
  53. {
  54. if (isset($_SESSION['social_exist']) &&
  55. $_SESSION['social_exist'] === true
  56. ) {
  57. if (api_get_setting('allow_social_tool') == 'true' && api_get_setting('allow_message_tool') == 'true') {
  58. $success = get_lang('MessageSentTo').
  59. "&nbsp;<b>".
  60. GetFullUserName($uid).
  61. "</b>";
  62. } else {
  63. $success = get_lang('MessageSentTo').
  64. "&nbsp;<b>".
  65. GetFullUserName($uid).
  66. "</b>";
  67. }
  68. } else {
  69. $success = get_lang('MessageSentTo').
  70. "&nbsp;<b>".
  71. GetFullUserName($uid).
  72. "</b>";
  73. }
  74. return Display::return_message(api_xml_http_response_encode($success), 'confirmation', false);
  75. }
  76. /**
  77. * Displays the wysiwyg html editor.
  78. * @deprecated
  79. */
  80. public static function display_html_editor_area($name, $resp)
  81. {
  82. api_disp_html_area($name, get_lang('TypeYourMessage'), '', '', null, array('ToolbarSet' => 'Messages', 'Width' => '95%', 'Height' => '250'));
  83. }
  84. /**
  85. * Get the new messages for the current user from the database.
  86. */
  87. public static function get_new_messages()
  88. {
  89. $table_message = Database::get_main_table(TABLE_MESSAGE);
  90. if (!api_get_user_id()) {
  91. return false;
  92. }
  93. $sql = "SELECT * FROM $table_message
  94. WHERE user_receiver_id=".api_get_user_id()." AND msg_status=".MESSAGE_STATUS_UNREAD;
  95. $result = Database::query($sql);
  96. $i = Database::num_rows($result);
  97. return $i;
  98. }
  99. /**
  100. * Get the list of user_ids of users who are online.
  101. */
  102. public static function users_connected_by_id()
  103. {
  104. $count = who_is_online_count();
  105. $user_connect = who_is_online(0, $count, null, null, 30, true);
  106. $user_id_list = array();
  107. for ($i = 0; $i < count($user_connect); $i++) {
  108. $user_id_list[$i] = $user_connect[$i][0];
  109. }
  110. return $user_id_list;
  111. }
  112. /**
  113. * Gets the total number of messages, used for the inbox sortable table
  114. */
  115. public static function get_number_of_messages($unread = false)
  116. {
  117. $table_message = Database::get_main_table(TABLE_MESSAGE);
  118. if ($unread) {
  119. $condition_msg_status = ' msg_status = '.MESSAGE_STATUS_UNREAD.' ';
  120. } else {
  121. $condition_msg_status = ' msg_status IN('.MESSAGE_STATUS_NEW.','.MESSAGE_STATUS_UNREAD.') ';
  122. }
  123. $sql = "SELECT COUNT(*) as number_messages FROM $table_message
  124. WHERE $condition_msg_status AND user_receiver_id=".api_get_user_id();
  125. $sql_result = Database::query($sql);
  126. $result = Database::fetch_array($sql_result);
  127. return $result['number_messages'];
  128. }
  129. /**
  130. * Gets information about some messages, used for the inbox sortable table
  131. * @param int $from
  132. * @param int $number_of_items
  133. * @param string $direction
  134. */
  135. public static function get_message_data($from, $number_of_items, $column, $direction)
  136. {
  137. $from = intval($from);
  138. $number_of_items = intval($number_of_items);
  139. //forcing this order
  140. if (!isset($direction)) {
  141. $column = 3;
  142. $direction = 'DESC';
  143. } else {
  144. $column = intval($column);
  145. if (!in_array($direction, array('ASC', 'DESC')))
  146. $direction = 'ASC';
  147. }
  148. $table_message = Database::get_main_table(TABLE_MESSAGE);
  149. $sql = "SELECT id as col0, user_sender_id as col1, title as col2, send_date as col3, msg_status as col4
  150. FROM $table_message
  151. WHERE user_receiver_id=".api_get_user_id()." AND msg_status IN (0,1)
  152. ORDER BY col$column $direction
  153. LIMIT $from,$number_of_items";
  154. $sql_result = Database::query($sql);
  155. $i = 0;
  156. $message_list = array();
  157. while ($result = Database::fetch_row($sql_result)) {
  158. $message[0] = $result[0];
  159. $result[2] = Security::remove_XSS($result[2], STUDENT, true);
  160. $result[2] = cut($result[2], 80, true);
  161. if ($result[4] == 1) {
  162. $class = 'class = "unread"';
  163. } else {
  164. $class = 'class = "read"';
  165. }
  166. $link = '';
  167. if (isset($_GET['f']) && $_GET['f'] == 'social') {
  168. $link = '&f=social';
  169. }
  170. $message[1] = '<a '.$class.' href="view_message.php?id='.$result[0].$link.'">'.$result[2].'</a><br />'.GetFullUserName(($result[1]));
  171. $message[3] = '<a href="new_message.php?re_id='.$result[0].$link.'">'.Display::return_icon('message_reply.png', get_lang('ReplyToMessage')).'</a>'.
  172. '&nbsp;&nbsp;<a onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang('ConfirmDeleteMessage')))."'".')) return false;" href="inbox.php?action=deleteone&id='.$result[0].$link.'">'.Display::return_icon('delete.png', get_lang('DeleteMessage')).'</a>';
  173. $message[2] = api_convert_and_format_date($result[3], DATE_TIME_FORMAT_LONG); //date stays the same
  174. foreach ($message as $key => $value) {
  175. $message[$key] = api_xml_http_response_encode($value);
  176. }
  177. $message_list[] = $message;
  178. $i++;
  179. }
  180. return $message_list;
  181. }
  182. /**
  183. * Sends a message to a user/group
  184. *
  185. * @param int $receiver_user_id
  186. * @param string $subject
  187. * @param string $content
  188. * @param array $file_attachments files array($_FILES) (optional)
  189. * @param array $file_comments about attachment files (optional)
  190. * @param int $group_id (optional)
  191. * @param int $parent_id (optional)
  192. * @param int $edit_message_id id for updating the message (optional)
  193. * @param int $topic_id (optional) the default value is the current user_id
  194. * @param int $sender_id
  195. * @return bool
  196. */
  197. public static function send_message(
  198. $receiver_user_id,
  199. $subject,
  200. $content,
  201. $file_attachments = array(),
  202. $file_comments = array(),
  203. $group_id = 0,
  204. $parent_id = 0,
  205. $edit_message_id = 0,
  206. $topic_id = 0,
  207. $sender_id = null
  208. ) {
  209. $table_message = Database::get_main_table(TABLE_MESSAGE);
  210. $group_id = intval($group_id);
  211. $receiver_user_id = intval($receiver_user_id);
  212. $parent_id = intval($parent_id);
  213. $edit_message_id = intval($edit_message_id);
  214. $topic_id = intval($topic_id);
  215. if (!empty($receiver_user_id)) {
  216. $receiverUserInfo = api_get_user_info($receiver_user_id);
  217. // Disabling messages for inactive users.
  218. if ($receiverUserInfo['active'] == 0) {
  219. return false;
  220. }
  221. }
  222. if (empty($sender_id)) {
  223. $user_sender_id = api_get_user_id();
  224. } else {
  225. $user_sender_id = intval($sender_id);
  226. }
  227. $total_filesize = 0;
  228. if (is_array($file_attachments)) {
  229. foreach ($file_attachments as $file_attach) {
  230. $total_filesize += $file_attach['size'];
  231. }
  232. }
  233. // Validating fields
  234. if (empty($subject) && empty($group_id)) {
  235. return get_lang('YouShouldWriteASubject');
  236. } else if ($total_filesize > intval(api_get_setting('message_max_upload_filesize'))) {
  237. return sprintf(
  238. get_lang("FilesSizeExceedsX"),
  239. format_file_size(api_get_setting('message_max_upload_filesize'))
  240. );
  241. }
  242. $inbox_last_id = null;
  243. //Just in case we replace the and \n and \n\r while saving in the DB
  244. $content = str_replace(array("\n", "\n\r"), '<br />', $content);
  245. $now = api_get_utc_datetime();
  246. if (!empty($receiver_user_id) || !empty($group_id)) {
  247. // message for user friend
  248. $clean_subject = Database::escape_string($subject);
  249. $clean_content = Database::escape_string($content);
  250. //message in inbox for user friend
  251. //@todo it's possible to edit a message? yes, only for groups
  252. if ($edit_message_id) {
  253. $query = " UPDATE $table_message SET
  254. update_date = '".$now."',
  255. content = '$clean_content'
  256. WHERE id = '$edit_message_id' ";
  257. Database::query($query);
  258. $inbox_last_id = $edit_message_id;
  259. } else {
  260. $query = "INSERT INTO $table_message (user_sender_id, user_receiver_id, msg_status, send_date, title, content, group_id, parent_id, update_date ) ".
  261. "VALUES ('$user_sender_id', '$receiver_user_id', '1', '".$now."','$clean_subject','$clean_content','$group_id','$parent_id', '".$now."')";
  262. Database::query($query);
  263. $inbox_last_id = Database::insert_id();
  264. }
  265. // Save attachment file for inbox messages
  266. if (is_array($file_attachments)) {
  267. $i = 0;
  268. foreach ($file_attachments as $file_attach) {
  269. if ($file_attach['error'] == 0) {
  270. self::save_message_attachment_file(
  271. $file_attach,
  272. $file_comments[$i],
  273. $inbox_last_id,
  274. null,
  275. $receiver_user_id,
  276. $group_id
  277. );
  278. }
  279. $i++;
  280. }
  281. }
  282. if (empty($group_id)) {
  283. //message in outbox for user friend or group
  284. $sql = "INSERT INTO $table_message (user_sender_id, user_receiver_id, msg_status, send_date, title, content, group_id, parent_id, update_date ) ".
  285. " VALUES ('$user_sender_id', '$receiver_user_id', '4', '".$now."','$clean_subject','$clean_content', '$group_id', '$parent_id', '".$now."')";
  286. Database::query($sql);
  287. $outbox_last_id = Database::insert_id();
  288. // save attachment file for outbox messages
  289. if (is_array($file_attachments)) {
  290. $o = 0;
  291. foreach ($file_attachments as $file_attach) {
  292. if ($file_attach['error'] == 0) {
  293. self::save_message_attachment_file(
  294. $file_attach,
  295. $file_comments[$o],
  296. $outbox_last_id,
  297. $user_sender_id
  298. );
  299. }
  300. $o++;
  301. }
  302. }
  303. }
  304. // Load user settings.
  305. $notification = new Notification();
  306. $sender_info = api_get_user_info($user_sender_id);
  307. if (empty($group_id)) {
  308. $notification->save_notification(
  309. Notification::NOTIFICATION_TYPE_MESSAGE,
  310. array($receiver_user_id),
  311. $subject,
  312. $content,
  313. $sender_info
  314. );
  315. } else {
  316. $group_info = GroupPortalManager::get_group_data($group_id);
  317. $group_info['topic_id'] = $topic_id;
  318. $group_info['msg_id'] = $inbox_last_id;
  319. $user_list = GroupPortalManager::get_users_by_group($group_id, false, array(), 0, 1000);
  320. // Adding more sense to the message group
  321. $subject = sprintf(get_lang('ThereIsANewMessageInTheGroupX'), $group_info['name']);
  322. $new_user_list = array();
  323. foreach ($user_list as $user_data) {
  324. $new_user_list[] = $user_data['user_id'];
  325. }
  326. $group_info = array('group_info' => $group_info, 'user_info' => $sender_info);
  327. $notification->save_notification(
  328. Notification::NOTIFICATION_TYPE_GROUP,
  329. $new_user_list,
  330. $subject,
  331. $content,
  332. $group_info
  333. );
  334. }
  335. return $inbox_last_id;
  336. }
  337. return false;
  338. }
  339. /**
  340. * A handy way to send message
  341. */
  342. public static function send_message_simple(
  343. $receiver_user_id,
  344. $subject,
  345. $message,
  346. $sender_id = null
  347. ) {
  348. return MessageManager::send_message(
  349. $receiver_user_id,
  350. $subject,
  351. $message,
  352. null,
  353. null,
  354. null,
  355. null,
  356. null,
  357. null,
  358. $sender_id
  359. );
  360. }
  361. /**
  362. * Update parent ids for other receiver user from current message in groups
  363. * @author Christian Fasanando Flores
  364. * @param int $parent_id
  365. * @param int $receiver_user_id
  366. * @param int $message_id
  367. * @return void
  368. */
  369. public static function update_parent_ids_from_reply($parent_id, $receiver_user_id, $message_id)
  370. {
  371. $table_message = Database::get_main_table(TABLE_MESSAGE);
  372. $parent_id = intval($parent_id);
  373. $receiver_user_id = intval($receiver_user_id);
  374. $message_id = intval($message_id);
  375. // first get data from message id (parent)
  376. $sql_message = "SELECT * FROM $table_message WHERE id = '$parent_id'";
  377. $rs_message = Database::query($sql_message);
  378. $row_message = Database::fetch_array($rs_message);
  379. // get message id from data found early for other receiver user
  380. $sql = "SELECT id FROM $table_message
  381. WHERE
  382. user_sender_id ='{$row_message['user_sender_id']}' AND
  383. title='{$row_message['title']}' AND
  384. content='{$row_message['content']}' AND
  385. group_id='{$row_message['group_id']}' AND
  386. user_receiver_id='$receiver_user_id'";
  387. $rs_msg_id = Database::query($sql);
  388. $row = Database::fetch_array($rs_msg_id);
  389. // update parent_id for other user receiver
  390. $sql = "UPDATE $table_message SET parent_id = '{$row[id]}'
  391. WHERE id = '$message_id'";
  392. Database::query($sql);
  393. }
  394. /**
  395. * @param int $user_receiver_id
  396. * @param int $id
  397. * @return bool|resource
  398. */
  399. public static function delete_message_by_user_receiver($user_receiver_id, $id)
  400. {
  401. $table_message = Database::get_main_table(TABLE_MESSAGE);
  402. if ($id != strval(intval($id)))
  403. return false;
  404. $user_receiver_id = intval($user_receiver_id);
  405. $id = intval($id);
  406. $sql = "SELECT * FROM $table_message WHERE id=".$id." AND msg_status<>4;";
  407. $rs = Database::query($sql);
  408. if (Database::num_rows($rs) > 0) {
  409. // delete attachment file
  410. self::delete_message_attachment_file($id, $user_receiver_id);
  411. // delete message
  412. $query = "UPDATE $table_message SET msg_status=3
  413. WHERE user_receiver_id=".$user_receiver_id." AND id=".$id;
  414. $result = Database::query($query);
  415. return $result;
  416. } else {
  417. return false;
  418. }
  419. }
  420. /**
  421. * Set status deleted
  422. * @author Isaac FLores Paz <isaac.flores@dokeos.com>
  423. * @param integer
  424. * @param integer
  425. * @return array
  426. */
  427. public static function delete_message_by_user_sender($user_sender_id, $id)
  428. {
  429. if ($id != strval(intval($id))) {
  430. return false;
  431. }
  432. $table_message = Database::get_main_table(TABLE_MESSAGE);
  433. $id = intval($id);
  434. $user_sender_id = intval($user_sender_id);
  435. $sql = "SELECT * FROM $table_message WHERE id='$id'";
  436. $rs = Database::query($sql);
  437. if (Database::num_rows($rs) > 0) {
  438. // delete attachment file
  439. self::delete_message_attachment_file($id, $user_sender_id);
  440. // delete message
  441. $query = "UPDATE $table_message SET msg_status=3
  442. WHERE user_sender_id='$user_sender_id' AND id='$id'";
  443. $result = Database::query($query);
  444. return $result;
  445. }
  446. return false;
  447. }
  448. /**
  449. * Saves a message attachment files
  450. * @param array $_FILES['name']
  451. * @param string a comment about the uploaded file
  452. * @param int message id
  453. * @param int receiver user id (optional)
  454. * @param int sender user id (optional)
  455. * @param int group id (optional)
  456. * @return void
  457. */
  458. public static function save_message_attachment_file(
  459. $file_attach,
  460. $file_comment,
  461. $message_id,
  462. $receiver_user_id = 0,
  463. $sender_user_id = 0,
  464. $group_id = 0
  465. ) {
  466. $tbl_message_attach = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
  467. // Try to add an extension to the file if it hasn't one
  468. $new_file_name = add_ext_on_mime(stripslashes($file_attach['name']), $file_attach['type']);
  469. // user's file name
  470. $file_name = $file_attach['name'];
  471. if (!filter_extension($new_file_name)) {
  472. Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  473. } else {
  474. $new_file_name = uniqid('');
  475. if (!empty($receiver_user_id)) {
  476. $message_user_id = $receiver_user_id;
  477. } else {
  478. $message_user_id = $sender_user_id;
  479. }
  480. // User-reserved directory where photos have to be placed.
  481. if (!empty($group_id)) {
  482. $path_user_info = GroupPortalManager::get_group_picture_path_by_id($group_id, 'system', true);
  483. } else {
  484. $path_user_info = UserManager::get_user_picture_path_by_id($message_user_id, 'system', true);
  485. }
  486. $path_message_attach = $path_user_info['dir'].'message_attachments/';
  487. // If this directory does not exist - we create it.
  488. if (!file_exists($path_message_attach)) {
  489. @mkdir($path_message_attach, api_get_permissions_for_new_directories(), true);
  490. }
  491. $new_path = $path_message_attach.$new_file_name;
  492. if (is_uploaded_file($file_attach['tmp_name'])) {
  493. @copy($file_attach['tmp_name'], $new_path);
  494. }
  495. $safe_file_comment = Database::escape_string($file_comment);
  496. $safe_file_name = Database::escape_string($file_name);
  497. $safe_new_file_name = Database::escape_string($new_file_name);
  498. // Storing the attachments if any
  499. $sql = "INSERT INTO $tbl_message_attach(filename,comment, path,message_id,size)
  500. VALUES ('$safe_file_name', '$safe_file_comment', '$safe_new_file_name' , '$message_id', '".$file_attach['size']."' )";
  501. Database::query($sql);
  502. }
  503. }
  504. /**
  505. * Delete message attachment files (logically updating the row with a suffix _DELETE_id)
  506. * @param int message id
  507. * @param int message user id (receiver user id or sender user id)
  508. * @param int group id (optional)
  509. * @return void
  510. */
  511. public static function delete_message_attachment_file($message_id, $message_uid, $group_id = 0)
  512. {
  513. $message_id = intval($message_id);
  514. $message_uid = intval($message_uid);
  515. $table_message_attach = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
  516. $sql = "SELECT * FROM $table_message_attach WHERE message_id = '$message_id'";
  517. $rs = Database::query($sql);
  518. while ($row = Database::fetch_array($rs)) {
  519. $path = $row['path'];
  520. $attach_id = $row['id'];
  521. $new_path = $path.'_DELETED_'.$attach_id;
  522. if (!empty($group_id)) {
  523. $path_user_info = GroupPortalManager::get_group_picture_path_by_id($group_id, 'system', true);
  524. } else {
  525. $path_user_info = UserManager::get_user_picture_path_by_id($message_uid, 'system', true);
  526. }
  527. $path_message_attach = $path_user_info['dir'].'message_attachments/';
  528. if (is_file($path_message_attach.$path)) {
  529. if (rename($path_message_attach.$path, $path_message_attach.$new_path)) {
  530. $sql_upd = "UPDATE $table_message_attach set path='$new_path' WHERE id ='$attach_id'";
  531. Database::query($sql_upd);
  532. }
  533. }
  534. }
  535. }
  536. /**
  537. * update messages by user id and message id
  538. * @param int user id
  539. * @param int message id
  540. * @return resource
  541. */
  542. public static function update_message($user_id, $message_id)
  543. {
  544. if ($message_id != strval(intval($message_id)) || $user_id != strval(intval($user_id)))
  545. return false;
  546. $table_message = Database::get_main_table(TABLE_MESSAGE);
  547. $sql = "UPDATE $table_message SET msg_status = '0'
  548. WHERE
  549. msg_status<>4 AND
  550. user_receiver_id=".intval($user_id)." AND
  551. id='".intval($message_id)."'";
  552. Database::query($sql);
  553. }
  554. /**
  555. * @param int $user_id
  556. * @param int $message_id
  557. * @param string $type
  558. * @return bool
  559. */
  560. public static function update_message_status($user_id, $message_id,$type)
  561. {
  562. $type = intval($type);
  563. if ($message_id != strval(intval($message_id)) || $user_id != strval(intval($user_id))) {
  564. return false;
  565. }
  566. $table_message = Database::get_main_table(TABLE_MESSAGE);
  567. $sql = "UPDATE $table_message SET msg_status = '$type'
  568. WHERE user_receiver_id=".intval($user_id)." AND id='".intval($message_id)."'";
  569. Database::query($sql);
  570. }
  571. /**
  572. * get messages by user id and message id
  573. * @param int user id
  574. * @param int message id
  575. * @return array
  576. */
  577. public static function get_message_by_user($user_id, $message_id)
  578. {
  579. if ($message_id != strval(intval($message_id)) || $user_id != strval(intval($user_id)))
  580. return false;
  581. $table_message = Database::get_main_table(TABLE_MESSAGE);
  582. $query = "SELECT * FROM $table_message
  583. WHERE user_receiver_id=".intval($user_id)." AND id='".intval($message_id)."'";
  584. $result = Database::query($query);
  585. return $row = Database::fetch_array($result);
  586. }
  587. /**
  588. * get messages by group id
  589. * @param int group id
  590. * @return array
  591. */
  592. public static function get_messages_by_group($group_id)
  593. {
  594. if ($group_id != strval(intval($group_id)))
  595. return false;
  596. $table_message = Database::get_main_table(TABLE_MESSAGE);
  597. $group_id = intval($group_id);
  598. $query = "SELECT * FROM $table_message
  599. WHERE
  600. group_id= $group_id AND
  601. msg_status NOT IN ('".MESSAGE_STATUS_OUTBOX."', '".MESSAGE_STATUS_DELETED."')
  602. ORDER BY id";
  603. $rs = Database::query($query);
  604. $data = array();
  605. if (Database::num_rows($rs) > 0) {
  606. while ($row = Database::fetch_array($rs, 'ASSOC')) {
  607. $data[] = $row;
  608. }
  609. }
  610. return $data;
  611. }
  612. /**
  613. * get messages by group id
  614. * @param int $group_id
  615. * @param int $message_id
  616. * @return array
  617. */
  618. public static function get_messages_by_group_by_message($group_id, $message_id)
  619. {
  620. if ($group_id != strval(intval($group_id)))
  621. return false;
  622. $table_message = Database::get_main_table(TABLE_MESSAGE);
  623. $group_id = intval($group_id);
  624. $query = "SELECT * FROM $table_message
  625. WHERE
  626. group_id = $group_id AND
  627. msg_status NOT IN ('".MESSAGE_STATUS_OUTBOX."', '".MESSAGE_STATUS_DELETED."')
  628. ORDER BY id ";
  629. $rs = Database::query($query);
  630. $data = array();
  631. $parents = array();
  632. if (Database::num_rows($rs) > 0) {
  633. while ($row = Database::fetch_array($rs, 'ASSOC')) {
  634. if ($message_id == $row['parent_id'] || in_array($row['parent_id'], $parents)) {
  635. $parents[] = $row['id'];
  636. $data[] = $row;
  637. }
  638. }
  639. }
  640. return $data;
  641. }
  642. /**
  643. * get messages by parent id optionally with limit
  644. * @param int parent id
  645. * @param int group id (optional)
  646. * @param int offset (optional)
  647. * @param int limit (optional)
  648. * @return array
  649. */
  650. public static function get_messages_by_parent($parent_id, $group_id = '', $offset = 0, $limit = 0)
  651. {
  652. if ($parent_id != strval(intval($parent_id))) {
  653. return false;
  654. }
  655. $table_message = Database::get_main_table(TABLE_MESSAGE);
  656. $parent_id = intval($parent_id);
  657. $condition_group_id = "";
  658. if ($group_id !== '') {
  659. $group_id = intval($group_id);
  660. $condition_group_id = " AND group_id = '$group_id' ";
  661. }
  662. $condition_limit = "";
  663. if ($offset && $limit) {
  664. $offset = ($offset - 1) * $limit;
  665. $condition_limit = " LIMIT $offset,$limit ";
  666. }
  667. $query = "SELECT * FROM $table_message
  668. WHERE parent_id='$parent_id' AND msg_status <> ".MESSAGE_STATUS_OUTBOX." $condition_group_id
  669. ORDER BY send_date DESC $condition_limit ";
  670. $rs = Database::query($query);
  671. $data = array();
  672. if (Database::num_rows($rs) > 0) {
  673. while ($row = Database::fetch_array($rs)) {
  674. $data[$row['id']] = $row;
  675. }
  676. }
  677. return $data;
  678. }
  679. /**
  680. * Gets information about if exist messages
  681. * @author Isaac FLores Paz <isaac.flores@dokeos.com>
  682. * @param integer
  683. * @param integer
  684. * @return boolean
  685. */
  686. public static function exist_message($user_id, $id)
  687. {
  688. if ($id != strval(intval($id)) || $user_id != strval(intval($user_id)))
  689. return false;
  690. $table_message = Database::get_main_table(TABLE_MESSAGE);
  691. $query = "SELECT id FROM $table_message
  692. WHERE
  693. user_receiver_id = ".intval($user_id)." AND
  694. id = '".intval($id)."'";
  695. $result = Database::query($query);
  696. $num = Database::num_rows($result);
  697. if ($num > 0) {
  698. return true;
  699. } else {
  700. return false;
  701. }
  702. }
  703. /**
  704. * Gets information about messages sent
  705. * @param integer
  706. * @param integer
  707. * @param string
  708. * @return array
  709. */
  710. public static function get_message_data_sent($from, $number_of_items, $column, $direction)
  711. {
  712. $from = intval($from);
  713. $number_of_items = intval($number_of_items);
  714. if (!isset($direction)) {
  715. $column = 3;
  716. $direction = 'DESC';
  717. } else {
  718. $column = intval($column);
  719. if (!in_array($direction, array('ASC', 'DESC')))
  720. $direction = 'ASC';
  721. }
  722. $table_message = Database::get_main_table(TABLE_MESSAGE);
  723. $request = api_is_xml_http_request();
  724. $sql = "SELECT
  725. id as col0, user_sender_id as col1, title as col2, send_date as col3, user_receiver_id as col4, msg_status as col5
  726. FROM $table_message
  727. WHERE
  728. user_sender_id=".api_get_user_id()." AND
  729. msg_status=".MESSAGE_STATUS_OUTBOX."
  730. ORDER BY col$column $direction
  731. LIMIT $from, $number_of_items";
  732. $sql_result = Database::query($sql);
  733. $i = 0;
  734. $message_list = array();
  735. while ($result = Database::fetch_row($sql_result)) {
  736. if ($request === true) {
  737. $message[0] = '<input type="checkbox" value='.$result[0].' name="out[]">';
  738. } else {
  739. $message[0] = ($result[0]);
  740. }
  741. $class = 'class = "read"';
  742. $result[2] = Security::remove_XSS($result[2]);
  743. if ($request === true) {
  744. $message[1] = '<a onclick="show_sent_message('.$result[0].')" href="javascript:void(0)">'.GetFullUserName($result[4]).'</a>';
  745. $message[2] = '<a onclick="show_sent_message('.$result[0].')" href="javascript:void(0)">'.str_replace("\\", "", $result[2]).'</a>';
  746. $message[3] = api_convert_and_format_date($result[3], DATE_TIME_FORMAT_LONG); //date stays the same
  747. $message[4] = '&nbsp;&nbsp;<a onclick="delete_one_message_outbox('.$result[0].')" href="javascript:void(0)" >'.Display::return_icon('delete.png', get_lang('DeleteMessage')).'</a>';
  748. } else {
  749. $link = '';
  750. if ($_GET['f'] == 'social') {
  751. $link = '&f=social';
  752. }
  753. $message[1] = '<a '.$class.' onclick="show_sent_message ('.$result[0].')" href="../messages/view_message.php?id_send='.$result[0].$link.'">'.$result[2].'</a><br />'.GetFullUserName($result[4]);
  754. //$message[2] = '<a '.$class.' onclick="show_sent_message ('.$result[0].')" href="../messages/view_message.php?id_send='.$result[0].$link.'">'.$result[2].'</a>';
  755. $message[2] = api_convert_and_format_date($result[3], DATE_TIME_FORMAT_LONG); //date stays the same
  756. $message[3] = '<a href="outbox.php?action=deleteone&id='.$result[0].'&f='.Security::remove_XSS($_GET['f']).'" onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang('ConfirmDeleteMessage')))."'".')) return false;" >'.Display::return_icon('delete.png', get_lang('DeleteMessage')).'</a>';
  757. }
  758. foreach ($message as $key => $value) {
  759. $message[$key] = $value;
  760. }
  761. $message_list[] = $message;
  762. $i++;
  763. }
  764. return $message_list;
  765. }
  766. /**
  767. * Gets information about number messages sent
  768. * @author Isaac FLores Paz <isaac.flores@dokeos.com>
  769. * @param void
  770. * @return integer
  771. */
  772. public static function get_number_of_messages_sent()
  773. {
  774. $table_message = Database::get_main_table(TABLE_MESSAGE);
  775. $sql = "SELECT COUNT(*) as number_messages FROM $table_message
  776. WHERE msg_status=".MESSAGE_STATUS_OUTBOX." AND user_sender_id=".api_get_user_id();
  777. $sql_result = Database::query($sql);
  778. $result = Database::fetch_array($sql_result);
  779. return $result['number_messages'];
  780. }
  781. /**
  782. * display message box in the inbox
  783. * @param int the message id
  784. * @param string inbox or outbox strings are available
  785. * @todo replace numbers with letters in the $row array pff...
  786. * @return string html with the message content
  787. */
  788. public static function show_message_box($message_id, $source = 'inbox')
  789. {
  790. $table_message = Database::get_main_table(TABLE_MESSAGE);
  791. $message_id = intval($message_id);
  792. if ($source == 'outbox') {
  793. if (isset($message_id) && is_numeric($message_id)) {
  794. $query = "SELECT * FROM $table_message
  795. WHERE user_sender_id=".api_get_user_id()." AND id=".$message_id." AND msg_status=4;";
  796. $result = Database::query($query);
  797. $path = 'outbox.php';
  798. }
  799. } else {
  800. if (is_numeric($message_id) && !empty($message_id)) {
  801. $query = "UPDATE $table_message SET msg_status = '".MESSAGE_STATUS_NEW."'
  802. WHERE user_receiver_id=".api_get_user_id()." AND id='".$message_id."';";
  803. Database::query($query);
  804. $query = "SELECT * FROM $table_message
  805. WHERE msg_status<>4 AND user_receiver_id=".api_get_user_id()." AND id='".$message_id."';";
  806. $result = Database::query($query);
  807. }
  808. $path = 'inbox.php';
  809. }
  810. $row = Database::fetch_array($result, 'ASSOC');
  811. $user_sender_id = $row['user_sender_id'];
  812. // get file attachments by message id
  813. $files_attachments = self::get_links_message_attachment_files($message_id, $source);
  814. $user_con = self::users_connected_by_id();
  815. $band = 0;
  816. for ($i = 0; $i < count($user_con); $i++) {
  817. if ($user_sender_id == $user_con[$i]) {
  818. $band = 1;
  819. }
  820. }
  821. $title = Security::remove_XSS($row['title'], STUDENT, true);
  822. $content = Security::remove_XSS($row['content'], STUDENT, true);
  823. $from_user = UserManager::get_user_info_by_id($user_sender_id);
  824. $name = api_get_person_name($from_user['firstname'], $from_user['lastname']);
  825. $user_image = UserManager::get_picture_user($row['user_sender_id'], $from_user['picture_uri'], 80);
  826. $user_image = Display::img($user_image['file'], $name, array('title' => $name));
  827. $message_content = Display::page_subheader(str_replace("\\", "", $title));
  828. if (api_get_setting('allow_social_tool') == 'true') {
  829. $message_content .= $user_image.' ';
  830. }
  831. $message_content .='<tr>';
  832. if (api_get_setting('allow_social_tool') == 'true') {
  833. if ($source == 'outbox') {
  834. $message_content .= get_lang('From').': <a href="'.api_get_path(WEB_PATH).'main/social/profile.php?u='.$user_sender_id.'">'.$name.'</a> '.api_strtolower(get_lang('To')).'&nbsp;<b>'.GetFullUserName($row[2]).'</b>';
  835. } else {
  836. $message_content .= get_lang('From').' <a href="'.api_get_path(WEB_PATH).'main/social/profile.php?u='.$user_sender_id.'">'.$name.'</a> '.api_strtolower(get_lang('To')).'&nbsp;<b>'.get_lang('Me').'</b>';
  837. }
  838. } else {
  839. if ($source == 'outbox') {
  840. $message_content .= get_lang('From').':&nbsp;'.$name.'</b> '.api_strtolower(get_lang('To')).' <b>'.GetFullUserName($row['user_receiver_id']).'</b>';
  841. } else {
  842. $message_content .= get_lang('From').':&nbsp;'.$name.'</b> '.api_strtolower(get_lang('To')).' <b>'.get_lang('Me').'</b>';
  843. }
  844. }
  845. $message_content .=' '.get_lang('Date').': '.api_get_local_time($row['send_date']).'
  846. <br />
  847. <hr style="color:#ddd" />
  848. <table height="209px" width="100%">
  849. <tr>
  850. <td valign=top class="view-message-content">'.str_replace("\\", "", $content).'</td>
  851. </tr>
  852. </table>
  853. <div id="message-attach">'.(!empty($files_attachments) ? implode('<br />', $files_attachments) : '').'</div>
  854. <div style="padding: 15px 0px 5px 0px">';
  855. $social_link = '';
  856. if (isset($_GET['f']) && $_GET['f'] == 'social') {
  857. $social_link = 'f=social';
  858. }
  859. if ($source == 'outbox') {
  860. $message_content .= '<a href="outbox.php?'.$social_link.'">'.Display::return_icon('back.png', get_lang('ReturnToOutbox')).'</a> &nbsp';
  861. } else {
  862. $message_content .= '<a href="inbox.php?'.$social_link.'">'.Display::return_icon('back.png', get_lang('ReturnToInbox')).'</a> &nbsp';
  863. $message_content .= '<a href="new_message.php?re_id='.$message_id.'&'.$social_link.'">'.Display::return_icon('message_reply.png', get_lang('ReplyToMessage')).'</a> &nbsp';
  864. }
  865. $message_content .= '<a href="inbox.php?action=deleteone&id='.$message_id.'&'.$social_link.'" >'.Display::return_icon('delete.png', get_lang('DeleteMessage')).'</a>&nbsp';
  866. $message_content .='</div></td>
  867. <td width=10></td>
  868. </tr>
  869. </table>';
  870. return $message_content;
  871. }
  872. /**
  873. * display message box sent showing it into outbox
  874. * @return void
  875. */
  876. public static function show_message_box_sent()
  877. {
  878. $table_message = Database::get_main_table(TABLE_MESSAGE);
  879. $tbl_message_attach = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
  880. $message_id = '';
  881. if (is_numeric($_GET['id_send'])) {
  882. $query = "SELECT * FROM $table_message
  883. WHERE
  884. user_sender_id=".api_get_user_id()." AND
  885. id=".intval($_GET['id_send'])." AND
  886. msg_status = 4;";
  887. $result = Database::query($query);
  888. $message_id = intval($_GET['id_send']);
  889. }
  890. $path = 'outbox.php';
  891. // get file attachments by message id
  892. $files_attachments = self::get_links_message_attachment_files($message_id, 'outbox');
  893. $row = Database::fetch_array($result);
  894. $user_con = self::users_connected_by_id();
  895. $band = 0;
  896. $reply = '';
  897. for ($i = 0; $i < count($user_con); $i++)
  898. if ($row[1] == $user_con[$i])
  899. $band = 1;
  900. echo '<div class=actions>';
  901. echo '<a onclick="close_and_open_outbox()" href="javascript:void(0)">'.Display::return_icon('folder_up.gif', api_xml_http_response_encode(get_lang('BackToOutbox'))).api_xml_http_response_encode(get_lang('BackToOutbox')).'</a>';
  902. echo '<a onclick="delete_one_message_outbox('.$row[0].')" href="javascript:void(0)" >'.Display::return_icon('delete.png', api_xml_http_response_encode(get_lang('DeleteMessage'))).api_xml_http_response_encode(get_lang('DeleteMessage')).'</a>';
  903. echo '</div><br />';
  904. echo '
  905. <table class="message_view_table" >
  906. <TR>
  907. <TD width=10>&nbsp; </TD>
  908. <TD vAlign=top width="100%">
  909. <TABLE>
  910. <TR>
  911. <TD width="100%">
  912. <TR> <h1>'.str_replace("\\", "", api_xml_http_response_encode($row[5])).'</h1></TR>
  913. </TD>
  914. <TR>
  915. <TD>'.api_xml_http_response_encode(get_lang('From').'&nbsp;<b>'.GetFullUserName($row[1]).'</b> '.api_strtolower(get_lang('To')).'&nbsp; <b>'.GetFullUserName($row[2])).'</b> </TD>
  916. </TR>
  917. <TR>
  918. <TD >'.api_xml_http_response_encode(get_lang('Date').'&nbsp; '.$row[4]).'</TD>
  919. </TR>
  920. </TR>
  921. </TABLE>
  922. <br />
  923. <TABLE height="209px" width="100%" bgColor=#ffffff>
  924. <TBODY>
  925. <TR>
  926. <TD vAlign=top>'.str_replace("\\", "", api_xml_http_response_encode($row[6])).'</TD>
  927. </TR>
  928. </TBODY>
  929. </TABLE>
  930. <div id="message-attach">'.(!empty($files_attachments) ? implode('<br />', $files_attachments) : '').'</div>
  931. <DIV class=HT style="PADDING-BOTTOM: 5px"> </DIV></TD>
  932. <TD width=10>&nbsp;</TD>
  933. </TR>
  934. </TABLE>';
  935. }
  936. /**
  937. * get user id by user email
  938. * @param string $user_email
  939. * @return int user id
  940. */
  941. public static function get_user_id_by_email($user_email)
  942. {
  943. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  944. $sql = 'SELECT user_id FROM '.$tbl_user.'
  945. WHERE email="'.Database::escape_string($user_email).'";';
  946. $rs = Database::query($sql);
  947. $row = Database::fetch_array($rs, 'ASSOC');
  948. if (isset($row['user_id'])) {
  949. return $row['user_id'];
  950. } else {
  951. return null;
  952. }
  953. }
  954. /**
  955. * Displays messages of a group with nested view
  956. *
  957. * @param int $group_id
  958. */
  959. public static function display_messages_for_group($group_id)
  960. {
  961. global $my_group_role;
  962. $rows = self::get_messages_by_group($group_id);
  963. $topics_per_page = 10;
  964. $html_messages = '';
  965. $query_vars = array('id' => $group_id, 'topics_page_nr' => 0);
  966. if (is_array($rows) && count($rows) > 0) {
  967. // prepare array for topics with its items
  968. $topics = array();
  969. $x = 0;
  970. foreach ($rows as $index => $value) {
  971. if (empty($value['parent_id'])) {
  972. $topics[$value['id']] = $value;
  973. }
  974. }
  975. $new_topics = array();
  976. foreach ($topics as $id => $value) {
  977. $rows = null;
  978. $rows = self::get_messages_by_group_by_message($group_id, $value['id']);
  979. if (!empty($rows)) {
  980. $count = count(self::calculate_children($rows, $value['id']));
  981. } else {
  982. $count = 0;
  983. }
  984. $value['count'] = $count;
  985. $new_topics[$id] = $value;
  986. }
  987. //$new_topics = sort_column($new_topics,'count');
  988. $param_names = array_keys($_GET);
  989. $array_html = array();
  990. foreach ($new_topics as $index => $topic) {
  991. $html = '';
  992. // topics
  993. //$indent = 0;
  994. $user_sender_info = UserManager::get_user_info_by_id($topic['user_sender_id']);
  995. //$files_attachments = self::get_links_message_attachment_files($topic['id']);
  996. $name = api_get_person_name($user_sender_info['firstname'], $user_sender_info['lastname']);
  997. $html .= '<div class="row">';
  998. $items = $topic['count'];
  999. $reply_label = ($items == 1) ? get_lang('GroupReply') : get_lang('GroupReplies');
  1000. $html .= '<div class="span1">';
  1001. $html .= Display::div(Display::tag('span', $items).$reply_label, array('class' => 'group_discussions_replies'));
  1002. $html .= '</div>';
  1003. $topic['title'] = trim($topic['title']);
  1004. if (empty($topic['title'])) {
  1005. $topic['title'] = get_lang('Untitled');
  1006. }
  1007. $html .= '<div class="span4">';
  1008. $html .= Display::tag('h4', Display::url(Security::remove_XSS($topic['title'], STUDENT, true), 'group_topics.php?id='.$group_id.'&topic_id='.$topic['id']));
  1009. if ($my_group_role == GROUP_USER_PERMISSION_ADMIN || $my_group_role == GROUP_USER_PERMISSION_MODERATOR) {
  1010. $actions = '<br />'.Display::url(get_lang('Delete'), api_get_path(WEB_CODE_PATH).'social/group_topics.php?action=delete&id='.$group_id.'&topic_id='.$topic['id'], array('class' => 'btn'));
  1011. }
  1012. $date = '';
  1013. if ($topic['send_date'] != $topic['update_date']) {
  1014. if (!empty($topic['update_date']) && $topic['update_date'] != '0000-00-00 00:00:00') {
  1015. $date .= '<div class="message-group-date" > <i>'.get_lang('LastUpdate').' '.date_to_str_ago($topic['update_date']).'</i></div>';
  1016. }
  1017. } else {
  1018. $date .= '<div class="message-group-date"> <i>'.get_lang('Created').' '.date_to_str_ago($topic['send_date']).'</i></div>';
  1019. }
  1020. $html .= $date.$actions;
  1021. $html .= '</div>';
  1022. $image_path = UserManager::get_user_picture_path_by_id($topic['user_sender_id'], 'web', false, true);
  1023. $image_repository = $image_path['dir'];
  1024. $existing_image = $image_path['file'];
  1025. $user_info = '<td valign="top"><a href="'.api_get_path(WEB_PATH).'main/social/profile.php?u='.$topic['user_sender_id'].'">'.$name.'&nbsp;</a>';
  1026. $user_info .= '<div class="message-group-author"><img src="'.$image_repository.$existing_image.'" alt="'.$name.'" width="32" height="32" title="'.$name.'" /></div>';
  1027. $user_info .= '</td>';
  1028. $html .= '<div class="span2">';
  1029. $html .= $user_info;
  1030. $html .= '</div>';
  1031. $html .= '</div>';
  1032. $array_html[] = array($html);
  1033. }
  1034. // grids for items and topics with paginations
  1035. $html_messages .= Display::return_sortable_grid(
  1036. 'topics',
  1037. array(),
  1038. $array_html,
  1039. array(
  1040. 'hide_navigation' => false,
  1041. 'per_page' => $topics_per_page
  1042. ),
  1043. $query_vars,
  1044. false,
  1045. array(true, true, true, false),
  1046. false
  1047. );
  1048. }
  1049. return $html_messages;
  1050. }
  1051. /**
  1052. * Displays messages of a group with nested view
  1053. * @param $group_id
  1054. * @param $topic_id
  1055. * @param $is_member
  1056. * @param $message_id
  1057. * @return string
  1058. */
  1059. public static function display_message_for_group($group_id, $topic_id, $is_member, $message_id)
  1060. {
  1061. global $my_group_role;
  1062. $main_message = self::get_message_by_id($topic_id);
  1063. if (empty($main_message)) {
  1064. return false;
  1065. }
  1066. $rows = self::get_messages_by_group_by_message($group_id, $topic_id);
  1067. $rows = self::calculate_children($rows, $topic_id);
  1068. $current_user_id = api_get_user_id();
  1069. $items_per_page = 50;
  1070. $query_vars = array('id' => $group_id, 'topic_id' => $topic_id, 'topics_page_nr' => 0);
  1071. // Main message
  1072. $user_link = '';
  1073. $links = '';
  1074. $main_content = '';
  1075. $items_page_nr = null;
  1076. $html = '';
  1077. $delete_button = '';
  1078. if (api_is_platform_admin()) {
  1079. $delete_button = Display::url(Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL), 'group_topics.php?action=delete&id='.$group_id.'&topic_id='.$topic_id);
  1080. }
  1081. $html .= Display::page_subheader(Security::remove_XSS($main_message['title'].$delete_button, STUDENT, true));
  1082. $user_sender_info = UserManager::get_user_info_by_id($main_message['user_sender_id']);
  1083. $files_attachments = self::get_links_message_attachment_files($main_message['id']);
  1084. $name = api_get_person_name($user_sender_info['firstname'], $user_sender_info['lastname']);
  1085. $topic_page_nr = isset($_GET['topics_page_nr']) ? intval($_GET['topics_page_nr']) : null;
  1086. $links.= '<div id="message-reply-link">';
  1087. if (($my_group_role == GROUP_USER_PERMISSION_ADMIN || $my_group_role == GROUP_USER_PERMISSION_MODERATOR) || $main_message['user_sender_id'] == $current_user_id) {
  1088. $links.= '<a href="'.api_get_path(WEB_CODE_PATH).'social/message_for_group_form.inc.php?view_panel=1&height=390&width=610&&user_friend='.$current_user_id.'&group_id='.$group_id.'&message_id='.$main_message['id'].'&action=edit_message_group&anchor_topic=topic_'.$main_message['id'].'&topics_page_nr='.$topic_page_nr.'&items_page_nr='.$items_page_nr.'&topic_id='.$main_message['id'].'" class="group_message_popup" title="'.get_lang('Edit').'">';
  1089. $links.= Display :: return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL).'</a>';
  1090. }
  1091. $links.= '&nbsp;&nbsp;<a href="'.api_get_path(WEB_CODE_PATH).'social/message_for_group_form.inc.php?view_panel=1&height=390&width=610&&user_friend='.api_get_user_id().'&group_id='.$group_id.'&message_id='.$main_message['id'].'&action=reply_message_group&anchor_topic=topic_'.$main_message['id'].'&topics_page_nr='.$topic_page_nr.'&items_page_nr='.$items_page_nr.'&topic_id='.$main_message['id'].'" class="group_message_popup" title="'.get_lang('Reply').'">';
  1092. $links.= Display :: return_icon('talk.png', get_lang('Reply')).'</a>';
  1093. $links.= '</div>';
  1094. $image_path = UserManager::get_user_picture_path_by_id($main_message['user_sender_id'], 'web', false, true);
  1095. $image_repository = $image_path['dir'];
  1096. $existing_image = $image_path['file'];
  1097. $main_content.= '<div class="message-group-author"><img src="'.$image_repository.$existing_image.'" alt="'.$name.'" width="32" height="32" title="'.$name.'" /></div>';
  1098. $user_link = '<a href="'.api_get_path(WEB_PATH).'main/social/profile.php?u='.$main_message['user_sender_id'].'">'.$name.'&nbsp;</a>';
  1099. $date = '';
  1100. if ($main_message['send_date'] != $main_message['update_date']) {
  1101. if (!empty($main_message['update_date']) && $main_message['update_date'] != '0000-00-00 00:00:00') {
  1102. $date = '<div class="message-group-date"> '.get_lang('LastUpdate').' '.date_to_str_ago($main_message['update_date']).'</div>';
  1103. }
  1104. } else {
  1105. $date = '<div class="message-group-date"> '.get_lang('Created').' '.date_to_str_ago($main_message['send_date']).'</div>';
  1106. }
  1107. $attachment = '<div class="message-attach">'.(!empty($files_attachments) ? implode('<br />', $files_attachments) : '').'</div>';
  1108. $main_content.= '<div class="message-group-content">'.$links.$user_link.' '.$date.$main_message['content'].$attachment.'</div>';
  1109. $main_content = Security::remove_XSS($main_content, STUDENT, true);
  1110. $html .= Display::div(Display::div(Display::div($main_content, array('class' => 'group_social_sub_item', 'style' => 'background-color:#fff;')), array('class' => 'group_social_item')), array('class' => 'group_social_grid'));
  1111. $topic_id = $main_message['id'];
  1112. if (is_array($rows) && count($rows) > 0) {
  1113. $topics = $rows;
  1114. $array_html_items = array();
  1115. foreach ($topics as $index => $topic) {
  1116. if (empty($topic['id'])) {
  1117. continue;
  1118. }
  1119. $items_page_nr = isset($_GET['items_'.$topic['id'].'_page_nr']) ? intval($_GET['items_'.$topic['id'].'_page_nr']) : null;
  1120. $user_link = '';
  1121. $links = '';
  1122. $html_items = '';
  1123. $user_sender_info = UserManager::get_user_info_by_id($topic['user_sender_id']);
  1124. $files_attachments = self::get_links_message_attachment_files($topic['id']);
  1125. $name = api_get_person_name($user_sender_info['firstname'], $user_sender_info['lastname']);
  1126. $links.= '<div id="message-reply-link">';
  1127. if (($my_group_role == GROUP_USER_PERMISSION_ADMIN || $my_group_role == GROUP_USER_PERMISSION_MODERATOR) || $topic['user_sender_id'] == $current_user_id) {
  1128. $links.= '<a href="'.api_get_path(WEB_CODE_PATH).'social/message_for_group_form.inc.php?view_panel=1&height=390&width=610&&user_friend='.$current_user_id.'&group_id='.$group_id.'&message_id='.$topic['id'].'&action=edit_message_group&anchor_topic=topic_'.$topic_id.'&topics_page_nr='.$topic_page_nr.'&items_page_nr='.$items_page_nr.'&topic_id='.$topic_id.'" class="group_message_popup" title="'.get_lang('Edit').'">'.Display :: return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL).'</a>';
  1129. }
  1130. $links.= '&nbsp;&nbsp;<a href="'.api_get_path(WEB_CODE_PATH).'social/message_for_group_form.inc.php?view_panel=1&height=390&width=610&&user_friend='.api_get_user_id().'&group_id='.$group_id.'&message_id='.$topic['id'].'&action=reply_message_group&anchor_topic=topic_'.$topic_id.'&topics_page_nr='.$topic_page_nr.'&items_page_nr='.$items_page_nr.'&topic_id='.$topic_id.'" class="group_message_popup" title="'.get_lang('Reply').'">';
  1131. $links.= Display :: return_icon('talk.png', get_lang('Reply')).'</a>';
  1132. $links.= '</div>';
  1133. $image_path = UserManager::get_user_picture_path_by_id($topic['user_sender_id'], 'web', false, true);
  1134. $image_repository = $image_path['dir'];
  1135. $existing_image = $image_path['file'];
  1136. $html_items.= '<div class="message-group-author"><img src="'.$image_repository.$existing_image.'" alt="'.$name.'" width="32" height="32" title="'.$name.'" /></div>';
  1137. $user_link = '<a href="'.api_get_path(WEB_PATH).'main/social/profile.php?u='.$topic['user_sender_id'].'">'.$name.'&nbsp;</a>';
  1138. $date = '';
  1139. if ($topic['send_date'] != $topic['update_date']) {
  1140. if (!empty($topic['update_date']) && $topic['update_date'] != '0000-00-00 00:00:00') {
  1141. $date = '<div class="message-group-date"> '.get_lang('LastUpdate').' '.date_to_str_ago($topic['update_date']).'</div>';
  1142. }
  1143. } else {
  1144. $date = '<div class="message-group-date"> '.get_lang('Created').' '.date_to_str_ago($topic['send_date']).'</div>';
  1145. }
  1146. $attachment = '<div class="message-attach">'.(!empty($files_attachments) ? implode('<br />', $files_attachments) : '').'</div>';
  1147. $html_items.= '<div class="message-group-content">'.$links.$user_link.' '.$date.Security::remove_XSS($topic['content'], STUDENT, true).$attachment.'</div>';
  1148. $base_padding = 20;
  1149. if ($topic['indent_cnt'] == 0) {
  1150. $indent = $base_padding;
  1151. } else {
  1152. $indent = intval($topic['indent_cnt']) * $base_padding + $base_padding;
  1153. }
  1154. $class = 'group_social_sub_item';
  1155. if (isset($message_id) && $message_id == $topic['id']) {
  1156. $class .= ' group_social_sub_item_highlight';
  1157. }
  1158. $html_items = Display::div($html_items, array('class' => $class, 'id' => 'msg_'.$topic['id']));
  1159. $html_items = Display::div($html_items, array('class' => '', 'style' => 'margin-left:'.$indent.'px'));
  1160. $array_html_items[] = array($html_items);
  1161. }
  1162. // grids for items with paginations
  1163. $options = array('hide_navigation' => false, 'per_page' => $items_per_page);
  1164. $visibility = array(true, true, true, false);
  1165. $style_class = array('item' => array('class' => 'group_social_item'), 'main' => array('class' => 'group_social_grid'));
  1166. if (!empty($array_html_items)) {
  1167. $html .= Display::return_sortable_grid('items_'.$topic['id'], array(), $array_html_items, $options, $query_vars, null, $visibility, false, $style_class);
  1168. }
  1169. }
  1170. return $html;
  1171. }
  1172. /**
  1173. * Add children to messages by id is used for nested view messages
  1174. * @param array rows of messages
  1175. * @return array new list adding the item children
  1176. */
  1177. public static function calculate_children($rows, $first_seed)
  1178. {
  1179. $rows_with_children = array();
  1180. foreach ($rows as $row) {
  1181. $rows_with_children[$row["id"]] = $row;
  1182. $rows_with_children[$row["parent_id"]]["children"][] = $row["id"];
  1183. }
  1184. $rows = $rows_with_children;
  1185. $sorted_rows = array(0 => array());
  1186. self::message_recursive_sort($rows, $sorted_rows, $first_seed);
  1187. unset($sorted_rows[0]);
  1188. return $sorted_rows;
  1189. }
  1190. /**
  1191. * Sort recursively the messages, is used for for nested view messages
  1192. * @param array original rows of messages
  1193. * @param array list recursive of messages
  1194. * @param int seed for calculate the indent
  1195. * @param int indent for nested view
  1196. * @return void
  1197. */
  1198. public static function message_recursive_sort($rows, &$messages, $seed = 0, $indent = 0)
  1199. {
  1200. if ($seed > 0 && isset($rows[$seed]["id"])) {
  1201. $messages[$rows[$seed]["id"]] = $rows[$seed];
  1202. $messages[$rows[$seed]["id"]]["indent_cnt"] = $indent;
  1203. $indent++;
  1204. }
  1205. if (isset($rows[$seed]["children"])) {
  1206. foreach ($rows[$seed]["children"] as $child) {
  1207. self::message_recursive_sort($rows, $messages, $child, $indent);
  1208. }
  1209. }
  1210. }
  1211. /**
  1212. * Sort date by desc from a multi-dimensional array
  1213. * @param array1 first array to compare
  1214. * @param array2 second array to compare
  1215. * @return bool
  1216. */
  1217. public function order_desc_date($array1, $array2)
  1218. {
  1219. return strcmp($array2['send_date'], $array1['send_date']);
  1220. }
  1221. /**
  1222. * Get array of links (download) for message attachment files
  1223. * @param int message id
  1224. * @param string type message list (inbox/outbox)
  1225. * @return array
  1226. */
  1227. public static function get_links_message_attachment_files($message_id, $type = '')
  1228. {
  1229. $tbl_message_attach = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
  1230. $message_id = intval($message_id);
  1231. // get file attachments by message id
  1232. $links_attach_file = array();
  1233. if (!empty($message_id)) {
  1234. $sql = "SELECT * FROM $tbl_message_attach WHERE message_id = '$message_id'";
  1235. $rs_file = Database::query($sql);
  1236. if (Database::num_rows($rs_file) > 0) {
  1237. $attach_icon = Display::return_icon('attachment.gif', '');
  1238. $archiveURL = api_get_path(WEB_CODE_PATH).'messages/download.php?type='.$type.'&file=';
  1239. while ($row_file = Database::fetch_array($rs_file)) {
  1240. $archiveFile = $row_file['path'];
  1241. $filename = $row_file['filename'];
  1242. $filesize = format_file_size($row_file['size']);
  1243. $filecomment = $row_file['comment'];
  1244. $links_attach_file[] = $attach_icon.'&nbsp;<a href="'.$archiveURL.$archiveFile.'">'.$filename.'</a>&nbsp;('.$filesize.')'.(!empty($filecomment) ? '&nbsp;-&nbsp;<i>'.$filecomment.'</i>' : '');
  1245. }
  1246. }
  1247. }
  1248. return $links_attach_file;
  1249. }
  1250. /**
  1251. * Get message list by id
  1252. * @param int message id
  1253. * @return array
  1254. */
  1255. public static function get_message_by_id($message_id)
  1256. {
  1257. $tbl_message = Database::get_main_table(TABLE_MESSAGE);
  1258. $message_id = intval($message_id);
  1259. $sql = "SELECT * FROM $tbl_message WHERE id = '$message_id' AND msg_status <> '".MESSAGE_STATUS_DELETED."' ";
  1260. $res = Database::query($sql);
  1261. $item = array();
  1262. if (Database::num_rows($res) > 0) {
  1263. $item = Database::fetch_array($res, 'ASSOC');
  1264. }
  1265. return $item;
  1266. }
  1267. /**
  1268. * @param $id
  1269. * @param array $params
  1270. * @return string
  1271. */
  1272. public static function generate_message_form($id, $params = array())
  1273. {
  1274. $form = new FormValidator('send_message', null, 'post', null, array('id' => $id.'_form', 'class' => 'form-vertical'));
  1275. $form->addElement('text', 'subject', get_lang('Subject'), array('id' => 'subject_id', 'class' => 'span5'));
  1276. $form->addElement('textarea', 'content', get_lang('Message'), array('id' => 'content_id', 'rows' => '5', 'class' => 'span5'));
  1277. $div = Display::div($form->return_form(), array('id' => $id.'_div', 'style' => 'display:none'));
  1278. return $div;
  1279. }
  1280. public static function generate_invitation_form($id, $params = array())
  1281. {
  1282. $form = new FormValidator('send_invitation', null, 'post', null, array('id' => $id.'_form', 'class' => 'form-vertical'));
  1283. //$form->addElement('text', 'subject', get_lang('Subject'), array('id' => 'subject_id'));
  1284. $form->addElement('textarea', 'content', get_lang('AddPersonalMessage'), array('id' => 'content_invitation_id', 'rows' => '5', 'class' => 'span5'));
  1285. $div = Display::div($form->return_form(), array('id' => $id.'_div', 'style' => 'display:none'));
  1286. return $div;
  1287. }
  1288. //@todo this functions should be in the message class
  1289. public static function inbox_display()
  1290. {
  1291. $success = get_lang('SelectedMessagesDeleted');
  1292. $success_read = get_lang('SelectedMessagesRead');
  1293. $success_unread = get_lang('SelectedMessagesUnRead');
  1294. $html = '';
  1295. if (isset($_REQUEST['action'])) {
  1296. switch ($_REQUEST['action']) {
  1297. case 'mark_as_unread' :
  1298. $number_of_selected_messages = count($_POST['id']);
  1299. if (is_array($_POST['id'])) {
  1300. foreach ($_POST['id'] as $index => $message_id) {
  1301. MessageManager::update_message_status(api_get_user_id(), $message_id, MESSAGE_STATUS_UNREAD);
  1302. }
  1303. }
  1304. $html .= Display::return_message(api_xml_http_response_encode($success_unread), 'normal', false);
  1305. break;
  1306. case 'mark_as_read' :
  1307. $number_of_selected_messages = count($_POST['id']);
  1308. if (is_array($_POST['id'])) {
  1309. foreach ($_POST['id'] as $index => $message_id) {
  1310. MessageManager::update_message_status(api_get_user_id(), $message_id, MESSAGE_STATUS_NEW);
  1311. }
  1312. }
  1313. $html .= Display::return_message(api_xml_http_response_encode($success_read), 'normal', false);
  1314. break;
  1315. case 'delete' :
  1316. $number_of_selected_messages = count($_POST['id']);
  1317. foreach ($_POST['id'] as $index => $message_id) {
  1318. MessageManager::delete_message_by_user_receiver(api_get_user_id(), $message_id);
  1319. }
  1320. $html .= Display::return_message(api_xml_http_response_encode($success), 'normal', false);
  1321. break;
  1322. case 'deleteone' :
  1323. MessageManager::delete_message_by_user_receiver(api_get_user_id(), $_GET['id']);
  1324. $html .= Display::return_message(api_xml_http_response_encode($success), 'confirmation', false);
  1325. break;
  1326. }
  1327. }
  1328. // display sortable table with messages of the current user
  1329. $table = new SortableTable('message_inbox', array('MessageManager', 'get_number_of_messages'), array('MessageManager', 'get_message_data'), 3, 20, 'DESC');
  1330. $table->set_header(0, '', false, array('style' => 'width:15px;'));
  1331. $table->set_header(1, get_lang('Messages'), false);
  1332. $table->set_header(2, get_lang('Date'), true, array('style' => 'width:180px;'));
  1333. $table->set_header(3, get_lang('Modify'), false, array('style' => 'width:70px;'));
  1334. if (isset($_REQUEST['f']) && $_REQUEST['f'] == 'social') {
  1335. $parameters['f'] = 'social';
  1336. $table->set_additional_parameters($parameters);
  1337. }
  1338. $table->set_form_actions(array('delete' => get_lang('DeleteSelectedMessages'),'mark_as_unread' => get_lang('MailMarkSelectedAsUnread'),'mark_as_read' => get_lang('MailMarkSelectedAsRead')));
  1339. $html .= $table->return_table();
  1340. return $html;
  1341. }
  1342. /**
  1343. * @return string
  1344. */
  1345. static function outbox_display()
  1346. {
  1347. $social_link = false;
  1348. if (isset($_REQUEST['f']) && $_REQUEST['f'] == 'social') {
  1349. $social_link = 'f=social';
  1350. }
  1351. $success = get_lang('SelectedMessagesDeleted').'&nbsp</b><br /><a href="outbox.php?'.$social_link.'">'.get_lang('BackToOutbox').'</a>';
  1352. $html = null;
  1353. if (isset($_REQUEST['action'])) {
  1354. switch ($_REQUEST['action']) {
  1355. case 'delete' :
  1356. $number_of_selected_messages = count($_POST['id']);
  1357. if ($number_of_selected_messages != 0) {
  1358. foreach ($_POST['id'] as $index => $message_id) {
  1359. MessageManager::delete_message_by_user_receiver(api_get_user_id(), $message_id);
  1360. }
  1361. }
  1362. $html .= Display::return_message(api_xml_http_response_encode($success), 'normal', false);
  1363. break;
  1364. case 'deleteone' :
  1365. MessageManager::delete_message_by_user_receiver(api_get_user_id(), $_GET['id']);
  1366. $html .=Display::return_message(api_xml_http_response_encode($success), 'normal', false);
  1367. $html .= '<br/>';
  1368. break;
  1369. }
  1370. }
  1371. // display sortable table with messages of the current user
  1372. $table = new SortableTable('message_outbox', array('MessageManager', 'get_number_of_messages_sent'), array('MessageManager', 'get_message_data_sent'), 3, 20, 'DESC');
  1373. $parameters['f'] = Security::remove_XSS($_GET['f']);
  1374. $table->set_additional_parameters($parameters);
  1375. $table->set_header(0, '', false, array('style' => 'width:15px;'));
  1376. $table->set_header(1, get_lang('Messages'), false);
  1377. $table->set_header(2, get_lang('Date'), true, array('style' => 'width:160px;'));
  1378. $table->set_header(3, get_lang('Modify'), false, array('style' => 'width:70px;'));
  1379. $table->set_form_actions(array('delete' => get_lang('DeleteSelectedMessages')));
  1380. $html .= $table->return_table();
  1381. return $html;
  1382. }
  1383. /**
  1384. * Get the count of the last received messages for a user
  1385. * @param int $userId The user id
  1386. * @param int $lastId The id of the last received message
  1387. * @return int The count of new messages
  1388. */
  1389. public static function countMessagesFromLastReceivedMessage($userId, $lastId = 0)
  1390. {
  1391. $userId = intval($userId);
  1392. $lastId = intval($lastId);
  1393. if (empty($userId)) {
  1394. return 0;
  1395. }
  1396. $messagesTable = Database::get_main_table(TABLE_MESSAGE);
  1397. $conditions = array(
  1398. 'where' => array(
  1399. 'user_receiver_id = ?' => $userId,
  1400. 'AND msg_status = ?' => MESSAGE_STATUS_UNREAD,
  1401. 'AND id > ?' => $lastId
  1402. )
  1403. );
  1404. $result = Database::select('COUNT(1) AS qty', $messagesTable, $conditions);
  1405. if (!empty($result)) {
  1406. $row = current($result);
  1407. return $row['qty'];
  1408. }
  1409. return 0;
  1410. }
  1411. /**
  1412. * Get the data of the last received messages for a user
  1413. * @param int $userId The user id
  1414. * @param int $lastId The id of the last received message
  1415. * @return int The count of new messages
  1416. */
  1417. public static function getMessagesFromLastReceivedMessage($userId, $lastId = 0)
  1418. {
  1419. $userId = intval($userId);
  1420. $lastId = intval($lastId);
  1421. if (empty($userId)) {
  1422. return 0;
  1423. }
  1424. $messagesTable = Database::get_main_table(TABLE_MESSAGE);
  1425. $userTable = Database::get_main_table(TABLE_MAIN_USER);
  1426. $messages = array();
  1427. $sql = "SELECT m.*, u.user_id, u.lastname, u.firstname "
  1428. . "FROM $messagesTable as m "
  1429. . "INNER JOIN $userTable as u "
  1430. . "ON m.user_sender_id = u.user_id "
  1431. . "WHERE m.user_receiver_id = $userId "
  1432. . "AND m.msg_status = " . MESSAGE_STATUS_UNREAD . " "
  1433. . "AND m.id > $lastId "
  1434. . "ORDER BY m.send_date DESC";
  1435. $result = Database::query($sql);
  1436. if ($result !== false) {
  1437. while ($row = Database::fetch_assoc($result)) {
  1438. $messages[] = $row;
  1439. }
  1440. }
  1441. return $messages;
  1442. }
  1443. /**
  1444. * Check whether a message has attachments
  1445. * @param int $messageId The message id
  1446. * @return boolean Whether the message has attachments return true. Otherwise return false
  1447. */
  1448. public static function hasAttachments($messageId)
  1449. {
  1450. $messageId = intval($messageId);
  1451. if (empty($messageId)) {
  1452. return false;
  1453. }
  1454. $messageAttachmentTable = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
  1455. $conditions = array(
  1456. 'where' => array(
  1457. 'message_id = ?' => $messageId
  1458. )
  1459. );
  1460. $result = Database::select('COUNT(1) AS qty', $messageAttachmentTable, $conditions, 'first');
  1461. if (!empty($result)) {
  1462. if ($result['qty'] > 0) {
  1463. return true;
  1464. }
  1465. }
  1466. return false;
  1467. }
  1468. }