Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 25 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ChoiceParser | |
0.00% |
0 / 25 |
|
0.00% |
0 / 4 |
72 | |
0.00% |
0 / 1 |
| parse | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
6 | |||
| findKeysByMask | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| findMatchingColumn | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| findCorrectAnswers | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2021 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoQtiItem\model\import\Parser; |
| 24 | |
| 25 | use oat\oatbox\service\ConfigurableService; |
| 26 | use oat\taoQtiItem\model\import\ParsedChoice; |
| 27 | |
| 28 | class ChoiceParser extends ConfigurableService implements ColumnParserInterface |
| 29 | { |
| 30 | public function parse(array $line, array $rules, array $fields): array |
| 31 | { |
| 32 | $parsedChoices = []; |
| 33 | $columnName = $fields['columnName']; |
| 34 | $columnPattern = $this->findMatchingColumn($rules['header']); |
| 35 | |
| 36 | $choices = array_filter($this->findKeysByMask($columnName, $line)); |
| 37 | $choicesScores = $this->findKeysByMask($columnPattern, $line); |
| 38 | |
| 39 | $correctAnswers = $this->findCorrectAnswers($line); |
| 40 | |
| 41 | foreach ($choices as $choiceId => $choice) { |
| 42 | $parsedChoices[] = new ParsedChoice( |
| 43 | $choiceId, |
| 44 | $choice, |
| 45 | (float)$choicesScores[$choiceId . '_score'], |
| 46 | in_array($choiceId, $correctAnswers) |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | return $parsedChoices; |
| 51 | } |
| 52 | |
| 53 | private function findKeysByMask(string $pattern, array $input): array |
| 54 | { |
| 55 | $pattern = '/\b(' . $pattern . ')\b/'; |
| 56 | |
| 57 | return array_intersect_key($input, array_flip(preg_grep($pattern, array_keys($input)))); |
| 58 | } |
| 59 | |
| 60 | private function findMatchingColumn(string $rules): ?string |
| 61 | { |
| 62 | foreach (explode('|', $rules) as $validation) { |
| 63 | if (strpos($validation, 'match_header:') !== false) { |
| 64 | return str_replace('match_header:', '', $validation); |
| 65 | } |
| 66 | } |
| 67 | return null; |
| 68 | } |
| 69 | |
| 70 | private function findCorrectAnswers(array $line): array |
| 71 | { |
| 72 | return (array)( |
| 73 | $line['correct_answer'] |
| 74 | ? explode(',', str_replace('', '', $line['correct_answer'])) |
| 75 | : [] |
| 76 | ); |
| 77 | } |
| 78 | } |