message.lib.php 62 KB

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