diff options
author | cash <cash.costello@gmail.com> | 2011-12-03 17:26:27 -0500 |
---|---|---|
committer | cash <cash.costello@gmail.com> | 2011-12-03 17:26:27 -0500 |
commit | 183e2841c50b9bdd7683680580f2b13eacbfbeca (patch) | |
tree | 97573f8f572af73fbfdb053bb14785f758177cae | |
parent | f057f68c6f946244b3afe0340ae11efdde0d8136 (diff) | |
download | elgg-183e2841c50b9bdd7683680580f2b13eacbfbeca.tar.gz elgg-183e2841c50b9bdd7683680580f2b13eacbfbeca.tar.bz2 |
basic view counting
-rw-r--r-- | classes/TidypicsImage.php | 168 | ||||
-rw-r--r-- | pages/photos/image/view.php | 5 | ||||
-rw-r--r-- | start.php | 3 | ||||
-rw-r--r-- | views/default/photos/css.php | 1 |
4 files changed, 99 insertions, 78 deletions
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 * @@ -43,6 +44,47 @@ class TidypicsImage extends ElggFile { } /** + * 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 * * @return string @@ -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 */ @@ -321,55 +384,6 @@ class TidypicsImage extends ElggFile { } /** - * 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 * * The thumbnails are not actually ElggObjects so we create diff --git a/pages/photos/image/view.php b/pages/photos/image/view.php index f172b7cd6..22ccfb34d 100644 --- a/pages/photos/image/view.php +++ b/pages/photos/image/view.php @@ -11,6 +11,11 @@ group_gatekeeper(); // get the photo entity $photo_guid = (int) get_input('guid'); $photo = get_entity($photo_guid); +if (!$photo) { + +} + +$photo->addView(); // set page owner based on owner of photo album $album = $photo->getContainerEntity(); @@ -285,7 +285,8 @@ function tidypics_entity_menu_setup($hook, $type, $return, $params) { if (elgg_instanceof($entity, 'object', 'image')) { if (elgg_get_plugin_setting('view_count', 'tidypics')) { - $status_text = '27 views'; + $view_info = $entity->getViewInfo(); + $status_text = (int)$view_info['total'] . ' views'; $options = array( 'name' => 'published_status', 'text' => "<span>$status_text</span>", diff --git a/views/default/photos/css.php b/views/default/photos/css.php index 0c0f33c2c..119d98c09 100644 --- a/views/default/photos/css.php +++ b/views/default/photos/css.php @@ -20,6 +20,7 @@ .tidypics-photo { margin: 0 auto; + display: block; } .tidypics-heading { |