2 /****************************************************************/
4 /****************************************************************/
5 /* Copyright (c) 2002-2009 */
6 /* Inclusive Design Institute */
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 * Social Groups, this is different from the Groups class in ATutor.
25 function SocialGroups(){}
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.
35 function addGroup($type_id, $name, $description, $privacy){
36 global $db, $addslashes;
38 $type_id = intval($type_id);
39 $name = $addslashes($name);
40 $description = $addslashes($description);
41 $privacy = intval($privacy);
42 $member_id = $_SESSION['member_id'];
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();
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);
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);
63 * Removing a group, invovles removing everything related to this group.
64 * @param int the group id
66 function removeGroup($id){
68 $social_group = new SocialGroup($id);
70 //remove group activities
71 $status = $social_group->removeGroupActivities();
74 $status &= $social_group->removeGroupForums();
76 //remove group members
77 $status &= $social_group->removeGroupMembers();
79 //remove group requests
80 $status &= $social_group->removeGroupRequests();
82 //remove group invitations
83 $status &= $social_group->removeGroupInvitations();
85 //remove message board
86 $status &= $social_group->removeAllMessages();
89 $status &= $social_group->removeGroupLogo();
92 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_groups WHERE id='.$id;
93 $status &= mysql_query($sql, $db);
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
107 function updateGroup($group_id, $member_id, $type_id, $name, $description, $logo, $privacy){
108 global $db, $addslashes;
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.
119 $logo_sql = "`logo`='$logo', ";
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);
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);
138 * Get all the group types
139 * @return list of group types
141 function getAllGroupType(){
142 global $db, $addslashes;
143 $group_types = array();
145 $sql = 'SELECT * FROM '.TABLE_PREFIX.'social_groups_types';
146 $result = mysql_query($sql, $db);
148 while($row = mysql_fetch_assoc($result)){
149 $group_types[$row['type_id']] = $row['title'];
157 * Get the group infromation from the sql
158 * @param int group id
159 * @return mixed SocialGroup obj
161 function getGroup($group_id){
162 $socialGroup = new SocialGroup($group_id);
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
172 function getMemberGroups($member_id, $offset=-1){
174 $my_groups = array();
176 $sql = 'SELECT group_id FROM '.TABLE_PREFIX.'social_groups_members WHERE member_id='.$member_id;
178 $sql .= " LIMIT $offset, ".SOCIAL_GROUP_MAX;
180 $result = mysql_query($sql, $db);
182 while($row = mysql_fetch_assoc($result)){
183 $my_groups[] = $row['group_id'];
192 * @param string query string for the search
193 * @param int the index of which the entry to get
194 * @param string filters
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?
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.
206 global $db, $addslashes;
208 $sql = 'SELECT * FROM '.TABLE_PREFIX.'social_groups';
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 ";
217 $extra = substr($extra, 0, -3);
219 $sql .= ' WHERE '.$extra;
221 $result = mysql_query($sql, $db);
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']);
229 if (!empty($search_result)){
230 uasort($search_result, array($this, 'search_cmp'));
231 $search_result = array_reverse($search_result);
235 $search_result = array_slice($search_result, $offset, SOCIAL_GROUP_MAX);
239 return $search_result;
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
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==''){
254 foreach ($words as $index=>$word){
255 if (trim($word)=='') continue;
256 $count += substr_count($str, strtolower($word));
262 function search_cmp($row1, $row2){
263 if ($row1['weight'] == $row2['weight']) return 0;
264 return ($row1['weight'] < $row2['weight']) ? -1 : 1;