move code up one directory
[atutor.git] / mods / _standard / social / lib / classes / SocialGroups / SocialGroup.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  * Class for individual social group
19  */
20 class SocialGroup {
21         var $group_id;                  //group id
22         var $user_id;                   //group creator
23         var $logo;                              //logo
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
33
34         /**
35          * Constructor
36          */
37         function SocialGroup($group_id){
38                 global $db;
39                 $this->group_id = intval($group_id);
40
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;
57                         } else {
58                                 //group does not exist, most likely deleted
59                                 $this->is_valid                 = false;
60                         }
61                 }
62         }
63
64
65         /**
66          * Retrieve a list of group members 
67          * @param       int             group id
68          * @return      mixed   array of members object
69          */
70          function getGroupMembers(){
71                 global $db;
72                 if (!empty($this->group_members)){
73                         return $this->group_members;
74                 }
75                 $members = array();
76                 $sql = 'SELECT * FROM '.TABLE_PREFIX.'social_groups_members WHERE group_id='.$this->group_id;
77                 $result = mysql_query($sql, $db);
78                 if ($result){
79                         while ($row = mysql_fetch_assoc($result)){
80                                 $members[] = new Member($row['member_id']);
81                         }
82                 }
83
84                 //TODO Return empty array or should i return error?
85                 return $members;
86          } 
87
88
89         /**
90          * Get the group activities, all the activities that happens in this group.
91          * @param       int             group id
92          * @return      mixed   array of activities 
93          */
94          function getGroupActivities(){
95                  global $db;
96                  if (!empty($this->group_activities)){
97                         return $this->group_activities;
98                 }
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);
102                  if ($result){
103                          while($row = mysql_fetch_assoc($result)){
104                                  $activities[$row['id']] = $row['title'];
105                          }
106                  }
107                  return $activities;
108          }
109
110         /**
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
115          */
116         function getMessage($id, $member_id){
117                 global $db;
118                 $id = intval($id);
119                 $member_id = intval($member_id);
120                 
121                 $sql = 'SELECT body FROM '.TABLE_PREFIX.'social_groups_board WHERE group_id='.$this->getID().' AND id='.$id;
122                 
123                 //if not moderator
124                 if($member_id!=$this->user_id){
125                         $sql .= ' AND member_id='.$member_id;
126                 } 
127
128                 $rs = mysql_query($sql, $db);
129                 if($rs){
130                         list($body) = mysql_fetch_array($rs);
131                         return htmlentities_utf8($body);
132                 }
133                 return false;
134         }
135
136         /**
137          * Get message boards message, return a list sorted by date, in descending order
138          */
139          function getMessages(){
140                  global $db;
141                  $result = array();     
142
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);
145
146                  if ($rs){
147                          while ($row = mysql_fetch_assoc($rs)){
148                                 $row['body'] = htmlentities_utf8($row['body']); //escape xss attack
149                                 $result [$row['id']] = $row;
150                          }
151                  }
152                  return $result;
153          }
154
155
156         /**
157          * Get the group information
158          */
159          function getID(){
160                  return $this->group_id;
161          }
162          function getUser(){
163                 return $this->user_id;
164          }
165          function getGroupType(){
166                  global $db;
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);
172          }
173          function getLogo()     {
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>';
176                 } else {
177                         $str = '<img src="'.AT_SOCIAL_BASENAME.'images/placelogo.png" alt="'._AT('placelogo').'" title="'._AT('placelogo').'"/>';
178                 }
179                 
180                  return $str;
181          }
182          function getName() {
183                  return htmlentities_utf8($this->name);
184          }
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);
188          }
189          function getCreatedDate(){
190                  return $this->created_date;
191          }
192          function getPrivacy(){
193                  //0 for public, 1 for private
194                  return $this->privacy;
195          }
196          function getLastUpdated(){
197                  return $this->last_updated;
198          }
199          function isValid(){
200                  return $this->is_valid;
201          }
202
203         /**
204          * Add a member to the group
205          * @param       int             member id
206          * @return      boolean true if succeded, false otherwise.
207          */
208          function addMember($member_id){
209                 global $db;
210                 $member_id = intval($member_id);
211
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);
214                 if ($result){
215                         //add a record to the activities
216                         $act = new Activity();          
217                         $str1 = _AT('has_joined_group', '<a href="'. url_rewrite(AT_SOCIAL_BASENAME . 'groups/view.php?id='.$this->getID(), AT_PRETTY_URL_IS_HEADER).'">'.htmlentities_utf8($this->getName()).'</a>');
218
219                         $act->addActivity($member_id, $str1);
220                         unset($act);
221                         return true;
222                 }
223                 return false;
224          }
225
226
227         /**
228          * Sends an invitation to a member
229          * @param       int             member_id
230          */
231          function addInvitation($member_id) {
232                 global $db;
233                 $member_id = intval($member_id);
234
235                 $sql = 'INSERT INTO '.TABLE_PREFIX.'social_groups_invitations (sender_id, member_id, group_id) VALUES ('
236                                 .$_SESSION['member_id'].', '.$member_id.', '.$this->getID().')';
237                 $result = mysql_query($sql, $db);
238          }
239
240
241         /** 
242          * Sends a request to the group creator 
243          * @param       int             member_id
244          */
245         function addRequest(){
246                 global $db;
247         
248                 $sql = 'INSERT INTO '.TABLE_PREFIX.'social_groups_requests (sender_id, member_id, group_id) VALUES ('
249                                 .$_SESSION['member_id'].', '.$this->getUser().', '.$this->getID().')';
250                 $result = mysql_query($sql, $db);
251                 return $result;
252         }
253         
254         /**
255          * Add message to the message board
256          * @param       string  the message body
257          */
258          function addMessage($body) {
259                  global $db, $addslashes;
260                  $body = $addslashes($body);
261                  $member_id = $_SESSION['member_id'];
262                  $group_id = $this->getID();
263
264                  $sql = 'INSERT INTO '.TABLE_PREFIX."social_groups_board (member_id, group_id, body, created_date) VALUES ($member_id, $group_id, '$body', NOW())";
265                  $result = mysql_query($sql, $db);
266                  return $result;
267          }
268
269         
270         /**
271          * Update group logo
272          * @param       string  filename of the logo. <name.extension>
273          */
274         function updateGroupLogo($logo) {
275                 global $db, $addslashes;
276                 $logo = $addslashes($logo);
277                 $sql = 'UPDATE '.TABLE_PREFIX."social_groups SET logo='$logo' WHERE id=".$this->getID();
278                 $result = mysql_query($sql, $db);
279                 return $result;
280         }
281         
282
283         /** 
284          * Edit message 
285          * @param       int             the id of the message
286          * @param       string  message body
287          */
288          function updateMessage($id, $body){
289                  global $db, $addslashes;
290                  $id = intval($id);
291                  $body = $addslashes($body);
292                 if ($id <= 0){
293                         return false;
294                 }
295
296                 $sql = 'UPDATE '.TABLE_PREFIX."social_groups_board SET body='$body' WHERE id=$id";
297                 $result = mysql_query($sql, $db);
298                 return $result;
299          }
300         
301
302         /** 
303          * Remove a member from the group
304          * @param       int             member_id
305          * @retrun      boolean troe successful and false otherwise.
306          */
307          function removeMember($member_id){
308                 global $db;
309                 $member_id = intval($member_id);
310
311                 //quit if member_id = creator id
312                 if ($member_id == $this->getUser()){
313                         return false;
314                 }
315
316                 $sql = 'DELETE FROM '.TABLE_PREFIX."social_groups_members WHERE member_id=$member_id AND group_id=".$this->group_id;
317                 $result = mysql_query($sql, $db);
318                 if ($result){
319                         return true;
320                 }
321                 return false;
322          }
323
324         /**
325          * Remove logo from content/social folder
326          */
327         function removeGroupLogo(){     
328                 if ($this->logo!=''){
329                         unlink(AT_CONTENT_DIR.'social/'. $this->logo);
330                 }
331                 return file_exists(AT_CONTENT_DIR.'social/'. $this->logo);
332         }
333
334
335
336         /**
337          * Delete all group activities
338          * ToDo: Delete just one?
339          */
340          function removeGroupActivities(){
341                  global $db;
342
343                  $act_obj = new Activity();
344
345                  //First remove groups activities from activity table
346                  $allActs = $this->group_activities;
347                  foreach ($allActs as $id=>$garbage) {
348                         $act_obj->deleteActivity($id);
349                  }
350
351                  //Then remove the associations from social_groups_activities
352                  $sql = 'DELETE FROM '.TABLE_PREFIX."social_groups_activities WHERE group_id=".$this->group_id;
353                  $result = mysql_query($sql, $db);
354                  if ($result){
355                          return true;
356                  }
357                  return false;
358          }
359
360
361         /**
362          * Delete all group forums
363          */
364         function removeGroupForums(){
365                 global $db;
366                 include(AT_INCLUDE_PATH.'../mods/_standard/forums/lib/forums.inc.php');
367                 
368                 //delete all forums for this social group
369                 $sql = 'SELECT forum_id FROM '.TABLE_PREFIX.'social_groups_forums WHERE group_id='.$this->group_id;
370                 $result = mysql_query($sql, $db);
371                 if ($result){
372                         while ($row = mysql_fetch_assoc($result)){
373                                 delete_forum($row['forum_id']);
374                         }
375                 }
376
377                 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_groups_forums WHERE group_id='.$this->group_id;
378                 $result = mysql_query($sql, $db);
379                 if ($result){
380                         return true;
381                 } 
382                 return false;
383         }
384
385         
386         /**
387          * Delete all group members
388          */
389         function removeGroupMembers(){
390                 global $db;
391                 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_groups_members WHERE group_id='.$this->group_id;
392                 $result = mysql_query($sql, $db);
393                 if ($result){
394                         return true;
395                 }
396                 return false;
397         }
398
399
400         /** 
401          * Delete all requests inside this group
402          */
403         function removeGroupRequests(){
404                 global $db;
405                 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_groups_requests WHERE group_id='.$this->group_id;
406                 $result = mysql_query($sql, $db);
407                 if ($result){
408                         return true;
409                 }
410                 return false;
411         }
412
413
414         /**
415          * Delete all invitations inside this group
416          */
417         function removeGroupInvitations(){
418                 global $db;
419                 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_groups_invitations WHERE group_id='.$this->group_id;
420                 $result = mysql_query($sql, $db);
421                 if ($result){
422                         return true;
423                 }
424                 return false;
425         }
426
427         /** 
428          * Delete a message from the board
429          * @param       int             member_id
430          */
431         function removeMessage($id, $member_id){
432                 global $db;
433                 $id = intval ($id);
434                 $member_id = intval($member_id);
435                 $sql = 'DELETE FROM '.TABLE_PREFIX."social_groups_board WHERE id=$id ";
436
437                 //if not moderator.
438                 if ($member_id != $this->user_id){
439                         $sql .= " AND member_id=$member_id";                    
440                 } 
441                 $result = mysql_query($sql, $db);
442                 return $result;
443         }
444
445         
446         /**
447          * Delete all the messages from the board
448          */
449         function removeAllMessages(){
450                 global $db;
451
452                 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_groups_board WHERE group_id='.$this->getID();
453                 $result = mysql_query($sql, $db);
454                 return $result;
455         }
456
457
458         /**
459          * Search members
460          * TODO: Maybe to make a general search($string, $member_obj_array) that takess any member obj array.  
461          *               This can be used for friends search as well.  Optimize the code and structure a bit.
462          * @param       string  member name
463          * @return      array  of Members Object
464          */
465         function searchMembers($name){
466                 global $db, $addslashes;
467
468                 //break the names by space, then accumulate the query
469                 $name = $addslashes($name);     
470                 $sub_names = explode(' ', $name);
471                 foreach($sub_names as $piece){
472                         $query .= "(first_name LIKE '%$piece%' OR second_name LIKE '%$piece%' OR last_name LIKE '%$piece%' OR email LIKE '$piece') AND ";
473                 }
474                 //trim back the extra "AND "
475                 $query = substr($query, 0, -4);
476                 
477                 $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;
478                 $rs = mysql_query($sql, $db);
479
480                 while ($row=mysql_fetch_assoc($rs)) {
481                         $result[$row['member_id']] = new Member($row['member_id']);
482                 }
483                 return $result;
484         }
485 }
486 ?>