Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 199
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
MlpEvalForm
0.00% covered (danger)
0.00%
0 / 199
0.00% covered (danger)
0.00%
0 / 6
1190
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 addControls
0.00% covered (danger)
0.00%
0 / 149
0.00% covered (danger)
0.00%
0 / 1
240
 addStateFields
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 addButtons
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
 addOptions
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
56
 saveSubstepField
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3use MediaWiki\Extension\Math\MathConfig;
4use MediaWiki\MediaWikiServices;
5
6class MlpEvalForm extends OOUIHTMLForm {
7
8    /** @var SpecialMlpEval */
9    private $eval;
10    /** @var int */
11    private $step;
12
13    /**
14     * @param SpecialMlpEval $specialPage
15     */
16    public function __construct( SpecialMlpEval $specialPage ) {
17        $this->eval = $specialPage;
18        $this->step = $specialPage->getStep();
19        $formDescriptor = [];
20        $this->addControls( $formDescriptor );
21        $this->addOptions( $formDescriptor );
22        parent::__construct( $formDescriptor, $specialPage->getContext() );
23        // $this->mWasSubmitted = false;
24        $this->addStateFields();
25        $this->addButtons();
26        $this->setSubmitCallback( static function (){
27            return false;
28        } );
29    }
30
31    private function addControls( &$formDescriptor ) {
32        switch ( $this->step ) {
33            case SpecialMlpEval::STEP_PAGE:
34                $formDescriptor['1-page'] = [
35                    'class'         => 'HTMLTitleTextField',
36                    'default'       => $this->eval->getRandomPageText()
37                ];
38                break;
39            case SpecialMlpEval::STEP_FORMULA:
40                $formDescriptor['2-content'] = [
41                        'type'    => 'radio',
42                        'default' => 1,
43                ];
44                $formDescriptor['2-domain'] = [
45                        'type'    => 'radio',
46                        'default' => 1,
47                ];
48                break;
49            case SpecialMlpEval::STEP_TEX:
50                $formDescriptor['3-skip'] = [
51                        'type' => 'submit',
52                        'value' => 'Skip step.'
53                ];
54                $formDescriptor['3-pretty'] = [
55                        'type'    => 'radio',
56                        'default' => 3,
57                        'disabled' => !$this->eval->isTexInputChanged()
58                ];
59                $formDescriptor['3-assessment'] = [
60                        'type'    => 'radio',
61                        'default' => 4,
62                ];
63                $formDescriptor['3-problems'] = [
64                        'type'    => 'multiselect',
65                ];
66                $formDescriptor['3-suggestion'] = [
67                        'type'    => 'text',
68                        'required' => false,
69                ];
70                break;
71            case SpecialMlpEval::STEP_RENDERING:
72                $subStep = $this->eval->getSubStep();
73                if ( $subStep == '4' ) {
74                    $formDescriptor['4-best'] = [
75                            'type'    => 'radio',
76                            'options' => [
77                                    $this->eval->getMathMLRenderingAsHtmlFragment(
78                                        MathConfig::MODE_MATHML ) => 'mathoid',
79                                    $this->eval->getMathMLRenderingAsHtmlFragment(
80                                        MathConfig::MODE_NATIVE_MML ) => 'native',
81                                    $this->eval->getMathMLRenderingAsHtmlFragment(
82                                        MathConfig::MODE_LATEXML ) => 'latexml',
83                            ],
84                            'default' => 'mathoid'
85                    ];
86                } else {
87                    $formDescriptor['4-size'] = [
88                            'type'    => 'radio',
89                            'default' => 1
90                    ];
91                    $formDescriptor['4-spacing'] = [
92                            'type'    => 'radio',
93                            'default' => 1
94                    ];
95                    $formDescriptor['4-integration'] = [
96                            'type'    => 'radio',
97                            'default' => 1
98                    ];
99                    $formDescriptor['4-font'] = [
100                            'type'    => 'radio',
101                            'default' => 1
102                    ];
103                    $formDescriptor['4-absolute'] = [
104                                'type'    => 'radio',
105                                'default' => 1
106                    ];
107                }
108                break;
109            case SpecialMlpEval::STEP_IDENTIFIERS:
110                $options = [];
111                // TODO: defaults currently do not work because request->wasPosted() is set to true.
112                $default = [];
113                foreach ( $this->eval->getIdentifiers() as $id ) {
114                    /** @var MathRenderer $renderer */
115                    $renderer = MediaWikiServices::getInstance()
116                        ->get( 'Math.RendererFactory' )
117                        ->getRenderer( $id, [] );
118                    if ( $renderer->render() ) {
119                        $rendered = $renderer->getHtmlOutput();
120                    } else {
121                        $rendered = $renderer->getLastError();
122                    }
123                    $options[$rendered] = $id;
124                    $default[] = $id;
125                }
126                if ( !empty( $options ) ) {
127                    $formDescriptor['5-identifiers'] = [
128                        'type' => 'multiselect',
129                        'options' => $options,
130                        'default' => $default,
131                        // 'invert'=> true,
132                    ];
133                }
134                $formDescriptor['5-missing'] = [
135                        'type' => 'textarea',
136                        'rows' => 3, # Display height of field
137                        // 'cols' => 30 # Display width of field
138                ];
139                break;
140            case SpecialMlpEval::STEP_DEFINITIONS:
141                foreach ( $this->eval->getIdentifiers() as $key => $id ) {
142                    $options = [];
143                    $formDescriptor["6-separator-$key"] = [
144                        'type'    => 'info',
145                        'default' => '<h3>' .
146                            wfMessage( 'math-lp-6-separator-message', $id )->parseAsBlock() . '</h3>',
147                        'raw'     => true
148                    ];
149                    $rels = $this->eval->getRelations( $id );
150                    foreach ( $rels as $rel ) {
151                        $options[$rel] = $rel;
152                    }
153                    $options['other'] = 'other';
154                    if ( count( $rels ) ) {
155                        $formDescriptor["6-id-$key"] = [
156                                'label' => "Select definitions for $id",
157                                'type'      => 'multiselect',
158                                'options'   => $options,
159                                // 'raw' => true
160                        ];
161                    }
162                    $formDescriptor["6-id-$key-other"] = [
163                            'label' => "Other for $id",
164                            'type'      => 'text'
165                    ];
166                }
167                $srt = $this->eval->getSpeechRuleText();
168                $formDescriptor["6-srt"] = [
169                    'type'    => 'info',
170                    'default' => "<pre>$srt</pre>",
171                    'raw'     => true
172                ];
173                $formDescriptor['6-srt-assessment'] = [
174                    'type'    => 'radio',
175                    'default' => 2,
176                ];
177                $formDescriptor['6-srt-suggestion'] = [
178                    'type'     => 'text',
179                    'required' => false,
180                ];
181                break;
182            case SpecialMlpEval::STEP_FINISHED:
183                $formDescriptor['feedback'] = [
184                        'type' => 'text',
185                ];
186                break;
187        }
188        $formDescriptor['submit-info'] = [
189            'type' => 'info',
190            // 'label-message' => 'math-lp-submit-info-label',
191            'default' => wfMessage( 'math-lp-submit-info' )->text(),
192            // 'raw' => true # if true, the above string won't be html-escaped.
193        ];
194    }
195
196    private function addStateFields() {
197        $specialPage = $this->eval;
198        $this->addHiddenField( 'oldId', $specialPage->getOldId() );
199        $this->addHiddenField( 'fId', $specialPage->getFId() );
200        $this->addHiddenField( 'oldStep', $this->step );
201        $this->addHiddenField( 'oldSubStep', $this->eval->getSubStep() );
202        if ( $this->step == 4 ) {
203            $subFields = $this->eval->getRenderingFields();
204            foreach ( $subFields as $key ) {
205                $this->saveSubstepField( $key );
206            }
207        }
208    }
209
210    private function addButtons() {
211        $this->addButton( "pgRst", wfMessage( 'math-lp-new-article' )->text() );
212        if ( $this->step > 1 && $this->step < 7 ) {
213            $this->addButton( "fRst", wfMessage( 'math-lp-new-formula' )->text() );
214        }
215        if ( $this->step < 6 ) {
216            $this->setSubmitTextMsg(
217                wfMessage( 'math-lp-submit-label' )->params( $this->eval->getNextStep() )
218            );
219        } elseif ( $this->step == 6 ) {
220            $this->setSubmitTextMsg( wfMessage( 'math-lp-finish-label' ) );
221        } else {
222            $this->setSubmitTextMsg( wfMessage( 'math-lp-new-formula' ) );
223        }
224    }
225
226    private function addOptions( &$form ) {
227        static $elements = [ 'label', 'help' ];
228        foreach ( $form as $key => $control ) {
229            foreach ( $elements as $element ) {
230                $msg = "math-lp-$key-$element";
231                if ( wfMessage( $msg )->exists() ) {
232                    $form[$key]["$element-message"] = $msg;
233                }
234            }
235            if ( wfMessage( "math-lp-$key-option-1" )->exists() ) {
236                $options = [];
237                for ( $i = 1;$i < 20;$i++ ) {
238                    $msg = "math-lp-$key-option-$i";
239                    if ( wfMessage( "math-lp-$key-option-$i" )->exists() ) {
240                            $txt = wfMessage( "math-lp-$key-option-$i", $i )->parseAsBlock();
241                            $options[$txt] = $i;
242                    } else {
243                        break;
244                    }
245                }
246                $form[$key]["options"] = $options;
247            }
248        }
249    }
250
251    /**
252     * @param string $key
253     */
254    private function saveSubstepField( $key ) {
255        $substeps = [ '4', '4a', '4b', '4c' ];
256        foreach ( $substeps as $substep ) {
257            $val = $this->getRequest()->getVal( "4-$key-$substep" );
258            if ( $val ) {
259                $this->addHiddenField( "4-$key-$substep", $val );
260            }
261        }
262    }
263}