Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
19.70% covered (danger)
19.70%
13 / 66
33.33% covered (danger)
33.33%
2 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApprovalBallot
19.70% covered (danger)
19.70%
13 / 66
33.33% covered (danger)
33.33%
2 / 6
166.66
0.00% covered (danger)
0.00%
0 / 1
 getTallyTypes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getQuestionForm
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
6
 submitQuestion
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 packRecord
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 unpackRecord
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
20
 convertScores
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace MediaWiki\Extension\SecurePoll\Ballots;
4
5use MediaWiki\Extension\SecurePoll\Entities\Question;
6
7/**
8 * Checkbox approval voting.
9 */
10class ApprovalBallot extends Ballot {
11    public static function getTallyTypes() {
12        return [ 'plurality' ];
13    }
14
15    /**
16     * @param Question $question
17     * @param array $options
18     * @return \OOUI\FieldsetLayout
19     */
20    public function getQuestionForm( $question, $options ) {
21        $name = 'securepoll_q' . $question->getId();
22
23        $fieldset = new \OOUI\FieldsetLayout( [
24            'classes' => [ 'securepoll-option-approval ' ]
25        ] );
26
27        foreach ( $options as $option ) {
28            $optionHTML = $option->parseMessageInline( 'text' );
29            $optionId = $option->getId();
30            $inputId = "{$name}_opt{$optionId}";
31
32            $fieldset->addItems( [
33                new \OOUI\FieldLayout( new \OOUI\CheckboxInputWidget( [
34                    'name' => $inputId,
35                    'selected' => $this->getRequest()->getBool( $inputId ),
36                    'value' => 1
37                ] ), [
38                    'label' => new \OOUI\HtmlSnippet( $optionHTML ),
39                    'align' => 'inline'
40                ] )
41            ] );
42        }
43
44        return $fieldset;
45    }
46
47    /**
48     * @param Question $question
49     * @param BallotStatus $status
50     * @return string
51     */
52    public function submitQuestion( $question, $status ) {
53        $options = $question->getOptions();
54        $record = '';
55        foreach ( $options as $option ) {
56            $id = 'securepoll_q' . $question->getId() . '_opt' . $option->getId();
57            $checked = $this->getRequest()->getBool( $id );
58            $record .= $this->packRecord( $question, $option, $checked );
59        }
60
61        return $record;
62    }
63
64    public function packRecord( $question, $option, $checked ) {
65        return sprintf(
66            'Q%08X-A%08X-%s--',
67            $question->getId(),
68            $option->getId(),
69            $checked ? 'y' : 'n'
70        );
71    }
72
73    public function unpackRecord( $record ) {
74        $scores = [];
75        $itemLength = 2 * 8 + 7;
76        for ( $offset = 0, $len = strlen( $record ); $offset < $len; $offset += $itemLength ) {
77            if ( !preg_match(
78                '/Q([0-9A-F]{8})-A([0-9A-F]{8})-([yn])--/A',
79                $record,
80                $m,
81                0,
82                $offset
83            )
84            ) {
85                wfDebug( __METHOD__ . ": regex doesn't match\n" );
86
87                return false;
88            }
89            $qid = intval( base_convert( $m[1], 16, 10 ) );
90            $oid = intval( base_convert( $m[2], 16, 10 ) );
91            $score = ( $m[3] === 'y' ) ? 1 : 0;
92            $scores[$qid][$oid] = $score;
93        }
94
95        return $scores;
96    }
97
98    public function convertScores( $scores, $params = [] ) {
99        $result = [];
100        foreach ( $this->election->getQuestions() as $question ) {
101            $qid = $question->getId();
102            if ( !isset( $scores[$qid] ) ) {
103                return false;
104            }
105            $s = '';
106            $qscores = $scores[$qid];
107            ksort( $qscores );
108            $first = true;
109            foreach ( $qscores as $score ) {
110                if ( $first ) {
111                    $first = false;
112                } else {
113                    $s .= ', ';
114                }
115                $s .= $score ? 'y' : 'n';
116            }
117            $result[$qid] = $s;
118        }
119
120        return $result;
121    }
122
123}