Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
ColumnIdProvider
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
5 / 5
13
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 provide
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
8
 provideFromColumnArray
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createColumnDataHash
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 provideVariableColumnId
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
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) 2022 (original work) Open Assessment Technologies SA;
19 */
20
21declare(strict_types=1);
22
23namespace oat\taoOutcomeUi\model\table\ColumnDataProvider;
24
25use oat\taoOutcomeUi\model\ItemResultStrategy;
26use oat\taoOutcomeUi\model\table\ContextTypePropertyColumn;
27use oat\taoOutcomeUi\model\table\DeliveryExecutionColumn;
28use oat\taoOutcomeUi\model\table\ResponseColumn;
29use oat\taoOutcomeUi\model\table\TestCenterColumn;
30use oat\taoOutcomeUi\model\table\TraceVariableColumn;
31use oat\taoOutcomeUi\model\table\VariableColumn;
32use tao_models_classes_table_Column;
33
34class ColumnIdProvider
35{
36    private ItemResultStrategy $itemResultStrategy;
37
38    public function __construct(ItemResultStrategy $itemResultStrategy)
39    {
40        $this->itemResultStrategy = $itemResultStrategy;
41    }
42
43    public function provide(tao_models_classes_table_Column $column): string
44    {
45        switch (true) {
46            case $column instanceof ResponseColumn:
47                return $column->getContextIdentifier() . '_' . $column->getIdentifier();
48            case $column instanceof ContextTypePropertyColumn:
49                return $column->getProperty()->getUri() . '_' . $column->getContextType();
50            case $column instanceof TestCenterColumn:
51                return $column->getProperty()->getUri();
52            case $column instanceof VariableColumn:
53                return $this->provideVariableColumnId($column);
54            case $column instanceof TraceVariableColumn:
55            case $column instanceof DeliveryExecutionColumn:
56                return $column->getContextIdentifier() . '_' . $column->getIdentifier();
57            default:
58                return $this->createColumnDataHash($column->getLabel());
59        }
60    }
61
62    public function provideFromColumnArray(array $columnArray): string
63    {
64        return $this->provide(tao_models_classes_table_Column::buildColumnFromArray($columnArray));
65    }
66
67    public function createColumnDataHash(string ...$elements): string
68    {
69        return md5(implode($elements));
70    }
71
72    private function provideVariableColumnId(VariableColumn $variableColumn): string
73    {
74        if (!$this->itemResultStrategy->isItemEntityBased()) {
75            return $this->createColumnDataHash(
76                $variableColumn->getContextIdentifier(),
77                $variableColumn->getRefId() ?? '',
78                $variableColumn->getIdentifier()
79            );
80        }
81
82        return $variableColumn->getContextIdentifier() . '_' . $variableColumn->getIdentifier();
83    }
84}