Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private function getTilesForSearch($search)
- {
- $tiles = array();
- $userOccurences = array();
- $channelOccurences = array();
- $totalResults = 0;
- $r = r('@video_list')->setParameter('sort', 'relevance')->setParameter('search', $search);
- $searchTileId = DM_Tile::getInstanceFromUQL($r->buildURI())->getId();
- foreach ($r->getIterator() as $video)
- {
- $totalResults++;
- $tiles[] = array('id' => $searchTileId, 'video' => $video);
- $userId = $video->getOwner()->getId();
- $userOccurences[$userId] = isset($userOccurences[$userId]) ? $userOccurences[$userId] + 1 : 1;
- $channel = $video->getChannel();
- $channelOccurences[$channel] = isset($channelOccurences[$channel]) ? $channelOccurences[$channel] + 1 : 1;
- }
- // If one channel is represented in 50% of more of the videos in the result, do some special things with it
- if (current($channelOccurences) / $totalResults >= 0.5)
- {
- $smartChannel = key($channelOccurences);
- array_unshift($tiles, array('id' => DM_Tile::getInstanceFromUQL('/channel/' . $smartChannel)->getId()));
- }
- $relativeIndex = 0;
- arsort($userOccurences, SORT_NUMERIC);
- foreach ($userOccurences as $userId => $occurences)
- {
- $ratio = $occurences / $totalResults;
- error_log(DM_User::getInstance($userId)->getLogin() . ': ' . $ratio);
- if ($ratio >= 0.5)
- {
- // If user is represented by more than 50% of the results, put it on top of the search result
- $login = DM_User::getInstance($userId)->getLogin();
- array_unshift($tiles, array('id' => DM_Tile::getInstanceFromUQL('/user/' . $login)->getId()));
- }
- elseif ($ratio >= 0.1)
- {
- // If user is between 10 and 50%, add it to the search results between fourth and 15th position
- $pos = round(.5 / $ratio * 3);
- $login = DM_User::getInstance($userId)->getLogin();
- array_splice($tiles, $relativeIndex + $pos, 0, array(array('id' => DM_Tile::getInstanceFromUQL('/user/' . $login)->getId())));
- $relativeIndex++;
- }
- else
- {
- break;
- }
- }
- return $tiles;
- }
Add Comment
Please, Sign In to add comment