00001 <?php
00002
00003
00004
00005 class VotesAggregates {
00006
00007
00008
00009
00010
00011
00012 public static function get_for_instance(IDataObject $inst) {
00013 return DB::get_item('votesaggregates', 'instance', InstanceReferenceSerializier::instance_to_string($inst));
00014 }
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 public static function aggregate_for_instance(IDataObject $instance) {
00028 $dao = Votes::create_instance_adapter($instance);
00029
00030
00031
00032
00033 $query = $dao->create_select_query();
00034 $query->set_fields(array(
00035 'SUM(value * weight)' => 'value',
00036 'SUM(weight)' => 'count'
00037 ));
00038 $result = DB::query($query->get_sql());
00039
00040 $data = $result->fetch();
00041 $value = $data['value'];
00042 $count = $data['count'];
00043 $avg = ($count > 0) ? $value / $count : 0;
00044
00045 $ret = array(
00046 'instance' => $instance,
00047 'average' => $avg,
00048 'numtotal' => $count
00049 );
00050 return $ret;
00051 }
00052
00053
00054
00055
00056
00057
00058
00059 public static function create_type_adapter($instance_type) {
00060 $dao = new DAOVotesaggregates();
00061 $dao->add_where('instance', DBWhere::OP_LIKE, "$instance_type%");
00062 return $dao;
00063 }
00064
00065
00066
00067
00068
00069
00070
00071
00072 public static function get_top_voted_of_type($type, $limit) {
00073 $dao = self::create_type_adapter($type);
00074
00075 $dao->sort('average', DAOVotesaggregates::DESC);
00076 $dao->limit(0, $limit);
00077 return $dao->find_array();
00078 }
00079 }