Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
TestTakerService
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 9
240
0.00% covered (danger)
0.00%
0 / 1
 getRootClass
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 createInstance
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 createSubClass
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 deleteResource
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 deleteClass
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 deleteSubject
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 isSubjectClass
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 setTestTakerRole
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 cloneInstance
0.00% covered (danger)
0.00%
0 / 9
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 *               2013-2014 (update and modification) Open Assessment Technologies SA
25 */
26
27namespace oat\taoTestTaker\models;
28
29use core_kernel_classes_Class;
30use core_kernel_classes_Resource;
31use oat\generis\model\GenerisRdf;
32use oat\oatbox\event\EventManagerAwareTrait;
33use oat\tao\model\TaoOntology;
34use oat\taoTestTaker\models\events\TestTakerClassCreatedEvent;
35use oat\taoTestTaker\models\events\TestTakerClassRemovedEvent;
36use oat\taoTestTaker\models\events\TestTakerCreatedEvent;
37use oat\taoTestTaker\models\events\TestTakerRemovedEvent;
38use oat\tao\model\OntologyClassService;
39
40/**
41 * Service methods to manage the Subjects business models using the RDF API.
42 *
43 * @access public
44 * @author Joel Bout, <joel.bout@tudor.lu>
45 */
46class TestTakerService extends OntologyClassService
47{
48    use EventManagerAwareTrait;
49
50    public const CLASS_URI_SUBJECT = 'http://www.tao.lu/Ontologies/TAOSubject.rdf#Subject';
51
52    public const ROLE_SUBJECT_MANAGER = 'http://www.tao.lu/Ontologies/TAOSubject.rdf#SubjectsManagerRole';
53
54    /**
55     * @return core_kernel_classes_Class|null
56     */
57    public function getRootClass()
58    {
59        return $this->getClass(TaoOntology::SUBJECT_CLASS_URI);
60    }
61
62    /**
63     * @param core_kernel_classes_Class $clazz
64     * @param string $label
65     * @return core_kernel_classes_Resource
66     */
67    public function createInstance(core_kernel_classes_Class $clazz, $label = '')
68    {
69        $instance = parent::createInstance($clazz, $label);
70
71        $this->getEventManager()->trigger(new TestTakerCreatedEvent($instance->getUri()));
72
73        return $instance;
74    }
75
76    /**
77     * @param core_kernel_classes_Class $parentClazz
78     * @param string $label
79     * @return core_kernel_classes_Class
80     */
81    public function createSubClass(core_kernel_classes_Class $parentClazz, $label = '')
82    {
83        $subClass = parent::createSubClass($parentClazz, $label);
84
85        $this->getEventManager()->trigger(new TestTakerClassCreatedEvent($subClass->getUri()));
86
87        return $subClass;
88    }
89
90    /**
91     * @param core_kernel_classes_Resource $resource
92     * @return bool
93     */
94    public function deleteResource(core_kernel_classes_Resource $resource)
95    {
96        $this->getEventManager()->trigger(new TestTakerRemovedEvent($resource->getUri()));
97
98        return parent::deleteResource($resource);
99    }
100
101    /**
102     * @param core_kernel_classes_Class $clazz
103     * @return bool
104     */
105    public function deleteClass(core_kernel_classes_Class $clazz)
106    {
107        $this->getEventManager()->trigger(new TestTakerClassRemovedEvent($clazz->getUri()));
108
109        return parent::deleteClass($clazz);
110    }
111
112    /**
113     * delete a subject instance
114     *
115     * @access public
116     * @author Joel Bout, <joel.bout@tudor.lu>
117     * @param \core_kernel_classes_Resource $subject
118     * @return boolean
119     */
120    public function deleteSubject(\core_kernel_classes_Resource $subject)
121    {
122        $returnValue = (bool) false;
123
124        if (! is_null($subject)) {
125            $this->getEventManager()->trigger(new TestTakerRemovedEvent($subject->getUri()));
126            $returnValue = $subject->delete();
127        }
128
129        return (bool) $returnValue;
130    }
131
132    /**
133     * Check if the Class in parameter is a subclass of Subject
134     *
135     * @access public
136     * @author Joel Bout, <joel.bout@tudor.lu>
137     * @param \core_kernel_classes_Class $clazz
138     * @return boolean
139     */
140    public function isSubjectClass(\core_kernel_classes_Class $clazz)
141    {
142        $returnValue = (bool) false;
143
144        if ($clazz->getUri() == $this->getRootClass()->getUri()) {
145            $returnValue = true;
146        } else {
147            foreach ($this->getRootClass()->getSubClasses(true) as $subclass) {
148                if ($clazz->getUri() == $subclass->getUri()) {
149                    $returnValue = true;
150                    break;
151                }
152            }
153        }
154
155        return (bool) $returnValue;
156    }
157
158    /**
159     * Set the proper role to the testTaker
160     *
161     * @author Lionel Lecaque, lionel@taotesting.com
162     * @param \core_kernel_classes_Resource $instance
163     */
164    public function setTestTakerRole(\core_kernel_classes_Resource $instance)
165    {
166        $roleProperty = new \core_kernel_classes_Property(GenerisRdf::PROPERTY_USER_ROLES);
167        $subjectRole = new \core_kernel_classes_Resource(TaoOntology::PROPERTY_INSTANCE_ROLE_DELIVERY);
168        $instance->setPropertyValue($roleProperty, $subjectRole);
169    }
170
171    /**
172     * Short description of method cloneInstance
173     *
174     * @access public
175     * @author Joel Bout, <joel.bout@tudor.lu>
176     * @param \core_kernel_classes_Resource $instance
177     * @param \core_kernel_classes_Class $clazz
178     * @throws \common_Exception
179     * @throws \core_kernel_classes_EmptyProperty
180     * @return core_kernel_classes_Resource
181     */
182    public function cloneInstance(\core_kernel_classes_Resource $instance, \core_kernel_classes_Class $clazz = null)
183    {
184        $loginProperty = new \core_kernel_classes_Property(GenerisRdf::PROPERTY_USER_LOGIN);
185        $login = $instance->getUniquePropertyValue($loginProperty);
186
187        $returnValue = parent::cloneInstance($instance, $clazz);
188        $userService = \tao_models_classes_UserService::singleton();
189        try {
190            while ($userService->loginExists($login)) {
191                $login .= (string) rand(0, 9);
192            }
193
194            $returnValue->editPropertyValues($loginProperty, $login);
195        } catch (common_Exception $ce) {
196            // empty
197        }
198
199        return $returnValue;
200    }
201}