Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
QtiRunner
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 5
702
0.00% covered (danger)
0.00%
0 / 1
 getVariableValues
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
42
 getPrivatePathByLanguage
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 getContentVariableElements
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getRubricBlocks
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
56
 getFeedbacks
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
90
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) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
19 *
20 *
21 */
22
23namespace oat\taoQtiItem\helpers;
24
25use qtism\common\enums\BaseType;
26use qtism\common\enums\Cardinality;
27use qtism\runtime\common\Variable;
28use qtism\runtime\tests\AssessmentItemSession;
29use tao_models_classes_service_StorageDirectory;
30
31/**
32 * Qti Item Runner helper
33 *
34 * @author Somsack Sipasseuth <sam@taotesting.com>
35 * @author Jérôme Bogaerts <jerome@taotesting.com>
36 * @package taoQtiItem
37 *
38 * @license GPLv2  http://www.opensource.org/licenses/gpl-2.0.php
39 */
40class QtiRunner
41{
42    /**
43     * Get the intrinsic values of a given QTI $variable.
44     *
45     * @param Variable $variable
46     * @return array
47     */
48    public static function getVariableValues(Variable $variable)
49    {
50
51        $returnValue = [];
52
53        $baseType = $variable->getBaseType();
54        $cardinalityType = $variable->getCardinality();
55        $value = $variable->getValue();
56
57        // This only works if the variable has a value ;)
58        if ($value !== null) {
59            if ($baseType === BaseType::IDENTIFIER) {
60                if ($cardinalityType === Cardinality::SINGLE) {
61                    $returnValue[] = $value->getValue();
62                } elseif ($cardinalityType === Cardinality::MULTIPLE) {
63                    foreach ($variable->getValue() as $value) {
64                        $returnValue[] = $value->getValue();
65                    }
66                }
67            }
68        }
69
70        return $returnValue;
71    }
72
73    /**
74     * Get the flysystem path to the compilation folder described by $directory.
75     *
76     * @param tao_models_classes_service_StorageDirectory $directory The root directory resource where the item
77     *                                                               is stored.
78     * @return string The flysystem path to the private folder with a trailing directory separator.
79     */
80    public static function getPrivatePathByLanguage(tao_models_classes_service_StorageDirectory $directory)
81    {
82        $lang = \common_session_SessionManager::getSession()->getDataLanguage();
83
84        if (!$directory->has($lang) && $directory->has(DEFAULT_LANG)) {
85            $lang = DEFAULT_LANG;
86        }
87
88        return $lang . DIRECTORY_SEPARATOR;
89    }
90
91    /**
92     * Get the JSON QTI Model representing the elements (A.K.A. components) that vary over time for
93     * the item stored in $directory.
94     *
95     * @param tao_models_classes_service_StorageDirectory $directory
96     * @return array A JSON decoded array.
97     */
98    public static function getContentVariableElements(tao_models_classes_service_StorageDirectory $directory)
99    {
100        $jsonFile = self::getPrivatePathByLanguage($directory) . 'variableElements.json';
101        $data = $directory->read($jsonFile);
102        return json_decode($data, true);
103    }
104
105    /**
106     * Get rubric block visible by the given "view"
107     *
108     * @param tao_models_classes_service_StorageDirectory $directory
109     * @param type $view
110     * @return array
111     */
112    public static function getRubricBlocks(tao_models_classes_service_StorageDirectory $directory, $view)
113    {
114
115        $returnValue = [];
116
117        $elements = self::getContentVariableElements($directory);
118
119        foreach ($elements as $serial => $data) {
120            if (isset($data['qtiClass']) && $data['qtiClass'] == 'rubricBlock') {
121                if (
122                    !empty($data['attributes'])
123                    && is_array($data['attributes']['view'])
124                    && in_array($view, $data['attributes']['view'])
125                ) {
126                    $returnValue[$serial] = $data;
127                }
128            }
129        }
130
131        return $returnValue;
132    }
133
134    /**
135     * Get the feedback to be displayed on an AssessmentItemSession
136     *
137     * @param tao_models_classes_service_StorageDirectory $directory
138     * @param \qtism\runtime\tests\AssessmentItemSession $itemSession
139     * @return array
140     */
141    public static function getFeedbacks(
142        tao_models_classes_service_StorageDirectory $directory,
143        AssessmentItemSession $itemSession
144    ) {
145
146        $returnValue = [];
147
148        $feedbackClasses = ['modalFeedback', 'feedbackInline', 'feedbackBlock'];
149        $elements = self::getContentVariableElements($directory);
150
151        $outcomes = [];
152        foreach ($elements as $data) {
153            if (empty($data['qtiClass']) === false && in_array($data['qtiClass'], $feedbackClasses)) {
154                $feedbackIdentifier = $data['attributes']['identifier'];
155                $outcomeIdentifier = $data['attributes']['outcomeIdentifier'];
156
157                if (!isset($outcomes[$outcomeIdentifier])) {
158                    $outcomes[$outcomeIdentifier] = [];
159                }
160
161                $outcomes[$outcomeIdentifier][$feedbackIdentifier] = $data;
162            }
163        }
164
165        foreach ($itemSession->getAllVariables() as $var) {
166            $identifier = $var->getIdentifier();
167
168            if (isset($outcomes[$identifier])) {
169                $feedbacks = $outcomes[$identifier];
170                $feedbackIds = QtiRunner::getVariableValues($var);
171
172                foreach ($feedbackIds as $feedbackId) {
173                    if (isset($feedbacks[$feedbackId])) {
174                        $data = $feedbacks[$feedbackId];
175                        $returnValue[$data['serial']] = $data;
176                    }
177                }
178            }
179        }
180
181        return $returnValue;
182    }
183}