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