PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/mods/_standard/social/lib/classes/SocialGroups/SocialGroup.class.php

https://github.com/harriswong/ATutor
PHP | 486 lines | 302 code | 66 blank | 118 comment | 30 complexity | 4febdb8463b7a092621f1664fb2e7465 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. require_once(AT_SOCIAL_INCLUDE.'classes/Activity.class.php');
  15. /**
  16. * Class for individual social group
  17. */
  18. class SocialGroup {
  19. var $group_id; //group id
  20. var $user_id; //group creator
  21. var $logo; //logo
  22. var $name; //group name
  23. var $type_id; //the type_id
  24. var $privacy; //privacy, 0 for public, 1 for private
  25. var $description; //description of this group
  26. var $created_date; //sql timestamp
  27. var $last_updated; //sql timestamp
  28. var $group_members; //group members
  29. var $group_activities; //group activities
  30. var $is_valid; //set false if this is not a valid group
  31. /**
  32. * Constructor
  33. */
  34. function SocialGroup($group_id){
  35. global $db;
  36. $this->group_id = intval($group_id);
  37. if ($this->group_id > 0){
  38. $sql = 'SELECT * FROM '.TABLE_PREFIX.'social_groups WHERE id='.$this->group_id;
  39. $result = mysql_query($sql, $db);
  40. if (mysql_num_rows($result) > 0){
  41. $row = mysql_fetch_assoc($result);
  42. $this->user_id = $row['member_id'];
  43. $this->logo = $row['logo'];
  44. $this->name = $row['name'];
  45. $this->type_id = $row['type_id'];
  46. $this->privacy = $row['privacy'];
  47. $this->description = $row['description'];
  48. $this->created_date = $row['created_date'];
  49. $this->last_updated = $row['last_updated'];
  50. $this->group_members = $this->getGroupMembers();
  51. $this->group_activities = $this->getGroupActivities();
  52. $this->is_valid = true;
  53. } else {
  54. //group does not exist, most likely deleted
  55. $this->is_valid = false;
  56. }
  57. }
  58. }
  59. /**
  60. * Retrieve a list of group members
  61. * @param int group id
  62. * @return mixed array of members object
  63. */
  64. function getGroupMembers(){
  65. global $db;
  66. if (!empty($this->group_members)){
  67. return $this->group_members;
  68. }
  69. $members = array();
  70. $sql = 'SELECT * FROM '.TABLE_PREFIX.'social_groups_members WHERE group_id='.$this->group_id;
  71. $result = mysql_query($sql, $db);
  72. if ($result){
  73. while ($row = mysql_fetch_assoc($result)){
  74. $members[] = new Member($row['member_id']);
  75. }
  76. }
  77. //TODO Return empty array or should i return error?
  78. return $members;
  79. }
  80. /**
  81. * Get the group activities, all the activities that happens in this group.
  82. * @param int group id
  83. * @return mixed array of activities
  84. */
  85. function getGroupActivities(){
  86. global $db;
  87. if (!empty($this->group_activities)){
  88. return $this->group_activities;
  89. }
  90. $activities = array();
  91. $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;
  92. $result = mysql_query($sql, $db);
  93. if ($result){
  94. while($row = mysql_fetch_assoc($result)){
  95. $activities[$row['id']] = $row['title'];
  96. }
  97. }
  98. return $activities;
  99. }
  100. /**
  101. * Get a specific mesage from the given user.
  102. * @param int the message id.
  103. * @param int the member id, the member who created this message, or the moderator.
  104. * @return the text of the message
  105. */
  106. function getMessage($id, $member_id){
  107. global $db;
  108. $id = intval($id);
  109. $member_id = intval($member_id);
  110. $sql = 'SELECT body FROM '.TABLE_PREFIX.'social_groups_board WHERE group_id='.$this->getID().' AND id='.$id;
  111. //if not moderator
  112. if($member_id!=$this->user_id){
  113. $sql .= ' AND member_id='.$member_id;
  114. }
  115. $rs = mysql_query($sql, $db);
  116. if($rs){
  117. list($body) = mysql_fetch_array($rs);
  118. return htmlentities_utf8($body);
  119. }
  120. return false;
  121. }
  122. /**
  123. * Get message boards message, return a list sorted by date, in descending order
  124. */
  125. function getMessages(){
  126. global $db;
  127. $result = array();
  128. $sql = 'SELECT * FROM '.TABLE_PREFIX.'social_groups_board WHERE group_id='.$this->getID().' ORDER BY created_date DESC';
  129. $rs = mysql_query($sql, $db);
  130. if ($rs){
  131. while ($row = mysql_fetch_assoc($rs)){
  132. $row['body'] = htmlentities_utf8($row['body']); //escape xss attack
  133. $result [$row['id']] = $row;
  134. }
  135. }
  136. return $result;
  137. }
  138. /**
  139. * Get the group information
  140. */
  141. function getID(){
  142. return $this->group_id;
  143. }
  144. function getUser(){
  145. return $this->user_id;
  146. }
  147. function getGroupType(){
  148. global $db;
  149. //or maybe print out the exact type name
  150. $sql = 'SELECT title FROM '.TABLE_PREFIX.'social_groups_types WHERE type_id='.$this->type_id;
  151. $result = mysql_query($sql, $db);
  152. list($type_name) = mysql_fetch_row($result);
  153. return _AT($type_name);
  154. }
  155. function getLogo() {
  156. if (!empty($this->logo)) {
  157. $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>';
  158. } else {
  159. $str = '<img src="'.AT_SOCIAL_BASENAME.'images/placelogo.png" alt="'._AT('placelogo').'" title="'._AT('placelogo').'"/>';
  160. }
  161. return $str;
  162. }
  163. function getName() {
  164. return htmlentities_utf8($this->name);
  165. }
  166. //@param boolean change all carrier returns to <br/> if true.
  167. function getDescription($use_nl2br=true){
  168. return htmlentities_utf8($this->description, $use_nl2br);
  169. }
  170. function getCreatedDate(){
  171. return $this->created_date;
  172. }
  173. function getPrivacy(){
  174. //0 for public, 1 for private
  175. return $this->privacy;
  176. }
  177. function getLastUpdated(){
  178. return $this->last_updated;
  179. }
  180. function isValid(){
  181. return $this->is_valid;
  182. }
  183. /**
  184. * Add a member to the group
  185. * @param int member id
  186. * @return boolean true if succeded, false otherwise.
  187. */
  188. function addMember($member_id){
  189. global $db;
  190. $member_id = intval($member_id);
  191. $sql = 'INSERT INTO '.TABLE_PREFIX.'social_groups_members (group_id, member_id) VALUES ('.$this->group_id.", $member_id)";
  192. $result = mysql_query($sql, $db);
  193. if ($result){
  194. //add a record to the activities
  195. $act = new Activity();
  196. $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>');
  197. $act->addActivity($member_id, $str1);
  198. unset($act);
  199. return true;
  200. }
  201. return false;
  202. }
  203. /**
  204. * Sends an invitation to a member
  205. * @param int member_id
  206. */
  207. function addInvitation($member_id) {
  208. global $db;
  209. $member_id = intval($member_id);
  210. $sql = 'INSERT INTO '.TABLE_PREFIX.'social_groups_invitations (sender_id, member_id, group_id) VALUES ('
  211. .$_SESSION['member_id'].', '.$member_id.', '.$this->getID().')';
  212. $result = mysql_query($sql, $db);
  213. }
  214. /**
  215. * Sends a request to the group creator
  216. * @param int member_id
  217. */
  218. function addRequest(){
  219. global $db;
  220. $sql = 'INSERT INTO '.TABLE_PREFIX.'social_groups_requests (sender_id, member_id, group_id) VALUES ('
  221. .$_SESSION['member_id'].', '.$this->getUser().', '.$this->getID().')';
  222. $result = mysql_query($sql, $db);
  223. return $result;
  224. }
  225. /**
  226. * Add message to the message board
  227. * @param string the message body
  228. */
  229. function addMessage($body) {
  230. global $db, $addslashes;
  231. $body = $addslashes($body);
  232. $member_id = $_SESSION['member_id'];
  233. $group_id = $this->getID();
  234. $sql = 'INSERT INTO '.TABLE_PREFIX."social_groups_board (member_id, group_id, body, created_date) VALUES ($member_id, $group_id, '$body', NOW())";
  235. $result = mysql_query($sql, $db);
  236. return $result;
  237. }
  238. /**
  239. * Update group logo
  240. * @param string filename of the logo. <name.extension>
  241. */
  242. function updateGroupLogo($logo) {
  243. global $db, $addslashes;
  244. $logo = $addslashes($logo);
  245. $sql = 'UPDATE '.TABLE_PREFIX."social_groups SET logo='$logo' WHERE id=".$this->getID();
  246. $result = mysql_query($sql, $db);
  247. return $result;
  248. }
  249. /**
  250. * Edit message
  251. * @param int the id of the message
  252. * @param string message body
  253. */
  254. function updateMessage($id, $body){
  255. global $db, $addslashes;
  256. $id = intval($id);
  257. $body = $addslashes($body);
  258. if ($id <= 0){
  259. return false;
  260. }
  261. $sql = 'UPDATE '.TABLE_PREFIX."social_groups_board SET body='$body' WHERE id=$id";
  262. $result = mysql_query($sql, $db);
  263. return $result;
  264. }
  265. /**
  266. * Remove a member from the group
  267. * @param int member_id
  268. * @retrun boolean troe successful and false otherwise.
  269. */
  270. function removeMember($member_id){
  271. global $db;
  272. $member_id = intval($member_id);
  273. //quit if member_id = creator id
  274. if ($member_id == $this->getUser()){
  275. return false;
  276. }
  277. $sql = 'DELETE FROM '.TABLE_PREFIX."social_groups_members WHERE member_id=$member_id AND group_id=".$this->group_id;
  278. $result = mysql_query($sql, $db);
  279. if ($result){
  280. return true;
  281. }
  282. return false;
  283. }
  284. /**
  285. * Remove logo from content/social folder
  286. */
  287. function removeGroupLogo(){
  288. if ($this->logo!=''){
  289. unlink(AT_CONTENT_DIR.'social/'. $this->logo);
  290. }
  291. return file_exists(AT_CONTENT_DIR.'social/'. $this->logo);
  292. }
  293. /**
  294. * Delete all group activities
  295. * ToDo: Delete just one?
  296. */
  297. function removeGroupActivities(){
  298. global $db;
  299. $act_obj = new Activity();
  300. //First remove groups activities from activity table
  301. $allActs = $this->group_activities;
  302. foreach ($allActs as $id=>$garbage) {
  303. $act_obj->deleteActivity($id);
  304. }
  305. //Then remove the associations from social_groups_activities
  306. $sql = 'DELETE FROM '.TABLE_PREFIX."social_groups_activities WHERE group_id=".$this->group_id;
  307. $result = mysql_query($sql, $db);
  308. if ($result){
  309. return true;
  310. }
  311. return false;
  312. }
  313. /**
  314. * Delete all group forums
  315. */
  316. function removeGroupForums(){
  317. global $db;
  318. include(AT_INCLUDE_PATH.'../mods/_standard/forums/lib/forums.inc.php');
  319. //delete all forums for this social group
  320. $sql = 'SELECT forum_id FROM '.TABLE_PREFIX.'social_groups_forums WHERE group_id='.$this->group_id;
  321. $result = mysql_query($sql, $db);
  322. if ($result){
  323. while ($row = mysql_fetch_assoc($result)){
  324. delete_forum($row['forum_id']);
  325. }
  326. }
  327. $sql = 'DELETE FROM '.TABLE_PREFIX.'social_groups_forums WHERE group_id='.$this->group_id;
  328. $result = mysql_query($sql, $db);
  329. if ($result){
  330. return true;
  331. }
  332. return false;
  333. }
  334. /**
  335. * Delete all group members
  336. */
  337. function removeGroupMembers(){
  338. global $db;
  339. $sql = 'DELETE FROM '.TABLE_PREFIX.'social_groups_members WHERE group_id='.$this->group_id;
  340. $result = mysql_query($sql, $db);
  341. if ($result){
  342. return true;
  343. }
  344. return false;
  345. }
  346. /**
  347. * Delete all requests inside this group
  348. */
  349. function removeGroupRequests(){
  350. global $db;
  351. $sql = 'DELETE FROM '.TABLE_PREFIX.'social_groups_requests 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. * Delete all invitations inside this group
  360. */
  361. function removeGroupInvitations(){
  362. global $db;
  363. $sql = 'DELETE FROM '.TABLE_PREFIX.'social_groups_invitations WHERE group_id='.$this->group_id;
  364. $result = mysql_query($sql, $db);
  365. if ($result){
  366. return true;
  367. }
  368. return false;
  369. }
  370. /**
  371. * Delete a message from the board
  372. * @param int member_id
  373. */
  374. function removeMessage($id, $member_id){
  375. global $db;
  376. $id = intval ($id);
  377. $member_id = intval($member_id);
  378. $sql = 'DELETE FROM '.TABLE_PREFIX."social_groups_board WHERE id=$id ";
  379. //if not moderator.
  380. if ($member_id != $this->user_id){
  381. $sql .= " AND member_id=$member_id";
  382. }
  383. $result = mysql_query($sql, $db);
  384. return $result;
  385. }
  386. /**
  387. * Delete all the messages from the board
  388. */
  389. function removeAllMessages(){
  390. global $db;
  391. $sql = 'DELETE FROM '.TABLE_PREFIX.'social_groups_board WHERE group_id='.$this->getID();
  392. $result = mysql_query($sql, $db);
  393. return $result;
  394. }
  395. /**
  396. * Search members
  397. * TODO: Maybe to make a general search($string, $member_obj_array) that takess any member obj array.
  398. * This can be used for friends search as well. Optimize the code and structure a bit.
  399. * @param string member name
  400. * @return array of Members Object
  401. */
  402. function searchMembers($name){
  403. global $db, $addslashes;
  404. //break the names by space, then accumulate the query
  405. $name = $addslashes($name);
  406. $sub_names = explode(' ', $name);
  407. foreach($sub_names as $piece){
  408. $query .= "(first_name LIKE '%$piece%' OR second_name LIKE '%$piece%' OR last_name LIKE '%$piece%' OR email LIKE '$piece') AND ";
  409. }
  410. //trim back the extra "AND "
  411. $query = substr($query, 0, -4);
  412. $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;
  413. $rs = mysql_query($sql, $db);
  414. while ($row=mysql_fetch_assoc($rs)) {
  415. $result[$row['member_id']] = new Member($row['member_id']);
  416. }
  417. return $result;
  418. }
  419. }
  420. ?>