/mods/_standard/social/lib/classes/Activity.class.php

https://github.com/harriswong/ATutor · PHP · 151 lines · 80 code · 17 blank · 54 comment · 10 complexity · 3a076298957f84430db1b8cb0f02ab40 MD5 · raw file

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