changed git call from https to git readonly
[atutor.git] / mods / social / lib / classes / SocialGroups / SocialGroup.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  * 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 = 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);
219                         unset($act);
220                         return true;
221                 }
222                 return false;
223          }
224
225
226         /**
227          * Sends an invitation to a member
228          * @param       int             member_id
229          */
230          function addInvitation($member_id) {
231                 global $db;
232                 $member_id = intval($member_id);
233
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);
237          }
238
239
240         /** 
241          * Sends a request to the group creator 
242          * @param       int             member_id
243          */
244         function addRequest(){
245                 global $db;
246         
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);
250                 return $result;
251         }
252         
253         /**
254          * Add message to the message board
255          * @param       string  the message body
256          */
257          function addMessage($body) {
258                  global $db, $addslashes;
259                  $body = $addslashes($body);
260                  $member_id = $_SESSION['member_id'];
261                  $group_id = $this->getID();
262
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);
265                  return $result;
266          }
267
268         
269         /**
270          * Update group logo
271          * @param       string  filename of the logo. <name.extension>
272          */
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);
278                 return $result;
279         }
280         
281
282         /** 
283          * Edit message 
284          * @param       int             the id of the message
285          * @param       string  message body
286          */
287          function updateMessage($id, $body){
288                  global $db, $addslashes;
289                  $id = intval($id);
290                  $body = $addslashes($body);
291                 if ($id <= 0){
292                         return false;
293                 }
294
295                 $sql = 'UPDATE '.TABLE_PREFIX."social_groups_board SET body='$body' WHERE id=$id";
296                 $result = mysql_query($sql, $db);
297                 return $result;
298          }
299         
300
301         /** 
302          * Remove a member from the group
303          * @param       int             member_id
304          * @retrun      boolean troe successful and false otherwise.
305          */
306          function removeMember($member_id){
307                 global $db;
308                 $member_id = intval($member_id);
309
310                 //quit if member_id = creator id
311                 if ($member_id == $this->getUser()){
312                         return false;
313                 }
314
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);
317                 if ($result){
318                         return true;
319                 }
320                 return false;
321          }
322
323         /**
324          * Remove logo from content/social folder
325          */
326         function removeGroupLogo(){     
327                 if ($this->logo!=''){
328                         unlink(AT_CONTENT_DIR.'social/'. $this->logo);
329                 }
330                 return file_exists(AT_CONTENT_DIR.'social/'. $this->logo);
331         }
332
333
334
335         /**
336          * Delete all group activities
337          * ToDo: Delete just one?
338          */
339          function removeGroupActivities(){
340                  global $db;
341
342                  $act_obj = new Activity();
343
344                  //First remove groups activities from activity table
345                  $allActs = $this->group_activities;
346                  foreach ($allActs as $id=>$garbage) {
347                         $act_obj->deleteActivity($id);
348                  }
349
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);
353                  if ($result){
354                          return true;
355                  }
356                  return false;
357          }
358
359
360         /**
361          * Delete all group forums
362          */
363         function removeGroupForums(){
364                 global $db;
365                 include(AT_INCLUDE_PATH.'lib/forums.inc.php');
366
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);
370                 if ($result){
371                         while ($row = mysql_fetch_assoc($result)){
372                                 delete_forum($row['forum_id']);
373                         }
374                 }
375
376                 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_groups_forums WHERE group_id='.$this->group_id;
377                 $result = mysql_query($sql, $db);
378                 if ($result){
379                         return true;
380                 } 
381                 return false;
382         }
383
384         
385         /**
386          * Delete all group members
387          */
388         function removeGroupMembers(){
389                 global $db;
390                 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_groups_members WHERE group_id='.$this->group_id;
391                 $result = mysql_query($sql, $db);
392                 if ($result){
393                         return true;
394                 }
395                 return false;
396         }
397
398
399         /** 
400          * Delete all requests inside this group
401          */
402         function removeGroupRequests(){
403                 global $db;
404                 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_groups_requests WHERE group_id='.$this->group_id;
405                 $result = mysql_query($sql, $db);
406                 if ($result){
407                         return true;
408                 }
409                 return false;
410         }
411
412
413         /**
414          * Delete all invitations inside this group
415          */
416         function removeGroupInvitations(){
417                 global $db;
418                 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_groups_invitations WHERE group_id='.$this->group_id;
419                 $result = mysql_query($sql, $db);
420                 if ($result){
421                         return true;
422                 }
423                 return false;
424         }
425
426         /** 
427          * Delete a message from the board
428          * @param       int             member_id
429          */
430         function removeMessage($id, $member_id){
431                 global $db;
432                 $id = intval ($id);
433                 $member_id = intval($member_id);
434                 $sql = 'DELETE FROM '.TABLE_PREFIX."social_groups_board WHERE id=$id ";
435
436                 //if not moderator.
437                 if ($member_id != $this->user_id){
438                         $sql .= " AND member_id=$member_id";                    
439                 } 
440                 $result = mysql_query($sql, $db);
441                 return $result;
442         }
443
444         
445         /**
446          * Delete all the messages from the board
447          */
448         function removeAllMessages(){
449                 global $db;
450
451                 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_groups_board WHERE group_id='.$this->getID();
452                 $result = mysql_query($sql, $db);
453                 return $result;
454         }
455
456
457         /**
458          * Search members
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
463          */
464         function searchMembers($name){
465                 global $db, $addslashes;
466
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 ";
472                 }
473                 //trim back the extra "AND "
474                 $query = substr($query, 0, -4);
475                 
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);
478
479                 while ($row=mysql_fetch_assoc($rs)) {
480                         $result[$row['member_id']] = new Member($row['member_id']);
481                 }
482                 return $result;
483         }
484 }
485 ?>