Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
5.17% |
3 / 58 |
|
20.00% |
2 / 10 |
CRAP | |
0.00% |
0 / 1 |
| TimeRule | |
5.17% |
3 / 58 |
|
20.00% |
2 / 10 |
434.71 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setRequiredAction | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| check | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| completed | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| __toPhpCode | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| checkTime | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
42 | |||
| getExecutionTime | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| getInterval | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| getUser | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getActionExecution | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| 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; |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\tao\model\requiredAction\implementation; |
| 24 | |
| 25 | use oat\tao\model\requiredAction\RequiredActionRuleInterface; |
| 26 | use oat\tao\model\requiredAction\RequiredActionInterface; |
| 27 | use DateTime; |
| 28 | use DateInterval; |
| 29 | use oat\oatbox\user\User; |
| 30 | |
| 31 | /** |
| 32 | * Class TimeRule |
| 33 | * @author Aleh Hutnilau <hutnikau@1pt.com> |
| 34 | */ |
| 35 | class TimeRule implements RequiredActionRuleInterface |
| 36 | { |
| 37 | public const CLASS_URI = 'http://www.tao.lu/Ontologies/TAO.rdf#RequiredActionTimeRule'; |
| 38 | public const PROPERTY_NAME = 'http://www.tao.lu/Ontologies/TAO.rdf#RequiredActionName'; |
| 39 | public const PROPERTY_EXECUTION_TIME = 'http://www.tao.lu/Ontologies/TAO.rdf#RequiredActionExecutionTime'; |
| 40 | public const PROPERTY_SUBJECT = 'http://www.tao.lu/Ontologies/TAO.rdf#RequiredActionSubject'; |
| 41 | |
| 42 | /** |
| 43 | * @var string|DateTime |
| 44 | */ |
| 45 | protected $executionTime; |
| 46 | |
| 47 | /** |
| 48 | * @var string|DateInterval |
| 49 | */ |
| 50 | protected $interval; |
| 51 | |
| 52 | /** |
| 53 | * @var RequiredActionInterface |
| 54 | */ |
| 55 | protected $requiredAction; |
| 56 | |
| 57 | /** |
| 58 | * TimeRule constructor. |
| 59 | * @param DateInterval|null $interval Interval to specify how often action should be performed |
| 60 | * @param DateTime|null $executionTime Time when the action was executed last time |
| 61 | */ |
| 62 | public function __construct(DateInterval $interval = null, DateTime $executionTime = null) |
| 63 | { |
| 64 | $this->interval = $interval; |
| 65 | $this->executionTime = $executionTime; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Set required action instance |
| 70 | * @param \oat\tao\model\requiredAction\RequiredActionInterface $requiredAction |
| 71 | * @return null |
| 72 | */ |
| 73 | public function setRequiredAction(RequiredActionInterface $requiredAction) |
| 74 | { |
| 75 | $this->requiredAction = $requiredAction; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Check the rule. |
| 80 | * @return bool |
| 81 | */ |
| 82 | public function check() |
| 83 | { |
| 84 | return $this->checkTime(); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Mark rule as executed and save time of completed. |
| 89 | * @return \core_kernel_classes_Resource |
| 90 | */ |
| 91 | public function completed() |
| 92 | { |
| 93 | $resource = $this->getActionExecution(); |
| 94 | if ($resource === null) { |
| 95 | $requiredActionClass = new \core_kernel_classes_Class(self::CLASS_URI); |
| 96 | $resource = $requiredActionClass->createInstanceWithProperties([ |
| 97 | self::PROPERTY_SUBJECT => $this->getUser()->getIdentifier(), |
| 98 | self::PROPERTY_NAME => $this->requiredAction->getName(), |
| 99 | self::PROPERTY_EXECUTION_TIME => time(), |
| 100 | ]); |
| 101 | } |
| 102 | $timeProperty = (new \core_kernel_classes_Property(self::PROPERTY_EXECUTION_TIME)); |
| 103 | $resource->editPropertyValues($timeProperty, time()); |
| 104 | return $resource; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @see \oat\oatbox\PhpSerializable::__toPhpCode() |
| 109 | */ |
| 110 | public function __toPhpCode() |
| 111 | { |
| 112 | $class = get_class($this); |
| 113 | $interval = $this->getInterval(); |
| 114 | $serializedInterval = 'new \DateInterval("' . $interval->format('P%yY%mM%dDT%hH%iM%sS') . '")'; |
| 115 | |
| 116 | return "new $class( |
| 117 | $serializedInterval |
| 118 | )"; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Check if it is time to perform an action. |
| 123 | * If `$this->lastExecution` is null (action has never been executed) |
| 124 | * or since the last execution took time more than specified interval (`$this->interval`) then action must be |
| 125 | * performed. |
| 126 | * @return bool |
| 127 | */ |
| 128 | protected function checkTime() |
| 129 | { |
| 130 | $result = false; |
| 131 | |
| 132 | $lastExecution = $this->getExecutionTime(); |
| 133 | $interval = $this->getInterval(); |
| 134 | $anonymous = \common_session_SessionManager::isAnonymous(); |
| 135 | |
| 136 | if ($lastExecution === null && !$anonymous) { |
| 137 | $result = true; |
| 138 | } elseif ($lastExecution !== null && $interval !== null && !$anonymous) { |
| 139 | $mustBeExecutedAt = clone($lastExecution); |
| 140 | $mustBeExecutedAt->add($interval); |
| 141 | $now = new DateTime('now'); |
| 142 | $result = ($mustBeExecutedAt < $now); |
| 143 | } |
| 144 | |
| 145 | return $result; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Get last execution time. If an action was not executed before returns `null` |
| 150 | * @return DateTime|null |
| 151 | */ |
| 152 | protected function getExecutionTime() |
| 153 | { |
| 154 | if ($this->executionTime === null) { |
| 155 | $resource = $this->getActionExecution(); |
| 156 | |
| 157 | if ($resource !== null) { |
| 158 | /** @var \core_kernel_classes_Resource $resource */ |
| 159 | $time = (string) $resource->getOnePropertyValue( |
| 160 | new \core_kernel_classes_Property(self::PROPERTY_EXECUTION_TIME) |
| 161 | ); |
| 162 | if (!empty($time)) { |
| 163 | $this->executionTime = new DateTime('@' . $time); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | return $this->executionTime; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * @return DateInterval|null |
| 172 | */ |
| 173 | protected function getInterval() |
| 174 | { |
| 175 | if (is_string($this->interval)) { |
| 176 | $this->interval = new DateInterval($this->interval); |
| 177 | } |
| 178 | return $this->interval instanceof DateInterval ? $this->interval : null; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * @return User |
| 183 | */ |
| 184 | protected function getUser() |
| 185 | { |
| 186 | $user = \common_session_SessionManager::getSession()->getUser(); |
| 187 | return $user; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * @return \core_kernel_classes_Resource|null |
| 192 | */ |
| 193 | protected function getActionExecution() |
| 194 | { |
| 195 | $result = null; |
| 196 | $requiredActionClass = new \core_kernel_classes_Class(self::CLASS_URI); |
| 197 | $resources = $requiredActionClass->searchInstances([ |
| 198 | self::PROPERTY_NAME => $this->requiredAction->getName(), |
| 199 | self::PROPERTY_SUBJECT => $this->getUser()->getIdentifier(), |
| 200 | ], [ |
| 201 | 'like' => false, |
| 202 | ]); |
| 203 | |
| 204 | if (!empty($resources)) { |
| 205 | /** @var \core_kernel_classes_Resource $resource */ |
| 206 | $result = current($resources); |
| 207 | } |
| 208 | return $result; |
| 209 | } |
| 210 | } |