<?php

$ranks = SuggestionsModel::getRanksAsArray($_SESSION['lang']);
$states = SuggestionsModel::getStatesAsArray();
$optionStates = SuggestionOption::getStatesAsArray($_SESSION['lang']);
?>
<table id="results">
    <tr>
        <td><h2><?php 
echo htmlspecialchars($this->suggestion->summary, ENT_COMPAT, 'utf-8');
?>
</h2></td>
        <td colspan="2" class="rank"><strong><?php 
if ($this->suggestion->voteCount) {
    echo $words->get('SuggestionsVotesGiven', $this->suggestion->voteCount);
}
?>
</strong></td>
        <td class="rank"><strong><?php 
echo $words->get($states[$this->suggestion->state]);
?>
</strong></td>
    </tr>
    <tr><td colspan="4"><?php 
echo $this->purifier->purify($this->suggestion->description);
?>
</td></tr>
    <tr><td colspan="4"><hr class="suggestion" /></td></tr>
    <?php 
if ($this->suggestion->optionsVisibleCount == 0) {
    ?>
 private function calculateResults()
 {
     $entityFactory = new RoxEntityFactory();
     $ranks = $this->getRanks();
     $count = $this->voteCount;
     // Calculate medians for all options
     $medians = $this->calculateMedians($ranks, $count);
     asort($medians);
     // Now break ties for the different medians
     // Hint: Majority judgement would only break the tie for the best rank
     // as we allow options that overlap we need to break tie for all ranks
     // to get a meaningful order
     $ties = array();
     $goodOrExcellent = array();
     foreach ($medians as $optionId => $median) {
         if (!isset($ties[$median])) {
             $ties[$median] = array();
         }
         $ties[$median][$optionId] = $ranks[$optionId];
     }
     $orderedOptionIds = array();
     foreach ($ties as $median => $tie) {
         $optionIds = $this->breakTie($tie, $count);
         $orderedOptionIds = array_merge($orderedOptionIds, $optionIds);
     }
     $orderedOptionIds = array_reverse($orderedOptionIds, true);
     $rankNames = SuggestionsModel::getRanksAsArray();
     $postText = '<p>Voting for \'' . $this->summary . '\' is no longer possible.</p>';
     $postText .= '<p>Number of votes given: ' . $this->voteCount . '</p>';
     $postText .= '<p>Detailed results:</p>';
     $postText .= '<table id="votingresults"><tr><th class="description">Solution</th><th class="results" colspan="2">Results</th><th class="rank">Median</th></tr>';
     foreach ($orderedOptionIds as $order => $optionId) {
         $option = $entityFactory->create('SuggestionOption')->findById($optionId);
         $option->orderHint = $order;
         $option->rank = $medians[$optionId];
         $option->update();
         if ($medians[$optionId] > 2) {
             $goodOrExcellent[$optionId] = $option;
         }
         foreach ($rankNames as $rank => $rankName) {
             if ($rank == 4) {
                 $postText .= '<tr><td class="description" rowspan="4">' . $option->summary . '</td>' . '<td class="resultsleft">' . $rankName . ':</td><td class="resultsright">' . $ranks[$optionId][$rank] . '</td><td class="rank" rowspan="4">' . $rankNames[$medians[$optionId]] . '</td></tr>';
             } else {
                 $postText .= '<tr><td class="resultsleft">' . $rankName . ':</td><td class="resultsright">' . $ranks[$optionId][$rank] . '</td></tr>';
             }
         }
     }
     $postText .= '</table>';
     // Check if any option got a higher rank than 'acceptable (2)'
     if (empty($goodOrExcellent)) {
         $postText .= '<p>No solution got a better median than \'' . $rankNames[2] . '\' therefore the suggestion is rejected.</p>';
         $this->state = SuggestionsModel::SUGGESTIONS_REJECTED;
     } else {
         // Remove options that are exclusive with the winning option(s)
         $remove = array();
         foreach ($goodOrExcellent as $optionId => $option) {
             if ($option->mutuallyExclusive === 'All') {
                 foreach ($goodOrExcellent as $checkId => $checkOption) {
                     if ($optionId != $checkId && isset($goodOrExcellent[$checkId]) && $option->orderHint > $checkOption->orderHint) {
                         $remove[] = $checkId;
                     }
                 }
             } else {
                 foreach ($option->mutuallyExclusive as $exclude) {
                     if (isset($goodOrExcellent[$exclude]) && $option->orderHint > $goodOrExcellent[$exclude]->orderHint) {
                         $remove[] = $exclude;
                     }
                 }
             }
         }
         foreach ($remove as $id) {
             if (isset($goodOrExcellent[$id])) {
                 unset($goodOrExcellent[$id]);
             }
         }
         // Mention remaining options
         if (count($goodOrExcellent) == 1) {
             $option = reset($goodOrExcellent);
             $option->state = SuggestionOption::RANKING;
             $option->update();
             $postText .= '<p>The solution \'' . $option->summary . '\' won the vote and can be ranked now.</p>';
         } else {
             foreach ($goodOrExcellent as $option) {
                 $postText .= '<p>The solution \'' . $option->summary . '\' reached a median above \'' . $rankNames[2] . '\' and can be ranked now.</p>';
                 $option->state = SuggestionOption::RANKING;
                 $option->update();
             }
             $postText .= '<p>All solutions not mentioned above (if any) were conflicting with a solution with a higher median.</p>';
         }
     }
     $suggestionsTeam = $entityFactory->create('Member')->findByUsername('SuggestionsTeam');
     if ($suggestionsTeam) {
         $suggestions = new SuggestionsModel();
         $postId = $suggestions->addPost($suggestionsTeam->id, $postText, $this->threadId);
         $suggestions->setForumNotification($postId, 'reply');
     }
     return true;
 }