2 /****************************************************************/
4 /****************************************************************/
5 /* Copyright (c) 2002-2009 */
6 /* Adaptive Technology Resource Centre / University of Toronto */
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 /****************************************************************/
15 require_once(AT_SOCIAL_INCLUDE.'classes/Activity.class.php');
18 * Class for individual social group
21 var $group_id; //group id
22 var $user_id; //group creator
24 var $name; //group name
25 var $type_id; //the type_id
26 var $privacy; //privacy, 0 for public, 1 for private
27 var $description; //description of this group
28 var $created_date; //sql timestamp
29 var $last_updated; //sql timestamp
30 var $group_members; //group members
31 var $group_activities; //group activities
32 var $is_valid; //set false if this is not a valid group
37 function SocialGroup($group_id){
39 $this->group_id = intval($group_id);
41 if ($this->group_id > 0){
42 $sql = 'SELECT * FROM '.TABLE_PREFIX.'social_groups WHERE id='.$this->group_id;
43 $result = mysql_query($sql, $db);
44 if (mysql_num_rows($result) > 0){
45 $row = mysql_fetch_assoc($result);
46 $this->user_id = $row['member_id'];
47 $this->logo = $row['logo'];
48 $this->name = $row['name'];
49 $this->type_id = $row['type_id'];
50 $this->privacy = $row['privacy'];
51 $this->description = $row['description'];
52 $this->created_date = $row['created_date'];
53 $this->last_updated = $row['last_updated'];
54 $this->group_members = $this->getGroupMembers();
55 $this->group_activities = $this->getGroupActivities();
56 $this->is_valid = true;
58 //group does not exist, most likely deleted
59 $this->is_valid = false;
66 * Retrieve a list of group members
68 * @return mixed array of members object
70 function getGroupMembers(){
72 if (!empty($this->group_members)){
73 return $this->group_members;
76 $sql = 'SELECT * FROM '.TABLE_PREFIX.'social_groups_members WHERE group_id='.$this->group_id;
77 $result = mysql_query($sql, $db);
79 while ($row = mysql_fetch_assoc($result)){
80 $members[] = new Member($row['member_id']);
84 //TODO Return empty array or should i return error?
90 * Get the group activities, all the activities that happens in this group.
92 * @return mixed array of activities
94 function getGroupActivities(){
96 if (!empty($this->group_activities)){
97 return $this->group_activities;
99 $activities = array();
100 $sql = 'SELECT a,id AS id, a.title AS title FROM '.TABLE_PREFIX.'social_groups_activities g LEFT JOIN '.TABLE_PREFIX.'social_activities a ON g.activity_id=a.id WHERE g.group_id='.$this->group_id;
101 $result = mysql_query($sql, $db);
103 while($row = mysql_fetch_assoc($result)){
104 $activities[$row['id']] = $row['title'];
111 * Get a specific mesage from the given user.
112 * @param int the message id.
113 * @param int the member id, the member who created this message, or the moderator.
114 * @return the text of the message
116 function getMessage($id, $member_id){
119 $member_id = intval($member_id);
121 $sql = 'SELECT body FROM '.TABLE_PREFIX.'social_groups_board WHERE group_id='.$this->getID().' AND id='.$id;
124 if($member_id!=$this->user_id){
125 $sql .= ' AND member_id='.$member_id;
128 $rs = mysql_query($sql, $db);
130 list($body) = mysql_fetch_array($rs);
131 return htmlentities_utf8($body);
137 * Get message boards message, return a list sorted by date, in descending order
139 function getMessages(){
143 $sql = 'SELECT * FROM '.TABLE_PREFIX.'social_groups_board WHERE group_id='.$this->getID().' ORDER BY created_date DESC';
144 $rs = mysql_query($sql, $db);
147 while ($row = mysql_fetch_assoc($rs)){
148 $row['body'] = htmlentities_utf8($row['body']); //escape xss attack
149 $result [$row['id']] = $row;
157 * Get the group information
160 return $this->group_id;
163 return $this->user_id;
165 function getGroupType(){
167 //or maybe print out the exact type name
168 $sql = 'SELECT title FROM '.TABLE_PREFIX.'social_groups_types WHERE type_id='.$this->type_id;
169 $result = mysql_query($sql, $db);
170 list($type_name) = mysql_fetch_row($result);
171 return _AT($type_name);
174 if (!empty($this->logo)) {
175 $str = '<a href="'.url_rewrite(AT_SOCIAL_BASENAME.'groups/view.php?id='.$this->getID()).'"><img border="0" src="'.AT_SOCIAL_BASENAME.'groups/get_sgroup_logo.php?id='.$this->getID().'" alt="'.$this->getName().'" title="'.$this->getName().'"/></a>';
177 $str = '<img src="'.AT_SOCIAL_BASENAME.'images/placelogo.png" alt="'._AT('placelogo').'" title="'._AT('placelogo').'"/>';
183 return htmlentities_utf8($this->name);
185 //@param boolean change all carrier returns to <br/> if true.
186 function getDescription($use_nl2br=true){
187 return htmlentities_utf8($this->description, $use_nl2br);
189 function getCreatedDate(){
190 return $this->created_date;
192 function getPrivacy(){
193 //0 for public, 1 for private
194 return $this->privacy;
196 function getLastUpdated(){
197 return $this->last_updated;
200 return $this->is_valid;
204 * Add a member to the group
205 * @param int member id
206 * @return boolean true if succeded, false otherwise.
208 function addMember($member_id){
210 $member_id = intval($member_id);
212 $sql = 'INSERT INTO '.TABLE_PREFIX.'social_groups_members (group_id, member_id) VALUES ('.$this->group_id.", $member_id)";
213 $result = mysql_query($sql, $db);
215 //add a record to the activities
216 $act = new Activity();
217 $str1 = printSocialName($friend_id).' has joined the group, <a href="social/groups/view.php?id='.$this->getID().'">'.htmlentities_utf8($this->getName()).'</a>';
218 $act->addActivity($member_id, $str1);
227 * Sends an invitation to a member
228 * @param int member_id
230 function addInvitation($member_id) {
232 $member_id = intval($member_id);
234 $sql = 'INSERT INTO '.TABLE_PREFIX.'social_groups_invitations (sender_id, member_id, group_id) VALUES ('
235 .$_SESSION['member_id'].', '.$member_id.', '.$this->getID().')';
236 $result = mysql_query($sql, $db);
241 * Sends a request to the group creator
242 * @param int member_id
244 function addRequest(){
247 $sql = 'INSERT INTO '.TABLE_PREFIX.'social_groups_requests (sender_id, member_id, group_id) VALUES ('
248 .$_SESSION['member_id'].', '.$this->getUser().', '.$this->getID().')';
249 $result = mysql_query($sql, $db);
254 * Add message to the message board
255 * @param string the message body
257 function addMessage($body) {
258 global $db, $addslashes;
259 $body = $addslashes($body);
260 $member_id = $_SESSION['member_id'];
261 $group_id = $this->getID();
263 $sql = 'INSERT INTO '.TABLE_PREFIX."social_groups_board (member_id, group_id, body, created_date) VALUES ($member_id, $group_id, '$body', NOW())";
264 $result = mysql_query($sql, $db);
271 * @param string filename of the logo. <name.extension>
273 function updateGroupLogo($logo) {
274 global $db, $addslashes;
275 $logo = $addslashes($logo);
276 $sql = 'UPDATE '.TABLE_PREFIX."social_groups SET logo='$logo' WHERE id=".$this->getID();
277 $result = mysql_query($sql, $db);
284 * @param int the id of the message
285 * @param string message body
287 function updateMessage($id, $body){
288 global $db, $addslashes;
290 $body = $addslashes($body);
295 $sql = 'UPDATE '.TABLE_PREFIX."social_groups_board SET body='$body' WHERE id=$id";
296 $result = mysql_query($sql, $db);
302 * Remove a member from the group
303 * @param int member_id
304 * @retrun boolean troe successful and false otherwise.
306 function removeMember($member_id){
308 $member_id = intval($member_id);
310 //quit if member_id = creator id
311 if ($member_id == $this->getUser()){
315 $sql = 'DELETE FROM '.TABLE_PREFIX."social_groups_members WHERE member_id=$member_id AND group_id=".$this->group_id;
316 $result = mysql_query($sql, $db);
324 * Remove logo from content/social folder
326 function removeGroupLogo(){
327 if ($this->logo!=''){
328 unlink(AT_CONTENT_DIR.'social/'. $this->logo);
330 return file_exists(AT_CONTENT_DIR.'social/'. $this->logo);
336 * Delete all group activities
337 * ToDo: Delete just one?
339 function removeGroupActivities(){
342 $act_obj = new Activity();
344 //First remove groups activities from activity table
345 $allActs = $this->group_activities;
346 foreach ($allActs as $id=>$garbage) {
347 $act_obj->deleteActivity($id);
350 //Then remove the associations from social_groups_activities
351 $sql = 'DELETE FROM '.TABLE_PREFIX."social_groups_activities WHERE group_id=".$this->group_id;
352 $result = mysql_query($sql, $db);
361 * Delete all group forums
363 function removeGroupForums(){
365 include(AT_INCLUDE_PATH.'lib/forums.inc.php');
367 //delete all forums for this social group
368 $sql = 'SELECT forum_id FROM '.TABLE_PREFIX.'social_groups_forums WHERE group_id='.$this->group_id;
369 $result = mysql_query($sql, $db);
371 while ($row = mysql_fetch_assoc($result)){
372 delete_forum($row['forum_id']);
376 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_groups_forums WHERE group_id='.$this->group_id;
377 $result = mysql_query($sql, $db);
386 * Delete all group members
388 function removeGroupMembers(){
390 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_groups_members WHERE group_id='.$this->group_id;
391 $result = mysql_query($sql, $db);
400 * Delete all requests inside this group
402 function removeGroupRequests(){
404 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_groups_requests WHERE group_id='.$this->group_id;
405 $result = mysql_query($sql, $db);
414 * Delete all invitations inside this group
416 function removeGroupInvitations(){
418 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_groups_invitations WHERE group_id='.$this->group_id;
419 $result = mysql_query($sql, $db);
427 * Delete a message from the board
428 * @param int member_id
430 function removeMessage($id, $member_id){
433 $member_id = intval($member_id);
434 $sql = 'DELETE FROM '.TABLE_PREFIX."social_groups_board WHERE id=$id ";
437 if ($member_id != $this->user_id){
438 $sql .= " AND member_id=$member_id";
440 $result = mysql_query($sql, $db);
446 * Delete all the messages from the board
448 function removeAllMessages(){
451 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_groups_board WHERE group_id='.$this->getID();
452 $result = mysql_query($sql, $db);
459 * TODO: Maybe to make a general search($string, $member_obj_array) that takess any member obj array.
460 * This can be used for friends search as well. Optimize the code and structure a bit.
461 * @param string member name
462 * @return array of Members Object
464 function searchMembers($name){
465 global $db, $addslashes;
467 //break the names by space, then accumulate the query
468 $name = $addslashes($name);
469 $sub_names = explode(' ', $name);
470 foreach($sub_names as $piece){
471 $query .= "(first_name LIKE '%$piece%' OR second_name LIKE '%$piece%' OR last_name LIKE '%$piece%' OR email LIKE '$piece') AND ";
473 //trim back the extra "AND "
474 $query = substr($query, 0, -4);
476 $sql = 'SELECT * FROM '.TABLE_PREFIX.'social_groups_members g LEFT JOIN '.TABLE_PREFIX.'members m ON g.member_id=m.member_id WHERE g.group_id='.$this->getID().' AND '.$query;
477 $rs = mysql_query($sql, $db);
479 while ($row=mysql_fetch_assoc($rs)) {
480 $result[$row['member_id']] = new Member($row['member_id']);