new_message.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php // $Id: new_message.php 17903 2009-01-21 19:50:57Z juliomontoya $
  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. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  17. Mail: info@dokeos.com
  18. ==============================================================================
  19. */
  20. /**
  21. * This script shows a compose area (wysiwyg editor if supported, otherwise
  22. * a simple textarea) where the user can type a message.
  23. * There are three modes
  24. * - standard: type a message, select a user to send it to, press send
  25. * - reply on message (when pressing reply when viewing a message)
  26. * - send to specific user (when pressing send message in the who is online list)
  27. */
  28. /*
  29. ==============================================================================
  30. INIT SECTION
  31. ==============================================================================
  32. */
  33. // name of the language file that needs to be included
  34. $language_file= 'messages';
  35. $cidReset=true;
  36. include_once('../inc/global.inc.php');
  37. api_block_anonymous_users();
  38. if (api_get_setting('allow_message_tool')!='true'){
  39. api_not_allowed();
  40. }
  41. require_once(api_get_path(LIBRARY_PATH).'message.lib.php');
  42. require_once(api_get_path(LIBRARY_PATH).'/text.lib.php');
  43. require_once(api_get_path(LIBRARY_PATH).'/formvalidator/FormValidator.class.php');
  44. $table_message = Database::get_course_table(TABLE_MESSAGE);
  45. /*
  46. -----------------------------------------------------------
  47. Constants and variables
  48. -----------------------------------------------------------
  49. */
  50. $htmlHeadXtra[]='
  51. <script language="javascript">
  52. function validate(form,list)
  53. {
  54. if(list.selectedIndex<0)
  55. {
  56. alert("Please select someone to send the message to.")
  57. return false
  58. }
  59. else
  60. return true
  61. }
  62. </script>';
  63. $nameTools = get_lang('ComposeMessage');
  64. /*
  65. ==============================================================================
  66. FUNCTIONS
  67. ==============================================================================
  68. */
  69. /**
  70. * Shows the compose area + a list of users to select from.
  71. */
  72. function show_compose_to_any($user_id)
  73. {
  74. $online_user_list = get_online_user_list($user_id);
  75. $default['user_list'] = $user_id;
  76. manage_form($default, $online_user_list);
  77. }
  78. function show_compose_reply_to_message($message_id, $receiver_id)
  79. {
  80. global $table_message;
  81. $query = "SELECT * FROM $table_message WHERE user_receiver_id=".$receiver_id." AND id='".$message_id."';";
  82. $result = api_sql_query($query,__FILE__,__LINE__);
  83. $row = Database::fetch_array($result);
  84. if(!isset($row[1]))
  85. {
  86. echo get_lang('InvalidMessageId');
  87. die();
  88. }
  89. echo get_lang('To').':&nbsp;<strong>'. GetFullUserName($row[1]).'</strong>';
  90. $default['title'] =get_lang('EnterTitle');
  91. $default['user_list'] = $row[1];
  92. manage_form($default);
  93. }
  94. function show_compose_to_user($receiver_id)
  95. {
  96. echo get_lang('To').':&nbsp;<strong>'. GetFullUserName($receiver_id).'</strong>';
  97. $default['title'] = get_lang('EnterTitle');
  98. $default['user_list'] = $receiver_id;
  99. manage_form($default);
  100. }
  101. function manage_form($default, $select_from_user_list = null)
  102. {
  103. global $table_message;
  104. $form = new FormValidator('compose_message');
  105. if (isset($select_from_user_list))
  106. {
  107. $form->addElement('select', 'user_list', get_lang('SendMessageTo'), $select_from_user_list);
  108. } else {
  109. $form->addElement('hidden', 'user_list');
  110. }
  111. $form->add_textfield('title', get_lang('Title'));
  112. $form->add_html_editor('content', '',false,false);
  113. $form->addElement('submit', 'compose', get_lang('Send'));
  114. $form->setDefaults($default);
  115. if($form->validate()) {
  116. $values = $form->exportValues();
  117. $receiver_user_id = $values['user_list'];
  118. $title = $values['title'];
  119. $content = $values['content'];
  120. //all is well, send the message
  121. send_message($receiver_user_id, $title, $content);
  122. display_success_message($receiver_user_id);
  123. } else {
  124. $form->display();
  125. }
  126. }
  127. /*
  128. ==============================================================================
  129. MAIN SECTION
  130. ==============================================================================
  131. */
  132. $interbreadcrumb[] = array ('url' => 'inbox.php', 'name' => get_lang('Messages'));
  133. Display::display_header($nameTools, get_lang('ComposeMessage'));
  134. api_display_tool_title($nameTools);
  135. if(!isset($_POST['compose'])) {
  136. if(isset($_GET['re_id'])) {
  137. $message_id = $_GET['re_id'];
  138. $receiver_id = api_get_user_id();
  139. show_compose_reply_to_message($message_id, $receiver_id);
  140. } elseif(isset($_GET['send_to_user'])) {
  141. show_compose_to_user($_GET['send_to_user']);
  142. } else {
  143. show_compose_to_any($_user['user_id']);
  144. }
  145. } else {
  146. if(api_get_user_id() && isset($_POST['user_list']) && isset($_POST['content'])) {
  147. $default['title'] = $_POST['title'];
  148. $default['user_list'] = $_POST['user_list'];
  149. manage_form($default);
  150. } else {
  151. Display::display_error_message(get_lang('ErrorSendingMessage'));
  152. }
  153. }
  154. /*
  155. ==============================================================================
  156. FOOTER
  157. ==============================================================================
  158. */
  159. Display::display_footer();
  160. ?>