Elgg\Database\Annotations::getEntitiesFromCalculation PHP Method

getEntitiesFromCalculation() public method

Get entities ordered by a mathematical calculation on annotation values
See also: elgg_get_annotations()
See also: elgg_get_entities_from_annotations()
public getEntitiesFromCalculation ( array $options ) : ElggEntity[] | integer
$options array An options array: 'calculation' => The calculation to use. Must be a valid MySQL function. Defaults to sum. Result selected as 'annotation_calculation'. Don't confuse this "calculation" option with the "annotation_calculation" option to elgg_get_annotations(). This "calculation" option is applied to each entity's set of annotations and is selected as annotation_calculation for that row. See the docs for elgg_get_annotations() for proper use of the "annotation_calculation" option. 'order_by' => The order for the sorting. Defaults to 'annotation_calculation desc'. 'annotation_names' => The names of annotations on the entity. 'annotation_values' => The values of annotations on the entity. 'metadata_names' => The name of metadata on the entity. 'metadata_values' => The value of metadata on the entitiy. 'callback' => Callback function to pass each row through. @tip This function is different from other ege* functions, as it uses a metastring-based getter function { @link elgg_get_annotations() }, therefore the callback function should be a derivative of { @link entity_row_to_elggstar() } and not of { @link row_to_annotation() }
return ElggEntity[] | integer An array or a count of entities
    function getEntitiesFromCalculation($options)
    {
        if (isset($options['count']) && $options['count']) {
            return elgg_get_entities_from_annotations($options);
        }
        $db_prefix = $this->db->prefix;
        $defaults = array('calculation' => 'sum', 'order_by' => 'annotation_calculation desc');
        $options = array_merge($defaults, $options);
        $function = sanitize_string(elgg_extract('calculation', $options, 'sum', false));
        // you must cast this as an int or it sorts wrong.
        $options['selects'][] = 'e.*';
        $options['selects'][] = "{$function}(CAST(n_table.value AS signed)) AS annotation_calculation";
        // don't need access control because it's taken care of by elgg_get_annotations.
        $options['group_by'] = 'n_table.entity_guid';
        // do not default to a callback function used in elgg_get_annotation()
        if (!isset($options['callback'])) {
            $options['callback'] = 'entity_row_to_elggstar';
        }
        return elgg_get_annotations($options);
    }