functions.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace AU\SuggestedFriends;
  3. /*
  4. * People usort callback
  5. */
  6. function suggested_friends_sorter($a, $b) {
  7. if ($a['priority'] == $b['priority']) {
  8. return 0;
  9. }
  10. return ($a['priority'] < $b['priority']) ? 1 : -1;
  11. }
  12. /**
  13. *
  14. * Returns array of people containing entity, mutuals (friends), groups (shared) and priority
  15. * @param Int $guid
  16. * @param Int $friends_limit
  17. * @param Int $groups_limit
  18. * @return Array
  19. */
  20. function get_suggestions($guid, $friends_of_friends_limit = 10, $groups_members_limit = 10) {
  21. $dbprefix = elgg_get_config('dbprefix');
  22. $guid = sanitize_int($guid);
  23. $suggestions = array();
  24. if ($friends_of_friends_limit) {
  25. // get some friends of friends
  26. $options = array(
  27. 'selects' => array(
  28. 'COUNT(fof.guid_two) as priority'
  29. ),
  30. 'type' => 'user',
  31. 'joins' => array(
  32. "JOIN {$dbprefix}users_entity ue ON ue.guid = e.guid",
  33. "JOIN {$dbprefix}entity_relationships fr ON fr.guid_one = {$guid} AND fr.relationship = 'friend'",
  34. "JOIN {$dbprefix}entity_relationships fof ON fof.guid_one = fr.guid_two AND fof.relationship = 'friend'"
  35. ),
  36. "wheres" => array(
  37. "ue.banned = 'no'",
  38. "e.guid NOT IN (SELECT f.guid_two FROM {$dbprefix}entity_relationships f WHERE f.guid_one = {$guid} AND f.relationship = 'friend')",
  39. "fof.guid_two = e.guid",
  40. "e.guid != {$guid}"
  41. ),
  42. 'group_by' => 'e.guid',
  43. 'order_by' => 'priority desc, ue.last_action desc',
  44. 'limit' => abs((int) $friends_of_friends_limit)
  45. );
  46. $fof = elgg_get_entities($options);
  47. foreach ($fof as $f) {
  48. $priority = (int) $f->getVolatileData('select:priority');
  49. $suggestions[$f->guid] = array(
  50. 'entity' => $f,
  51. 'mutuals' => $priority,
  52. 'groups' => 0,
  53. 'priority' => $priority
  54. );
  55. }
  56. }
  57. if ($groups_members_limit) {
  58. // get some mutual group members
  59. $options = array(
  60. 'selects' => array(
  61. 'COUNT(mog.guid_two) as priority'
  62. ),
  63. 'type' => 'user',
  64. 'joins' => array(
  65. "JOIN {$dbprefix}users_entity ue ON ue.guid = e.guid",
  66. "JOIN {$dbprefix}entity_relationships g ON g.guid_one = {$guid} AND g.relationship = 'member'",
  67. "JOIN {$dbprefix}groups_entity ge ON ge.guid = g.guid_two", //ensure it's a group
  68. "JOIN {$dbprefix}entity_relationships mog ON mog.guid_two = g.guid_two AND mog.relationship = 'member'"
  69. ),
  70. "wheres" => array(
  71. "ue.banned = 'no'",
  72. "e.guid NOT IN (SELECT f.guid_two FROM {$dbprefix}entity_relationships f WHERE f.guid_one = {$guid} AND f.relationship = 'friend')",
  73. "mog.guid_one = e.guid",
  74. "e.guid != {$guid}"
  75. ),
  76. 'group_by' => 'e.guid',
  77. 'order_by' => 'priority desc, ue.last_action desc',
  78. 'limit' => 3 //abs((int) $groups_members_limit)
  79. );
  80. // get members of groups
  81. $mog = elgg_get_entities($options);
  82. foreach ($mog as $m) {
  83. if (!isset($suggestions[$m->guid])) {
  84. $priority = (int) $m->getVolatileData('select:priority');
  85. $suggestions[$m->guid] = array(
  86. 'entity' => $m,
  87. 'mutuals' => 0,
  88. 'groups' => $priority,
  89. 'priority' => $priority
  90. );
  91. } else {
  92. $priority = (int) $m->getVolatileData('select:priority');
  93. $suggestions[$m->guid]['groups'] = $priority;
  94. $suggestions[$m->guid]['priority'] += $priority;
  95. }
  96. }
  97. }
  98. // sort by priority
  99. usort($suggestions, __NAMESPACE__ . '\\suggested_friends_sorter');
  100. return $suggestions;
  101. }