From 183e2841c50b9bdd7683680580f2b13eacbfbeca Mon Sep 17 00:00:00 2001 From: cash Date: Sat, 3 Dec 2011 17:26:27 -0500 Subject: basic view counting --- classes/TidypicsImage.php | 168 +++++++++++++++++++++++++--------------------- 1 file changed, 91 insertions(+), 77 deletions(-) (limited to 'classes') diff --git a/classes/TidypicsImage.php b/classes/TidypicsImage.php index 1f6812760..aec48b474 100644 --- a/classes/TidypicsImage.php +++ b/classes/TidypicsImage.php @@ -20,6 +20,7 @@ class TidypicsImage extends ElggFile { } /** + * Save the image * * @warning container_guid must be set first * @@ -42,6 +43,47 @@ class TidypicsImage extends ElggFile { return true; } + /** + * Delete image + * + * @return bool + */ + public function delete() { + + // check if batch should be deleted + $batch = elgg_get_entities_from_relationship(array( + 'relationship' => 'belongs_to_batch', + 'relationship_guid' => $this->guid, + 'inverse_relationship' => false, + )); + if ($batch) { + $batch = $batch[0]; + $count = elgg_get_entities_from_relationship(array( + 'relationship' => 'belongs_to_batch', + 'relationship_guid' => $batch->guid, + 'inverse_relationship' => true, + 'count' => true, + )); + if ($count == 1) { + // last image so delete batch + $batch->delete(); + } + } + + $album = get_entity($this->container_guid); + if ($album) { + $album->removeImage($this->guid); + } + + $this->removeThumbnails(); + + // update quota + $owner = $this->getOwnerEntity(); + $owner->image_repo_size = (int)$owner->image_repo_size - $this->size(); + + return parent::delete(); + } + /** * Get the title of the image * @@ -75,46 +117,67 @@ class TidypicsImage extends ElggFile { } /** - * delete image + * Get the view information for this image * - * @return bool + * @param $viewer_guid The guid of the viewer + * @return array with number of views, number of unique viewers, and number of views for this viewer */ - public function delete() { + public function getViewInfo($viewer_guid = 0) { + if ($viewer_guid == 0) { + $viewer_guid = elgg_get_logged_in_user_guid(); + } - // check if batch should be deleted - $batch = elgg_get_entities_from_relationship(array( - 'relationship' => 'belongs_to_batch', - 'relationship_guid' => $this->guid, - 'inverse_relationship' => false, + $views = elgg_get_annotations(array( + 'guid' => $this->getGUID(), + 'annotation_name' => 'tp_view', + 'limit' => 0, )); - if ($batch) { - $batch = $batch[0]; - $count = elgg_get_entities_from_relationship(array( - 'relationship' => 'belongs_to_batch', - 'relationship_guid' => $batch->guid, - 'inverse_relationship' => true, - 'count' => true, - )); - if ($count == 1) { - // last image so delete batch - $batch->delete(); + if ($views) { + $total_views = count($views); + + if ($this->getOwnerGUID() == $viewer_guid) { + // get unique number of viewers + $diff_viewers = array(); + foreach ($views as $view) { + $diff_viewers[$view->owner_guid] = 1; + } + $unique_viewers = count($diff_viewers); + } else if ($viewer_guid) { + // get the number of times this user has viewed the photo + $my_views = 0; + foreach ($views as $view) { + if ($view->owner_guid == $viewer_guid) { + $my_views++; + } + } } - } - $album = get_entity($this->container_guid); - if ($album) { - $album->removeImage($this->guid); + $view_info = array("total" => $total_views, "unique" => $unique_viewers, "mine" => $my_views); + } + else { + $view_info = array("total" => 0, "unique" => 0, "mine" => 0); } - $this->removeThumbnails(); + return $view_info; + } - // update quota - $owner = $this->getOwnerEntity(); - $owner->image_repo_size = (int)$owner->image_repo_size - $this->size(); + /** + * Add a view to this image + * + * @param $viewer_guid + * @return void + */ + public function addView($viewer_guid = 0) { + if ($viewer_guid == 0) { + $viewer_guid = elgg_get_logged_in_user_guid(); + } - return parent::delete(); + if ($viewer_guid != $this->owner_guid && tp_is_person()) { + create_annotation($this->getGUID(), "tp_view", "1", "integer", $viewer_guid, ACCESS_PUBLIC); + } } + /** * Set the internal filenames */ @@ -320,55 +383,6 @@ class TidypicsImage extends ElggFile { return $ret_data; } - /** - * Get the view information for this image - * - * @param $viewer_guid the guid of the viewer (0 if not logged in) - * @return array with number of views, number of unique viewers, and number of views for this viewer - */ - public function getViewCount($viewer_guid) { - $views = get_annotations($this->getGUID(), "object", "image", "tp_view", "", 0, 99999); - if ($views) { - $total_views = count($views); - - if ($this->owner_guid == $viewer_guid) { - // get unique number of viewers - foreach ($views as $view) { - $diff_viewers[$view->owner_guid] = 1; - } - $unique_viewers = count($diff_viewers); - } - else if ($viewer_guid) { - // get the number of times this user has viewed the photo - $my_views = 0; - foreach ($views as $view) { - if ($view->owner_guid == $viewer_guid) { - $my_views++; - } - } - } - - $view_info = array("total" => $total_views, "unique" => $unique_viewers, "mine" => $my_views); - } - else { - $view_info = array("total" => 0, "unique" => 0, "mine" => 0); - } - - return $view_info; - } - - /** - * Add a tidypics view annotation to this image - * - * @param $viewer_guid - * @return none - */ - public function addView($viewer_guid) { - if ($viewer_guid != $this->owner_guid && tp_is_person()) { - create_annotation($this->getGUID(), "tp_view", "1", "integer", $viewer_guid, ACCESS_PUBLIC); - } - } - /** * Remove thumbnails - usually in preparation for deletion * -- cgit v1.2.3