message.lib.php 59 KB

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