Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
taoItems_models_classes_ItemExporter
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 9
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getItemService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getItem
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setItem
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getZip
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setZip
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getItemModel
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 getItemDirectory
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 addFile
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 export
n/a
0 / 0
n/a
0 / 0
0
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) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung
19 *                         (under the project TAO-TRANSFER);
20 *               2009-2012 (update and modification) Public Research Centre Henri Tudor
21 *                         (under the project TAO-SUSTAIN & TAO-DEV);
22 */
23
24/**
25 * The ItemExporter class acts as the foundation for exporting item in XML packages. Developpers
26 * who wants to create dedicated export behaviours can extend this class. To create a new export behaviour
27 * developpers only need to implement the export abstract method. This is especially useful if a developper
28 * needs to apply specific export procedures for items of a given Item Model. When you extend this class, you have
29 * to respect a class naming convention. Actually, the class name must logicaly bound to the related Item Model label
30 * in the knowledge base.
31 *
32 * QTI Items have their Item Model labeled QTI. Your ItemExporter should be then:
33 * - named QTIItemExport where QTI is the Item model label, and ItemExport a suffix by convention.
34 * - and the file containing its definition should be named class.QTIItemExport.php.
35 *
36 * When an item matching the QTI item model will be exported, the class QTIItemExporter will be dynamically loaded
37 * and its QTIItemExporter::export() method invoked.
38 *
39 * @author CRP Henri Tudor - TAO Team - {@link http://www.tao.lu}
40 * @license GPLv2  http://www.opensource.org/licenses/gpl-2.0.php
41 * @package taoItems
42
43 */
44
45use Psr\Http\Message\StreamInterface;
46
47abstract class taoItems_models_classes_ItemExporter
48{
49    private $item;
50    private $zip;
51
52    /**
53     * Creates a new instance of ItemExporter for a particular item.
54     *
55     * @param core_kernel_classes_Resource $item The item to be exported.
56     * @param ZipArchive $zip The ZIP archive were the files have to be exported.
57     */
58    public function __construct(core_kernel_classes_Resource $item, ZipArchive $zip)
59    {
60        $this->setItem($item);
61        $this->setZip($zip);
62    }
63
64    /**
65     * Obtains a reference on the TAO Item Service.
66     *
67     * @return taoItems_models_classes_ItemsService A TAO Item Service.
68     */
69    protected function getItemService()
70    {
71        return taoItems_models_classes_ItemsService::singleton();
72    }
73
74    /**
75     * Obtains a reference on the currently exported instance of Item.
76     *
77     * @return core_kernel_classes_Resource The currently exported item.
78     */
79    protected function getItem()
80    {
81        return $this->item;
82    }
83
84    protected function setItem(core_kernel_classes_Resource $item)
85    {
86        $this->item = $item;
87    }
88
89    /**
90     * Obtains a reference on the Zip Archive where the files related to the exported items will be stored.
91     *
92     * @return ZipArchive A created and open ZipArchive instance.
93     */
94    public function getZip()
95    {
96        return $this->zip;
97    }
98
99    public function setZip(ZipArchive $zip)
100    {
101        $this->zip = $zip;
102    }
103
104    /**
105     * Obtains a reference on the Item Model related to the currently exported item instance.
106     *
107     * @return core_kernel_classes_Container
108     */
109    protected function getItemModel()
110    {
111        try {
112            return $this
113                ->getItem()
114                ->getUniquePropertyValue(
115                    new core_kernel_classes_Property(taoItems_models_classes_ItemsService::PROPERTY_ITEM_MODEL)
116                );
117        } catch (common_Exception $e) {
118            return null;
119        }
120    }
121
122    protected function getItemDirectory()
123    {
124        return $this->getItemService()->getItemDirectory($this->getItem());
125    }
126
127    /**
128     * Add files or folders (and their content) to the Zip Archive that will contain all the files to the current export
129     * session.
130     * For instance, if you want to copy the file 'taoItems/data/i123/item.xml' as 'myitem.xml' to your archive call
131     * addFile('path_to_item_location/item.xml', 'myitem.xml').
132     * As a result, you will get a file entry in the final ZIP archive at '/i123/myitem.xml'.
133     *
134     * @param string | StreamInterface $src The path to the source file or folder to copy into the ZIP Archive.
135     * @param string *dest The <u>relative</u> to the destination within the ZIP archive.
136     * @return integer The amount of files that were transfered from TAO to the ZIP archive within the method call.
137     */
138    public function addFile($src, $dest)
139    {
140        $zip = $this->getZip();
141        return tao_helpers_File::addFilesToZip($zip, $src, $dest);
142    }
143
144    abstract public function export();
145}