Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
19.15% covered (danger)
19.15%
9 / 47
37.50% covered (danger)
37.50%
3 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
PluralityTallier
19.15% covered (danger)
19.15%
9 / 47
37.50% covered (danger)
37.50%
3 / 8
231.41
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 addVote
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
3.33
 finishTally
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 loadJSONResult
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getJSONResult
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getHtmlResult
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 getTextResult
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
42
 getRanks
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace MediaWiki\Extension\SecurePoll\Talliers;
4
5use MediaWiki\Extension\SecurePoll\Context;
6use MediaWiki\Extension\SecurePoll\Entities\Question;
7
8/**
9 * Tallier that supports choose-one, approval and range voting
10 */
11class PluralityTallier extends Tallier {
12    /** @var int[] */
13    public $tally = [];
14
15    /**
16     * @param Context $context
17     * @param ElectionTallier $electionTallier
18     * @param Question $question
19     */
20    public function __construct( $context, $electionTallier, $question ) {
21        parent::__construct( $context, $electionTallier, $question );
22        foreach ( $question->getOptions() as $option ) {
23            $this->tally[$option->getId()] = 0;
24        }
25    }
26
27    /**
28     * @inheritDoc
29     */
30    public function addVote( $scores ) {
31        foreach ( $scores as $oid => $score ) {
32            if ( !isset( $this->tally[$oid] ) ) {
33                wfDebug( __METHOD__ . ": unknown OID $oid\n" );
34
35                return false;
36            }
37            $this->tally[$oid] += $score;
38        }
39
40        return true;
41    }
42
43    /**
44     * @inheritDoc
45     *
46     */
47    public function finishTally() {
48        // Sort the scores
49        arsort( $this->tally );
50    }
51
52    /**
53     * @inheritDoc
54     *
55     */
56    public function loadJSONResult( $data ) {
57        $this->tally = $data;
58    }
59
60    /** @inheritDoc */
61    public function getJSONResult() {
62        return $this->tally;
63    }
64
65    /**
66     * @inheritDoc
67     *
68     */
69    public function getHtmlResult() {
70        // Show the results
71        $s = "<table class=\"securepoll-results\">\n";
72
73        foreach ( $this->tally as $oid => $rank ) {
74            $option = $this->optionsById[$oid];
75            $s .= '<tr><td>' . $option->parseMessageInline(
76                    'text'
77                ) . "</td>\n" . '<td>' . $this->tally[$oid] . "</td>\n" . "</tr>\n";
78        }
79        $s .= "</table>\n";
80
81        return $s;
82    }
83
84    /** @inheritDoc */
85    public function getTextResult() {
86        // Calculate column width
87        $width = 10;
88        foreach ( $this->tally as $oid => $rank ) {
89            $option = $this->optionsById[$oid];
90            $width = max( $width, strlen( $option->getMessage( 'text' ) ) );
91        }
92        if ( $width > 57 ) {
93            $width = 57;
94        }
95
96        // Show the results
97        $qtext = $this->question->getMessage( 'text' );
98        $s = '';
99        if ( $qtext !== '' ) {
100            $s .= wordwrap( $qtext ) . "\n";
101        }
102        foreach ( $this->tally as $oid => $rank ) {
103            $option = $this->optionsById[$oid];
104            $otext = $option->getMessage( 'text' );
105            if ( strlen( $otext ) > $width ) {
106                $otext = substr( $otext, 0, $width - 3 ) . '...';
107            } else {
108                $otext = str_pad( $otext, $width );
109            }
110            $s .= $otext . ' | ' . $this->tally[$option->getId()] . "\n";
111        }
112
113        return $s;
114    }
115
116    /**
117     * @return array
118     */
119    public function getRanks() {
120        $ranks = [];
121        $currentRank = 1;
122        $oids = array_keys( $this->tally );
123        $scores = array_values( $this->tally );
124        foreach ( $oids as $i => $oid ) {
125            if ( $i > 0 && $scores[$i - 1] !== $scores[$i] ) {
126                $currentRank = $i + 1;
127            }
128            $ranks[$oid] = $currentRank;
129        }
130
131        return $ranks;
132    }
133}