message.lib.php 71 KB

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