Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
9.43% |
5 / 53 |
|
28.57% |
2 / 7 |
CRAP | |
0.00% |
0 / 1 |
ChooseBallot | |
9.43% |
5 / 53 |
|
28.57% |
2 / 7 |
159.60 | |
0.00% |
0 / 1 |
getTallyTypes | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getCreateDescriptors | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
getQuestionForm | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
6 | |||
submitQuestion | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
packRecord | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
unpackRecord | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
convertScores | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\SecurePoll\Ballots; |
4 | |
5 | use MediaWiki\Extension\SecurePoll\Entities\Question; |
6 | use OOUI\FieldLayout; |
7 | use OOUI\FieldsetLayout; |
8 | use OOUI\HtmlSnippet; |
9 | use OOUI\RadioInputWidget; |
10 | |
11 | /** |
12 | * A ballot class which asks the user to choose one answer only from the |
13 | * given options, for each question. |
14 | */ |
15 | class ChooseBallot extends Ballot { |
16 | /** |
17 | * Get a list of names of tallying methods, which may be used to produce a |
18 | * result from this ballot type. |
19 | * @return array |
20 | */ |
21 | public static function getTallyTypes() { |
22 | return [ 'plurality' ]; |
23 | } |
24 | |
25 | public static function getCreateDescriptors() { |
26 | $ret = parent::getCreateDescriptors(); |
27 | $ret['option'] += [ |
28 | 'name' => [ |
29 | 'label-message' => 'securepoll-create-label-option-name', |
30 | 'type' => 'text', |
31 | 'SecurePoll_type' => 'message', |
32 | ], |
33 | ]; |
34 | |
35 | return $ret; |
36 | } |
37 | |
38 | /** |
39 | * Get the HTML form segment for a single question |
40 | * @param Question $question |
41 | * @param array $options Array of options, in the order they should be displayed |
42 | * @return FieldsetLayout |
43 | */ |
44 | public function getQuestionForm( $question, $options ) { |
45 | $name = 'securepoll_q' . $question->getId(); |
46 | $fieldset = new FieldsetLayout(); |
47 | foreach ( $options as $option ) { |
48 | $optionHTML = $option->parseMessageInline( 'text' ); |
49 | $optionId = $option->getId(); |
50 | |
51 | $fieldset->appendContent( [ |
52 | new FieldLayout( new RadioInputWidget( [ |
53 | 'name' => $name, |
54 | 'value' => $optionId, |
55 | 'required' => true, |
56 | ] ), [ |
57 | 'classes' => [ 'securepoll-option-choose' ], |
58 | 'label' => new HtmlSnippet( $optionHTML ), |
59 | 'align' => 'inline' |
60 | ] ) |
61 | ] ); |
62 | } |
63 | |
64 | return $fieldset; |
65 | } |
66 | |
67 | /** |
68 | * @param Question $question |
69 | * @param BallotStatus $status |
70 | * @return string |
71 | */ |
72 | public function submitQuestion( $question, $status ) { |
73 | $result = $this->getRequest()->getInt( 'securepoll_q' . $question->getId() ); |
74 | if ( !$result ) { |
75 | $status->fatal( 'securepoll-unanswered-questions' ); |
76 | } else { |
77 | return $this->packRecord( $question->getId(), $result ); |
78 | } |
79 | } |
80 | |
81 | public function packRecord( $qid, $oid ) { |
82 | return sprintf( 'Q%08XA%08X', $qid, $oid ); |
83 | } |
84 | |
85 | public function unpackRecord( $record ) { |
86 | $result = []; |
87 | $record = trim( $record ); |
88 | for ( $offset = 0, $len = strlen( $record ); $offset < $len; $offset += 18 ) { |
89 | if ( !preg_match( '/Q([0-9A-F]{8})A([0-9A-F]{8})/A', $record, $m, 0, $offset ) ) { |
90 | wfDebug( __METHOD__ . ": regex doesn't match\n" ); |
91 | |
92 | return false; |
93 | } |
94 | $qid = intval( base_convert( $m[1], 16, 10 ) ); |
95 | $oid = intval( base_convert( $m[2], 16, 10 ) ); |
96 | $result[$qid] = [ $oid => 1 ]; |
97 | } |
98 | |
99 | return $result; |
100 | } |
101 | |
102 | public function convertScores( $scores, $params = [] ) { |
103 | $s = ''; |
104 | foreach ( $this->election->getQuestions() as $question ) { |
105 | $qid = $question->getId(); |
106 | if ( !isset( $scores[$qid] ) ) { |
107 | return false; |
108 | } |
109 | if ( $s !== '' ) { |
110 | $s .= '; '; |
111 | } |
112 | $oid = key( $scores ); |
113 | // FIXME: getOption doesn't exist |
114 | // @phan-suppress-next-line PhanUndeclaredMethod |
115 | $option = $this->election->getOption( $oid ); |
116 | $s .= $option->getMessage( 'name' ); |
117 | } |
118 | |
119 | return $s; |
120 | } |
121 | } |