]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Added entity_owner_guid to get_annotations()
authorbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>
Wed, 26 Aug 2009 13:44:46 +0000 (13:44 +0000)
committerbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>
Wed, 26 Aug 2009 13:44:46 +0000 (13:44 +0000)
git-svn-id: https://code.elgg.org/elgg/trunk@3443 36083f99-b078-4883-b0ff-0f9b5a30f544

engine/lib/annotations.php

index 4e506a75ac17b58281115e1f5ac7baba33628e55..49ffc7840eca92b2354a41e6b68fe348123e7dc1 100644 (file)
-<?php\r
-       /**\r
-        * Elgg annotations\r
-        * Functions to manage object annotations.\r
-        * \r
-        * @package Elgg\r
-        * @subpackage Core\r
+<?php
+       /**
+        * Elgg annotations
+        * Functions to manage object annotations.
+        * 
+        * @package Elgg
+        * @subpackage Core
+
+        * @author Curverider Ltd <info@elgg.com>
+
+        * @link http://elgg.org/
+        */
+
+    /**
+     * Include the ElggExtender superclass
+     * 
+     */
+       require_once('extender.php');
+
+       /**
+        * ElggAnnotation
+        * 
+        * An annotation is similar to metadata each entity can contain more than one of each annotation.
+        * 
+        * @package Elgg
+        * @subpackage Core
+        * @author Curverider Ltd <info@elgg.com>
+        */
+       class ElggAnnotation extends ElggExtender
+       {
+               
+               /**
+                * Construct a new site object, optionally from a given id value or db row.
+                *
+                * @param mixed $id
+                */
+               function __construct($id = null) 
+               {
+                       $this->attributes = array();
+                       
+                       if (!empty($id)) {
+                               if ($id instanceof stdClass)
+                                       $annotation = $id;
+                               else
+                                       $annotation = get_annotation($id);
+                               
+                               if ($annotation) {
+                                       $objarray = (array) $annotation;
+                                       foreach($objarray as $key => $value) {
+                                               $this->attributes[$key] = $value;
+                                       }
+                                       $this->attributes['type'] = "annotation";
+                               }
+                       }
+               }
+               
+               /**
+                * Class member get overloading
+                *
+                * @param string $name
+                * @return mixed
+                */
+               function __get($name) {
+                       return $this->get($name);
+               }
+               
+               /**
+                * Class member set overloading
+                *
+                * @param string $name
+                * @param mixed $value
+                * @return void
+                */
+               function __set($name, $value) {
+                       return $this->set($name, $value);
+               }               
+               
+               /**
+                * Save this instance
+                *
+                * @return int an object id
+                */
+               function save()
+               {
+                       if ($this->id > 0)
+                               return update_annotation($this->id, $this->name, $this->value, $this->value_type, $this->owner_guid, $this->access_id);
+                       else
+                       { 
+                               $this->id = create_annotation($this->entity_guid, $this->name, $this->value, $this->value_type, $this->owner_guid, $this->access_id);
+                               if (!$this->id) throw new IOException(sprintf(elgg_new('IOException:UnableToSaveNew'), get_class()));
+                               return $this->id;
+                       }
+               }
+               
+               /**
+                * Delete a given site.
+                */
+               function delete() 
+               { 
+                       return delete_annotation($this->id); 
+               }
+               
+               /**
+                * Get a url for this annotation.
+                *
+                * @return string
+                */
+               public function getURL() { return get_annotation_url($this->id); }
+       
+               // SYSTEM LOG INTERFACE ////////////////////////////////////////////////////////////
+
+               /**
+                * For a given ID, return the object associated with it.
+                * This is used by the river functionality primarily.
+                * This is useful for checking access permissions etc on objects.
+                */
+               public function getObjectFromID($id) { return get_annotation($id); }
+       }
+       
+       /**
+        * Convert a database row to a new ElggAnnotation
+        *
+        * @param stdClass $row
+        * @return stdClass or ElggAnnotation
+        */
+       function row_to_elggannotation($row) 
+       {
+               if (!($row instanceof stdClass))
+                       return $row;
+                       
+               return new ElggAnnotation($row);
+       }
+       
+       /**
+        * Get a specific annotation.
+        *
+        * @param int $annotation_id
+        */
+       function get_annotation($annotation_id)
+       {
+               global $CONFIG;
+               
+               $annotation_id = (int) $annotation_id;
+               $access = get_access_sql_suffix("a");
+               
+               return row_to_elggannotation(get_data_row("SELECT a.*, n.string as name, v.string as value from {$CONFIG->dbprefix}annotations a JOIN {$CONFIG->dbprefix}metastrings n on a.name_id = n.id JOIN {$CONFIG->dbprefix}metastrings v on a.value_id = v.id where a.id=$annotation_id and $access"));                 
+       }
+       
+       /**
+        * Create a new annotation.
+        *
+        * @param int $entity_guid
+        * @param string $name
+        * @param string $value
+        * @param string $value_type
+        * @param int $owner_guid
+        * @param int $access_id
+        */
+       function create_annotation($entity_guid, $name, $value, $value_type, $owner_guid, $access_id = ACCESS_PRIVATE)
+       {
+               global $CONFIG;
+
+               $result = false;
+               
+               $entity_guid = (int)$entity_guid;
+               //$name = sanitise_string(trim($name));
+               //$value = sanitise_string(trim($value));
+               $value_type = detect_extender_valuetype($value, sanitise_string(trim($value_type)));
+               
+               $owner_guid = (int)$owner_guid;
+               if ($owner_guid==0) $owner_guid = get_loggedin_userid();
+               
+               $access_id = (int)$access_id;
+               
+               $time = time();
+               
+               // Add the metastring
+               $value = add_metastring($value);
+               if (!$value) return false;
+               
+               $name = add_metastring($name);
+               if (!$name) return false;
+               
+               $entity = get_entity($entity_guid);
+               
+               if (trigger_elgg_event('annotate',$entity->type,$entity)) {
+                       system_log($entity, 'annotate');
+                       
+                       // If ok then add it
+                       $result = insert_data("INSERT into {$CONFIG->dbprefix}annotations (entity_guid, name_id, value_id, value_type, owner_guid, time_created, access_id) VALUES ($entity_guid,'$name',$value,'$value_type', $owner_guid, $time, $access_id)");
+                       if ($result!==false) {
+                               $obj = get_annotation($result);
+                               if (trigger_elgg_event('create', 'annotation', $obj)) {
+                                       return true;
+                               } else {
+                                       delete_annotation($result);
+                               }
+                       }
+               }
+               
+               return $result;
+       }
+       
+       /**
+        * Update an annotation.
+        *
+        * @param int $annotation_id
+        * @param string $name
+        * @param string $value
+        * @param string $value_type
+        * @param int $owner_guid
+        * @param int $access_id
+        */
+       function update_annotation($annotation_id, $name, $value, $value_type, $owner_guid, $access_id)
+       {
+               global $CONFIG;
+
+               $annotation_id = (int)$annotation_id;
+               $name = (trim($name));
+               $value = (trim($value));
+               $value_type = detect_extender_valuetype($value, sanitise_string(trim($value_type)));
+               
+               $owner_guid = (int)$owner_guid;
+               if ($owner_guid==0) $owner_guid = get_loggedin_userid();
+               
+               $access_id = (int)$access_id;
+               
+               $access = get_access_sql_suffix();
+               
+               // Add the metastring
+               $value = add_metastring($value);
+               if (!$value) return false;
+               
+               $name = add_metastring($name);
+               if (!$name) return false;
+               
+               // If ok then add it            
+               $result = update_data("UPDATE {$CONFIG->dbprefix}annotations set value_id='$value', value_type='$value_type', access_id=$access_id, owner_guid=$owner_guid where id=$annotation_id and name_id='$name' and $access");
+               if ($result!==false) {
+                       $obj = get_annotation($annotation_id);
+                       if (trigger_elgg_event('update', 'annotation', $obj)) {
+                               return true;
+                       } else {
+                               delete_annotation($annotation_id);
+                       }
+               }
+               
+               return $result;
+       }
+       
+       /**
+        * Get a list of annotations for a given object/user/annotation type.
+        *
+        * @param int|array $entity_guid
+        * @param string $entity_type
+        * @param string $entity_subtype
+        * @param string $name
+        * @param mixed $value
+        * @param int|array $owner_guid
+        * @param int $limit
+        * @param int $offset
+        * @param string $order_by
+        */
+       function get_annotations($entity_guid = 0, $entity_type = "", $entity_subtype = "", $name = "", 
+       $value = "", $owner_guid = 0, $limit = 10, $offset = 0, $order_by = "asc", $timelower = 0, $timeupper = 0, $entity_owner_guid = 0)
+       {
+               global $CONFIG;
+               
+               $timelower = (int) $timelower;
+               $timeupper = (int) $timeupper;
+               
+               if (is_array($entity_guid)) {
+                       if (sizeof($entity_guid) > 0) {
+                               foreach($entity_guid as $key => $val) {
+                                       $entity_guid[$key] = (int) $val;
+                               }
+                       } else {
+                               $entity_guid = 0;
+                       }
+               } else {
+                       $entity_guid = (int)$entity_guid;
+               }
+               $entity_type = sanitise_string($entity_type);
+               $entity_subtype = get_subtype_id($entity_type, $entity_subtype);
+               if ($name)
+               {
+                       $name = get_metastring_id($name);
+               
+                       if ($name === false)
+                               $name = 0;
+               }
+               if ($value != "") $value = get_metastring_id($value);
+               if (is_array($owner_guid)) {
+                       if (sizeof($owner_guid) > 0) {
+                               foreach($owner_guid as $key => $val) {
+                                       $owner_guid[$key] = (int) $val;
+                               }
+                       } else {
+                               $owner_guid = 0;
+                       }
+               } else {
+                       $owner_guid = (int)$owner_guid;
+               }
 
-        * @author Curverider Ltd <info@elgg.com>\r
+               if (is_array($entity_owner_guid)) {
+                       if (sizeof($entity_owner_guid) > 0) {
+                               foreach($entity_owner_guid as $key => $val) {
+                                       $entity_owner_guid[$key] = (int) $val;
+                               }
+                       } else {
+                               $entity_owner_guid = 0;
+                       }
+               } else {
+                       $entity_owner_guid = (int)$entity_owner_guid;
+               }
+               
+               $limit = (int)$limit;
+               $offset = (int)$offset;
+               if($order_by == 'asc')
+                   $order_by = "a.time_created asc";
+                   
+               if($order_by == 'desc')
+                   $order_by = "a.time_created desc";
+               
+               $where = array();
+               
+               if ($entity_guid != 0 && !is_array($entity_guid)) {
+                       $where[] = "a.entity_guid=$entity_guid";
+               } else if (is_array($entity_guid)) {
+                       $where[] = "a.entity_guid in (". implode(",",$entity_guid) . ")";
+               }
+                       
+               if ($entity_type != "")
+                       $where[] = "e.type='$entity_type'";
+                       
+               if ($entity_subtype != "")
+                       $where[] = "e.subtype='$entity_subtype'";
+               
+               if ($owner_guid != 0 && !is_array($owner_guid)) {
+                       $where[] = "a.owner_guid=$owner_guid";
+               } else {
+                       if (is_array($owner_guid))
+                               $where[] = "a.owner_guid in (" . implode(",",$owner_guid) . ")";
+               }
+               
+               if ($entity_owner_guid != 0 && !is_array($entity_owner_guid)) {
+                       $where[] = "e.owner_guid=$entity_owner_guid";
+               } else {
+                       if (is_array($entity_owner_guid))
+                               $where[] = "e.owner_guid in (" . implode(",",$entity_owner_guid) . ")";
+               }
+                       
+               if ($name !== "")
+                       $where[] = "a.name_id='$name'";
+                       
+               if ($value != "")
+                       $where[] = "a.value_id='$value'";
+                       
+               if ($timelower)
+                       $where[] = "a.time_created >= {$timelower}";
+               if ($timeupper)
+                       $where[] = "a.time_created <= {$timeupper}";
+                       
+               $query = "SELECT a.*, n.string as name, v.string as value from {$CONFIG->dbprefix}annotations a JOIN {$CONFIG->dbprefix}entities e on a.entity_guid = e.guid JOIN {$CONFIG->dbprefix}metastrings v on a.value_id=v.id JOIN {$CONFIG->dbprefix}metastrings n on a.name_id = n.id where ";
+               foreach ($where as $w)
+                       $query .= " $w and ";
+               $query .= get_access_sql_suffix("a"); // Add access controls
+               $query .= " order by $order_by limit $offset,$limit"; // Add order and limit
+               
+               return get_data($query, "row_to_elggannotation");
+               
+       }
+       
+       /**
+        * Return a list of entities which are annotated with a specific annotation. 
+        * These can be ordered by when the annotation was created/updated.
+        * 
+        * @param string $entity_type Type of entity.
+        * @param string $entity_subtype Subtype of entity.
+        * @param string $name Name of annotation.
+        * @param string $value Value of annotation.
+        * @param int $owner_guid Owner.
+        * @param int $group_guid Group container. Currently this is only supported if $entity_type == 'object'
+        * @param int $limit Maximum number of results to return.
+        * @param int $offset Place to start.
+        * @param string $order_by How to order results.
+        * @param boolean $count Whether to count entities rather than return them
+        * @param int $timelower The earliest time the annotation can have been created. Default: all
+        * @param int $timeupper The latest time the annotation can have been created. Default: all
+        */
+       function get_entities_from_annotations($entity_type = "", $entity_subtype = "", $name = "", $value = "", $owner_guid = 0, $group_guid = 0, $limit = 10, $offset = 0, $order_by = "asc", $count = false, $timelower = 0, $timeupper = 0)
+       {
+               global $CONFIG;
+               
+               $entity_type = sanitise_string($entity_type);
+               $entity_subtype = get_subtype_id($entity_type, $entity_subtype);
+               $timelower = (int) $timelower;
+               $timeupper = (int) $timeupper;
+               
+               if ($name)
+               {
+                       $name = get_metastring_id($name);
+               
+                       if ($name === false)
+                               $name = 0;
+               }
+               if ($value != "") $value = get_metastring_id($value);
+               if (is_array($owner_guid)) {
+                       if (sizeof($owner_guid) > 0) {
+                               foreach($owner_guid as $key => $val) {
+                                       $owner_guid[$key] = (int) $val;
+                               }
+                       } else {
+                               $owner_guid = 0;
+                       }
+               } else {
+                       $owner_guid = (int)$owner_guid;
+               }
+               $group_guid = (int)$group_guid;
+               
+               $limit = (int)$limit;
+               $offset = (int)$offset;
+               if($order_by == 'asc')
+                   $order_by = "maxtime asc";
+                   
+               if($order_by == 'desc')
+                   $order_by = "maxtime desc";
+               
+               $where = array();
+               
+               if ($entity_type != "")
+                       $where[] = "e.type='$entity_type'";
+                       
+               if ($entity_subtype != "")
+                       $where[] = "e.subtype='$entity_subtype'";
+               
+               if ($owner_guid != 0 && !is_array($owner_guid)) {
+                       $where[] = "a.owner_guid=$owner_guid";
+               } else {
+                       if (is_array($owner_guid))
+                               $where[] = "a.owner_guid in (" . implode(",",$owner_guid) . ")";
+               }
+               
+               if (($group_guid != 0) && ($entity_type=='object'))
+                       $where[] = "e.container_guid = $group_guid";
+                       
+               if ($name !== "")
+                       $where[] = "a.name_id='$name'";
+                       
+               if ($value != "")
+                       $where[] = "a.value_id='$value'";
+
+               if ($timelower)
+                       $where[] = "a.time_created >= {$timelower}";
+               if ($timeupper)
+                       $where[] = "a.time_created <= {$timeupper}";
+               
+               if ($count) {
+                       $query = "SELECT count(distinct e.guid) as total ";
+               } else {
+                       $query = "SELECT e.*, max(a.time_created) as maxtime ";                 
+               }
+               $query .= "from {$CONFIG->dbprefix}annotations a JOIN {$CONFIG->dbprefix}entities e on e.guid = a.entity_guid ";
+               if ($value != "")
+                       $query .= " JOIN {$CONFIG->dbprefix}metastrings v on a.value_id=v.id";
+               
+               if (($group_guid != 0) && ($entity_type=='object')) $query .= "JOIN {$CONFIG->dbprefix}objects_entity o on o.guid = e.guid";
+               $query .= " where";
+                   
+               foreach ($where as $w)
+                       $query .= " $w and ";
+               $query .= get_access_sql_suffix("a"); // Add access controls
+               $query .= ' and ' . get_access_sql_suffix("e"); // Add access controls
+               
+               if ($count) {
+                       $row = get_data_row($query);
+                       return $row->total;
+               } else {
+                       $query .= " group by a.entity_guid order by $order_by limit $offset,$limit"; // Add order and limit
+                       return get_data($query, "entity_row_to_elggstar");
+               }    
+       }
+       
+       /**
+        * Lists entities
+        *
+        * @see elgg_view_entity_list
+        * 
+        * @param string $entity_type Type of entity.
+        * @param string $entity_subtype Subtype of entity.
+        * @param string $name Name of annotation.
+        * @param string $value Value of annotation.
+        * @param int $limit Maximum number of results to return.
+        * @param int $owner_guid Owner.
+        * @param int $group_guid Group container. Currently this is only supported if $entity_type == 'object'
+        * @param boolean $asc Whether to list in ascending or descending order (default: desc)
+        * @param boolean $fullview Whether to display the entities in full
+        * @param boolean $viewtypetoggle Determines whether or not the 'gallery' view can be displayed (default: no)
+        * @return string Formatted entity list
+        */
+       function list_entities_from_annotations($entity_type = "", $entity_subtype = "", $name = "", $value = "", $limit = 10, $owner_guid = 0, $group_guid = 0, $asc = false, $fullview = true, $viewtypetoggle = false) {
+               
+               if ($asc) {
+                       $asc = "asc";
+               } else {
+                       $asc = "desc";
+               }
+               $count = get_entities_from_annotations($entity_type, $entity_subtype, $name, $value, $owner_guid, $group_guid, null, null, $asc, true);
+               $offset = (int) get_input("offset",0);
+               $entities = get_entities_from_annotations($entity_type, $entity_subtype, $name, $value, $owner_guid, $group_guid, $limit, $offset, $asc);
 
-        * @link http://elgg.org/\r
-        */\r
-\r
-    /**\r
-     * Include the ElggExtender superclass\r
-     * \r
-     */\r
-       require_once('extender.php');\r
-\r
-       /**\r
-        * ElggAnnotation\r
-        * \r
-        * An annotation is similar to metadata each entity can contain more than one of each annotation.\r
-        * \r
-        * @package Elgg\r
-        * @subpackage Core\r
-        * @author Curverider Ltd <info@elgg.com>\r
-        */\r
-       class ElggAnnotation extends ElggExtender\r
-       {\r
-               \r
-               /**\r
-                * Construct a new site object, optionally from a given id value or db row.\r
-                *\r
-                * @param mixed $id\r
-                */\r
-               function __construct($id = null) \r
-               {\r
-                       $this->attributes = array();\r
-                       \r
-                       if (!empty($id)) {\r
-                               if ($id instanceof stdClass)\r
-                                       $annotation = $id;\r
-                               else\r
-                                       $annotation = get_annotation($id);\r
-                               \r
-                               if ($annotation) {\r
-                                       $objarray = (array) $annotation;\r
-                                       foreach($objarray as $key => $value) {\r
-                                               $this->attributes[$key] = $value;\r
-                                       }\r
-                                       $this->attributes['type'] = "annotation";\r
-                               }\r
-                       }\r
-               }\r
-               \r
-               /**\r
-                * Class member get overloading\r
-                *\r
-                * @param string $name\r
-                * @return mixed\r
-                */\r
-               function __get($name) {\r
-                       return $this->get($name);\r
-               }\r
-               \r
-               /**\r
-                * Class member set overloading\r
-                *\r
-                * @param string $name\r
-                * @param mixed $value\r
-                * @return void\r
-                */\r
-               function __set($name, $value) {\r
-                       return $this->set($name, $value);\r
-               }               \r
-               \r
-               /**\r
-                * Save this instance\r
-                *\r
-                * @return int an object id\r
-                */\r
-               function save()\r
-               {\r
-                       if ($this->id > 0)\r
-                               return update_annotation($this->id, $this->name, $this->value, $this->value_type, $this->owner_guid, $this->access_id);\r
-                       else\r
-                       { \r
-                               $this->id = create_annotation($this->entity_guid, $this->name, $this->value, $this->value_type, $this->owner_guid, $this->access_id);\r
-                               if (!$this->id) throw new IOException(sprintf(elgg_new('IOException:UnableToSaveNew'), get_class()));\r
-                               return $this->id;\r
-                       }\r
-               }\r
-               \r
-               /**\r
-                * Delete a given site.\r
-                */\r
-               function delete() \r
-               { \r
-                       return delete_annotation($this->id); \r
-               }\r
-               \r
-               /**\r
-                * Get a url for this annotation.\r
-                *\r
-                * @return string\r
-                */\r
-               public function getURL() { return get_annotation_url($this->id); }\r
-       \r
-               // SYSTEM LOG INTERFACE ////////////////////////////////////////////////////////////\r
-\r
-               /**\r
-                * For a given ID, return the object associated with it.\r
-                * This is used by the river functionality primarily.\r
-                * This is useful for checking access permissions etc on objects.\r
-                */\r
-               public function getObjectFromID($id) { return get_annotation($id); }\r
-       }\r
-       \r
-       /**\r
-        * Convert a database row to a new ElggAnnotation\r
-        *\r
-        * @param stdClass $row\r
-        * @return stdClass or ElggAnnotation\r
-        */\r
-       function row_to_elggannotation($row) \r
-       {\r
-               if (!($row instanceof stdClass))\r
-                       return $row;\r
-                       \r
-               return new ElggAnnotation($row);\r
-       }\r
-       \r
-       /**\r
-        * Get a specific annotation.\r
-        *\r
-        * @param int $annotation_id\r
-        */\r
-       function get_annotation($annotation_id)\r
-       {\r
-               global $CONFIG;\r
-               \r
-               $annotation_id = (int) $annotation_id;\r
-               $access = get_access_sql_suffix("a");\r
-               \r
-               return row_to_elggannotation(get_data_row("SELECT a.*, n.string as name, v.string as value from {$CONFIG->dbprefix}annotations a JOIN {$CONFIG->dbprefix}metastrings n on a.name_id = n.id JOIN {$CONFIG->dbprefix}metastrings v on a.value_id = v.id where a.id=$annotation_id and $access"));                 \r
-       }\r
-       \r
-       /**\r
-        * Create a new annotation.\r
-        *\r
-        * @param int $entity_guid\r
-        * @param string $name\r
-        * @param string $value\r
-        * @param string $value_type\r
-        * @param int $owner_guid\r
-        * @param int $access_id\r
-        */\r
-       function create_annotation($entity_guid, $name, $value, $value_type, $owner_guid, $access_id = ACCESS_PRIVATE)\r
-       {\r
-               global $CONFIG;\r
-\r
-               $result = false;\r
-               \r
-               $entity_guid = (int)$entity_guid;\r
-               //$name = sanitise_string(trim($name));\r
-               //$value = sanitise_string(trim($value));\r
-               $value_type = detect_extender_valuetype($value, sanitise_string(trim($value_type)));\r
-               \r
-               $owner_guid = (int)$owner_guid;\r
-               if ($owner_guid==0) $owner_guid = get_loggedin_userid();\r
-               \r
-               $access_id = (int)$access_id;\r
-               \r
-               $time = time();\r
-               \r
-               // Add the metastring\r
-               $value = add_metastring($value);\r
-               if (!$value) return false;\r
-               \r
-               $name = add_metastring($name);\r
-               if (!$name) return false;\r
-               \r
-               $entity = get_entity($entity_guid);\r
-               \r
-               if (trigger_elgg_event('annotate',$entity->type,$entity)) {\r
-                       system_log($entity, 'annotate');\r
-                       \r
-                       // If ok then add it\r
-                       $result = insert_data("INSERT into {$CONFIG->dbprefix}annotations (entity_guid, name_id, value_id, value_type, owner_guid, time_created, access_id) VALUES ($entity_guid,'$name',$value,'$value_type', $owner_guid, $time, $access_id)");\r
-                       if ($result!==false) {\r
-                               $obj = get_annotation($result);\r
-                               if (trigger_elgg_event('create', 'annotation', $obj)) {\r
-                                       return true;\r
-                               } else {\r
-                                       delete_annotation($result);\r
-                               }\r
-                       }\r
-               }\r
-               \r
-               return $result;\r
-       }\r
-       \r
-       /**\r
-        * Update an annotation.\r
-        *\r
-        * @param int $annotation_id\r
-        * @param string $name\r
-        * @param string $value\r
-        * @param string $value_type\r
-        * @param int $owner_guid\r
-        * @param int $access_id\r
-        */\r
-       function update_annotation($annotation_id, $name, $value, $value_type, $owner_guid, $access_id)\r
-       {\r
-               global $CONFIG;\r
-\r
-               $annotation_id = (int)$annotation_id;\r
-               $name = (trim($name));\r
-               $value = (trim($value));\r
-               $value_type = detect_extender_valuetype($value, sanitise_string(trim($value_type)));\r
-               \r
-               $owner_guid = (int)$owner_guid;\r
-               if ($owner_guid==0) $owner_guid = get_loggedin_userid();\r
-               \r
-               $access_id = (int)$access_id;\r
-               \r
-               $access = get_access_sql_suffix();\r
-               \r
-               // Add the metastring\r
-               $value = add_metastring($value);\r
-               if (!$value) return false;\r
-               \r
-               $name = add_metastring($name);\r
-               if (!$name) return false;\r
-               \r
-               // If ok then add it            \r
-               $result = update_data("UPDATE {$CONFIG->dbprefix}annotations set value_id='$value', value_type='$value_type', access_id=$access_id, owner_guid=$owner_guid where id=$annotation_id and name_id='$name' and $access");\r
-               if ($result!==false) {\r
-                       $obj = get_annotation($annotation_id);\r
-                       if (trigger_elgg_event('update', 'annotation', $obj)) {\r
-                               return true;\r
-                       } else {\r
-                               delete_annotation($annotation_id);\r
-                       }\r
-               }\r
-               \r
-               return $result;\r
-       }\r
-       \r
-       /**\r
-        * Get a list of annotations for a given object/user/annotation type.\r
-        *\r
-        * @param int|array $entity_guid\r
-        * @param string $entity_type\r
-        * @param string $entity_subtype\r
-        * @param string $name\r
-        * @param mixed $value\r
-        * @param int|array $owner_guid\r
-        * @param int $limit\r
-        * @param int $offset\r
-        * @param string $order_by\r
-        */\r
-       function get_annotations($entity_guid = 0, $entity_type = "", $entity_subtype = "", $name = "", \r
-       $value = "", $owner_guid = 0, $limit = 10, $offset = 0, $order_by = "asc", $timelower = 0, $timeupper = 0)\r
-       {\r
-               global $CONFIG;\r
-               \r
-               $timelower = (int) $timelower;\r
-               $timeupper = (int) $timeupper;\r
-               \r
-               if (is_array($entity_guid)) {\r
-                       if (sizeof($entity_guid) > 0) {\r
-                               foreach($entity_guid as $key => $val) {\r
-                                       $entity_guid[$key] = (int) $val;                        \r
-                               }\r
-                       } else {\r
-                               $entity_guid = 0;\r
-                       }\r
-               } else {\r
-                       $entity_guid = (int)$entity_guid;\r
-               }\r
-               $entity_type = sanitise_string($entity_type);\r
-               $entity_subtype = get_subtype_id($entity_type, $entity_subtype);\r
-               if ($name)\r
-               {\r
-                       $name = get_metastring_id($name);\r
-               \r
-                       if ($name === false)\r
-                               $name = 0;\r
-               }\r
-               if ($value != "") $value = get_metastring_id($value);\r
-               if (is_array($owner_guid)) {\r
-                       if (sizeof($owner_guid) > 0) {\r
-                               foreach($owner_guid as $key => $val) {\r
-                                       $owner_guid[$key] = (int) $val;\r
-                               }\r
-                       } else {\r
-                               $owner_guid = 0;\r
-                       }\r
-               } else {\r
-                       $owner_guid = (int)$owner_guid;\r
-               }\r
-               $limit = (int)$limit;\r
-               $offset = (int)$offset;\r
-               if($order_by == 'asc')\r
-                   $order_by = "a.time_created asc";\r
-                   \r
-               if($order_by == 'desc')\r
-                   $order_by = "a.time_created desc";\r
-               \r
-               $where = array();\r
-               \r
-               if ($entity_guid != 0 && !is_array($entity_guid)) {\r
-                       $where[] = "a.entity_guid=$entity_guid";\r
-               } else if (is_array($entity_guid)) {\r
-                       $where[] = "a.entity_guid in (". implode(",",$entity_guid) . ")";\r
-               }\r
-                       \r
-               if ($entity_type != "")\r
-                       $where[] = "e.type='$entity_type'";\r
-                       \r
-               if ($entity_subtype != "")\r
-                       $where[] = "e.subtype='$entity_subtype'";\r
-               \r
-               if ($owner_guid != 0 && !is_array($owner_guid)) {\r
-                       $where[] = "a.owner_guid=$owner_guid";\r
-               } else {\r
-                       if (is_array($owner_guid))\r
-                               $where[] = "a.owner_guid in (" . implode(",",$owner_guid) . ")";\r
-               }\r
-                       \r
-               if ($name !== "")\r
-                       $where[] = "a.name_id='$name'";\r
-                       \r
-               if ($value != "")\r
-                       $where[] = "a.value_id='$value'";\r
-                       \r
-               if ($timelower)\r
-                       $where[] = "a.time_created >= {$timelower}";\r
-               if ($timeupper)\r
-                       $where[] = "a.time_created <= {$timeupper}";\r
-                       \r
-               $query = "SELECT a.*, n.string as name, v.string as value from {$CONFIG->dbprefix}annotations a JOIN {$CONFIG->dbprefix}entities e on a.entity_guid = e.guid JOIN {$CONFIG->dbprefix}metastrings v on a.value_id=v.id JOIN {$CONFIG->dbprefix}metastrings n on a.name_id = n.id where ";\r
-               foreach ($where as $w)\r
-                       $query .= " $w and ";\r
-               $query .= get_access_sql_suffix("a"); // Add access controls\r
-               $query .= " order by $order_by limit $offset,$limit"; // Add order and limit\r
-               \r
-               return get_data($query, "row_to_elggannotation");\r
-               \r
-       }\r
-       \r
-       /**\r
-        * Return a list of entities which are annotated with a specific annotation. \r
-        * These can be ordered by when the annotation was created/updated.\r
-        * \r
-        * @param string $entity_type Type of entity.\r
-        * @param string $entity_subtype Subtype of entity.\r
-        * @param string $name Name of annotation.\r
-        * @param string $value Value of annotation.\r
-        * @param int $owner_guid Owner.\r
-        * @param int $group_guid Group container. Currently this is only supported if $entity_type == 'object'\r
-        * @param int $limit Maximum number of results to return.\r
-        * @param int $offset Place to start.\r
-        * @param string $order_by How to order results.\r
-        * @param boolean $count Whether to count entities rather than return them\r
-        * @param int $timelower The earliest time the annotation can have been created. Default: all\r
-        * @param int $timeupper The latest time the annotation can have been created. Default: all\r
-        */\r
-       function get_entities_from_annotations($entity_type = "", $entity_subtype = "", $name = "", $value = "", $owner_guid = 0, $group_guid = 0, $limit = 10, $offset = 0, $order_by = "asc", $count = false, $timelower = 0, $timeupper = 0)\r
-       {\r
-               global $CONFIG;\r
-               \r
-               $entity_type = sanitise_string($entity_type);\r
-               $entity_subtype = get_subtype_id($entity_type, $entity_subtype);\r
-               $timelower = (int) $timelower;\r
-               $timeupper = (int) $timeupper;\r
-               \r
-               if ($name)\r
-               {\r
-                       $name = get_metastring_id($name);\r
-               \r
-                       if ($name === false)\r
-                               $name = 0;\r
-               }\r
-               if ($value != "") $value = get_metastring_id($value);\r
-               if (is_array($owner_guid)) {\r
-                       if (sizeof($owner_guid) > 0) {\r
-                               foreach($owner_guid as $key => $val) {\r
-                                       $owner_guid[$key] = (int) $val;\r
-                               }\r
-                       } else {\r
-                               $owner_guid = 0;\r
-                       }\r
-               } else {\r
-                       $owner_guid = (int)$owner_guid;\r
-               }\r
-               $group_guid = (int)$group_guid;\r
-               \r
-               $limit = (int)$limit;\r
-               $offset = (int)$offset;\r
-               if($order_by == 'asc')\r
-                   $order_by = "maxtime asc";\r
-                   \r
-               if($order_by == 'desc')\r
-                   $order_by = "maxtime desc";\r
-               \r
-               $where = array();\r
-               \r
-               if ($entity_type != "")\r
-                       $where[] = "e.type='$entity_type'";\r
-                       \r
-               if ($entity_subtype != "")\r
-                       $where[] = "e.subtype='$entity_subtype'";\r
-               \r
-               if ($owner_guid != 0 && !is_array($owner_guid)) {\r
-                       $where[] = "a.owner_guid=$owner_guid";\r
-               } else {\r
-                       if (is_array($owner_guid))\r
-                               $where[] = "a.owner_guid in (" . implode(",",$owner_guid) . ")";\r
-               }\r
-               \r
-               if (($group_guid != 0) && ($entity_type=='object'))\r
-                       $where[] = "e.container_guid = $group_guid";\r
-                       \r
-               if ($name !== "")\r
-                       $where[] = "a.name_id='$name'";\r
-                       \r
-               if ($value != "")\r
-                       $where[] = "a.value_id='$value'";\r
-\r
-               if ($timelower)\r
-                       $where[] = "a.time_created >= {$timelower}";\r
-               if ($timeupper)\r
-                       $where[] = "a.time_created <= {$timeupper}";\r
-               \r
-               if ($count) {\r
-                       $query = "SELECT count(distinct e.guid) as total ";\r
-               } else {\r
-                       $query = "SELECT e.*, max(a.time_created) as maxtime ";                 \r
-               }\r
-               $query .= "from {$CONFIG->dbprefix}annotations a JOIN {$CONFIG->dbprefix}entities e on e.guid = a.entity_guid ";\r
-               if ($value != "")\r
-                       $query .= " JOIN {$CONFIG->dbprefix}metastrings v on a.value_id=v.id";\r
-               \r
-               if (($group_guid != 0) && ($entity_type=='object')) $query .= "JOIN {$CONFIG->dbprefix}objects_entity o on o.guid = e.guid";\r
-               $query .= " where";\r
-                   \r
-               foreach ($where as $w)\r
-                       $query .= " $w and ";\r
-               $query .= get_access_sql_suffix("a"); // Add access controls\r
-               $query .= ' and ' . get_access_sql_suffix("e"); // Add access controls\r
-               \r
-               if ($count) {\r
-                       $row = get_data_row($query);\r
-                       return $row->total;\r
-               } else {\r
-                       $query .= " group by a.entity_guid order by $order_by limit $offset,$limit"; // Add order and limit\r
-                       return get_data($query, "entity_row_to_elggstar");\r
-               }    \r
-       }\r
-       \r
-       /**\r
-        * Lists entities\r
-        *\r
-        * @see elgg_view_entity_list\r
-        * \r
-        * @param string $entity_type Type of entity.\r
-        * @param string $entity_subtype Subtype of entity.\r
-        * @param string $name Name of annotation.\r
-        * @param string $value Value of annotation.\r
-        * @param int $limit Maximum number of results to return.\r
-        * @param int $owner_guid Owner.\r
-        * @param int $group_guid Group container. Currently this is only supported if $entity_type == 'object'\r
-        * @param boolean $asc Whether to list in ascending or descending order (default: desc)\r
-        * @param boolean $fullview Whether to display the entities in full\r
-        * @param boolean $viewtypetoggle Determines whether or not the 'gallery' view can be displayed (default: no)\r
-        * @return string Formatted entity list\r
-        */\r
-       function list_entities_from_annotations($entity_type = "", $entity_subtype = "", $name = "", $value = "", $limit = 10, $owner_guid = 0, $group_guid = 0, $asc = false, $fullview = true, $viewtypetoggle = false) {\r
-               \r
-               if ($asc) {\r
-                       $asc = "asc";\r
-               } else {\r
-                       $asc = "desc";\r
-               }\r
-               $count = get_entities_from_annotations($entity_type, $entity_subtype, $name, $value, $owner_guid, $group_guid, null, null, $asc, true);\r
-               $offset = (int) get_input("offset",0);\r
-               $entities = get_entities_from_annotations($entity_type, $entity_subtype, $name, $value, $owner_guid, $group_guid, $limit, $offset, $asc);\r
-\r
-               return elgg_view_entity_list($entities, $count, $offset, $limit, $fullview, $viewtypetoggle);\r
-               \r
-       }\r
-       \r
-       /**\r
-        * Returns a human-readable list of annotations on a particular entity.\r
-        *\r
-        * @param int $entity_guid The entity GUID\r
-        * @param string $name The name of the kind of annotation\r
-        * @param int $limit The number of annotations to display at once\r
-        * @param true|false $asc Whether or not the annotations are displayed in ascending order. (Default: true)\r
-        * @return string HTML (etc) version of the annotation list\r
-        */\r
-       function list_annotations($entity_guid, $name = "", $limit = 25, $asc = true) {\r
-               \r
-               if ($asc) {\r
-                       $asc = "asc";\r
-               } else {\r
-                       $asc = "desc";\r
-               }\r
-               $count = count_annotations($entity_guid, "", "", $name);\r
-               $offset = (int) get_input("annoff",0);\r
-               $annotations = get_annotations($entity_guid, "", "", $name, "", "", $limit, $offset, $asc);\r
-               \r
-               return elgg_view_annotation_list($annotations, $count, $offset, $limit);\r
-               \r
-       }\r
-\r
-       /**\r
-        * Return the sum of a given integer annotation.\r
-        * \r
-        * @param $entity_guid int\r
-        * @param $entity_type string\r
-        * @param $entity_subtype string\r
-        * @param $name string\r
-        */\r
-       function get_annotations_sum($entity_guid, $entity_type = "", $entity_subtype = "", $name = "", $value = "", $value_type = "", $owner_guid = 0)\r
-       {\r
-               return __get_annotations_calculate_x("sum", $entity_guid, $entity_type, $entity_subtype, $name, $value, $value_type, $owner_guid);\r
-       }\r
-       \r
-       /**\r
-        * Return the max of a given integer annotation.\r
-        * \r
-        * @param $entity_guid int\r
-        * @param $entity_type string\r
-        * @param $entity_subtype string\r
-        * @param $name string\r
-        */\r
-       function get_annotations_max($entity_guid, $entity_type = "", $entity_subtype = "", $name = "", $value = "", $value_type = "", $owner_guid = 0)\r
-       {\r
-               return __get_annotations_calculate_x("max", $entity_guid, $entity_type, $entity_subtype, $name, $value, $value_type, $owner_guid);\r
-       }\r
-       \r
-       /**\r
-        * Return the minumum of a given integer annotation.\r
-        * \r
-        * @param $entity_guid int\r
-        * @param $entity_type string\r
-        * @param $entity_subtype string\r
-        * @param $name string\r
-        */\r
-       function get_annotations_min($entity_guid, $entity_type = "", $entity_subtype = "", $name = "", $value = "", $value_type = "", $owner_guid = 0)\r
-       {\r
-               return __get_annotations_calculate_x("min", $entity_guid, $entity_type, $entity_subtype, $name, $value, $value_type, $owner_guid);\r
-       }\r
-       \r
-       /**\r
-        * Return the average of a given integer annotation.\r
-        * \r
-        * @param $entity_guid int\r
-        * @param $entity_type string\r
-        * @param $entity_subtype string\r
-        * @param $name string\r
-        */\r
-       function get_annotations_avg($entity_guid, $entity_type = "", $entity_subtype = "", $name = "", $value = "", $value_type = "", $owner_guid = 0)\r
-       {\r
-               return __get_annotations_calculate_x("avg", $entity_guid, $entity_type, $entity_subtype, $name, $value, $value_type, $owner_guid);\r
-       }\r
-       \r
-       /**\r
-        * Count the number of annotations based on search parameters\r
-        *\r
-        * @param int $entity_guid\r
-        * @param string $entity_type\r
-        * @param string $entity_subtype\r
-        * @param string $name\r
-        */\r
-       function count_annotations($entity_guid = 0, $entity_type = "", $entity_subtype = "", $name = "", $value = "", $value_type = "", $owner_guid = 0, $timelower = 0, $timeupper = 0)\r
-       {\r
-               return __get_annotations_calculate_x("count", $entity_guid, $entity_type, $entity_subtype, $name, $value, $value_type, $owner_guid, $timelower, $timeupper);\r
-       }\r
-       \r
-       /**\r
-        * Perform a mathmatical calculation on integer annotations.\r
-        * \r
-        * @param $sum string\r
-        * @param $entity_id int\r
-        * @param $entity_type string\r
-        * @param $entity_subtype string\r
-        * @param $name string\r
-        */\r
-       function __get_annotations_calculate_x($sum = "avg", $entity_guid, $entity_type = "", $entity_subtype = "", $name = "", $value = "", $value_type = "", $owner_guid = 0, $timelower = 0, $timeupper = 0)\r
-       {\r
-               global $CONFIG;\r
-               \r
-               $sum = sanitise_string($sum);\r
-               $entity_guid = (int)$entity_guid;\r
-               $entity_type = sanitise_string($entity_type);\r
-               $timeupper = (int)$timeupper;\r
-               $timelower = (int)$timelower;\r
-               $entity_subtype = get_subtype_id($entity_type, $entity_subtype);\r
-               if ($name != '' AND !$name = get_metastring_id($name))\r
+               return elgg_view_entity_list($entities, $count, $offset, $limit, $fullview, $viewtypetoggle);
+               
+       }
+       
+       /**
+        * Returns a human-readable list of annotations on a particular entity.
+        *
+        * @param int $entity_guid The entity GUID
+        * @param string $name The name of the kind of annotation
+        * @param int $limit The number of annotations to display at once
+        * @param true|false $asc Whether or not the annotations are displayed in ascending order. (Default: true)
+        * @return string HTML (etc) version of the annotation list
+        */
+       function list_annotations($entity_guid, $name = "", $limit = 25, $asc = true) {
+               
+               if ($asc) {
+                       $asc = "asc";
+               } else {
+                       $asc = "desc";
+               }
+               $count = count_annotations($entity_guid, "", "", $name);
+               $offset = (int) get_input("annoff",0);
+               $annotations = get_annotations($entity_guid, "", "", $name, "", "", $limit, $offset, $asc);
+               
+               return elgg_view_annotation_list($annotations, $count, $offset, $limit);
+               
+       }
+
+       /**
+        * Return the sum of a given integer annotation.
+        * 
+        * @param $entity_guid int
+        * @param $entity_type string
+        * @param $entity_subtype string
+        * @param $name string
+        */
+       function get_annotations_sum($entity_guid, $entity_type = "", $entity_subtype = "", $name = "", $value = "", $value_type = "", $owner_guid = 0)
+       {
+               return __get_annotations_calculate_x("sum", $entity_guid, $entity_type, $entity_subtype, $name, $value, $value_type, $owner_guid);
+       }
+       
+       /**
+        * Return the max of a given integer annotation.
+        * 
+        * @param $entity_guid int
+        * @param $entity_type string
+        * @param $entity_subtype string
+        * @param $name string
+        */
+       function get_annotations_max($entity_guid, $entity_type = "", $entity_subtype = "", $name = "", $value = "", $value_type = "", $owner_guid = 0)
+       {
+               return __get_annotations_calculate_x("max", $entity_guid, $entity_type, $entity_subtype, $name, $value, $value_type, $owner_guid);
+       }
+       
+       /**
+        * Return the minumum of a given integer annotation.
+        * 
+        * @param $entity_guid int
+        * @param $entity_type string
+        * @param $entity_subtype string
+        * @param $name string
+        */
+       function get_annotations_min($entity_guid, $entity_type = "", $entity_subtype = "", $name = "", $value = "", $value_type = "", $owner_guid = 0)
+       {
+               return __get_annotations_calculate_x("min", $entity_guid, $entity_type, $entity_subtype, $name, $value, $value_type, $owner_guid);
+       }
+       
+       /**
+        * Return the average of a given integer annotation.
+        * 
+        * @param $entity_guid int
+        * @param $entity_type string
+        * @param $entity_subtype string
+        * @param $name string
+        */
+       function get_annotations_avg($entity_guid, $entity_type = "", $entity_subtype = "", $name = "", $value = "", $value_type = "", $owner_guid = 0)
+       {
+               return __get_annotations_calculate_x("avg", $entity_guid, $entity_type, $entity_subtype, $name, $value, $value_type, $owner_guid);
+       }
+       
+       /**
+        * Count the number of annotations based on search parameters
+        *
+        * @param int $entity_guid
+        * @param string $entity_type
+        * @param string $entity_subtype
+        * @param string $name
+        */
+       function count_annotations($entity_guid = 0, $entity_type = "", $entity_subtype = "", $name = "", $value = "", $value_type = "", $owner_guid = 0, $timelower = 0, $timeupper = 0)
+       {
+               return __get_annotations_calculate_x("count", $entity_guid, $entity_type, $entity_subtype, $name, $value, $value_type, $owner_guid, $timelower, $timeupper);
+       }
+       
+       /**
+        * Perform a mathmatical calculation on integer annotations.
+        * 
+        * @param $sum string
+        * @param $entity_id int
+        * @param $entity_type string
+        * @param $entity_subtype string
+        * @param $name string
+        */
+       function __get_annotations_calculate_x($sum = "avg", $entity_guid, $entity_type = "", $entity_subtype = "", $name = "", $value = "", $value_type = "", $owner_guid = 0, $timelower = 0, $timeupper = 0)
+       {
+               global $CONFIG;
+               
+               $sum = sanitise_string($sum);
+               $entity_guid = (int)$entity_guid;
+               $entity_type = sanitise_string($entity_type);
+               $timeupper = (int)$timeupper;
+               $timelower = (int)$timelower;
+               $entity_subtype = get_subtype_id($entity_type, $entity_subtype);
+               if ($name != '' AND !$name = get_metastring_id($name))
                        return 0;
-               if ($value != '' AND !$value = get_metastring_id($value))\r
+               if ($value != '' AND !$value = get_metastring_id($value))
                        return 0;
                $value_type = sanitise_string($value_type);
-               $owner_guid = (int)$owner_guid;\r
-               \r
-               // if (empty($name)) return 0;\r
-               \r
-               $where = array();\r
-               \r
-               if ($entity_guid)\r
-                       $where[] = "e.guid=$entity_guid";\r
-               if ($entity_type!="")\r
-                       $where[] = "e.type='$entity_type'";\r
-               if ($entity_subtype)\r
-                       $where[] = "e.subtype=$entity_subtype";\r
-               if ($name!="")\r
+               $owner_guid = (int)$owner_guid;
+               
+               // if (empty($name)) return 0;
+               
+               $where = array();
+               
+               if ($entity_guid)
+                       $where[] = "e.guid=$entity_guid";
+               if ($entity_type!="")
+                       $where[] = "e.type='$entity_type'";
+               if ($entity_subtype)
+                       $where[] = "e.subtype=$entity_subtype";
+               if ($name!="")
                        $where[] = "a.name_id='$name'";
                if ($value!="")
                        $where[] = "a.value_id='$value'";
                if ($value_type!="")
                        $where[] = "a.value_type='$value_type'";
                if ($owner_guid)
-                       $where[] = "a.owner_guid='$owner_guid'";\r
-               if ($timelower)\r
-                       $where[] = "a.time_created >= {$timelower}";\r
-               if ($timeupper)\r
-                       $where[] = "a.time_created <= {$timeupper}";\r
-                       \r
-               if ($sum != "count")\r
-                       $where[] = "a.value_type='integer'"; // Limit on integer types\r
-               \r
-               $query = "SELECT $sum(ms.string) as sum from {$CONFIG->dbprefix}annotations a JOIN {$CONFIG->dbprefix}entities e on a.entity_guid = e.guid JOIN {$CONFIG->dbprefix}metastrings ms on a.value_id=ms.id WHERE ";\r
-               foreach ($where as $w)\r
-                       $query .= " $w and ";\r
-               $query .= get_access_sql_suffix("a"); // now add access\r
-               $query .= ' and ' . get_access_sql_suffix("e"); // now add access\r
-               \r
-               $row = get_data_row($query);\r
-               if ($row)\r
-                       return $row->sum;\r
-                       \r
-               return false;\r
-       }\r
-\r
-       /**\r
-        * Get entities ordered by a mathematical calculation\r
-        *\r
-        * @param $sum string\r
-        * @param $entity_type string\r
-        * @param $entity_subtype string\r
-        * @param $name string\r
-        * @param $mdname string\r
-        * @param $mdvalue string\r
-        * @param $limit int\r
-        * @param string $orderdir Default: asc - the sort order\r
-        * @return unknown\r
-        */\r
-       function __get_entities_from_annotations_calculate_x($sum = "sum", $entity_type = "", $entity_subtype = "", $name = "", $mdname = '', $mdvalue = '', $owner_guid = 0, $limit = 10, $offset = 0, $orderdir = 'desc', $count = false)\r
-       {\r
-               global $CONFIG;\r
-               \r
-               $sum = sanitise_string($sum);\r
-               $entity_type = sanitise_string($entity_type);\r
-               $entity_subtype = get_subtype_id($entity_type, $entity_subtype);\r
-               $name = get_metastring_id($name);\r
-               $limit = (int) $limit;\r
-               $offset = (int) $offset;\r
-               $owner_guid = (int) $owner_guid;\r
-               if (!empty($mdname) && !empty($mdvalue)) {\r
-                       $meta_n = get_metastring_id($mdname);\r
-                       $meta_v = get_metastring_id($mdvalue);\r
-               }\r
-               \r
-               if (empty($name)) return 0;\r
-               \r
-               $where = array();\r
-               \r
-               if ($entity_type!="")\r
-                       $where[] = "e.type='$entity_type'";\r
-               if ($owner_guid > 0)\r
-                       $where[] = "e.container_guid = $owner_guid";\r
-               if ($entity_subtype)\r
-                       $where[] = "e.subtype=$entity_subtype";\r
-               if ($name!="")\r
-                       $where[] = "a.name_id='$name'";\r
-                       \r
-               if (!empty($mdname) && !empty($mdvalue)) {\r
-                       if ($mdname!="")\r
-                               $where[] = "m.name_id='$meta_n'";\r
-                       if ($mdvalue!="")\r
-                               $where[] = "m.value_id='$meta_v'";\r
-               }\r
-                       \r
-               if ($sum != "count")\r
-                       $where[] = "a.value_type='integer'"; // Limit on integer types\r
-\r
-               if (!$count) {\r
-                       $query = "SELECT distinct e.*, $sum(ms.string) as sum ";\r
-               } else {\r
-                       $query = "SELECT count(distinct e.guid) as num, $sum(ms.string) as sum ";\r
-               }\r
-               $query .= " from {$CONFIG->dbprefix}entities e JOIN {$CONFIG->dbprefix}annotations a on a.entity_guid = e.guid JOIN {$CONFIG->dbprefix}metastrings ms on a.value_id=ms.id ";\r
-               \r
-               if (!empty($mdname) && !empty($mdvalue)) {\r
-                       $query .= " JOIN {$CONFIG->dbprefix}metadata m on m.entity_guid = e.guid "; \r
-               }\r
-               \r
-               $query .= " WHERE ";\r
-               foreach ($where as $w)\r
-                       $query .= " $w and ";\r
-               $query .= get_access_sql_suffix("a"); // now add access\r
-               $query .= ' and ' . get_access_sql_suffix("e"); // now add access\r
-               if (!$count) $query .= ' group by e.guid';\r
-               \r
-               if (!$count) {\r
-                       $query .= ' order by sum ' . $orderdir;\r
-                       $query .= ' limit ' . $offset . ' , ' . $limit;\r
-                       return get_data($query, "entity_row_to_elggstar");\r
-               } else {\r
-                       if ($row = get_data_row($query)) {\r
-                               return $row->num;\r
-                       }\r
-               }\r
-               return false;\r
-       }\r
-\r
-       /**\r
-        * Returns entities ordered by the sum of an annotation\r
-        *\r
-        * @param unknown_type $entity_type\r
-        * @param unknown_type $entity_subtype\r
-        * @param unknown_type $name\r
-        * @param string $mdname\r
-        * @param string $mdvalue\r
-        * @param unknown_type $owner_guid\r
-        * @param int $limit\r
-        * @param int $offset\r
-        * @param true|false $count\r
-        * @return unknown\r
-        */\r
-       function get_entities_from_annotation_count($entity_type = "", $entity_subtype = "", $name = "", $mdname = '', $mdvalue = '', $owner_guid = 0, $limit = 10, $offset = 0, $orderdir = 'desc', $count = false) {\r
-               return __get_entities_from_annotations_calculate_x('sum',$entity_type,$entity_subtype,$name,$mdname, $mdvalue, $owner_guid,$limit, $offset, $orderdir, $count);\r
-       }\r
-\r
-       /**\r
-        * Lists entities by the totals of a particular kind of annotation\r
-        *\r
-        * @param string $entity_type Type of entity.\r
-        * @param string $entity_subtype Subtype of entity.\r
-        * @param string $name Name of annotation.\r
-        * @param int $limit Maximum number of results to return.\r
-        * @param int $owner_guid Owner.\r
-        * @param int $group_guid Group container. Currently this is only supported if $entity_type == 'object'\r
-        * @param boolean $asc Whether to list in ascending or descending order (default: desc)\r
-        * @param boolean $fullview Whether to display the entities in full\r
-        * @param boolean $viewtypetoggle Determines whether or not the 'gallery' view can be displayed (default: no)\r
-        * @return string Formatted entity list\r
-        */\r
-       function list_entities_from_annotation_count($entity_type = "", $entity_subtype = "", $name = "", $limit = 10, $owner_guid = 0, $group_guid = 0, $asc = false, $fullview = true, $viewtypetoggle = false, $pagination = true, $orderdir = 'desc') {\r
-               \r
-               if ($asc) {\r
-                       $asc = "asc";\r
-               } else {\r
-                       $asc = "desc";\r
-               }\r
-               \r
-               $offset = (int) get_input("offset",0);\r
-               $count = get_entities_from_annotation_count($entity_type, $entity_subtype, $name, '', '', $owner_guid, $limit, $offset, $orderdir, true); \r
-               $entities = get_entities_from_annotation_count($entity_type, $entity_subtype, $name, '', '', $owner_guid, $limit, $offset, $orderdir, false);\r
-\r
-               return elgg_view_entity_list($entities, $count, $offset, $limit, $fullview, $viewtypetoggle, $pagination);\r
-               \r
-       }\r
-       \r
-       /**\r
-        * Lists entities by the totals of a particular kind of annotation AND the value of a piece of metadata\r
-        *\r
-        * @param string $entity_type Type of entity.\r
-        * @param string $entity_subtype Subtype of entity.\r
-        * @param string $name Name of annotation.\r
-        * @param string $mdname Metadata name\r
-        * @param string $mdvalue Metadata value\r
-        * @param int $limit Maximum number of results to return.\r
-        * @param int $owner_guid Owner.\r
-        * @param int $group_guid Group container. Currently this is only supported if $entity_type == 'object'\r
-        * @param boolean $asc Whether to list in ascending or descending order (default: desc)\r
-        * @param boolean $fullview Whether to display the entities in full\r
-        * @param boolean $viewtypetoggle Determines whether or not the 'gallery' view can be displayed (default: no)\r
-        * @return string Formatted entity list\r
-        */\r
-       function list_entities_from_annotation_count_by_metadata($entity_type = "", $entity_subtype = "", $name = "", $mdname = '', $mdvalue = '', $limit = 10, $owner_guid = 0, $group_guid = 0, $asc = false, $fullview = true, $viewtypetoggle = false, $pagination = true, $orderdir = 'desc') {\r
-               \r
-               if ($asc) {\r
-                       $asc = "asc";\r
-               } else {\r
-                       $asc = "desc";\r
-               }\r
-               \r
-               $offset = (int) get_input("offset",0);\r
-               $count = get_entities_from_annotation_count($entity_type, $entity_subtype, $name, $mdname, $mdvalue, $owner_guid, $limit, $offset, $orderdir, true); \r
-               $entities = get_entities_from_annotation_count($entity_type, $entity_subtype, $name, $mdname, $mdvalue, $owner_guid, $limit, $offset, $orderdir, false);\r
-\r
-               return elgg_view_entity_list($entities, $count, $offset, $limit, $fullview, $viewtypetoggle, $pagination);\r
-               \r
-       }\r
-       \r
-       /**\r
-        * Delete a given annotation.\r
-        * \r
-        * @param $id int The id\r
-        */\r
-       function delete_annotation($id)\r
-       {\r
-               global $CONFIG;\r
-\r
-               $id = (int)$id;\r
-               \r
-               $access = get_access_sql_suffix();\r
+                       $where[] = "a.owner_guid='$owner_guid'";
+               if ($timelower)
+                       $where[] = "a.time_created >= {$timelower}";
+               if ($timeupper)
+                       $where[] = "a.time_created <= {$timeupper}";
+                       
+               if ($sum != "count")
+                       $where[] = "a.value_type='integer'"; // Limit on integer types
+               
+               $query = "SELECT $sum(ms.string) as sum from {$CONFIG->dbprefix}annotations a JOIN {$CONFIG->dbprefix}entities e on a.entity_guid = e.guid JOIN {$CONFIG->dbprefix}metastrings ms on a.value_id=ms.id WHERE ";
+               foreach ($where as $w)
+                       $query .= " $w and ";
+               $query .= get_access_sql_suffix("a"); // now add access
+               $query .= ' and ' . get_access_sql_suffix("e"); // now add access
+               
+               $row = get_data_row($query);
+               if ($row)
+                       return $row->sum;
+                       
+               return false;
+       }
+
+       /**
+        * Get entities ordered by a mathematical calculation
+        *
+        * @param $sum string
+        * @param $entity_type string
+        * @param $entity_subtype string
+        * @param $name string
+        * @param $mdname string
+        * @param $mdvalue string
+        * @param $limit int
+        * @param string $orderdir Default: asc - the sort order
+        * @return unknown
+        */
+       function __get_entities_from_annotations_calculate_x($sum = "sum", $entity_type = "", $entity_subtype = "", $name = "", $mdname = '', $mdvalue = '', $owner_guid = 0, $limit = 10, $offset = 0, $orderdir = 'desc', $count = false)
+       {
+               global $CONFIG;
+               
+               $sum = sanitise_string($sum);
+               $entity_type = sanitise_string($entity_type);
+               $entity_subtype = get_subtype_id($entity_type, $entity_subtype);
+               $name = get_metastring_id($name);
+               $limit = (int) $limit;
+               $offset = (int) $offset;
+               $owner_guid = (int) $owner_guid;
+               if (!empty($mdname) && !empty($mdvalue)) {
+                       $meta_n = get_metastring_id($mdname);
+                       $meta_v = get_metastring_id($mdvalue);
+               }
+               
+               if (empty($name)) return 0;
+               
+               $where = array();
+               
+               if ($entity_type!="")
+                       $where[] = "e.type='$entity_type'";
+               if ($owner_guid > 0)
+                       $where[] = "e.container_guid = $owner_guid";
+               if ($entity_subtype)
+                       $where[] = "e.subtype=$entity_subtype";
+               if ($name!="")
+                       $where[] = "a.name_id='$name'";
+                       
+               if (!empty($mdname) && !empty($mdvalue)) {
+                       if ($mdname!="")
+                               $where[] = "m.name_id='$meta_n'";
+                       if ($mdvalue!="")
+                               $where[] = "m.value_id='$meta_v'";
+               }
+                       
+               if ($sum != "count")
+                       $where[] = "a.value_type='integer'"; // Limit on integer types
+
+               if (!$count) {
+                       $query = "SELECT distinct e.*, $sum(ms.string) as sum ";
+               } else {
+                       $query = "SELECT count(distinct e.guid) as num, $sum(ms.string) as sum ";
+               }
+               $query .= " from {$CONFIG->dbprefix}entities e JOIN {$CONFIG->dbprefix}annotations a on a.entity_guid = e.guid JOIN {$CONFIG->dbprefix}metastrings ms on a.value_id=ms.id ";
+               
+               if (!empty($mdname) && !empty($mdvalue)) {
+                       $query .= " JOIN {$CONFIG->dbprefix}metadata m on m.entity_guid = e.guid "; 
+               }
+               
+               $query .= " WHERE ";
+               foreach ($where as $w)
+                       $query .= " $w and ";
+               $query .= get_access_sql_suffix("a"); // now add access
+               $query .= ' and ' . get_access_sql_suffix("e"); // now add access
+               if (!$count) $query .= ' group by e.guid';
+               
+               if (!$count) {
+                       $query .= ' order by sum ' . $orderdir;
+                       $query .= ' limit ' . $offset . ' , ' . $limit;
+                       return get_data($query, "entity_row_to_elggstar");
+               } else {
+                       if ($row = get_data_row($query)) {
+                               return $row->num;
+                       }
+               }
+               return false;
+       }
+
+       /**
+        * Returns entities ordered by the sum of an annotation
+        *
+        * @param unknown_type $entity_type
+        * @param unknown_type $entity_subtype
+        * @param unknown_type $name
+        * @param string $mdname
+        * @param string $mdvalue
+        * @param unknown_type $owner_guid
+        * @param int $limit
+        * @param int $offset
+        * @param true|false $count
+        * @return unknown
+        */
+       function get_entities_from_annotation_count($entity_type = "", $entity_subtype = "", $name = "", $mdname = '', $mdvalue = '', $owner_guid = 0, $limit = 10, $offset = 0, $orderdir = 'desc', $count = false) {
+               return __get_entities_from_annotations_calculate_x('sum',$entity_type,$entity_subtype,$name,$mdname, $mdvalue, $owner_guid,$limit, $offset, $orderdir, $count);
+       }
+
+       /**
+        * Lists entities by the totals of a particular kind of annotation
+        *
+        * @param string $entity_type Type of entity.
+        * @param string $entity_subtype Subtype of entity.
+        * @param string $name Name of annotation.
+        * @param int $limit Maximum number of results to return.
+        * @param int $owner_guid Owner.
+        * @param int $group_guid Group container. Currently this is only supported if $entity_type == 'object'
+        * @param boolean $asc Whether to list in ascending or descending order (default: desc)
+        * @param boolean $fullview Whether to display the entities in full
+        * @param boolean $viewtypetoggle Determines whether or not the 'gallery' view can be displayed (default: no)
+        * @return string Formatted entity list
+        */
+       function list_entities_from_annotation_count($entity_type = "", $entity_subtype = "", $name = "", $limit = 10, $owner_guid = 0, $group_guid = 0, $asc = false, $fullview = true, $viewtypetoggle = false, $pagination = true, $orderdir = 'desc') {
+               
+               if ($asc) {
+                       $asc = "asc";
+               } else {
+                       $asc = "desc";
+               }
+               
+               $offset = (int) get_input("offset",0);
+               $count = get_entities_from_annotation_count($entity_type, $entity_subtype, $name, '', '', $owner_guid, $limit, $offset, $orderdir, true); 
+               $entities = get_entities_from_annotation_count($entity_type, $entity_subtype, $name, '', '', $owner_guid, $limit, $offset, $orderdir, false);
+
+               return elgg_view_entity_list($entities, $count, $offset, $limit, $fullview, $viewtypetoggle, $pagination);
+               
+       }
+       
+       /**
+        * Lists entities by the totals of a particular kind of annotation AND the value of a piece of metadata
+        *
+        * @param string $entity_type Type of entity.
+        * @param string $entity_subtype Subtype of entity.
+        * @param string $name Name of annotation.
+        * @param string $mdname Metadata name
+        * @param string $mdvalue Metadata value
+        * @param int $limit Maximum number of results to return.
+        * @param int $owner_guid Owner.
+        * @param int $group_guid Group container. Currently this is only supported if $entity_type == 'object'
+        * @param boolean $asc Whether to list in ascending or descending order (default: desc)
+        * @param boolean $fullview Whether to display the entities in full
+        * @param boolean $viewtypetoggle Determines whether or not the 'gallery' view can be displayed (default: no)
+        * @return string Formatted entity list
+        */
+       function list_entities_from_annotation_count_by_metadata($entity_type = "", $entity_subtype = "", $name = "", $mdname = '', $mdvalue = '', $limit = 10, $owner_guid = 0, $group_guid = 0, $asc = false, $fullview = true, $viewtypetoggle = false, $pagination = true, $orderdir = 'desc') {
+               
+               if ($asc) {
+                       $asc = "asc";
+               } else {
+                       $asc = "desc";
+               }
+               
+               $offset = (int) get_input("offset",0);
+               $count = get_entities_from_annotation_count($entity_type, $entity_subtype, $name, $mdname, $mdvalue, $owner_guid, $limit, $offset, $orderdir, true); 
+               $entities = get_entities_from_annotation_count($entity_type, $entity_subtype, $name, $mdname, $mdvalue, $owner_guid, $limit, $offset, $orderdir, false);
+
+               return elgg_view_entity_list($entities, $count, $offset, $limit, $fullview, $viewtypetoggle, $pagination);
+               
+       }
+       
+       /**
+        * Delete a given annotation.
+        * 
+        * @param $id int The id
+        */
+       function delete_annotation($id)
+       {
+               global $CONFIG;
+
+               $id = (int)$id;
+               
+               $access = get_access_sql_suffix();
                $annotation = get_annotation($id);
                
-               if (trigger_elgg_event('delete', 'annotation', $annotation))\r
+               if (trigger_elgg_event('delete', 'annotation', $annotation))
                        return delete_data("DELETE from {$CONFIG->dbprefix}annotations  where id=$id and $access");
                        
-               return false;\r
-       }\r
-       \r
-       /**\r
-        * Clear all the annotations for a given entity, assuming you have access to that metadata.\r
-        * \r
-        * @param int $guid\r
-        */\r
-       function clear_annotations($guid, $name = "")\r
-       {\r
-               global $CONFIG;\r
-               \r
-               $guid = (int)$guid;\r
-               \r
-               if (!empty($name))\r
-                       $name = get_metastring_id($name);\r
-               \r
-               $entity_guid = (int) $guid;\r
-               if ($entity = get_entity($entity_guid)) {\r
-                       if ($entity->canEdit()) {\r
-               \r
-                               $where = array();\r
-                               \r
-                               if ($name != "")\r
-                                       $where[] = " name_id='$name'";\r
-                               \r
-                               $query = "DELETE from {$CONFIG->dbprefix}annotations where entity_guid=$guid "; \r
-                               foreach ($where as $w)\r
-                                       $query .= " and $w";\r
-                               \r
-                               return delete_data($query);\r
-               \r
-                       }\r
-               }\r
+               return false;
+       }
+       
+       /**
+        * Clear all the annotations for a given entity, assuming you have access to that metadata.
+        * 
+        * @param int $guid
+        */
+       function clear_annotations($guid, $name = "")
+       {
+               global $CONFIG;
+               
+               $guid = (int)$guid;
+               
+               if (!empty($name))
+                       $name = get_metastring_id($name);
+               
+               $entity_guid = (int) $guid;
+               if ($entity = get_entity($entity_guid)) {
+                       if ($entity->canEdit()) {
+               
+                               $where = array();
+                               
+                               if ($name != "")
+                                       $where[] = " name_id='$name'";
+                               
+                               $query = "DELETE from {$CONFIG->dbprefix}annotations where entity_guid=$guid "; 
+                               foreach ($where as $w)
+                                       $query .= " and $w";
+                               
+                               return delete_data($query);
+               
+                       }
+               }
        }
        
        /**
                }
                
                return $deleted;
-       }\r
-       \r
-       /**\r
-        * Handler called by trigger_plugin_hook on the "export" event.\r
-        */\r
-       function export_annotation_plugin_hook($hook, $entity_type, $returnvalue, $params)\r
-       {\r
-               // Sanity check values\r
-               if ((!is_array($params)) && (!isset($params['guid'])))\r
-                       throw new InvalidParameterException(elgg_echo('InvalidParameterException:GUIDNotForExport'));\r
-                       \r
-               if (!is_array($returnvalue))\r
-                       throw new InvalidParameterException(elgg_echo('InvalidParameterException:NonArrayReturnValue'));\r
-               \r
-               $guid = (int)$params['guid'];\r
-               $name = $params['name'];        \r
-               \r
-               $result = get_annotations($guid); \r
-                               \r
-               if ($result)\r
-               {\r
-                       foreach ($result as $r)\r
-                               $returnvalue[] = $r->export();\r
-               }\r
-               \r
-               return $returnvalue;\r
-       }\r
-       \r
-       /**\r
-        * Get the URL for this item of metadata, by default this links to the export handler in the current view.\r
-        *\r
-        * @param int $id\r
-        */\r
-       function get_annotation_url($id)\r
-       {\r
-               $id = (int)$id;\r
-               \r
-               if ($extender = get_annotation($id)) {\r
-                       return get_extender_url($extender);     \r
-               }\r
-               return false;\r
-       }\r
-       \r
-       \r
-       /**\r
-        * Register an annotation url handler.\r
-        *\r
-        * @param string $function_name The function.\r
-        * @param string $extender_name The name, default 'all'.\r
-        */\r
-       function register_annotation_url_handler($function_name, $extender_name = "all") {\r
-               return register_extender_url_handler($function_name, 'annotation', $extender_name);\r
-       }\r
-       \r
-       /** Register the hook */\r
-       register_plugin_hook("export", "all", "export_annotation_plugin_hook", 2);\r
-?>
\ No newline at end of file
+       }
+       
+       /**
+        * Handler called by trigger_plugin_hook on the "export" event.
+        */
+       function export_annotation_plugin_hook($hook, $entity_type, $returnvalue, $params)
+       {
+               // Sanity check values
+               if ((!is_array($params)) && (!isset($params['guid'])))
+                       throw new InvalidParameterException(elgg_echo('InvalidParameterException:GUIDNotForExport'));
+                       
+               if (!is_array($returnvalue))
+                       throw new InvalidParameterException(elgg_echo('InvalidParameterException:NonArrayReturnValue'));
+               
+               $guid = (int)$params['guid'];
+               $name = $params['name'];        
+               
+               $result = get_annotations($guid); 
+                               
+               if ($result)
+               {
+                       foreach ($result as $r)
+                               $returnvalue[] = $r->export();
+               }
+               
+               return $returnvalue;
+       }
+       
+       /**
+        * Get the URL for this item of metadata, by default this links to the export handler in the current view.
+        *
+        * @param int $id
+        */
+       function get_annotation_url($id)
+       {
+               $id = (int)$id;
+               
+               if ($extender = get_annotation($id)) {
+                       return get_extender_url($extender);     
+               }
+               return false;
+       }
+       
+       
+       /**
+        * Register an annotation url handler.
+        *
+        * @param string $function_name The function.
+        * @param string $extender_name The name, default 'all'.
+        */
+       function register_annotation_url_handler($function_name, $extender_name = "all") {
+               return register_extender_url_handler($function_name, 'annotation', $extender_name);
+       }
+       
+       /** Register the hook */
+       register_plugin_hook("export", "all", "export_annotation_plugin_hook", 2);
+?>