ea088bd40ce86267cfb3bdebdd79e67d9b4e4a5f
[atutor.git] / mods / social / connections.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2009                                                                              */
6 /* Adaptive Technology Resource Centre / University of Toronto  */
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 require(AT_SOCIAL_INCLUDE.'constants.inc.php');
19 require(AT_SOCIAL_INCLUDE.'friends.inc.php');
20 require(AT_SOCIAL_INCLUDE.'classes/PrivacyControl/PrivacyObject.class.php');
21 require(AT_SOCIAL_INCLUDE.'classes/PrivacyControl/PrivacyController.class.php');
22 $_custom_css = $_base_path . AT_SOCIAL_BASENAME . 'module.css'; // use a custom stylesheet
23
24
25 if (!$_SESSION['valid_user']) {
26         require(AT_INCLUDE_PATH.'header.inc.php');
27         $info = array('INVALID_USER', $_SESSION['course_id']);
28         $msg->printInfos($info);
29         require(AT_INCLUDE_PATH.'footer.inc.php');
30         exit;
31 }
32
33 // default display my friends
34 $friends = getFriends($_SESSION['member_id']);
35 $rand_key = $addslashes($_POST['rand_key']);    //should we excape?
36
37 //paginator settings
38 $page = intval($_GET['p']);
39 if (!$page) {
40         $page = 1;
41 }       
42 $count  = (($page-1) * SOCIAL_FRIEND_SEARCH_MAX) + 1;
43 $offset = ($page-1) * SOCIAL_FRIEND_SEARCH_MAX;
44
45
46 //if $_GET['q'] is set, handle Ajax.
47 if (isset($_GET['q'])){
48         $query = $_GET['q'];    //don't need to addslashes here cause searchFriends will do it.
49
50         if (isset($_POST['myFriendsOnly'])){
51                 //retrieve a list of my friends
52                 $search_result = searchFriends($query, true);
53         } else {
54                 //retrieve a list of friends by the search
55                 $search_result = searchFriends($query);
56         }
57
58         if (!empty($search_result)){
59                 echo '<div class="suggestions">'._AT('suggestions').':<br/>';
60                 $counter = 0;
61                 foreach($search_result as $member_id=>$member_array){
62                         //display 10 suggestions
63                         if ($counter > 10){
64                                 break;
65                         }
66
67                         echo '<a href="javascript:void(0);" onclick="document.getElementById(\'search_friends\').value=\''.printSocialName($member_id, false).'\'; document.getElementById(\'search_friends_form\').submit();">'.printSocialName($member_id, false).'</a><br/>';
68                         $counter++;
69                 }
70                 echo '</div>';
71         }
72         exit;
73 }
74
75 //safe guard
76 if (isset($_GET['id'])){
77         $id = intval($_GET['id']);
78         if($id > 0){
79                 addFriendRequest($id);
80                 $msg->addFeedback('REQUEST_FRIEND_ADDED');
81                 $sql_notify = "SELECT first_name, last_name, email FROM ".TABLE_PREFIX."members WHERE member_id=$id";
82                 $result_notify = mysql_query($sql_notify, $db);
83                 $row_notify = mysql_fetch_assoc($result_notify);
84
85                 if ($row_notify['email'] != '') {
86                         require(AT_INCLUDE_PATH . 'classes/phpmailer/atutormailer.class.php');
87                         $body = _AT('notification_new_contact', get_display_name($_SESSION['member_id']), $_base_href.AT_SOCIAL_BASENAME.'index_mystart.php');
88                         $sender = get_display_name($_SESSION['member_id']);
89                         $mail = new ATutorMailer;
90                         $mail->AddAddress($row_notify['email'], $sender);
91                         $mail->FromName = $_config['site_name'];
92                         $mail->From     = $_config['contact_email'];
93                         $mail->Subject  = _AT('contact_request');
94                         $mail->Body     = $body;
95
96                         if(!$mail->Send()) {
97                                 $msg->addError('SENDING_ERROR');
98                         }
99                         unset($mail);
100                 }
101
102                 header('Location: '.url_rewrite(AT_SOCIAL_BASENAME.'connections.php', AT_PRETTY_URL_IS_HEADER));
103                 exit;
104         }
105 }
106
107 //handle search friends request
108 if(($rand_key!='' && isset($_POST['search_friends_'.$rand_key])) || isset($_GET['search_friends'])){
109         if (empty($_POST['search_friends_'.$rand_key]) && !isset($_GET['search_friends'])){
110                 $msg->addError('CANNOT_BE_EMPTY');
111                 header('Location: '.url_rewrite(AT_SOCIAL_BASENAME.'connections.php', AT_PRETTY_URL_IS_HEADER));
112                 exit;
113         }
114         //to adapt paginator GET queries
115         //don't need to apply addslashes here cause searchFriends will do it.
116         if($_GET['search_friends']){
117                 $search_field = $_GET['search_friends'];
118         } else {
119                 $search_field = $_POST['search_friends_'.$rand_key];
120         }
121         if (isset($_POST['myFriendsOnly'])){
122                 //retrieve a list of my friends
123                 $friends = searchFriends($search_field, true);
124         } else {
125                 //retrieve a list of friends by the search
126                 $friends = searchFriends($search_field);        //to calculate the total number. TODO: need a better way, wasting runtime.
127                 $num_pages = max(ceil(sizeof($friends) / SOCIAL_FRIEND_SEARCH_MAX), 1);
128                 $friends = searchFriends($search_field, false, $offset);
129         }
130 }       
131
132 //mark those that are already added
133 $friends = markFriends($_SESSION['member_id'], $friends);
134 include(AT_INCLUDE_PATH.'header.inc.php');
135 $savant->display('pubmenu.tmpl.php');
136 $savant->assign('page', $page);
137 $savant->assign('num_pages', $num_pages);
138 $savant->assign('search_field', $search_field);
139 $savant->assign('friends', $friends);
140 $savant->assign('rand_key', $rand_key);
141 $savant->display('connections.tmpl.php');
142 include(AT_INCLUDE_PATH.'footer.inc.php');
143 ?>