Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 72
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Template
0.00% covered (danger)
0.00%
0 / 72
0.00% covered (danger)
0.00%
0 / 7
462
0.00% covered (danger)
0.00%
0 / 1
 getRule
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 getTemplateContent
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
42
 __construct
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
30
 toQTI
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 getUri
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 toArray
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
 getUsedAttributes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 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) 2013-2018 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
19 *
20 *
21 */
22
23namespace oat\taoQtiItem\model\qti\response;
24
25use oat\taoQtiItem\model\qti\exception\QtiModelException;
26use oat\taoQtiItem\model\qti\exception\TemplateException;
27use oat\taoQtiItem\helpers\QtiSerializer;
28use taoItems_models_classes_TemplateRenderer;
29
30/**
31 * Short description of class oat\taoQtiItem\model\qti\response\Template
32 *
33 * @access public
34 * @author Cedric Alfonsi, <cedric.alfonsi@tudor.lu>
35 * @package taoQTI
36
37 */
38class Template extends ResponseProcessing implements Rule
39{
40    /**
41     * QTI 2.1 Match Correct RP Template URL.
42     *
43     * @var string
44     */
45    public const MATCH_CORRECT = 'http://www.imsglobal.org/question/qti_v2p1/rptemplates/match_correct';
46
47    /**
48     * QTI 2.1 Map Response RP Template URL.
49     *
50     * @var string
51     */
52    public const MAP_RESPONSE = 'http://www.imsglobal.org/question/qti_v2p1/rptemplates/map_response';
53
54    /**
55     * QTI 2.1 Map Response Point RP Template URL.
56     *
57     * @var string
58     */
59    public const MAP_RESPONSE_POINT = 'http://www.imsglobal.org/question/qti_v2p1/rptemplates/map_response_point';
60
61    // phpcs:disable Generic.NamingConventions.UpperCaseConstantName,Generic.Files.LineLength
62    /**
63     * QTI 2.0 Match Correct RP Template URL.
64     *
65     * @var string
66     */
67    public const MATCH_CORRECT_qtiv2p0 = 'http://www.imsglobal.org/question/qti_v2p0/rptemplates/match_correct';
68
69    /**
70     * QTI 2.0 Map Response RP Template URL.
71     *
72     * @var string
73     */
74    public const MAP_RESPONSE_qtiv2p0 = 'http://www.imsglobal.org/question/qti_v2p0/rptemplates/map_response';
75
76    /**
77     * QTI 2.0 Map Response Point RP Template URL.
78     *
79     * @var string
80     */
81    public const MAP_RESPONSE_POINT_qtiv2p0 = 'http://www.imsglobal.org/question/qti_v2p0/rptemplates/map_response_point';
82
83    /**
84     * QTI 2.2 Match Correct RP Template URL.
85     *
86     * @var string
87     */
88    public const MATCH_CORRECT_qtiv2p2 = 'http://www.imsglobal.org/question/qti_v2p2/rptemplates/match_correct';
89
90    /**
91     * QTI 2.2 Map Response RP Template URL.
92     *
93     * @var string
94     */
95    public const MAP_RESPONSE_qtiv2p2 = 'http://www.imsglobal.org/question/qti_v2p2/rptemplates/map_response';
96
97    /**
98     * QTI 2.2 Map Response Point RP Template URL.
99     *
100     * @var string
101     */
102    public const MAP_RESPONSE_POINT_qtiv2p2 = 'http://www.imsglobal.org/question/qti_v2p2/rptemplates/map_response_point';
103
104    /**
105     * QTI 3.0
106     */
107    public const MAP_RESPONSE_POINT_QTI_V3 = [
108        'http://www.imsglobal.org/question/qti_v3p0/rptemplates/map_response_point',
109        'http://purl.imsglobal.org/spec/qti/v3p0/rptemplates/map_response_point',
110        'https://www.imsglobal.org/question/qti_v3p0/rptemplates/map_response_point',
111        'https://purl.imsglobal.org/spec/qti/v3p0/rptemplates/map_response_point',
112        'https://www.imsglobal.org/question/qti_v3p0/rptemplates/map_response_point.xml',
113        'https://purl.imsglobal.org/spec/qti/v3p0/rptemplates/map_response_point.xml',
114    ];
115    public const MATCH_CORRECT_QTI_V3 = [
116        'http://www.imsglobal.org/question/qti_v3p0/rptemplates/match_correct',
117        'http://purl.imsglobal.org/spec/qti/v3p0/rptemplates/match_correct',
118        'https://www.imsglobal.org/question/qti_v3p0/rptemplates/match_correct',
119        'https://purl.imsglobal.org/spec/qti/v3p0/rptemplates/match_correct',
120        'https://www.imsglobal.org/question/qti_v3p0/rptemplates/match_correct.xml',
121        'https://purl.imsglobal.org/spec/qti/v3p0/rptemplates/match_correct.xml',
122    ];
123    public const MAP_RESPONSE_QTI_V3 = [
124        'http://www.imsglobal.org/question/qti_v3p0/rptemplates/map_response',
125        'http://purl.imsglobal.org/spec/qti/v3p0/rptemplates/map_response',
126        'https://www.imsglobal.org/question/qti_v3p0/rptemplates/map_response',
127        'https://purl.imsglobal.org/spec/qti/v3p0/rptemplates/map_response',
128        'https://www.imsglobal.org/question/qti_v3p0/rptemplates/map_response.xml',
129        'https://purl.imsglobal.org/spec/qti/v3p0/rptemplates/map_response.xml',
130    ];
131
132    // phpcs:enable Generic.NamingConventions.UpperCaseConstantName,Generic.Files.LineLength
133
134    /**
135     * Template to apply when no response processing should take place
136     */
137    public const NONE = 'no_response_processing';
138
139    /**
140     * Short description of attribute uri
141     *
142     * @access protected
143     * @var string
144     */
145    protected $uri = '';
146
147    /**
148     * Short description of attribute file
149     *
150     * @access protected
151     * @var string
152     */
153    protected $file = '';
154
155    /**
156     * Short description of method getRule
157     *
158     * @access public
159     * @author Cedric Alfonsi, <cedric.alfonsi@tudor.lu>
160     * @return string
161     */
162    public function getRule()
163    {
164        $returnValue = (string) '';
165
166
167        if ($this->uri == self::MATCH_CORRECT) {
168            $returnValue = taoQTI_models_classes_Matching_Matching::MATCH_CORRECT;
169        } elseif ($this->uri == self::MAP_RESPONSE) {
170            $returnValue = taoQTI_models_classes_Matching_Matching::MAP_RESPONSE;
171        } elseif ($this->uri == self::MAP_RESPONSE_POINT) {
172            $returnValue = taoQTI_models_classes_Matching_Matching::MAP_RESPONSE_POINT;
173        }
174
175
176
177        return (string) $returnValue;
178    }
179
180    /**
181     * Get the content of the response processing template identified by its uri
182     *
183     * @todo make it dynamic in the future
184     * @return string
185     * @throws \oat\taoQtiItem\model\qti\exception\QtiModelException
186     */
187    public function getTemplateContent()
188    {
189
190        $standardRpTemplateFolder = dirname(__FILE__) . '/../data/qtiv2p1/rptemplates/';
191        switch ($this->uri) {
192            case self::MATCH_CORRECT:
193                $returnValue = file_get_contents($standardRpTemplateFolder . 'match_correct.xml');
194                break;
195            case self::MAP_RESPONSE:
196                $returnValue = file_get_contents($standardRpTemplateFolder . 'map_response.xml');
197                break;
198            case self::MAP_RESPONSE_POINT:
199                $returnValue = file_get_contents($standardRpTemplateFolder . 'map_response_point.xml');
200                break;
201            case self::NONE:
202                $returnValue = '';
203                break;
204            default:
205                throw new QtiModelException('unknown rp template');
206        }
207        return $returnValue;
208    }
209
210    /**
211     * Short description of method __construct
212     *
213     * @access public
214     * @author Cedric Alfonsi, <cedric.alfonsi@tudor.lu>
215     * @param  string uri
216     * @throws QtiModelException
217     * @throws TemplateException
218     */
219    public function __construct($uri)
220    {
221        //automatically transform to qti 2.1 templates:
222        if (
223            in_array($uri, [
224            self::MATCH_CORRECT,
225            self::MATCH_CORRECT_qtiv2p0,
226            self::MATCH_CORRECT_qtiv2p2,
227            ...self::MATCH_CORRECT_QTI_V3
228            ], true)
229        ) {
230            $this->uri = self::MATCH_CORRECT;
231        } elseif (
232            in_array($uri, [
233            self::MAP_RESPONSE,
234            self::MAP_RESPONSE_qtiv2p0,
235            self::MAP_RESPONSE_qtiv2p2,
236            ...self::MAP_RESPONSE_QTI_V3
237            ], true)
238        ) {
239            $this->uri = self::MAP_RESPONSE;
240        } elseif (
241            in_array($uri, [
242            self::MAP_RESPONSE_POINT,
243            self::MAP_RESPONSE_POINT_qtiv2p0,
244            self::MAP_RESPONSE_POINT_qtiv2p2,
245            ...self::MAP_RESPONSE_POINT_QTI_V3
246            ], true)
247        ) {
248            $this->uri = self::MAP_RESPONSE_POINT;
249        } elseif ($uri === self::NONE) {
250            $this->uri = self::NONE;
251        } else {
252            throw new TemplateException("Unknown response processing template '$uri'");
253        }
254
255        parent::__construct();
256    }
257
258    /**
259     * Short description of method toQTI
260     *
261     * @access public
262     * @author Cedric Alfonsi, <cedric.alfonsi@tudor.lu>
263     * @return string
264     */
265    public function toQTI()
266    {
267
268        $returnValue = '';
269
270        if ($this->uri != self::NONE) {
271            //if there is actually a real response template involved, render the template
272            $tplRenderer = new taoItems_models_classes_TemplateRenderer(
273                static::getTemplatePath() . '/qti.rptemplate.tpl.php',
274                ['uri' => $this->uri]
275            );
276            $returnValue = $tplRenderer->render();
277        }
278
279        return (string) $returnValue;
280    }
281
282    /**
283     * Short description of method getUri
284     *
285     * @access public
286     * @author Cedric Alfonsi, <cedric.alfonsi@tudor.lu>
287     * @return string
288     */
289    public function getUri()
290    {
291        return (string) $this->uri;
292    }
293
294    public function toArray($filterVariableContent = false, &$filtered = [])
295    {
296
297        $returnValue = parent::toArray($filterVariableContent, $filtered);
298        $rp = $this->getTemplateContent();
299        $rpSerialized = QtiSerializer::parseResponseProcessingXml(simplexml_load_string($rp));
300        $protectedData = [
301            'processingType' => 'template',
302            'data' => $this->uri,
303            'responseRules' => $rpSerialized['responseRules']
304        ];
305
306        if ($filterVariableContent) {
307            $filtered[$this->getSerial()] = $protectedData;
308        } else {
309            $returnValue = array_merge($returnValue, $protectedData);
310        }
311
312        return $returnValue;
313    }
314
315    protected function getUsedAttributes()
316    {
317        return [];
318    }
319}