message.lib.php 63 KB

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