Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
taoItems_models_classes_ItemCompiler
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 4
42
0.00% covered (danger)
0.00%
0 / 1
 getContentUsedLanguages
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 deployItem
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
12
 createService
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 getSubCompilerClass
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 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
19 *
20 */
21
22/**
23 * Generic item compiler.
24 *
25 * @access public
26 * @author Joel Bout, <joel@taotesting.com>
27 * @author Jérôme Bogaerts, <jerome@taotesting.com>
28 * @package taoItems
29
30 */
31abstract class taoItems_models_classes_ItemCompiler extends tao_models_classes_Compiler
32{
33    /**
34     * Get the languages in use for the item content.
35     *
36     * @return array An array of language tags (string).
37     */
38    protected function getContentUsedLanguages()
39    {
40        return $this
41            ->getResource()
42            ->getUsedLanguages(
43                new core_kernel_classes_Property(taoItems_models_classes_ItemsService::PROPERTY_ITEM_CONTENT)
44            );
45    }
46
47    /**
48     * deploys the item into the given absolute directory
49     *
50     * @param core_kernel_classes_Resource $item
51     * @param string $languageCode
52     * @param string $compiledDirectory
53     * @return common_report_Report
54     */
55    protected function deployItem(core_kernel_classes_Resource $item, $languageCode, $compiledDirectory)
56    {
57        $itemService = taoItems_models_classes_ItemsService::singleton();
58
59        // copy local files
60        $sourceDir = $itemService->getItemDirectory($item, $languageCode);
61        $success = taoItems_helpers_Deployment::copyResources(
62            $sourceDir->getPrefix(),
63            $compiledDirectory,
64            ['index.html']
65        );
66
67        if (!$success) {
68            return $this->fail(__('Unable to copy resources for language %s', $languageCode));
69        }
70
71        // render item
72
73        $xhtml = $itemService->render($item, $languageCode);
74
75        // retrieve external resources
76        $subReport = taoItems_helpers_Deployment::retrieveExternalResources($xhtml, $compiledDirectory);
77        if ($subReport->getType() == common_report_Report::TYPE_SUCCESS) {
78            $xhtml = $subReport->getData();
79            // write index.html
80            file_put_contents($compiledDirectory . 'index.html', $xhtml);
81            return new common_report_Report(
82                common_report_Report::TYPE_SUCCESS,
83                __('Published "%1$s" in language "%2$s"', $item->getLabel(), $languageCode)
84            );
85        } else {
86            return $subReport;
87        }
88    }
89
90    /**
91     * Create the item's ServiceCall.
92     *
93     * @param core_kernel_classes_Resource $item
94     * @param tao_models_classes_service_StorageDirectory $destinationDirectory
95     * @return tao_models_classes_service_ServiceCall
96     */
97    protected function createService(
98        core_kernel_classes_Resource $item,
99        tao_models_classes_service_StorageDirectory $destinationDirectory
100    ) {
101        $service = new tao_models_classes_service_ServiceCall(
102            new core_kernel_classes_Resource(taoItems_models_classes_ItemsService::INSTANCE_SERVICE_ITEM_RUNNER)
103        );
104        $service->addInParameter(new tao_models_classes_service_ConstantParameter(
105            new core_kernel_classes_Resource(taoItems_models_classes_ItemsService::INSTANCE_FORMAL_PARAM_ITEM_PATH),
106            $destinationDirectory->getId()
107        ));
108        $service->addInParameter(new tao_models_classes_service_ConstantParameter(
109            new core_kernel_classes_Resource(taoItems_models_classes_ItemsService::INSTANCE_FORMAL_PARAM_ITEM_URI),
110            $item
111        ));
112
113        return $service;
114    }
115
116    protected function getSubCompilerClass(core_kernel_classes_Resource $resource)
117    {
118        throw new common_Exception('Items cannot include other resources');
119    }
120}