message.lib.php 58 KB

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