move code up one directory
[atutor.git] / inbox / send_message.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2010                                      */
6 /* Inclusive Design Institute                                   */
7 /* http://atutor.ca                                                                                             */
8 /*                                                              */
9 /* This program is free software. You can redistribute it and/or*/
10 /* modify it under the terms of the GNU General Public License  */
11 /* as published by the Free Software Foundation.                                */
12 /****************************************************************/
13 // $Id$
14 $_user_location = 'public';
15
16 define('AT_INCLUDE_PATH', '../include/');
17 require (AT_INCLUDE_PATH.'vitals.inc.php');
18
19
20 if (!$_SESSION['valid_user']) {
21         require(AT_INCLUDE_PATH.'header.inc.php');
22
23         $msg->printInfos('MSG_SEND_LOGIN');
24         
25         require(AT_INCLUDE_PATH.'footer.inc.php');
26         exit;
27 }
28
29 if (isset($_POST['cancel'])) {
30         $msg->addFeedback('CANCELLED');
31         header('Location: index.php');
32         exit;
33 } else if (($_POST['submit']) || ($_POST['submit_delete'])) {
34         $missing_fields = array();
35
36         if (($_POST['to'] == '') || ($_POST['to'] == 0)) {
37                 $missing_fields[] = _AT('to');
38         }
39         if ($_POST['subject'] == '') {
40                 $missing_fields[] = _AT('subject');
41         }
42         if ($_POST['message'] == '') {
43                 $missing_fields[] = _AT('body');
44         }
45
46         if ($missing_fields) {
47                 $missing_fields = implode(', ', $missing_fields);
48                 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
49         }
50
51         if (!$msg->containsErrors()) {
52                 $_POST['subject'] = $addslashes($_POST['subject']);
53                 $_POST['message'] = $addslashes($_POST['message']);
54                 $_POST['to'] = intval($_POST['to']);
55
56                 $sql = "INSERT INTO ".TABLE_PREFIX."messages VALUES (NULL, $_SESSION[course_id], $_SESSION[member_id], $_POST[to], NOW(), 1, 0, '$_POST[subject]', '$_POST[message]')";
57                 $result = mysql_query($sql,$db);
58
59                 // sent message box:
60                 $sql = "INSERT INTO ".TABLE_PREFIX."messages_sent VALUES (NULL, $_SESSION[course_id], $_SESSION[member_id], $_POST[to], NOW(), '$_POST[subject]', '$_POST[message]')";
61                 $result = mysql_query($sql,$db);
62
63                 //send email notification if recipient has message notification enabled
64                 $sql_notify = "SELECT first_name, last_name, email, inbox_notify FROM ".TABLE_PREFIX."members WHERE member_id=$_POST[to]";
65                 $result_notify = mysql_query($sql_notify, $db);
66                 $row_notify = mysql_fetch_assoc($result_notify);
67
68                 if ($row_notify['inbox_notify'] == 1) {
69                         require(AT_INCLUDE_PATH . 'classes/phpmailer/atutormailer.class.php');
70
71                         $body = _AT('notification_new_inbox', get_display_name($_SESSION['member_id']), $_base_href.'bounce.php?course='.$_SESSION['course_id']);
72                         $sender = get_display_name($_SESSION['member_id']);
73                         $mail = new ATutorMailer;
74                         $mail->AddAddress($row_notify['email'], $sender);
75                         $mail->FromName = $_config['site_name'];
76                         $mail->From     = $_config['contact_email'];
77                         $mail->Subject  = _AT('message_notification');
78                         $mail->Body     = $body;
79
80                         if(!$mail->Send()) {
81                                 $msg->addError('SENDING_ERROR');
82                         }
83                         unset($mail);
84                 }
85
86                 if ($_POST['submit_delete']) {
87                         $result = mysql_query("DELETE FROM ".TABLE_PREFIX."messages WHERE message_id=$_POST[replied] AND to_member_id=$_SESSION[member_id]",$db);
88                 } else if ($_POST['replied'] != '') {
89                         $result = mysql_query("UPDATE ".TABLE_PREFIX."messages SET replied=1, date_sent=date_sent WHERE message_id=$_POST[replied]",$db);
90                 }
91
92                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
93                 if (isset($_SESSION['last_visited_page'])){
94                         $page = $_SESSION['last_visited_page'];
95                         unset($_SESSION['last_visited_page']);
96                         header('Location: '.$page);
97                         exit;
98                 }               
99                 header('Location: index.php');
100                 exit;
101         }
102 }
103
104 $sql    = "SELECT COUNT(*) AS cnt FROM ".TABLE_PREFIX."course_enrollment WHERE member_id=$_SESSION[member_id] AND (approved='y' OR approved='a')";
105 $result = mysql_query($sql, $db);
106 $row    = mysql_fetch_array($result);
107
108 if ($row['cnt'] == 0) {
109         require(AT_INCLUDE_PATH.'header.inc.php');
110
111         $msg->printErrors('SEND_ENROL');
112
113         require(AT_INCLUDE_PATH.'footer.inc.php');
114         exit;
115 }
116
117
118 if (($_GET['reply'] == '') && $_GET['id']) {
119         $onload = 'document.form.subject.focus();';
120 } else if ($_GET['reply'] == '') {
121         $onload = 'document.form.to.focus();';
122 } else {
123         $onload = 'document.form.body.focus();';
124 }
125
126 require(AT_INCLUDE_PATH.'header.inc.php');
127
128 $_GET['reply'] = intval($_GET['reply']);
129 $_GET['forward'] = intval($_GET['forward']);
130
131 if ($_GET['reply']) {
132         // get the member_id of the sender
133         $result = mysql_query("SELECT from_member_id,subject,body FROM ".TABLE_PREFIX."messages WHERE message_id=$_GET[reply] AND to_member_id=$_SESSION[member_id]",$db);
134         if ($myinfo = mysql_fetch_assoc($result)) {
135                 $reply_to       = $myinfo['from_member_id'];
136                 $subject        = $myinfo['subject'];
137                 $body           = $myinfo['body'];
138         }
139 } else if ($_GET['forward']) {
140         // get the member_id of the sender
141         $result = mysql_query("SELECT subject, body FROM ".TABLE_PREFIX."messages_sent WHERE message_id=$_GET[forward] AND from_member_id=$_SESSION[member_id]",$db);
142         if ($myinfo = mysql_fetch_assoc($result)) {
143                 $reply_to       = 0;
144                 $subject        = $myinfo['subject'];
145                 $body           = $myinfo['body'];
146         }
147 }
148 if (isset($_GET['id'])) {
149         $reply_to = intval($_GET['id']);
150 }
151
152 /* check to make sure we're in the same course */
153 if ($reply_to) {
154         $sql    = "SELECT COUNT(*) AS cnt FROM ".TABLE_PREFIX."course_enrollment E1, ".TABLE_PREFIX."course_enrollment E2 WHERE E1.member_id=$_SESSION[member_id] AND E2.member_id=$reply_to AND E1.course_id=E2.course_id AND (E1.approved='y' OR E1.approved='a') AND (E2.approved='y' OR E2.approved='a')";
155         $result = mysql_query($sql, $db);
156         $row    = mysql_fetch_assoc($result);
157         $num_of_classmates = $row['cnt'];
158
159         $sql    = "SELECT COUNT(*) AS cnt FROM ".TABLE_PREFIX."social_friends SC 
160                    WHERE SC.member_id = ".$_SESSION[member_id]." 
161                    AND SC.friend_id = ".$reply_to." 
162                    OR SC.member_id = ".$reply_to." 
163                    AND SC.friend_id = ".$_SESSION[member_id];
164         $result = mysql_query($sql, $db);
165         $row    = mysql_fetch_assoc($result);
166         $num_of_contacts = $row['cnt'];
167
168         if ($num_of_classmates+$num_of_contacts == 0) {
169                 $msg->printErrors('SEND_MEMBERS');
170                 require(AT_INCLUDE_PATH.'footer.inc.php');
171                 exit;
172         }
173 }
174
175 ?>
176 <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="form">
177 <input type="hidden" name="replied" value="<?php echo $_GET['reply']; ?>" />
178
179 <div class="input-form">
180         <div class="row">
181                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="to"><?php echo _AT('to'); ?></label><br />
182                 <?php
183                         if (!$reply_to) {
184                                 $sql    = "SELECT DISTINCT M.first_name, M.second_name, M.last_name, M.login, M.member_id FROM ".TABLE_PREFIX."members M, ".TABLE_PREFIX."course_enrollment E1, ".TABLE_PREFIX."course_enrollment E2 WHERE E2.member_id=$_SESSION[member_id] AND E2.course_id=E1.course_id AND M.member_id=E1.member_id AND (E1.approved='y' OR E1.approved='a') AND (E2.approved='y' OR E2.approved='a') ORDER BY M.first_name, M.second_name, M.last_name, M.login";
185
186                                 $result = mysql_query($sql, $db);
187                                 $row    = mysql_fetch_assoc($result);
188                                 echo '<select name="to" size="1" id="to">';
189                                 do {
190                                         echo '<option value="'.$row['member_id'].'"';
191                                         if ($reply_to == $row['member_id']){
192                                                 echo ' selected="selected"';
193                                         } else if (isset($_POST['to']) && $_POST['to'] == $row['member_id']) {
194                                                 echo ' selected="selected"';
195                                         }
196                                         echo '>';
197                                         echo get_display_name($row['member_id']);
198                                         echo '</option>';
199                                 } while ($row = mysql_fetch_assoc($result));
200                                 echo '</select>';
201                         } else {
202                                 echo '<strong>'.get_display_name($reply_to).'</strong>';
203                                 echo '<input type="hidden" name="to" value="'.$reply_to.'" />';
204                         } ?>
205         </div>
206
207         <div class="row">
208                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="subject"><?php echo _AT('subject'); ?></label><br />
209                 <input type="text" name="subject" id="subject" value="<?php
210                         if (($subject != '') && ($_POST['subject'] == '')) {
211                                 if ($_GET['reply'] && !($substr($subject, 0, 2) == _AT('re'))) {
212                                         $subject = _AT('re').' : '.$subject;
213                                 } else if ($_GET['forward'] && !($substr($subject, 0, 2) == _AT('fwd'))) {
214                                         $subject = _AT('fwd').' : '.$subject;
215                                 }
216                                 echo ContentManager::cleanOutput($subject);
217                         } else {
218                                 echo ContentManager::cleanOutput($_POST['subject']);
219                         }
220                         ?>" size="40" maxlength="100" />
221         </div>
222
223         <div class="row">
224                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="body"><?php echo _AT('message'); ?></label><br />
225                 <textarea name="message" id="body" rows="15" cols="45"><?php
226                         if ($body != '') {
227                                 if ($strlen($body) > 400){
228                                         $body = $substr($body,0,400);
229                                         $pos = $strrpos($body,' ');
230                                         if ($pos===false){
231                                                 /* Unicode problem, not all language has spaces in between characters
232                                                  * No space found, chop off right on 400.
233                                                  */
234                                                  $body = $substr($body,0,400);
235                                         } else {
236                                                 $body = $substr($body,0,$pos);
237                                         }
238                                         $body .= ' ...';
239                                 }
240                                 $body  = "\n\n\n"._AT('in_reply_to').":\n".$body;
241                                 echo $body;
242                         } else {
243                                 echo $_POST['message'];
244                         }
245                 ?></textarea>
246         </div>
247
248         <div class="row buttons">
249                 <input type="submit" name="submit" value="<?php echo _AT('send'); ?>" accesskey="s" /><?php
250                 if ($reply != '') {
251                         echo '<input type="submit" name="submit_delete" value="'._AT('send_delete').'" accesskey="n" /> ';
252                 }
253                 ?> <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?>" />
254         </div>
255 </div>
256 </form> 
257
258 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>