You are here

function _flickr_block_random in Flickr 6

Same name and namespace in other branches
  1. 5 block/flickr_block.module \_flickr_block_random()
1 call to _flickr_block_random()
flickr_block in block/flickr_block.module
Implements hook_block().

File

block/flickr_block.module, line 489

Code

function _flickr_block_random($nsid, $show_n, $size, $media) {
  $output = '';
  $random_photos = array();
  if ($photos = flickr_photos_search($nsid, 1, array(
    'per_page' => 500,
    'media' => $media,
  ))) {
    $page_count = $photos['pages'];

    // We shouldn't try to return more than the total number of photos.
    $to = min($show_n, $photos['total']);
    $output = '';
    for ($i = 0; $i < $to; $i++) {
      sleep(0.125);

      // Request a random page.
      $photos = flickr_photos_search($nsid, rand(1, $page_count), array(
        'per_page' => 500,
        'media' => $media,
      ));

      // Then select a random photo.
      $index = rand(0, count($photos['photo']) - 1);
      $photo_id = $photos['photo'][$index]['id'];
      if (in_array($photo_id, $random_photos)) {

        // Photo already added.
        $i--;
      }
      else {
        $random_photos[] = $photo_id;
        $output .= theme('flickr_block_photo', $photos['photo'][$index], $size);
      }
    }
  }
  return $output;
}