move code up one directory
[atutor.git] / mods / _standard / social / lib / classes / Activity.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 /**
16  * This class is designed to handle the transactions for all the news, updates and
17  * activities that are issued by the user.
18  *
19  */
20 class Activity{
21         //constructor
22         function Activity(){}
23
24         /** 
25          * Adds a new activity into the database.
26          * @param       int             user id
27          * @param       string  the message of that describe this activity
28          * @param       int             the application that's linked with this activity.  Purpose is to display it with a link.
29          * TODO: What happens if title is empty? Don't add it?
30          */
31         function addActivity($id, $title, $app_id=0){
32                 global $db, $addslashes;
33                 $id = intval($id);
34                 $app_id = intval($app_id);
35                 $app_string = '';
36
37                 if ($app_id > 0){
38                         $app_string = ", application_id=$app_id";
39                         //overwrite title with an automated message
40                         $title = $this->generateApplicationTitle($app_id);
41                 }
42
43                 if ($id > 0 && $title!=''){
44                         $sql = 'INSERT INTO '.TABLE_PREFIX."social_activities SET member_id=$id, title='$title'".$app_string;
45                         mysql_query($sql, $db);
46                 }
47         }
48
49
50         /** 
51          * Retrieve this user's activity
52          * 
53          * @param       int             user id.
54          * @param       boolean set TRUE to display all entry           
55          * @return      The array of description of all the activities from the given user.
56          */
57         function getActivities($id, $displayAll=false){
58                 global $db;
59                 $activities = array();
60                 $id = intval($id);
61                 if ($id > 0){
62                         $sql = 'SELECT * FROM '.TABLE_PREFIX."social_activities WHERE member_id=$id ORDER BY created_date DESC";
63                         if (!$displayAll){
64                                 $sql .= ' LIMIT '.SOCIAL_FRIEND_ACTIVITIES_MAX;
65                         }
66                         $result = mysql_query($sql, $db);
67                         if ($result){
68                                 while($row = mysql_fetch_assoc($result)){
69                                         $activities[$row['id']]['member_id'] = $row['member_id'];
70                                         $activities[$row['id']]['title'] = $row['title'];
71                                         $activities[$row['id']]['created_date'] = $row['created_date'];
72                                 }
73                         }
74                         return $activities;
75                 }
76                 return;
77         }
78
79
80         /**
81          * Remove an activity
82          *
83          * @param       int             user id
84          * @return      true if activity is deleted.
85          */
86         function deleteActivity($id){
87                 global $db;
88
89                 $id = intval($id);
90                 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_activities WHERE member_id='.$_SESSION['member_id'].' AND id='.$id;
91                 mysql_query($sql, $db);
92                 if (mysql_affected_rows() > 0){
93                         return true;
94                 } else  {
95                         return false;
96                 }
97         }
98
99         
100         /**
101          * Retrieve friends' recent activities
102          *
103          * @param       int             user id
104          * @param       boolean set TRUE to display all entry           
105          * @return      The array of description of all the activities of the given user's friends.
106          */
107          function getFriendsActivities($id, $displayAll=false){
108                 global $db;
109                 $activities = array();
110
111                 $friends = getFriends($id);     
112                 $friends_ids = implode(', ', array_keys($friends));
113                 $sql = 'SELECT * FROM '.TABLE_PREFIX.'social_activities WHERE member_id IN ('.$friends_ids.') ORDER BY created_date DESC';
114                 if (!$displayAll){
115                         $sql .= ' LIMIT '.SOCIAL_FRIEND_ACTIVITIES_MAX;
116                 }
117                 $result = mysql_query($sql, $db);
118
119                 if ($result){
120                         while($row = mysql_fetch_assoc($result)){
121                                 $activities[$row['id']]['member_id'] = $row['member_id'];
122                                 $activities[$row['id']]['title'] = $row['title'];
123                                 $activities[$row['id']]['created_date'] = $row['created_date'];
124                         }
125                 }
126                 return $activities;
127          }
128
129
130          /** 
131           * Generate the title string for application.
132           *
133           * @param      int             application id
134           * @return     the title string that has a hyperlink to the application itself.
135           */
136          function generateApplicationTitle($app_id){
137                 global $db;
138                 $app_id = intval($app_id);
139                 
140                 //This here, it is actually better to use $url instead of app_id.
141                 //$url is the primary key.  $id is also a key, but it is not guranteed that it will be unique
142                 $sql = 'SELECT title FROM '.TABLE_PREFIX."social_applications WHERE id=$app_id";
143                 $result = mysql_query($sql, $db);
144                 $row = mysql_fetch_assoc($result);
145                 
146                 $msg = _AT("has_added_app", url_rewrite(AT_SOCIAL_BASENAME.'applications.php?app_id='.$app_id, AT_PRETTY_URL_IS_HEADER),
147                         htmlentities_utf8($row['title']));
148                 return $msg;
149          }
150 }
151 ?>