Guest User

Untitled

a guest
Oct 20th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. private function getTilesForSearch($search)
  2. {
  3. $tiles = array();
  4.  
  5. $userOccurences = array();
  6. $channelOccurences = array();
  7. $totalResults = 0;
  8. $r = r('@video_list')->setParameter('sort', 'relevance')->setParameter('search', $search);
  9. $searchTileId = DM_Tile::getInstanceFromUQL($r->buildURI())->getId();
  10. foreach ($r->getIterator() as $video)
  11. {
  12. $totalResults++;
  13. $tiles[] = array('id' => $searchTileId, 'video' => $video);
  14.  
  15. $userId = $video->getOwner()->getId();
  16. $userOccurences[$userId] = isset($userOccurences[$userId]) ? $userOccurences[$userId] + 1 : 1;
  17.  
  18. $channel = $video->getChannel();
  19. $channelOccurences[$channel] = isset($channelOccurences[$channel]) ? $channelOccurences[$channel] + 1 : 1;
  20. }
  21.  
  22. // If one channel is represented in 50% of more of the videos in the result, do some special things with it
  23. if (current($channelOccurences) / $totalResults >= 0.5)
  24. {
  25. $smartChannel = key($channelOccurences);
  26. array_unshift($tiles, array('id' => DM_Tile::getInstanceFromUQL('/channel/' . $smartChannel)->getId()));
  27. }
  28.  
  29. $relativeIndex = 0;
  30. arsort($userOccurences, SORT_NUMERIC);
  31. foreach ($userOccurences as $userId => $occurences)
  32. {
  33. $ratio = $occurences / $totalResults;
  34. error_log(DM_User::getInstance($userId)->getLogin() . ': ' . $ratio);
  35. if ($ratio >= 0.5)
  36. {
  37. // If user is represented by more than 50% of the results, put it on top of the search result
  38. $login = DM_User::getInstance($userId)->getLogin();
  39. array_unshift($tiles, array('id' => DM_Tile::getInstanceFromUQL('/user/' . $login)->getId()));
  40. }
  41. elseif ($ratio >= 0.1)
  42. {
  43. // If user is between 10 and 50%, add it to the search results between fourth and 15th position
  44. $pos = round(.5 / $ratio * 3);
  45. $login = DM_User::getInstance($userId)->getLogin();
  46. array_splice($tiles, $relativeIndex + $pos, 0, array(array('id' => DM_Tile::getInstanceFromUQL('/user/' . $login)->getId())));
  47. $relativeIndex++;
  48. }
  49. else
  50. {
  51. break;
  52. }
  53. }
  54.  
  55. return $tiles;
  56. }
Add Comment
Please, Sign In to add comment