Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
19.15% |
9 / 47 |
|
37.50% |
3 / 8 |
CRAP | |
0.00% |
0 / 1 |
PluralityTallier | |
19.15% |
9 / 47 |
|
37.50% |
3 / 8 |
231.41 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
addVote | |
66.67% |
4 / 6 |
|
0.00% |
0 / 1 |
3.33 | |||
finishTally | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
loadJSONResult | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getJSONResult | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getHtmlResult | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
getTextResult | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
42 | |||
getRanks | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\SecurePoll\Talliers; |
4 | |
5 | use MediaWiki\Extension\SecurePoll\Context; |
6 | use MediaWiki\Extension\SecurePoll\Entities\Question; |
7 | |
8 | /** |
9 | * Tallier that supports choose-one, approval and range voting |
10 | */ |
11 | class 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 | public function getJSONResult() { |
61 | return $this->tally; |
62 | } |
63 | |
64 | /** |
65 | * @inheritDoc |
66 | * |
67 | */ |
68 | public function getHtmlResult() { |
69 | // Show the results |
70 | $s = "<table class=\"securepoll-results\">\n"; |
71 | |
72 | foreach ( $this->tally as $oid => $rank ) { |
73 | $option = $this->optionsById[$oid]; |
74 | $s .= '<tr><td>' . $option->parseMessageInline( |
75 | 'text' |
76 | ) . "</td>\n" . '<td>' . $this->tally[$oid] . "</td>\n" . "</tr>\n"; |
77 | } |
78 | $s .= "</table>\n"; |
79 | |
80 | return $s; |
81 | } |
82 | |
83 | public function getTextResult() { |
84 | // Calculate column width |
85 | $width = 10; |
86 | foreach ( $this->tally as $oid => $rank ) { |
87 | $option = $this->optionsById[$oid]; |
88 | $width = max( $width, strlen( $option->getMessage( 'text' ) ) ); |
89 | } |
90 | if ( $width > 57 ) { |
91 | $width = 57; |
92 | } |
93 | |
94 | // Show the results |
95 | $qtext = $this->question->getMessage( 'text' ); |
96 | $s = ''; |
97 | if ( $qtext !== '' ) { |
98 | $s .= wordwrap( $qtext ) . "\n"; |
99 | } |
100 | foreach ( $this->tally as $oid => $rank ) { |
101 | $option = $this->optionsById[$oid]; |
102 | $otext = $option->getMessage( 'text' ); |
103 | if ( strlen( $otext ) > $width ) { |
104 | $otext = substr( $otext, 0, $width - 3 ) . '...'; |
105 | } else { |
106 | $otext = str_pad( $otext, $width ); |
107 | } |
108 | $s .= $otext . ' | ' . $this->tally[$option->getId()] . "\n"; |
109 | } |
110 | |
111 | return $s; |
112 | } |
113 | |
114 | public function getRanks() { |
115 | $ranks = []; |
116 | $currentRank = 1; |
117 | $oids = array_keys( $this->tally ); |
118 | $scores = array_values( $this->tally ); |
119 | foreach ( $oids as $i => $oid ) { |
120 | if ( $i > 0 && $scores[$i - 1] !== $scores[$i] ) { |
121 | $currentRank = $i + 1; |
122 | } |
123 | $ranks[$oid] = $currentRank; |
124 | } |
125 | |
126 | return $ranks; |
127 | } |
128 | } |