Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 69
0.00% covered (danger)
0.00%
0 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
AssignmentFactory
0.00% covered (danger)
0.00%
0 / 69
0.00% covered (danger)
0.00%
0 / 11
930
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getDeliveryId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getUserId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLabel
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDescription
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
56
 getStartable
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getStartTime
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getDeliveryOrder
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 buildDescriptionFromData
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
156
 toAssignment
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 __equals
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) 2015 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
19 *
20 */
21
22namespace oat\taoDeliveryRdf\model;
23
24use oat\oatbox\user\User;
25use core_kernel_classes_Property;
26use oat\taoDelivery\model\AttemptServiceInterface;
27use tao_helpers_Date;
28use Zend\ServiceManager\ServiceLocatorAwareInterface;
29use Zend\ServiceManager\ServiceLocatorAwareTrait;
30
31/**
32 * Service to manage the assignment of users to deliveries
33 *
34 * @access public
35 * @author Joel Bout, <joel@taotesting.com>
36 * @package taoDelivery
37 */
38class AssignmentFactory implements ServiceLocatorAwareInterface
39{
40    use ServiceLocatorAwareTrait;
41
42    protected $delivery;
43
44    private $user;
45
46    private $startable;
47
48    private $displayAttempts;
49
50    private $displayDates;
51
52    public function __construct(
53        \core_kernel_classes_Resource $delivery,
54        User $user,
55        $startable,
56        $displayAttempts = true,
57        $displayDates = true
58    ) {
59        $this->delivery = $delivery;
60        $this->user = $user;
61        $this->startable = $startable;
62        $this->displayAttempts = $displayAttempts;
63        $this->displayDates = $displayDates;
64    }
65
66    public function getDeliveryId()
67    {
68        return $this->delivery->getUri();
69    }
70
71    protected function getUserId()
72    {
73        return $this->user->getIdentifier();
74    }
75
76    protected function getLabel()
77    {
78        return $this->delivery->getLabel();
79    }
80
81    protected function getDescription()
82    {
83        $deliveryProps = $this->delivery->getPropertiesValues([
84            new core_kernel_classes_Property(DeliveryContainerService::PROPERTY_MAX_EXEC),
85            new core_kernel_classes_Property(DeliveryContainerService::PROPERTY_START),
86            new core_kernel_classes_Property(DeliveryContainerService::PROPERTY_END),
87        ]);
88
89        $propMaxExec = current($deliveryProps[DeliveryContainerService::PROPERTY_MAX_EXEC]);
90        $propStartExec = current($deliveryProps[DeliveryContainerService::PROPERTY_START]);
91        $propEndExec = current($deliveryProps[DeliveryContainerService::PROPERTY_END]);
92
93        $startTime = (!(is_object($propStartExec)) or ($propStartExec == "")) ? null : $propStartExec->literal;
94        $endTime = (!(is_object($propEndExec)) or ($propEndExec == "")) ? null : $propEndExec->literal;
95        $maxExecs = (!(is_object($propMaxExec)) or ($propMaxExec == "")) ? 0 : $propMaxExec->literal;
96
97        $countExecs = count($this->getServiceLocator()->get(AttemptServiceInterface::SERVICE_ID)
98            ->getAttempts($this->delivery->getUri(), $this->user));
99
100        return $this->buildDescriptionFromData($startTime, $endTime, $countExecs, $maxExecs);
101    }
102
103    protected function getStartable()
104    {
105        return $this->startable;
106    }
107
108    public function getStartTime()
109    {
110        $prop = $this->delivery->getOnePropertyValue(
111            new core_kernel_classes_Property(DeliveryContainerService::PROPERTY_START)
112        );
113        return is_null($prop) ? null : (string)$prop;
114    }
115
116    public function getDeliveryOrder()
117    {
118        $prop = $this->delivery->getOnePropertyValue(
119            new core_kernel_classes_Property(DeliveryAssemblyService::PROPERTY_DELIVERY_DISPLAY_ORDER_PROP)
120        );
121        return is_null($prop) ? 0 : intval((string)$prop);
122    }
123
124    protected function buildDescriptionFromData($startTime, $endTime, $countExecs, $maxExecs)
125    {
126        $descriptions = [];
127
128        if ($this->displayDates) {
129            if (!empty($startTime) && !empty($endTime)) {
130                $descriptions[] = __(
131                    'Available from %1$s to %2$s',
132                    tao_helpers_Date::displayeDate($startTime),
133                    tao_helpers_Date::displayeDate($endTime)
134                );
135            } elseif (!empty($startTime) && empty($endTime)) {
136                $descriptions[] = __('Available from %s', tao_helpers_Date::displayeDate($startTime));
137            } elseif (!empty($endTime)) {
138                $descriptions[] = __('Available until %s', tao_helpers_Date::displayeDate($endTime));
139            }
140        }
141
142        if ($maxExecs != 0 && $this->displayAttempts) {
143            if ($maxExecs == 1) {
144                $descriptions[] = __(
145                    'Attempt %1$s of %2$s',
146                    $countExecs,
147                    !empty($maxExecs)
148                    ? $maxExecs
149                    : __('unlimited')
150                );
151            } else {
152                $descriptions[] = __(
153                    'Attempts %1$s of %2$s',
154                    $countExecs,
155                    !empty($maxExecs)
156                    ? $maxExecs
157                    : __('unlimited')
158                );
159            }
160        }
161        return $descriptions;
162    }
163
164    public function toAssignment()
165    {
166        return new Assignment(
167            $this->getDeliveryId(),
168            $this->getUserId(),
169            $this->getLabel(),
170            $this->getDescription(),
171            $this->getStartable(),
172            $this->getDeliveryOrder()
173        );
174    }
175
176    public function __equals(AssignmentFactory $factory)
177    {
178        return $this->getDeliveryId() == $factory->getDeliveryId();
179    }
180}