/** * Increment the views counter for any elgg entity */ function increment_views_counter($entity_guid) { $entity = get_entity($entity_guid); if ($entity) { $user = elgg_get_logged_in_user_entity(); // If there is no loggedin user then lets updated the common views counter for all not loggedin users that has owner equal zero $user_guid = $user ? $user->guid : 0; $options = array('types' => array($entity->type), 'guids' => array($entity_guid), 'annotation_names' => array('views_counter'), 'annotation_owner_guids' => array($user_guid)); if ($entity->getSubtype()) { $options['subtypes'] = array($entity->getSubtype()); } $views_counter = elgg_get_annotations($options); // Update the last view time for this entity to the current time update_last_view_time($entity_guid, $user_guid); if ($views_counter) { return update_annotation($views_counter[0]->id, 'views_counter', $views_counter[0]->value + 1, 'integer', $user_guid, ACCESS_PUBLIC); } else { return create_annotation($entity->guid, 'views_counter', 1, 'integer', $user_guid, ACCESS_PUBLIC); } } }
/** * Hook that allow other plugins to update the last view time for an entity * * @param $hook * @param $type * @param $returnvalue * @param $params */ function update_last_view_time_hook($hook, $type, $returnvalue, $params) { if ($params['entity']) { $user_guid = $params['user'] ? $params['user']->guid : 0; return update_last_view_time($params['entity']->guid, $user_guid); } }