]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
basic view counting
authorcash <cash.costello@gmail.com>
Sat, 3 Dec 2011 22:26:27 +0000 (17:26 -0500)
committercash <cash.costello@gmail.com>
Sat, 3 Dec 2011 22:26:27 +0000 (17:26 -0500)
classes/TidypicsImage.php
pages/photos/image/view.php
start.php
views/default/photos/css.php

index 1f6812760b5d55f9ab23c692425dfdb0df28270b..aec48b4744218a3dde79185ede67ae180174ad49 100644 (file)
@@ -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
         *
index f172b7cd69b70a4a5ba4e17eae3c48bb992d064a..22ccfb34d0ad99724ff5a232c7c907243708d989 100644 (file)
@@ -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();
index f094bd2f43f83c32760c58ae60f0bbe11780ca7e..4942f949a19df5a7cfbeacb0061ea9ad032cea96 100644 (file)
--- a/start.php
+++ b/start.php
@@ -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>",
index 0c0f33c2c304f1eeaf68a688995f1e2c1139999a..119d98c09fa301e596d1181aec6330c5bff1a4e6 100644 (file)
@@ -20,6 +20,7 @@
 
 .tidypics-photo {
        margin: 0 auto;
+       display: block;
 }
 
 .tidypics-heading {