message.lib.php 69 KB

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