From d097b4fc027ff558bf5f580298c7bed3696dcd3d Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Thu, 26 Feb 2009 23:38:17 +0000 Subject: Update to image_block based on bharat's feedback 1) move the rand_key column into core 2) don't do a max rand, just try to a get a random number less than the current random number if that doesn't successd look the other way --- modules/image_block/helpers/image_block_event.php | 25 ---------------------- .../image_block/helpers/image_block_installer.php | 6 ------ modules/image_block/helpers/image_block_theme.php | 18 ++++++---------- 3 files changed, 6 insertions(+), 43 deletions(-) delete mode 100644 modules/image_block/helpers/image_block_event.php (limited to 'modules/image_block') diff --git a/modules/image_block/helpers/image_block_event.php b/modules/image_block/helpers/image_block_event.php deleted file mode 100644 index 0783773b..00000000 --- a/modules/image_block/helpers/image_block_event.php +++ /dev/null @@ -1,25 +0,0 @@ -query("UPDATE `items` SET `rand_key` = RAND() WHERE id = {$item->id}"); - } -} diff --git a/modules/image_block/helpers/image_block_installer.php b/modules/image_block/helpers/image_block_installer.php index 6b0ec20b..386ad2f5 100644 --- a/modules/image_block/helpers/image_block_installer.php +++ b/modules/image_block/helpers/image_block_installer.php @@ -24,18 +24,12 @@ class image_block_installer { if ($version == 0) { $db = Database::instance(); - $db->query("ALTER TABLE `items` ADD `rand_key` FLOAT DEFAULT NULL"); - $db->query("UPDATE `items` SET `rand_key` = RAND()"); - $db->query("CREATE INDEX `random_index` ON `items` (rand_key DESC)"); - module::set_version("image_block", 1); } } static function uninstall() { $db = Database::instance(); - // Dropping the column should drop the index as well. - $db->query("ALTER TABLE `items` DROP `rand_key`"); module::delete("image_block"); } diff --git a/modules/image_block/helpers/image_block_theme.php b/modules/image_block/helpers/image_block_theme.php index 0a103add..96ee58e2 100644 --- a/modules/image_block/helpers/image_block_theme.php +++ b/modules/image_block/helpers/image_block_theme.php @@ -19,11 +19,12 @@ */ class image_block_theme_Core { static function sidebar_blocks($theme) { - $result = ORM::factory("item") + // Leave as a count so we can filter based on viewable items. + $viewable_items = ORM::factory("item") ->viewable() ->select("COUNT(*) AS C") - ->find(); - if (empty($result->C)) { + ->find()->C; + if (empty($viewable_items)) { return ""; } @@ -32,18 +33,12 @@ class image_block_theme_Core { $block->title = t("Random Image"); $block->content = new View("image_block_block.html"); - $result = ORM::factory("item") - ->viewable() - ->select("MAX(rand_key) AS max_random") - ->find(); - - $max_rand = $result->max_random; $random = ((float)mt_rand()) / (float)mt_getrandmax(); $items = ORM::factory("item") ->viewable() ->where("type !=", "album") - ->where("rand_key < ", $max_rand * $random) + ->where("rand_key < ", $random) ->orderby(array("rand_key" => "DESC")) ->find_all(1); @@ -51,10 +46,9 @@ class image_block_theme_Core { $items = ORM::factory("item") ->viewable() ->where("type !=", "album") - ->where("rand_key > ", $max_rand * $random) + ->where("rand_key >= ", $random) ->orderby(array("rand_key" => "DESC")) ->find_all(1); - } $block->content->item = $items->current(); -- cgit v1.2.3