message.lib.php 64 KB

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