Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 58
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
tao_models_classes_ListService
0.00% covered (danger)
0.00%
0 / 58
0.00% covered (danger)
0.00%
0 / 9
702
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLists
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 getList
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 getListElement
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 getListElements
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
42
 removeList
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 removeListElement
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 createList
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 createListElement
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
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) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg
19 *                         (under the project TAO & TAO2);
20 *               2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung
21 *                         (under the project TAO-TRANSFER);
22 *               2009-2012 (update and modification) Public Research Centre Henri Tudor
23 *                         (under the project TAO-SUSTAIN & TAO-DEV);
24 *
25 */
26
27use oat\generis\model\GenerisRdf;
28use oat\tao\model\TaoOntology;
29
30/**
31 * This class provides the services on the list management
32 *
33 * @access public
34 * @author Joel Bout, <joel.bout@tudor.lu>
35 * @package tao
36
37 */
38class tao_models_classes_ListService extends tao_models_classes_GenerisService
39{
40    // --- ASSOCIATIONS ---
41
42
43    // --- ATTRIBUTES ---
44
45    /**
46     * the list parent class
47     *
48     * @access protected
49     * @var Class
50     */
51    protected $parentListClass = null;
52
53    // --- OPERATIONS ---
54
55    /**
56     * initialize the service
57     *
58     * @access protected
59     * @author Joel Bout, <joel.bout@tudor.lu>
60     * @return mixed
61     */
62    protected function __construct()
63    {
64        $this->parentListClass = new core_kernel_classes_Class(TaoOntology::CLASS_URI_LIST);
65    }
66
67    /**
68     * get all the lists class
69     *
70     * @access public
71     * @author Joel Bout, <joel.bout@tudor.lu>
72     * @return array
73     */
74    public function getLists()
75    {
76        $returnValue = [];
77
78        $returnValue[] = new core_kernel_classes_Class(GenerisRdf::GENERIS_BOOLEAN);
79
80        foreach ($this->parentListClass->getSubClasses(false) as $list) {
81            $returnValue[] = $list;
82        }
83
84        return (array) $returnValue;
85    }
86
87    /**
88     * Get a list class from the uri in parameter
89     *
90     * @access public
91     * @author Joel Bout, <joel.bout@tudor.lu>
92     * @param  string uri
93     * @return core_kernel_classes_Class
94     */
95    public function getList($uri)
96    {
97        $returnValue = null;
98
99        foreach ($this->getLists() as $list) {
100            if ($list->getUri() == $uri) {
101                $returnValue = $list;
102                break;
103            }
104        }
105
106        return $returnValue;
107    }
108
109    /**
110     * get the element of the list defined by listClass and identified by the
111     * in parameter
112     *
113     * @access public
114     * @author Joel Bout, <joel.bout@tudor.lu>
115     * @param  Class listClass
116     * @param  string uri
117     * @return core_kernel_classes_Resource
118     */
119    public function getListElement(core_kernel_classes_Class $listClass, $uri)
120    {
121        $returnValue = null;
122
123        if (!empty($uri)) {
124            foreach ($this->getListElements($listClass, false) as $element) {
125                if ($element->getUri() == $uri) {
126                    $returnValue = $element;
127                    break;
128                }
129            }
130        }
131
132        return $returnValue;
133    }
134
135    /**
136     * get all the elements of the list
137     *
138     * @access public
139     *
140     * @param core_kernel_classes_Class $listClass
141     * @param boolean sort
142     * @param int                       $limit
143     *
144     * @return array
145     * @throws core_kernel_persistence_Exception
146     * @author Joel Bout, <joel.bout@tudor.lu>
147     */
148    public function getListElements(core_kernel_classes_Class $listClass, $sort = true, $limit = 0)
149    {
150        $returnValue = [];
151
152        $parameters = $limit === 0 ? [] : ['limit' => $limit];
153
154        if ($sort) {
155            $levelProperty = new core_kernel_classes_Property(TaoOntology::PROPERTY_LIST_LEVEL);
156            foreach ($listClass->getInstances(false, $parameters) as $element) {
157                $literal = $element->getOnePropertyValue($levelProperty);
158                $level = is_null($literal) ? 0 : (string) $literal;
159                while (isset($returnValue[$level])) {
160                    $level++;
161                }
162                $returnValue[$level] = $element;
163            }
164            uksort($returnValue, 'strnatcasecmp');
165        } else {
166            $returnValue = $listClass->getInstances(false, $parameters);
167        }
168
169        return (array) $returnValue;
170    }
171
172    /**
173     * remove a list and it's elements
174     *
175     * @access public
176     * @author Joel Bout, <joel.bout@tudor.lu>
177     * @param  Class listClass
178     * @return boolean
179     */
180    public function removeList(core_kernel_classes_Class $listClass)
181    {
182        $returnValue = (bool) false;
183
184        if (!is_null($listClass)) {
185            foreach ($this->getListElements($listClass) as $element) {
186                $this->removeListElement($element);
187            }
188            $returnValue = $listClass->delete();
189        }
190
191        return (bool) $returnValue;
192    }
193
194    /**
195     * remove a list element
196     *
197     * @access public
198     * @author Joel Bout, <joel.bout@tudor.lu>
199     * @param  Resource element
200     * @return boolean
201     */
202    public function removeListElement(core_kernel_classes_Resource $element)
203    {
204        $returnValue = (bool) false;
205
206        if (!is_null($element)) {
207            $returnValue = $element->delete();
208        }
209
210        return (bool) $returnValue;
211    }
212
213    /**
214     * create a new list class
215     *
216     * @access public
217     * @author Joel Bout, <joel.bout@tudor.lu>
218     * @param  string label
219     * @return core_kernel_classes_Class
220     */
221    public function createList($label = '')
222    {
223        $returnValue = null;
224
225        if (empty($label)) {
226            $label = __('List') . ' ' . (count($this->getLists()) + 1);
227        }
228        $returnValue = $this->createSubClass($this->parentListClass, $label);
229
230        return $returnValue;
231    }
232
233    /**
234     * add a new element to a list
235     *
236     * @access public
237     * @author Joel Bout, <joel.bout@tudor.lu>
238     * @param  Class listClass
239     * @param  string label
240     * @return core_kernel_classes_Resource
241     */
242    public function createListElement(core_kernel_classes_Class $listClass, $label = '')
243    {
244        $returnValue = null;
245
246        if (!is_null($listClass)) {
247            $level = count($this->getListElements($listClass)) + 1;
248            if (empty($label)) {
249                $label = __('Element') . ' ' . $level;
250            }
251            $returnValue = $this->createInstance($listClass, $label);
252            $this->bindProperties(
253                $returnValue,
254                [TaoOntology::PROPERTY_LIST_LEVEL => count($this->getListElements($listClass, false))]
255            );
256        }
257
258        return $returnValue;
259    }
260}