Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
70.37% |
19 / 27 |
|
55.56% |
5 / 9 |
CRAP | |
0.00% |
0 / 1 |
| GroupsService | |
70.37% |
19 / 27 |
|
55.56% |
5 / 9 |
17.40 | |
0.00% |
0 / 1 |
| getRootClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| deleteGroup | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| isGroupClass | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| getGroups | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| getUsers | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| addUser | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| removeUser | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| cloneInstance | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getTestTakerService | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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-2023 (update and modification) Open Assessment Technologies SA |
| 25 | */ |
| 26 | |
| 27 | namespace oat\taoGroups\models; |
| 28 | |
| 29 | use common_Exception; |
| 30 | use common_exception_Error; |
| 31 | use core_kernel_classes_Class; |
| 32 | use core_kernel_classes_Resource; |
| 33 | use oat\oatbox\user\User; |
| 34 | use oat\tao\model\OntologyClassService; |
| 35 | use oat\tao\model\TaoOntology; |
| 36 | use oat\taoTestTaker\models\TestTakerService; |
| 37 | |
| 38 | /** |
| 39 | * Service methods to manage the Groups business models using the RDF API. |
| 40 | * |
| 41 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 42 | * |
| 43 | * @access public |
| 44 | * |
| 45 | * @package taoGroups |
| 46 | */ |
| 47 | class GroupsService extends OntologyClassService |
| 48 | { |
| 49 | public const CLASS_URI = TaoOntology::CLASS_URI_GROUP; |
| 50 | |
| 51 | public const PROPERTY_MEMBERS_URI = 'http://www.tao.lu/Ontologies/TAOGroup.rdf#member'; |
| 52 | |
| 53 | /** |
| 54 | * Returns the group top level class. |
| 55 | */ |
| 56 | public function getRootClass(): core_kernel_classes_Class |
| 57 | { |
| 58 | return $this->getClass(self::CLASS_URI); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Deletes a group instance. |
| 63 | */ |
| 64 | public function deleteGroup(core_kernel_classes_Resource $group): bool |
| 65 | { |
| 66 | return $group !== null && $group->delete(true); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Check if a given class is a subclass of the Group root class. |
| 71 | */ |
| 72 | public function isGroupClass(core_kernel_classes_Class $clazz): bool |
| 73 | { |
| 74 | return $clazz->equals($this->getRootClass()) |
| 75 | || $clazz->isSubClassOf($this->getRootClass()); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get the groups of a user. |
| 80 | * |
| 81 | * @return core_kernel_classes_Resource[] Group resources |
| 82 | */ |
| 83 | public function getGroups(User $user): array |
| 84 | { |
| 85 | return array_map( |
| 86 | fn (string $group): core_kernel_classes_Resource => $this->getModel()->getResource($group), |
| 87 | $user->getPropertyValues(self::PROPERTY_MEMBERS_URI) |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Gets the users of a group. |
| 93 | * |
| 94 | * @return core_kernel_classes_Resource[] User resources |
| 95 | */ |
| 96 | public function getUsers(?string $groupUri): array |
| 97 | { |
| 98 | if (empty($groupUri)) { |
| 99 | return []; |
| 100 | } |
| 101 | |
| 102 | return $this->getTestTakerService()->getRootClass()->searchInstances( |
| 103 | [self::PROPERTY_MEMBERS_URI => $groupUri], |
| 104 | ['recursive' => true, 'like' => false] |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Adds a user to a Group. |
| 110 | */ |
| 111 | public function addUser(string $userUri, core_kernel_classes_Resource $group): bool |
| 112 | { |
| 113 | return $this->getModel()->getResource($userUri)->setPropertyValue( |
| 114 | $this->getModel()->getProperty(self::PROPERTY_MEMBERS_URI), |
| 115 | $group |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Removes a user from a Group. |
| 121 | */ |
| 122 | public function removeUser(string $userUri, core_kernel_classes_Resource $group): bool |
| 123 | { |
| 124 | return $this->getModel()->getResource($userUri)->removePropertyValue( |
| 125 | $this->getModel()->getProperty(self::PROPERTY_MEMBERS_URI), |
| 126 | $group |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Creates a duplicate of the given group instance into the given class, |
| 132 | * copying associations for former Test Takers and Deliveries to the new |
| 133 | * group. |
| 134 | * |
| 135 | * Test takers assigned to the group are not copied by the parent class |
| 136 | * method (but deliveries assigned to the group are) so, after the parent |
| 137 | * method has copied the former relations pointing to deliveries, this |
| 138 | * method assigns all test takers from the former group to the new one. |
| 139 | * |
| 140 | * @throws common_Exception |
| 141 | * @throws common_exception_Error |
| 142 | */ |
| 143 | public function cloneInstance( |
| 144 | core_kernel_classes_Resource $instance, |
| 145 | core_kernel_classes_Class $class = null |
| 146 | ): core_kernel_classes_Resource { |
| 147 | $newGroup = parent::cloneInstance($instance, $class); |
| 148 | |
| 149 | foreach ($this->getUsers($instance->getUri()) as $user) { |
| 150 | $this->addUser($user->getUri(), $newGroup); |
| 151 | } |
| 152 | |
| 153 | return $newGroup; |
| 154 | } |
| 155 | |
| 156 | private function getTestTakerService(): TestTakerService |
| 157 | { |
| 158 | return $this->getServiceManager()->getContainer()->get(TestTakerService::class); |
| 159 | } |
| 160 | } |