af341621edd8059939a4c256ecdd4f4c26dcb29e
[atutor.git] / mods / social / lib / classes / SocialGroups / SocialGroups.class.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
15 require_once(AT_SOCIAL_INCLUDE.'classes/Activity.class.php');
16
17 /**
18  * Social Groups, this is different from the Groups class in ATutor.
19  */
20 class SocialGroups{
21
22         /**
23          * constructor
24          */
25          function SocialGroups(){}
26
27         /**
28          * Adding a group
29          * @param       int             the group type specified in the table, social_groups_types
30          * @param       string  name of the group
31          * @param       string  description of the group
32          * @param       int             privacy setting, public is 0, private is 1.  Public means everyone can see the message board and users.  Private is the opposite
33          * @return      the id of this new group if succeded, false otherwise.
34          */
35          function addGroup($type_id, $name, $description, $privacy){
36                  global $db, $addslashes;
37
38                  $type_id = intval($type_id);
39                  $name = $addslashes($name);
40                  $description = $addslashes($description);
41                  $privacy = intval($privacy);
42                  $member_id = $_SESSION['member_id'];
43
44                  $sql = 'INSERT INTO '.TABLE_PREFIX."social_groups (`member_id`, `type_id`, `name`, `description`, `privacy`, `created_date`, `last_updated`) VALUES ($member_id, $type_id, '$name', '$description', $privacy, NOW(), NOW())";
45                  $result = mysql_query($sql, $db);
46                  $group_id = mysql_insert_id();
47                  if ($result){
48                          //add it to the group member table
49                          $sql = 'INSERT INTO '.TABLE_PREFIX."social_groups_members (group_id, member_id) VALUES ($group_id, $_SESSION[member_id])";
50                          $result = mysql_query($sql, $db);
51                          if ($result){
52                                 $act = new Activity();          
53                                 $str1 = ' has added the group, <a href="'.AT_SOCIAL_BASENAME.'groups/view.php?id='.$group_id.'">'.htmlentities_utf8($name).'</a>';
54                                 $act->addActivity($member_id, $str1);
55                                 unset($act);
56                          }
57                          return $group_id;
58                  }
59                  return false;
60          }
61
62         /**
63          * Removing a group, invovles removing everything related to this group.
64          * @param       int             the group id
65          */
66          function removeGroup($id){
67                  global $db;
68                  $social_group = new SocialGroup($id);
69
70                  //remove group activities
71                  $status = $social_group->removeGroupActivities();
72
73                  //remove group forums
74                  $status &= $social_group->removeGroupForums();
75
76                  //remove group members
77                  $status &= $social_group->removeGroupMembers();
78
79                  //remove group requests
80                  $status &= $social_group->removeGroupRequests();
81
82                  //remove group invitations
83                  $status &= $social_group->removeGroupInvitations();
84
85                  //remove message board
86                  $status &= $social_group->removeAllMessages();
87
88                  //remove group logo
89                  $status &= $social_group->removeGroupLogo();
90
91                  //remove groups 
92                  $sql = 'DELETE FROM '.TABLE_PREFIX.'social_groups WHERE id='.$id;
93                  $status &= mysql_query($sql, $db);
94          }
95
96
97         /**
98          * Update a group
99          * @param       int             group id
100          * @param       int             member_id, to update the owner of this group
101          * @param       int             the group type specified in the table, social_groups_type
102          * @param       string  name of the group
103          * @param       string  description of the group
104          * @param       string  the filename of the logo
105          * @param       string  group privacy, private for 1, public for 0
106          */
107          function updateGroup($group_id, $member_id, $type_id, $name, $description, $logo, $privacy){
108                  global $db, $addslashes;
109
110                  $group_id = intval($group_id);
111                  $member_id = intval($member_id);
112                  $type_id = intval($type_id);
113                  $name = $addslashes($name);
114                  $description = $addslashes($description);
115                  $logo = $addslashes($logo);
116                  $privacy = ($privacy=='private')?1:0;
117                  //only include logo sql iff it is not empty, otherwise the old entry will be erased.
118                  if ($logo!=''){
119                          $logo_sql = "`logo`='$logo', ";
120                  } else {
121                          $logo_sql = '';
122                  }
123
124                  $sql = 'UPDATE '.TABLE_PREFIX."social_groups SET `member_id`=$member_id, `type_id`=$type_id, ".$logo_sql."`name`='$name', `privacy`=$privacy, `description`='$description', `last_updated`=NOW() WHERE id=$group_id";
125                  $result = mysql_query($sql, $db);
126                  if ($result){
127                          $act = new Activity();         
128                          $str1 = ' has updated the group, <a href="'.AT_SOCIAL_BASENAME.'groups/view.php?id='.$group_id.'">'.htmlentities_utf8($name).'</a>';
129                          $act->addActivity($member_id, $str1);
130                          unset($act);
131                          return true;
132                  } 
133                  return false;
134          }
135
136
137         /**
138          * Get all the group types
139          * @return      list of group types
140          */
141          function getAllGroupType(){
142                  global $db, $addslashes;
143                  $group_types = array();
144
145                  $sql = 'SELECT * FROM '.TABLE_PREFIX.'social_groups_types';
146                  $result = mysql_query($sql, $db);
147                  if ($result){
148                          while($row = mysql_fetch_assoc($result)){
149                                 $group_types[$row['type_id']] = $row['title'];
150                          }
151                  }
152                  return $group_types;
153          }
154
155           
156         /**
157          * Get the group infromation from the sql       
158          * @param       int             group id
159          * @return mixed        SocialGroup obj
160          */
161          function getGroup($group_id){
162                 $socialGroup = new SocialGroup($group_id);
163                 return $socialGroup;
164          }
165
166         
167         /**
168          * Get ALL of a person's groups
169          * @param       int     the groups that this member is in.
170          * @param       int     the index of which the entry to get
171          */
172          function getMemberGroups($member_id, $offset=-1){
173                  global $db;
174                  $my_groups = array();
175
176                  $sql = 'SELECT group_id FROM '.TABLE_PREFIX.'social_groups_members WHERE member_id='.$member_id;
177                  if ($offset >= 0){
178                         $sql .= " LIMIT $offset, ".SOCIAL_GROUP_MAX;
179                  }
180                  $result = mysql_query($sql, $db);
181                  if ($result){
182                          while($row = mysql_fetch_assoc($result)){
183                                 $my_groups[] = $row['group_id'];
184                          }
185                  }
186                  return $my_groups;
187          }
188
189
190          /**
191           * Search
192           * @param      string  query string for the search
193           * @param      string  filters
194           */
195          function search($query, $filters=''){
196                 /* Perform a simple search for now
197                  * That searches only the title? 
198                  * Use Joel's search idea? Point based system search? The Google's idea?
199                  *
200                  * Idea:
201                  * 1. Get all results that matches the query separtated by space
202                  * 2. Give points to the result that has more match, or whatever our "point system" is
203                  * 3. Sort it in the order of most points to the least points.  
204                  */
205                  global $db, $addslashes;
206
207                  if ($query=='') return array();  //quit if search query is empty
208
209                  $search_result = array();
210                  $query = $addslashes(trim($query));
211                  $words = explode(' ', $query);
212                  foreach($words as $piece){
213                         $extra .= "`name` LIKE '%$piece%' OR ";
214                         $extra .= "`description` LIKE '%$piece%' OR ";
215                  }
216                  $extra = substr($extra, 0, -3);
217                  $sql = 'SELECT * FROM '.TABLE_PREFIX.'social_groups WHERE '.$extra;
218                  $result = mysql_query($sql, $db);
219                  if ($result){
220                         while ($row = mysql_fetch_assoc($result)){
221                                 $search_result[$row['id']]['obj'] = new SocialGroup($row['id']);
222                                 $search_result[$row['id']]['weight'] = $this->inQuery($words, $row['name']);
223                         }
224                  }
225                  uasort($search_result, array($this, 'search_cmp'));
226                  return array_reverse($search_result);
227          }
228
229
230          /**
231           * Return the counts of word appeareance in the given string.  
232           * @param      array   the string that hte user typed in to search for
233           * @param      string  the name of the group
234           */
235          function inQuery($words, $str){
236                  $count = 0;
237                  foreach ($words as $index=>$word){
238                          if (trim($word)=='') continue;
239                          $count += substr_count($str, strtolower($word));
240                  }
241                  return $count;
242          }
243         
244
245         function search_cmp($row1, $row2){
246                 if ($row1['weight'] == $row2['weight']) return 0;
247                 return ($row1['weight'] < $row2['weight']) ? -1 : 1;
248         }
249 }
250 ?>