public static function getPenlties($type, $countryId)
 {
     if (Region::isContinent($countryId)) {
         $countryId = '_' . $countryId;
     }
     $ranksPenalties = self::model()->findAllByAttributes(array('type' => $type, 'countryId' => $countryId));
     $penalties = array();
     foreach ($ranksPenalties as $ranksPenalty) {
         $penalties[$ranksPenalty->eventId] = $ranksPenalty->penalty;
     }
     //some countries dont' have penalty for some events
     //because no person attend the events
     foreach (Events::getNormalEvents() as $eventId => $eventName) {
         if (!isset($penalties[$eventId])) {
             $penalties[$eventId] = 1;
         }
     }
     return $penalties;
 }
Exemple #2
0
 public static function build($statistic, $page = 1)
 {
     $gender = isset($statistic['gender']) ? $statistic['gender'] : 'all';
     $ranks = self::getRanks($statistic['type'], $statistic['region']);
     $eventIds = !empty($statistic['eventIds']) ? $statistic['eventIds'] : array_keys(Events::getNormalEvents());
     $eventIds = array_unique($eventIds);
     $columns = array(array('header' => 'Yii::t("statistics", "Person")', 'value' => 'Persons::getLinkByNameNId($data["personName"], $data["personId"])', 'type' => 'raw'));
     if (Region::isContinent($statistic['region']) || $statistic['region'] === 'World') {
         $columns[] = array('header' => 'Yii::t("common", "Region")', 'value' => 'Region::getIconName($data["countryName"], $data["iso2"])', 'type' => 'raw', 'htmlOptions' => array('class' => 'region'));
     }
     $columns[] = array('header' => 'Yii::t("statistics", "Sum")', 'value' => 'CHtml::tag("b", array(), $data["sum"])', 'type' => 'raw');
     //计算未参赛的项目应该排第几
     $penalty = RanksPenalty::getPenlties($statistic['type'], $statistic['region']);
     foreach ($eventIds as $key => $eventId) {
         if (!isset($ranks[$eventId])) {
             unset($eventIds[$key]);
             continue;
         }
     }
     $penalty = array_intersect_key($penalty, array_flip($eventIds));
     $allPenalties = array_sum($penalty);
     //计算每个人的排名
     $rankSum = array();
     foreach ($eventIds as $eventId) {
         foreach ($ranks[$eventId] as $personId => $row) {
             if (!isset($rankSum[$personId])) {
                 $rankSum[$personId] = $row;
                 $rankSum[$personId]['sum'] = $allPenalties;
             }
             $rankSum[$personId]['sum'] += $row['rank'] - $penalty[$eventId];
         }
         $columns[] = array('header' => "CHtml::tag('span', array(\r\n\t\t\t\t\t'class'=>'event-icon event-icon-white event-icon-{$eventId}'\r\n\t\t\t\t), ' ')", 'name' => $eventId, 'type' => 'raw');
     }
     uasort($rankSum, function ($rankA, $rankB) {
         return $rankA['sum'] - $rankB['sum'];
     });
     switch ($gender) {
         case 'female':
             $rankSum = array_filter($rankSum, function ($row) {
                 return $row['gender'] == 'f';
             });
             break;
         case 'male':
             $rankSum = array_filter($rankSum, function ($row) {
                 return $row['gender'] == 'm';
             });
             break;
     }
     $count = count($rankSum);
     if ($page > ceil($count / self::$limit)) {
         $page = ceil($count / self::$limit);
     }
     $rows = array();
     foreach (array_slice($rankSum, ($page - 1) * self::$limit, self::$limit) as $personId => $row) {
         foreach ($eventIds as $eventId) {
             $row[$eventId] = isset($ranks[$eventId][$personId]) ? $ranks[$eventId][$personId]['rank'] : $penalty[$eventId];
             if (isset($ranks[$eventId][$personId]) && $ranks[$eventId][$personId]['rank'] <= 10) {
                 $row[$eventId] = CHtml::tag('span', array('class' => 'top10'), $row[$eventId]);
             } elseif (!isset($ranks[$eventId][$personId])) {
                 $row[$eventId] = CHtml::tag('span', array('class' => 'penalty'), $row[$eventId]);
             }
         }
         $rows[] = $row;
     }
     $statistic['count'] = $count;
     $statistic['rank'] = isset($rows[0]) ? count(array_filter($rankSum, function ($row) use($rows) {
         return $row['sum'] < $rows[0]['sum'];
     })) : 0;
     $statistic['rankKey'] = 'sum';
     return self::makeStatisticsData($statistic, $columns, $rows);
 }