5db83fc551a8b0e596961324bbbd2158f0489538
[atutor.git] / mods / social / lib / classes / Member.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 /**
16  * Members class for Social Networking
17  * TODO: Extend it for the entire ATutor.
18  */
19 class Member {
20         var $id;                //member id
21         var $profile;   //profile details
22
23         function Member($id){
24                 $this->id = intval($id);
25         }
26
27
28         /**
29          * Add a new job position
30          * @param       string          Name of the company, in full.
31          * @param       string          Title of this position
32          * @param       int                     Started date for this position, in the format of yyyymm
33          * @param       int                     Position ended on this date, in the format of yyyymm, or 'NOW'. 
34          *                                              'NOW' means it is still on going.
35          * @param       string          Description of what the position was about
36          */
37         function addPosition($company, $title, $from, $to, $description){
38                 global $addslashes, $db;
39                 $member_id      = $this->id;
40                 $company        = $addslashes($company);
41                 $title          = $addslashes($title);
42                 $from           = $addslashes($from);
43                 $to                     = $addslashes($to);
44                 $description = $addslashes($description);
45
46                 $sql = 'INSERT INTO '.TABLE_PREFIX."social_member_position (member_id, company, title, `from`, `to`, description) VALUES ($member_id, '$company', '$title', '$from', '$to', '$description')";
47                 mysql_query($sql, $db);
48         }
49
50         
51         /**
52          * Add a new education
53          * TODO: University names can be generated from another table.
54          * 
55          * @param       string          Name of the University, in full. Might need to pull from another table.
56          * @param       int                     This education begins on this date, yyyymm
57          * @param       int                     This education ends on this date, yyyymm, or can be 'NOW'
58          * @param       string          The full name of the country this University is in, ie. Canada
59          * @param       string          The full name of the province this University is in, ie. Ontario
60          * @param       string          The name of the degree, ie. B.Sc.
61          * @param       string          The field of study, ie. Computer Science
62          * @param       string          The description of this education.
63          */
64         function addEducation($university, $from, $to, $country, 
65                                                   $province, $degree, $field, $description){ 
66                 global $addslashes, $db;
67                 $member_id                      = $this->id;
68                 $university                     = $addslashes($university);
69                 $from                           = $addslashes($from);
70                 $to                                     = $addslashes($to);
71                 $country                        = $addslashes($country);
72                 $province                       = $addslashes($province);
73                 $degree                         = $addslashes($degree);
74                 $field                          = $addslashes($field);
75                 $description            = $addslashes($description);
76                 
77                 $sql = 'INSERT INTO '.TABLE_PREFIX."social_member_education (member_id, university, `from`, `to`, country, province, degree, field, description) VALUES ($member_id, '$university', '$from', '$to', '$country', '$province', '$degree', '$field', '$description')";
78                 mysql_query($sql, $db);
79         }
80
81
82         /**
83          * Add a new website associated with this member, can be blog, work, portfolio, etc.
84          * @param       string          Unique URL of the website
85          * @param       string          A name for the website.
86          */
87         function addWebsite($url, $site_name){ 
88                 global $addslashes, $db;
89                 $member_id      = $this->id;
90                 $url            = urlencode($url);
91                 $site_name      = $addslashes($site_name);
92
93                 $sql = 'INSERT INTO '. TABLE_PREFIX ."social_member_websites (member_id, url, site_name) VALUES ($member_id, '$url', '$site_name')";
94                 mysql_query($sql, $db);
95         }
96
97
98         /** 
99          * Add new interest for this member, in CSV format
100          * @param       string          interest
101          */
102         function addInterests($interests){
103                 $this->updateAdditionalInformation($interests);
104         }
105
106
107         /** 
108          * Add new interest for this member, in CSV format
109          * @param       string          interest
110          */
111         function addAssociations($associations){
112                 $this->updateAdditionalInformation('', $associations);
113         }
114
115
116         /** 
117          * Add new interest for this member, in CSV format
118          * @param       string          interest
119          */
120         function addAwards($awards){
121                 $this->updateAdditionalInformation('', '', $awards);
122         }
123
124
125         /** 
126          * Add additional information, including interest, awards, associations.
127          * @param       string          CSV format of interests, ie. camping, biking, etc
128          * @param       string          CSV format of associations, clubs, groups, ie. IEEE
129          * @param       string          CSV format of awards, honors
130          * @param       string          expterise, occupation
131          * @param       string          any extra information
132          */
133         function addAdditionalInformation($interests, $associations, $awards, $expertise, $others){ 
134                 global $addslashes, $db;
135                 $member_id              = $this->id;
136                 $interests              = $addslashes($interests);
137                 $associations   = $addslashes($associations);
138                 $awards                 = $addslashes($awards);
139                 $expertise              = $addslashes($expertise);
140                 $others                 = $addslashes($others);
141                 $sql = 'INSERT INTO ' . TABLE_PREFIX . "social_member_additional_information (member_id, interests,  associations, awards, expertise, others) VALUES ($member_id, '$interests', '$associations', '$awards', '$expertise', '$others')";
142                 mysql_query($sql, $db);
143         }
144
145
146         /** 
147          * Add visitor
148          * @param       int             visitor id
149          */
150         function addVisitor($visitor_id){
151                 global $db;
152                 $visitor_id = intval($visitor_id);
153                 $sql = 'INSERT INTO '.TABLE_PREFIX."social_member_track (`member_id`, `visitor_id`, `timestamp`) VALUES (".$this->getID().", $visitor_id, NOW())";
154                 mysql_query($sql, $db);
155         }
156
157
158         /**
159          * Update a new job position
160          * @param       int                     The id of this entry
161          * @param       string          Name of the company, in full.
162          * @param       string          Tht title of this position
163          * @param       int                     Started date for this position, in the format of yyyymm
164          * @param       int                     Position ended on this date, in the format of yyyymm, or 'NOW'. 
165          *                                              'NOW' means it is still on going.
166          * @param       string          Description of the position
167          */
168         function updatePosition($id, $company, $title, $from, $to, $description){ 
169                 global $addslashes, $db;
170                 $id                      = intval($id);
171                 $company         = $addslashes($company);
172                 $title           = $addslashes($title);
173                 $form            = $addslashes($form);
174                 $to                      = $addslashes($to);
175                 $description = $addslashes($description);
176
177                 $sql = 'UPDATE '.TABLE_PREFIX."social_member_position SET company='$company', title='$title', `from`='$from', `to`='$to', description='$description' WHERE id=$id";
178                 mysql_query($sql, $db);
179         }
180
181
182         /**
183          * Update a new education
184          * TODO: University names can be generated from another table.
185          * 
186          * @param       int                     ID of this entry
187          * @param       string          Name of the University, in full. Might need to pull from another table.
188          * @param       int                     This education begins on this date, yyyymm
189          * @param       int                     This education ends on this date, yyyymm, or can be 'NOW'
190          * @param       string          The full name of the country this University is in, ie. Canada
191          * @param       string          The full name of the province this University is in, ie. Ontario
192          * @param       string          The name of the degree, ie. B.Sc.
193          * @param       string          The field of study, ie. Computer Science
194          * @param       string          The description of this education.
195          */
196         function updateEducation($id, $university, $from, $to, $country, $province, $degree, $field, $description){ 
197                 global $addslashes, $db;
198                 $id                                     = intval($id);
199                 $university                     = $addslashes($university);
200                 $from                           = $addslashes($from);
201                 $to                                     = $addslashes($to);
202                 $country                        = $addslashes($country);
203                 $province                       = $addslashes($province);
204                 $degree                         = $addslashes($degree);
205                 $field                          = $addslashes($field);
206                 $description            = $addslashes($description);
207
208                 $sql = 'UPDATE '.TABLE_PREFIX."social_member_education SET university='$university', `from`='$from', `to`='$to', country='$country', province='$province', degree='$degree', field='$field', description='$description' WHERE id=$id";
209                 mysql_query($sql, $db);         
210         }
211
212
213         /**
214          * Updates a new website associated with this member, can be blog, work, portfolio, etc.
215          * @param       int                     ID of this entry
216          * @param       string          Unique URL of the website
217          * @param       string          A name for the website.
218          */
219         function updateWebsite($id, $url, $site_name){ 
220                 global $addslashes, $db;
221                 $id                     = intval($id);
222                 $url            = $addslashes($url);
223                 $site_name      = $addslashes($site_name);
224
225                 $sql = 'UPDATE '.TABLE_PREFIX."social_member_websites SET url='$url', site_name='$site_name' WHERE id=$id";
226                 mysql_query($sql, $db);
227         }
228
229
230         /** 
231          * Update additional information, including interest, awards, associations.
232          * @param       string          CSV format of interests, ie. camping, biking, etc
233          * @param       string          CSV format of associations, clubs, groups, ie. IEEE
234          * @param       string          CSV format of awards, honors
235          * @param       string          expterise, occupation
236          * @param       string          any extra information
237          */
238         function updateAdditionalInformation($interests='', $associations='', $awards='', $expertise='', $others=''){ 
239                 global $addslashes, $db;
240                 $interests = $addslashes($interests);
241                 $associations = $addslashes($associations);
242                 $awards = $addslashes($awards);
243                 $expertise = $addslashes($expertise);
244                 $others = $addslashes($others);
245
246                 $sql = '';
247                 //tricky, not all fields get updated at once.  Update only the ones that has entries.
248                 if ($interests!=''){
249                         $sql .= "interests='$interests', ";
250                 }
251                 if ($associations!=''){
252                         $sql .= " associations='$associations', ";
253                 }
254                 if ($awards!=''){
255                         $sql .= "awards='$awards', ";
256                 }
257                 if ($expertise!=''){
258                         $sql .= "expertise='$expertise', ";
259                 }
260                 if ($others!=''){
261                         $sql .= "others='$others', ";           
262                 }
263                 if ($sql!=''){
264                         $sql = substr($sql, 0, -2);
265                 }
266
267                 $sql2 = 'INSERT INTO '.TABLE_PREFIX."social_member_additional_information SET ".$sql.", member_id=".$_SESSION['member_id'] . " ON DUPLICATE KEY UPDATE ".$sql;
268                 mysql_query($sql2, $db);
269         }
270
271
272         /**
273          * Get member info
274          * This method tends to be have a negative impact on system run time.  
275          */
276         function getDetails(){
277                 global $db;
278                 $sql =  'SELECT core.*, T.interests, T.associations, T.awards, T.expertise, T.others FROM '.
279                                 '(SELECT * FROM '.TABLE_PREFIX.'members WHERE member_id='.$this->id.') AS core '.
280                                 'LEFT JOIN '.
281                                 TABLE_PREFIX.'social_member_additional_information T ON core.member_id=T.member_id';
282                 $result = mysql_query($sql, $db);
283                 if ($result){
284                         $row = mysql_fetch_assoc($result);
285                         $this->profile = $row;
286                 }
287                 return $this->profile;
288         }
289
290
291         /**
292          * Get member address
293          */
294         function getAddress(){
295                 global $db;
296                 $sql = 'SELECT address, postal, city, province, country FROM '.TABLE_PREFIX.'members WHERE member_id='.$this->id;
297                 $result = mysql_query($sql, $db);
298                 if ($result){
299                         $row = mysql_fetch_assoc($result);
300                 }
301                 return $row;
302         }
303         
304
305         /**
306          * Get position info
307          * @return      the array of job/position
308          */
309         function getPosition(){
310                 global $db;
311                 $position = array();
312
313                 $sql = 'SELECT * FROM '.TABLE_PREFIX.'social_member_position WHERE member_id='.$this->id;
314                 $result = mysql_query($sql, $db);
315                 if ($result){
316                         while($row = mysql_fetch_assoc($result)){
317                                 $position[] = $row;
318                         }
319                 }
320                 return $position;
321         }
322
323
324         /**
325          * Get education info
326          * can be 1+ 
327          * @return      the array of education details
328          */
329         function getEducation(){
330                 global $db;
331                 $education = array();
332
333                 $sql = 'SELECT * FROM '.TABLE_PREFIX.'social_member_education WHERE member_id='.$this->id;
334                 $result = mysql_query($sql, $db);
335                 if ($result){
336                         while($row = mysql_fetch_assoc($result)){
337                                 $education[] = $row;
338                         }
339                 }
340                 return $education;
341         }
342
343
344         /** 
345          * Get websites. can be 1+
346          * @return      the array of website details.
347          */
348         function getWebsites(){
349                 global $db;
350                 $websites = array();
351
352                 $sql = 'SELECT * FROM '.TABLE_PREFIX.'social_member_websites WHERE member_id='.$this->id;
353                 $result = mysql_query($sql, $db);
354                 if ($result){
355                         while($row = mysql_fetch_assoc($result)){
356                                 $websites[] = $row;
357                         }
358                 }
359                 return $websites;
360         }
361
362
363         /**
364          * Get visitor counts within a month, the resultant array contains a daily, weekly, monthly, and a total count.
365          * @return      the count of all visitors on this page, within a month. 
366          */
367         function getVisitors(){
368                 global $db;
369                 $count = array('month'=>0, 'week'=>0, 'day'=>0, 'total'=>0);
370                 //Time offsets
371                 $month  = time() - 60*60*24*30; //month, within 30days.
372                 $week   = time() - 60*60*24*7;          //week, within 7 days.
373                 $day    = time() - 60*60*24;            //day, within 24 hours.
374
375                 $sql = 'SELECT visitor_id, UNIX_TIMESTAMP(timestamp) AS `current_time` FROM '.TABLE_PREFIX.'social_member_track WHERE member_id='.$this->id;
376                 $result = mysql_query($sql, $db);
377                 if ($result){
378                         while ($row = mysql_fetch_assoc($result)){
379                                 if($row['current_time'] >= $month && $row['current_time'] <= $week){
380                                         $count['month']++;
381                                 } elseif ($row['current_time'] > $week && $row['current_time'] <= $day){
382                                         $count['week']++;
383                                 } elseif ($row['current_time'] > $day){
384                                         $count['day']++;
385                                 } else {
386                                         continue;
387                                 }
388                                 $count['total']++;
389                         }
390                 }
391
392                 //clean up table randomly, 1%
393                 if (rand(1,100) == 1){
394                         $sql = 'DELETE FROM '.TABLE_PREFIX."social_member_track WHERE UNIX_TIMESTAMP(`timestamp`) < $month";
395                         mysql_query($sql, $db);
396                 }
397                 
398                 return $count;
399         }
400
401         
402         /**
403          * Delete position
404          * @param       int             position id
405          */
406         function deletePosition($id){
407                 global $db;
408
409                 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_member_position WHERE id='.$id;
410                 $result = mysql_query($sql, $db);
411          }
412
413         
414         /**
415          * Delete education
416          * @param       int             education id
417          */
418         function deleteEducation($id){
419                 global $db;
420
421                 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_member_education WHERE id='.$id;
422                 $result = mysql_query($sql, $db);
423         }
424
425         
426         /**
427          * Delete websites
428          * @param       int             websites id
429          */
430         function deleteWebsite($id){
431                 global $db;
432
433                 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_member_websites WHERE id='.$id;
434                 $result = mysql_query($sql, $db);
435         }
436         
437
438         /**
439          * Delete interest
440          */
441         function deleteInterests(){
442                 global $db;
443
444                 $sql = 'UPDATE '.TABLE_PREFIX."social_member_additional_information SET interests='' WHERE member_id=".$this->getID();
445                 $result = mysql_query($sql, $db);
446         }
447
448
449         /**
450          * Delete associations
451          */
452         function deleteAssociations(){
453                 global $db;
454
455                 $sql = 'UPDATE '.TABLE_PREFIX."social_member_additional_information SET associations='' WHERE member_id=".$this->getID();
456                 $result = mysql_query($sql, $db);
457         }
458
459         
460         /**
461          * Delete awards
462          */
463         function deleteAwards(){
464                 global $db;
465
466                 $sql = 'UPDATE '.TABLE_PREFIX."social_member_additional_information SET awards='' WHERE member_id=".$this->getID();
467                 $result = mysql_query($sql, $db);
468         }
469
470
471         /**
472          * Get the ID of this member
473          */
474         function getID(){
475                 return $this->id;
476         }
477 }
478 ?>