summaryrefslogtreecommitdiff
path: root/modules/gallery/helpers/item.php
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2010-01-08 14:56:08 -0800
committerBharat Mediratta <bharat@menalto.com>2010-01-08 14:56:08 -0800
commit9864ab4b2708ec54c39092a21828403cbbd25e2e (patch)
treee3542b02adbb8dc7da3c3185a35f1d46d944ef59 /modules/gallery/helpers/item.php
parentfb65a0a5854812a9837c770dfbb27a23bee49e3d (diff)
Move the random image functionality into the gallery REST helper since
choosing a random image is essentially a function on an item collection. Also implemented a bunch of other query filters for item collections. Created item::random_query() as a way of generating a reasonable starting point for random queries.
Diffstat (limited to 'modules/gallery/helpers/item.php')
-rw-r--r--modules/gallery/helpers/item.php25
1 files changed, 6 insertions, 19 deletions
diff --git a/modules/gallery/helpers/item.php b/modules/gallery/helpers/item.php
index eb528f8f..1fd9ef16 100644
--- a/modules/gallery/helpers/item.php
+++ b/modules/gallery/helpers/item.php
@@ -175,31 +175,18 @@ class item_Core {
}
/**
- * Return a random Item_Model, with optional filters
+ * Return a query to get a random Item_Model, with optional filters
*
* @param array (optional) where tuple
*/
- static function random($where=null) {
- $random = ((float)mt_rand()) / (float)mt_getrandmax();
-
- // Pick a random number and find the item that's got nearest smaller number. In the rare case
- // that we chose the smallest number in the system, choose the item with the smallest number.
+ static function random_query($where=null) {
+ // Pick a random number and find the item that's got nearest smaller number.
// This approach works best when the random numbers in the system are roughly evenly
// distributed so this is going to be more efficient with larger data sets.
- $items = ORM::factory("item")
+ return ORM::factory("item")
->viewable()
- ->where("rand_key", "<", $random)
+ ->where("rand_key", "<", ((float)mt_rand()) / (float)mt_getrandmax())
->merge_where($where)
- ->order_by("rand_key", "DESC")
- ->find_all(1);
-
- if ($items->count() == 0) {
- $items = ORM::factory("item")
- ->viewable()
- ->merge_where($where)
- ->order_by("rand_key", "ASC")
- ->find_all(1);
- }
- return $items;
+ ->order_by("rand_key", "DESC");
}
} \ No newline at end of file