message.lib.php 63 KB

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