Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
PciLoader
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 2
650
0.00% covered (danger)
0.00%
0 / 1
 load
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 1
600
 getRegistries
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) 2016 (original work) Open Assessment Technologies SA;
19 *
20 */
21
22namespace oat\qtiItemPci\controller;
23
24use oat\qtiItemPci\model\PciModel;
25use oat\qtiItemPci\model\IMSPciModel;
26use oat\taoQtiItem\model\portableElement\exception\PortableElementException;
27use tao_actions_CommonModule;
28
29class PciLoader extends tao_actions_CommonModule
30{
31    /**
32     * Load latest PCI runtimes
33     */
34    public function load()
35    {
36        $customInteractionDirs = \common_ext_ExtensionsManager::singleton()
37            ->getExtensionById('taoQtiItem')
38            ->getConfig('debug_portable_element');
39
40        try {
41            $result = array_reduce($this->getRegistries(), function ($acc, $registry) use ($customInteractionDirs) {
42                $latestRuntimes = $registry->getLatestRuntimes();
43                if (is_array($customInteractionDirs)) {
44                    foreach ($latestRuntimes as $id => &$versions) {
45                        if (isset($customInteractionDirs[$id])) {
46                            //add option to load from source (as opposed to from the default min.js files)
47                            foreach ($versions as &$version) {
48                                // IMS PCI
49                                if (isset($version['model']) && $version['model'] == 'IMSPCI') {
50                                    $modules = (isset($version['runtime']) && isset($version['runtime']['modules']))
51                                        ? $version['runtime']['modules']
52                                        : [];
53                                    $src = (isset($version['runtime']) && isset($version['runtime']['src']))
54                                        ? $version['runtime']['src']
55                                        : [];
56
57                                    // in case of a TAO bundled PCI (= we have a "src" entry),
58                                    // we redirect the module to the entry point of the PCI instead of its minified
59                                    // version
60                                    if (count($src) > 0) {
61                                        foreach ($modules as $moduleKey => &$allModulesFiles) {
62                                            if (strpos($moduleKey, '.min') == (strlen($moduleKey) - strlen('.min'))) {
63                                                foreach ($allModulesFiles as &$moduleFile) {
64                                                    $moduleFile = str_replace('.min.js', '.js', $moduleFile);
65                                                }
66                                            }
67                                        }
68                                    }
69
70                                    if (isset($version['runtime']) && isset($version['runtime']['modules'])) {
71                                        $version['runtime']['modules'] = $modules;
72                                    }
73                                    if (isset($version['creator']) && isset($version['creator']['src'])) {
74                                        //the first entry of the src array is always the entrypoint (PCI hook)
75                                        $version['creator']['hook'] = array_shift($version['creator']['src']);
76                                        $version['creator']['modules'] = $version['creator']['src'];
77                                        unset($version['creator']['src']);
78                                    }
79
80                                    // TAO PCI
81                                } else {
82                                    if (isset($version['runtime']) && isset($version['runtime']['src'])) {
83                                        $version['runtime']['libraries'] = $version['runtime']['src'];
84                                        unset($version['runtime']['hook']);
85                                        unset($version['runtime']['src']);
86                                    }
87                                    if (isset($version['creator']) && isset($version['creator']['src'])) {
88                                        //the first entry of the src array is always the entrypoint (PCI hook)
89                                        $version['creator']['hook'] = array_shift($version['creator']['src']);
90                                        $version['creator']['modules'] = $version['creator']['src'];
91                                        unset($version['creator']['src']);
92                                    }
93                                }
94                            }
95                        }
96                    }
97                }
98                return array_merge($acc, $latestRuntimes);
99            }, []);
100
101            $this->returnJson($result);
102        } catch (PortableElementException $e) {
103            $this->returnJson($e->getMessage(), 500);
104        }
105    }
106
107    protected function getRegistries()
108    {
109        return [(new PciModel())->getRegistry(), (new IMSPciModel())->getRegistry()];
110    }
111}