| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2025 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* Worker helper trait to use for status change behaviors. |
| 16 |
* |
| 17 |
* @since 1.18 (J) - 1.8 (WP) |
| 18 |
*/ |
| 19 |
trait VBOTaskStatusHelperWorker |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Mark the task as in progress. |
| 23 |
* |
| 24 |
* @param VBOTaskTaskregistry $task |
| 25 |
* |
| 26 |
* @return void |
| 27 |
*/ |
| 28 |
public function startWork(VBOTaskTaskregistry $task) |
| 29 |
{ |
| 30 |
$now = JFactory::getDate('now', JFactory::getApplication()->get('offset')); |
| 31 |
|
| 32 |
$data = [ |
| 33 |
'id' => $task->getID(), |
| 34 |
'workstartedon' => $now->toSql($local = false), |
| 35 |
'finishedby' => 0, |
| 36 |
]; |
| 37 |
|
| 38 |
if (!$task->get('beganon')) { |
| 39 |
// work started right now |
| 40 |
$data['beganon'] = $now->toSql($local = true); |
| 41 |
} |
| 42 |
|
| 43 |
if (!$task->get('beganby')) { |
| 44 |
// work started by the current user |
| 45 |
$data['beganby'] = $this->getWorkerID(); |
| 46 |
} |
| 47 |
|
| 48 |
try { |
| 49 |
// update the task |
| 50 |
VBOTaskModelTask::getInstance()->update($data); |
| 51 |
} catch (Exception $error) { |
| 52 |
// ignore and go ahead |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Mark the task as in pause. |
| 58 |
* |
| 59 |
* @param VBOTaskTaskregistry $task |
| 60 |
* |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
public function pauseWork(VBOTaskTaskregistry $task) |
| 64 |
{ |
| 65 |
try { |
| 66 |
// update the task |
| 67 |
VBOTaskModelTask::getInstance()->update([ |
| 68 |
'id' => $task->getID(), |
| 69 |
'workstartedon' => JFactory::getDate()->toSql($local = false), |
| 70 |
'realduration' => $this->calculateTotalDuration($task), |
| 71 |
]); |
| 72 |
} catch (Exception $error) { |
| 73 |
// ignore and go ahead |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Mark the task as finished. |
| 79 |
* |
| 80 |
* @param VBOTaskTaskregistry $task |
| 81 |
* |
| 82 |
* @return void |
| 83 |
*/ |
| 84 |
public function finishWork(VBOTaskTaskregistry $task) |
| 85 |
{ |
| 86 |
$now = JFactory::getDate('now', JFactory::getApplication()->get('offset')); |
| 87 |
|
| 88 |
$data = [ |
| 89 |
'id' => $task->getID(), |
| 90 |
'workstartedon' => $now->toSql($local = false), |
| 91 |
'realduration' => $this->calculateTotalDuration($task), |
| 92 |
]; |
| 93 |
|
| 94 |
if (!$task->get('finishedon')) { |
| 95 |
// work finished right now |
| 96 |
$data['finishedon'] = $now->toSql($local = true); |
| 97 |
} |
| 98 |
|
| 99 |
if (!$task->get('finishedby')) { |
| 100 |
// work finished by the current user |
| 101 |
$data['finishedby'] = $this->getWorkerID(); |
| 102 |
} |
| 103 |
|
| 104 |
try { |
| 105 |
// update the task |
| 106 |
VBOTaskModelTask::getInstance()->update($data); |
| 107 |
} catch (Exception $error) { |
| 108 |
// ignore and go ahead |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Displays the elsapsed duration for the ongoing work. |
| 114 |
* |
| 115 |
* @param VBOTaskTaskregistry $task |
| 116 |
* @param bool $paused |
| 117 |
* |
| 118 |
* @return string |
| 119 |
*/ |
| 120 |
public function displayOngoingWork(VBOTaskTaskregistry $task, bool $paused = false) |
| 121 |
{ |
| 122 |
if ($paused) { |
| 123 |
// use elapsed duration |
| 124 |
$duration = $task->get('realduration'); |
| 125 |
} else { |
| 126 |
// calculate duration on the fly |
| 127 |
$duration = $this->calculateTotalDuration($task); |
| 128 |
} |
| 129 |
|
| 130 |
if ($duration < 60) { |
| 131 |
return JText::translate('VBO_TASK_STATUS_DISPLAY_STARTED'); |
| 132 |
} |
| 133 |
|
| 134 |
return JText::sprintf('VBO_TASK_STATUS_DISPLAY_ONGOING', $this->formatSeconds($duration)); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Displays the elsapsed duration for the finished work. |
| 139 |
* |
| 140 |
* @param VBOTaskTaskregistry $task |
| 141 |
* |
| 142 |
* @return string |
| 143 |
*/ |
| 144 |
public function displayFinishedWork(VBOTaskTaskregistry $task) |
| 145 |
{ |
| 146 |
// use elapsed duration |
| 147 |
$duration = $task->get('realduration'); |
| 148 |
|
| 149 |
if ($duration < 60) { |
| 150 |
// do not display anything in case the duration is less than a minute |
| 151 |
return ''; |
| 152 |
} |
| 153 |
|
| 154 |
return JText::sprintf('VBO_TASK_STATUS_DISPLAY_FINISHED', $this->formatSeconds($duration)); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Calculate the total elapsed seconds since the work started. |
| 159 |
* |
| 160 |
* @param VBOTaskTaskregistry $task |
| 161 |
* |
| 162 |
* @return int |
| 163 |
*/ |
| 164 |
protected function calculateTotalDuration(VBOTaskTaskregistry $task) |
| 165 |
{ |
| 166 |
$duration = (int) $task->get('realduration', 0); |
| 167 |
|
| 168 |
if ($task->get('finishedby')) { |
| 169 |
// task already finished, do not calculate extra time |
| 170 |
return $duration; |
| 171 |
} |
| 172 |
|
| 173 |
// obtain the date and time when the work started |
| 174 |
$workStartedOn = $task->get('workstartedon'); |
| 175 |
|
| 176 |
if (!$workStartedOn) { |
| 177 |
// work never started, use the began date and time |
| 178 |
$workStartedOn = $task->get('beganon'); |
| 179 |
} |
| 180 |
|
| 181 |
if (!$workStartedOn) { |
| 182 |
// work never began, use the due date and time |
| 183 |
$workStartedOn = $task->get('dueon'); |
| 184 |
} |
| 185 |
|
| 186 |
if ($workStartedOn) { |
| 187 |
$workStartedOn = JFactory::getDate($workStartedOn); |
| 188 |
$now = JFactory::getDate('now'); |
| 189 |
|
| 190 |
// calculate the difference between the current time and the start working time |
| 191 |
if ($now > $workStartedOn) { |
| 192 |
$diff = $workStartedOn->diff($now); |
| 193 |
$duration += $diff->s + $diff->i * 60 + $diff->h * 3600 + $diff->days * 86400; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
return $duration; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Returns the identifier of the currently logged-in user. |
| 202 |
* |
| 203 |
* @return int The user ID. |
| 204 |
*/ |
| 205 |
protected function getWorkerID() |
| 206 |
{ |
| 207 |
if (JFactory::getApplication()->isClient('administrator')) { |
| 208 |
// return the CMS user ID |
| 209 |
return JFactory::getUser()->id; |
| 210 |
} |
| 211 |
|
| 212 |
// get logged in operator |
| 213 |
$operator = VikBooking::getOperatorInstance()->getOperatorAccount(); |
| 214 |
|
| 215 |
if (!$operator) { |
| 216 |
// return the CMS user ID, if any |
| 217 |
return JFactory::getUser()->id; |
| 218 |
} |
| 219 |
|
| 220 |
return (int) $operator['id']; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Helper method to format the specified seconds to the closest unit. |
| 225 |
* For example, 150 minutes will be formatted as "2 hours, 30 minutes". |
| 226 |
* |
| 227 |
* @param int $seconds |
| 228 |
* |
| 229 |
* @return string |
| 230 |
*/ |
| 231 |
protected function formatSeconds(int $seconds) |
| 232 |
{ |
| 233 |
// convert seconds in minutes |
| 234 |
$minutes = floor($seconds / 60); |
| 235 |
|
| 236 |
$units = []; |
| 237 |
|
| 238 |
while ($minutes >= 60) { |
| 239 |
if ($minutes >= 1440) { |
| 240 |
// calculate days |
| 241 |
$units[] = JText::plural('VBO_N_DAYS', floor($minutes / 1440)); |
| 242 |
|
| 243 |
// recalculate remaining minutes |
| 244 |
$minutes = $minutes % 1440; |
| 245 |
} else { |
| 246 |
// calculate hours |
| 247 |
$units[] = JText::plural('VBO_N_HOURS', floor($minutes / 60)); |
| 248 |
|
| 249 |
// recalculate remaining minutes |
| 250 |
$minutes = $minutes % 60; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
if ($minutes > 0) { |
| 255 |
$units[] = JText::plural('VBO_N_MINUTES', $minutes); |
| 256 |
} |
| 257 |
|
| 258 |
return implode(', ', $units); |
| 259 |
} |
| 260 |
} |
| 261 |
|