Index: trunk/extensions/SecurePoll/SecurePoll.php |
— | — | @@ -53,6 +53,7 @@ |
54 | 54 | 'SecurePoll_ChooseBallot' => "$dir/includes/ballots/ChooseBallot.php", |
55 | 55 | 'SecurePoll_PreferentialBallot' => "$dir/includes/ballots/PreferentialBallot.php", |
56 | 56 | 'SecurePoll_RadioRangeBallot' => "$dir/includes/ballots/RadioRangeBallot.php", |
| 57 | + 'SecurePoll_RadioRangeCommentBallot' => "$dir/includes/ballots/RadioRangeCommentBallot.php", |
57 | 58 | |
58 | 59 | # crypt |
59 | 60 | 'SecurePoll_Crypt' => "$dir/includes/crypt/Crypt.php", |
Index: trunk/extensions/SecurePoll/includes/ballots/RadioRangeCommentBallot.php |
— | — | @@ -0,0 +1,114 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * A ballot used specifically for the Wikimedia Referendum on the personal image filter. |
| 6 | + * Allows voters to send comments in with their ballot |
| 7 | + */ |
| 8 | +class SecurePoll_RadioRangeCommentBallot extends SecurePoll_RadioRangeBallot { |
| 9 | + public function getForm( $prevStatus = false ) { |
| 10 | + $form = parent::getForm( $prevStatus ); |
| 11 | + |
| 12 | + $form .= Html::element('hr'); |
| 13 | + // Add the comment call |
| 14 | + $callForComments = $this->election->parseMessage( 'comments' ); |
| 15 | + $form .= Html::rawElement( 'p', null, $callForComments ); |
| 16 | + |
| 17 | + // Add the comments boxes. |
| 18 | + $form .= Html::textarea( 'securepoll_comments_native', '', |
| 19 | + array( 'rows' => 10, 'cols' => 20 ) ); |
| 20 | + $form .= Html::textarea( 'securepoll_comments_en', '', |
| 21 | + array( 'rows' => 10, 'cols' => 20 ) ); |
| 22 | + |
| 23 | + return $form; |
| 24 | + } |
| 25 | + |
| 26 | + public function submitForm() { |
| 27 | + $status = parent::submitForm(); |
| 28 | + |
| 29 | + if ( ! $status->isGood() ) { |
| 30 | + return $status; |
| 31 | + } |
| 32 | + |
| 33 | + // Load comments |
| 34 | + global $wgRequest; |
| 35 | + |
| 36 | + $commentNative = $wgRequest->getText( 'securepoll_comments_native' ); |
| 37 | + $commentEnglish = $wgRequest->getText( 'securepoll_comments_en' ); |
| 38 | + |
| 39 | + $record = rtrim($status->value); |
| 40 | + |
| 41 | + $record .= '/'.strlen($commentNative).'/'.$commentNative; |
| 42 | + $record .= '--/'.strlen($commentEnglish).'/'.$commentEnglish; |
| 43 | + |
| 44 | + $status->value = $record; |
| 45 | + |
| 46 | + return $status; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Copy and modify from parent function, complex to refactor. |
| 51 | + */ |
| 52 | + function unpackRecord( $record ) { |
| 53 | + $scores = array(); |
| 54 | + $itemLength = 8 + 8 + 11 + 7; |
| 55 | + $questions = array(); |
| 56 | + foreach ( $this->election->getQuestions() as $question ) { |
| 57 | + $questions[$question->getId()] = $question; |
| 58 | + } |
| 59 | + for ( $offset = 0; $offset < strlen( $record ); $offset += $itemLength ) { |
| 60 | + if ( !preg_match( '/Q([0-9A-F]{8})-A([0-9A-F]{8})-S([+-][0-9]{10})--/A', |
| 61 | + $record, $m, 0, $offset ) ) |
| 62 | + { |
| 63 | + // Allow comments |
| 64 | + if ( $record[$offset] == '/' ) { |
| 65 | + break; |
| 66 | + } |
| 67 | + |
| 68 | + wfDebug( __METHOD__.": regex doesn't match\n" ); |
| 69 | + return false; |
| 70 | + } |
| 71 | + $qid = intval( base_convert( $m[1], 16, 10 ) ); |
| 72 | + $oid = intval( base_convert( $m[2], 16, 10 ) ); |
| 73 | + $score = intval( $m[3] ); |
| 74 | + if ( !isset( $questions[$qid] ) ) { |
| 75 | + wfDebug( __METHOD__.": invalid question ID\n" ); |
| 76 | + return false; |
| 77 | + } |
| 78 | + list( $min, $max ) = $this->getMinMax( $questions[$qid] ); |
| 79 | + if ( $score < $min || $score > $max ) { |
| 80 | + wfDebug( __METHOD__.": score out of range\n" ); |
| 81 | + return false; |
| 82 | + } |
| 83 | + $scores[$qid][$oid] = $score; |
| 84 | + } |
| 85 | + |
| 86 | + // Read comments |
| 87 | + $scores['comment'] = array(); |
| 88 | + |
| 89 | + $scores['comment']['native'] = $this->readComment( $record, $offset ); |
| 90 | + |
| 91 | + if ( substr( $record, $offset, 2 ) !== '--' ) { |
| 92 | + wfDebug( __METHOD__.": Invalid format\n" ); |
| 93 | + return false; |
| 94 | + } |
| 95 | + $offset += 2; |
| 96 | + |
| 97 | + $scores['comment']['en'] = $this->readComment( $record, $offset ); |
| 98 | + |
| 99 | + if ( $offset < strlen($record) ) { |
| 100 | + wfDebug( __METHOD__.": Invalid format\n" ); |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + return $scores; |
| 105 | + } |
| 106 | + |
| 107 | + function readComment( $record, &$offset ) { |
| 108 | + $commentOffset = strpos( $record, '/', $offset+1 ); |
| 109 | + $commentLength = intval(substr( $record, $offset+1, |
| 110 | + ($commentOffset - $offset) - 1 ) ); |
| 111 | + $result = substr( $record, $commentOffset + 1, $commentLength ); |
| 112 | + $offset = $commentOffset + $commentLength + 1; // Fast-forward |
| 113 | + return $result; |
| 114 | + } |
| 115 | +} |
Index: trunk/extensions/SecurePoll/includes/ballots/Ballot.php |
— | — | @@ -97,6 +97,8 @@ |
98 | 98 | return new SecurePoll_ChooseBallot( $context, $election ); |
99 | 99 | case 'radio-range': |
100 | 100 | return new SecurePoll_RadioRangeBallot( $context, $election ); |
| 101 | + case 'radio-range-comment': |
| 102 | + return new SecurePoll_RadioRangeCommentBallot( $context, $election ); |
101 | 103 | default: |
102 | 104 | throw new MWException( "Invalid ballot type: $type" ); |
103 | 105 | } |