new_message.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php // $Id: new_message.php 22097 2009-07-15 04:30:05Z ivantcholakov $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2009 Dokeos SPRL
  6. Copyright (c) 2009 Julio Montoya Armas <gugli100@gmail.com>
  7. Copyright (c) Facultad de Matematicas, UADY (México)
  8. Copyright (c) Evie, Free University of Brussels (Belgium)
  9. Copyright (c) 2009 Isaac Flores Paz <isaac.flores.paz@gmail.com>
  10. For a full list of contributors, see "credits.txt".
  11. The full license can be read in "license.txt".
  12. This program is free software; you can redistribute it and/or
  13. modify it under the terms of the GNU General Public License
  14. as published by the Free Software Foundation; either version 2
  15. of the License, or (at your option) any later version.
  16. See the GNU General Public License for more details.
  17. Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  18. Mail: info@dokeos.com
  19. ==============================================================================
  20. */
  21. /**
  22. * This script shows a compose area (wysiwyg editor if supported, otherwise
  23. * a simple textarea) where the user can type a message.
  24. * There are three modes
  25. * - standard: type a message, select a user to send it to, press send
  26. * - reply on message (when pressing reply when viewing a message)
  27. * - send to specific user (when pressing send message in the who is online list)
  28. */
  29. /*
  30. ==============================================================================
  31. INIT SECTION
  32. ==============================================================================
  33. */
  34. // name of the language file that needs to be included
  35. $language_file= 'messages';
  36. $cidReset=true;
  37. require_once '../inc/global.inc.php';
  38. api_block_anonymous_users();
  39. if (api_get_setting('allow_message_tool')!='true'){
  40. api_not_allowed();
  41. }
  42. require_once'../messages/message.class.php';
  43. require_once api_get_path(LIBRARY_PATH).'/text.lib.php';
  44. require_once api_get_path(LIBRARY_PATH).'/formvalidator/FormValidator.class.php';
  45. $request=api_is_xml_http_request();
  46. $nameTools = api_xml_http_response_encode(get_lang('Messages'));
  47. /*
  48. -----------------------------------------------------------
  49. Constants and variables
  50. -----------------------------------------------------------
  51. */
  52. $htmlHeadXtra[]='
  53. <script language="javascript">
  54. function validate(form,list)
  55. {
  56. if(list.selectedIndex<0)
  57. {
  58. alert("Please select someone to send the message to.")
  59. return false
  60. }
  61. else
  62. return true
  63. }
  64. </script>';
  65. $htmlHeadXtra[] = '<script src="../inc/lib/javascript/jquery.js" type="text/javascript" language="javascript"></script>'; //jQuery
  66. $htmlHeadXtra[] = '<script type="text/javascript">
  67. $(document).ready(function (){
  68. cont=0;
  69. $("#id_text_name").bind("keyup", function(){
  70. name=$("#id_text_name").get(0).value;
  71. $.ajax({
  72. contentType: "application/x-www-form-urlencoded",
  73. beforeSend: function(objeto) {
  74. /*$("#id_div_search").html("Searching...");*/ },
  75. type: "POST",
  76. url: "../social/select_options.php",
  77. data: "search="+name,
  78. success: function(datos){
  79. $("#id_div_search").html(datos)
  80. $("#id_search_name").bind("click", function(){
  81. name_option=$("select#id_search_name option:selected").text();
  82. code_option=$("select#id_search_name option:selected").val();
  83. $("#user_list").attr("value", code_option);
  84. $("#id_text_name").attr("value", name_option);
  85. $("#id_div_search").html("");
  86. cont++;
  87. });
  88. }
  89. });
  90. });
  91. });
  92. </script>';
  93. $nameTools = api_xml_http_response_encode(get_lang('ComposeMessage'));
  94. /*
  95. ==============================================================================
  96. FUNCTIONS
  97. ==============================================================================
  98. */
  99. /**
  100. * Shows the compose area + a list of users to select from.
  101. */
  102. function show_compose_to_any ($user_id) {
  103. $online_user_list = MessageManager::get_online_user_list($user_id);
  104. $default['user_list'] = 0;
  105. $online_user_list=null;
  106. manage_form($default, $online_user_list);
  107. }
  108. function show_compose_reply_to_message ($message_id, $receiver_id) {
  109. global $charset;
  110. $table_message = Database::get_main_table(TABLE_MESSAGE);
  111. $query = "SELECT * FROM $table_message WHERE user_receiver_id=".$receiver_id." AND id='".$message_id."';";
  112. $result = api_sql_query($query,__FILE__,__LINE__);
  113. $row = Database::fetch_array($result);
  114. if (!isset($row[1])) {
  115. echo api_xml_http_response_encode(get_lang('InvalidMessageId'));
  116. die();
  117. }
  118. echo api_xml_http_response_encode(get_lang('To').':&nbsp;<strong>'. GetFullUserName($row[1]).'</strong>');
  119. $default['title'] = api_xml_http_response_encode(get_lang('EnterTitle'));
  120. $default['user_list'] = $row[1];
  121. manage_form($default);
  122. }
  123. function show_compose_to_user ($receiver_id) {
  124. global $charset;
  125. echo get_lang('To').':&nbsp;<strong>'. GetFullUserName($receiver_id).'</strong>';
  126. $default['title'] = api_xml_http_response_encode(get_lang('EnterTitle'));
  127. $default['user_list'] = $receiver_id;
  128. manage_form($default);
  129. }
  130. function manage_form ($default, $select_from_user_list = null) {
  131. global $charset;
  132. $table_message = Database::get_main_table(TABLE_MESSAGE);
  133. $request=api_is_xml_http_request();
  134. if ($request===true) {
  135. $form = new FormValidator('compose_message','post','index.php?sendform=true#remote-tab-2');
  136. } else {
  137. $form = new FormValidator('compose_message');
  138. }
  139. if (isset($select_from_user_list)) {
  140. $form->add_textfield('id_text_name', api_xml_http_response_encode(get_lang('SendMessageTo')),true,array('size' => 40,'id'=>'id_text_name','onkeyup'=>'send_request_and_search()','autocomplete'=>'off','style'=>'padding:0px'));
  141. $form->addRule('id_text_name', api_xml_http_response_encode(get_lang('ThisFieldIsRequired')), 'required');
  142. $form->addElement('html','<div id="id_div_search" style="padding:0px" class="message-select-box" >&nbsp;</div>');
  143. $form->addElement('hidden','user_list',0,array('id'=>'user_list'));
  144. } else {
  145. if ($default['user_list']==0) {
  146. $form->add_textfield('id_text_name', api_xml_http_response_encode(get_lang('SendMessageTo')),true,array('size' => 40,'id'=>'id_text_name','onkeyup'=>'send_request_and_search()','autocomplete'=>'off','style'=>'padding:0px'));
  147. $form->addRule('id_text_name', api_xml_http_response_encode(get_lang('ThisFieldIsRequired')), 'required');
  148. $form->addElement('html','<div id="id_div_search" style="padding:0px" class="message-select-box" >&nbsp;</div>');
  149. }
  150. $form->addElement('hidden','user_list',0,array('id'=>'user_list'));
  151. }
  152. $form->add_textfield('title', api_xml_http_response_encode(get_lang('Title')));
  153. $form->add_html_editor('content', '', false, false, array('ToolbarSet' => 'Messages', 'Width' => '95%', 'Height' => '250'));
  154. if (isset($_GET['re_id'])) {
  155. $form->addElement('hidden','re_id',Security::remove_XSS($_GET['re_id']));
  156. $form->addElement('hidden','save_form','save_form');
  157. }
  158. $form->addElement('style_submit_button','compose',api_xml_http_response_encode(get_lang('SendMessage')),'class="save"');
  159. $form->setRequiredNote(api_xml_http_response_encode('<span class="form_required">*</span> <small>'.get_lang('ThisFieldIsRequired').'</small>'));
  160. $form->setDefaults($default);
  161. if ($form->validate()) {
  162. $values = $form->exportValues();
  163. $receiver_user_id = $values['user_list'];
  164. $title = $values['title'];
  165. $content = $values['content'];
  166. //all is well, send the message
  167. MessageManager::send_message($receiver_user_id, $title, $content);
  168. MessageManager::display_success_message($receiver_user_id);
  169. } else {
  170. $form->display();
  171. }
  172. }
  173. /*
  174. ==============================================================================
  175. MAIN SECTION
  176. ==============================================================================
  177. */
  178. if (isset($_GET['rs'])) {
  179. $interbreadcrumb[] = array ('url' => 'inbox.php', 'name' => get_lang('Messages'));
  180. $interbreadcrumb[]= array (
  181. 'url' => '../social/'.$_SESSION['social_dest'],
  182. 'name' => get_lang('SocialNetwork')
  183. );
  184. } else {
  185. $interbreadcrumb[] = array ('url' => 'javascript: void(0);', 'name' => get_lang('Messages'));
  186. $interbreadcrumb[]= array (
  187. 'url' => 'outbox.php',
  188. 'name' => get_lang('Outbox')
  189. );
  190. $interbreadcrumb[]= array (
  191. 'url' => 'inbox.php',
  192. 'name' => get_lang('Inbox')
  193. );
  194. }
  195. $interbreadcrumb[]= array (
  196. 'url' => 'javascript: void(0);',
  197. 'name' => get_lang('ComposeMessage')
  198. );
  199. if ($request===false) {
  200. Display::display_header('');
  201. }
  202. //api_display_tool_title($nameTools);
  203. echo '<div class=actions>';
  204. echo '<a onclick="close_div_show(\'div_content_messages\')" href="javascript:void(0)">'.Display::return_icon('folder_up.gif',api_xml_http_response_encode(get_lang('BackToInbox'))).api_xml_http_response_encode(get_lang('BackToInbox')).'</a>';
  205. echo '</div>';
  206. if (!isset($_POST['compose'])) {
  207. if(isset($_GET['re_id'])) {
  208. $message_id = $_GET['re_id'];
  209. $receiver_id = api_get_user_id();
  210. show_compose_reply_to_message($message_id, $receiver_id);
  211. } elseif(isset($_GET['send_to_user'])) {
  212. show_compose_to_user($_GET['send_to_user']);
  213. } else {
  214. show_compose_to_any($_user['user_id']);
  215. }
  216. } else {
  217. $restrict=isset($_POST['id_text_name']) ? $_POST['id_text_name'] : false;
  218. if ($restrict===false && isset($_GET['re_id'])) {
  219. }
  220. if (isset($_GET['re_id'])) {
  221. $default['title'] = api_xml_http_response_encode($_POST['title']);
  222. $default['content'] = api_xml_http_response_encode($_POST['content']);
  223. //$default['user_list'] = $_POST['user_list'];
  224. manage_form($default);
  225. } else {
  226. if ($restrict) {
  227. $default['title'] = api_xml_http_response_encode($_POST['title']);
  228. $default['id_text_name'] = api_xml_http_response_encode($_POST['id_text_name']);
  229. $default['user_list'] = $_POST['user_list'];
  230. manage_form($default);
  231. } else {
  232. Display::display_error_message(api_xml_http_response_encode(get_lang('ErrorSendingMessage')));
  233. }
  234. }
  235. }
  236. /*
  237. ==============================================================================
  238. FOOTER
  239. ==============================================================================
  240. */
  241. if ($request===false) {
  242. Display::display_footer();
  243. }
  244. ?>