message.lib.php 65 KB

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