if (elgg_in_context('profile') || elgg_in_context('groups')) { $page_owner = elgg_get_page_owner_entity(); } if (elgg_instanceof($page_owner, 'user')) { $owner_guid = $page_owner->guid; $container_guid = ELGG_ENTITIES_ANY_VALUE; } else { if (elgg_instanceof($page_owner, 'group')) { $owner_guid = ELGG_ENTITIES_ANY_VALUE; $container_guid = $page_owner->guid; } else { $owner_guid = ELGG_ENTITIES_ANY_VALUE; $container_guid = ELGG_ENTITIES_ANY_VALUE; } } $annotation_names = elgg_stars_get_rating_annotation_names(); $entity_rating = array('types' => $widget->content_type, 'owner_guid' => $owner_guid, 'container_guid' => $container_guid, 'annotation_names' => $annotation_names, 'calculation' => 'avg', 'order_by' => 'annotation_calculation desc', 'limit' => $widget->num_display); $entities = elgg_get_entities_from_annotation_calculation($entity_rating); elgg_push_context('starrating'); foreach ($entities as $entity) { $title = elgg_view('output/url', array('text' => isset($entity->title) ? $entity->title : $entity->name, 'href' => $entity->getURL(), 'is_trusted' => true)); if (!empty($entity->description)) { $desc = elgg_view('output/longtext', array('value' => elgg_get_excerpt($entity->description))); } else { $desc = ''; } $icon = elgg_view_entity_icon($entity, 'small'); $entity_rating = elgg_stars_get_entity_rating_values($entity, $annotation_names); $rating = elgg_view('output/stars', array('value' => $entity_rating['value'])); $rating .= '<div class="elgg-stars-rating-caption">' . elgg_echo('stars:stats', array($entity_rating['value'], $entity_rating['max'], $entity_rating['count'])) . '</div>'; $list .= '<li>' . elgg_view_image_block($icon, $title . $desc, array('image_alt' => $rating)) . '</li>';
* Display a list of ratings and a ratings form */ if (!(bool) elgg_get_plugin_setting('extend_comments', 'elgg_stars')) { return; } $entity = elgg_extract('entity', $vars); $show_add_form = elgg_extract('show_add_form', $vars, true); if (!elgg_instanceof($entity)) { return; } $type_subtype_pairs = elgg_stars_get_rateable_type_subtype_pairs(); $type = $entity->getType(); $subtype = $entity->getSubtype(); if (!array_key_exists($type, $type_subtype_pairs)) { return; } if ($subtype && array_search($subtype, $type_subtype_pairs[$type]) === false) { return; } if ($show_add_form) { $body .= elgg_view_form('stars/rate', array(), array('entity' => $entity)); } $title = elgg_echo('stars:ratings'); $annotation_names = elgg_stars_get_rating_annotation_names($entity); $body .= elgg_list_annotations(array('guid' => $entity->guid, 'annotation_names' => $annotation_names)); $entity_ratings = elgg_stars_get_entity_rating_values($entity, $annotation_names); $label = '<label>' . elgg_echo('stars:stats:totals') . '</label>'; $total = elgg_view('output/stars', array('value' => $entity_ratings['value'])); $stats = elgg_echo('stars:stats', array($entity_ratings['value'], $entity_ratings['max'], $entity_ratings['count'])); $footer = elgg_view_image_block($label, $total, array('image_alt' => $stats)); echo elgg_view_module('aside', $title, $body, array('footer' => $footer));
/** * Check if the user has casted a vote on a given entity with a given annotation name * * @param ElggEntity $entity * @param ElggUser $user * @param string $annotation_name * @return false|array */ function elgg_stars_has_user_voted($entity, $user = null, $annotation_names = null) { if (!$user) { $user = elgg_get_logged_in_user_entity(); } if (!elgg_instanceof($user)) { return false; } if (!$annotation_names) { $annotation_names = elgg_stars_get_rating_annotation_names($entity); } $votes = elgg_get_annotations(array('annotation_names' => $annotation_names, 'guids' => $entity->guid, 'annotation_owner_guids' => $user->guid, 'count' => true)); return (int) $votes > 0; }