5a1b4fe8e3c9a59d5183c70ad05e38fed732fc51
[atutor.git] / docs / mods / _standard / social / lib / classes / SocialGroups / SocialGroups.class.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2009                                                                              */
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
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 = _AT('has_added_group', '<a href="'. url_rewrite(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 = _AT('has_updated_group', '<a href="'. url_rewrite(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      int     the index of which the entry to get
194           * @param      string  filters
195           */
196          function search($query, $offset=-1, $filters=''){
197                 /* Perform a simple search for now
198                  * That searches only the title? 
199                  * Use Joel's search idea? Point based system search? The Google's idea?
200                  *
201                  * Idea:
202                  * 1. Get all results that matches the query separtated by space
203                  * 2. Give points to the result that has more match, or whatever our "point system" is
204                  * 3. Sort it in the order of most points to the least points.  
205                  */
206                  global $db, $addslashes;
207
208                  $sql = 'SELECT * FROM '.TABLE_PREFIX.'social_groups';
209                  if ($query!=''){
210                          $search_result = array();
211                          $query = $addslashes(trim($query));
212                          $words = explode(' ', $query);
213                          foreach($words as $piece){
214                                 $extra .= "`name` LIKE '%$piece%' OR ";
215                                 $extra .= "`description` LIKE '%$piece%' OR ";
216                          }
217                          $extra = substr($extra, 0, -3);
218                          
219                          $sql .= ' WHERE '.$extra;
220                  }
221                  $result = mysql_query($sql, $db);
222
223                  if ($result){
224                         while ($row = mysql_fetch_assoc($result)){
225                                 $search_result[$row['id']]['obj'] = new SocialGroup($row['id']);
226                                 $search_result[$row['id']]['weight'] = $this->inQuery($words, $row['name']);
227                         }
228                  }
229                  if (!empty($search_result)){
230                          uasort($search_result, array($this, 'search_cmp'));
231                          $search_result = array_reverse($search_result);
232
233                          //for paginator
234                          if ($offset >= 0){
235                                 $search_result = array_slice($search_result, $offset, SOCIAL_GROUP_MAX);
236                          }
237                  }
238
239                  return $search_result;
240          }
241
242
243          /**
244           * Return the counts of word appeareance in the given string.  
245           * @param      array   the string that the user typed in to search for
246           * @param      string  the name of the group
247           */
248          function inQuery($words, $str){
249                  //if either of the input is empty, there is no comparison thus no count.
250                  if (empty($words) || $str==''){
251                          return 0;
252                  }
253                  $count = 0;
254                  foreach ($words as $index=>$word){
255                          if (trim($word)=='') continue;
256                          $count += substr_count($str, strtolower($word));
257                  }
258                  return $count;
259          }
260         
261
262         function search_cmp($row1, $row2){
263                 if ($row1['weight'] == $row2['weight']) return 0;
264                 return ($row1['weight'] < $row2['weight']) ? -1 : 1;
265         }
266 }
267 ?>