62 lines
1.9 KiB
PHP
62 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace OCA\Deckflow\Service;
|
|
|
|
use OCP\IDBConnection;
|
|
use OCP\IUserSession;
|
|
use OCA\Deck\Service\DeckService;
|
|
|
|
class WorkflowService {
|
|
|
|
private $db;
|
|
private $userSession;
|
|
private $deckService;
|
|
|
|
public function __construct(IDBConnection $db, IUserSession $userSession, DeckService $deckService) {
|
|
$this->db = $db;
|
|
$this->userSession = $userSession;
|
|
$this->deckService = $deckService;
|
|
}
|
|
|
|
// Speichert die Workflows
|
|
public function saveWorkflows($deckId, $sourceStackId, $targetStackId) {
|
|
$userId = $this->userSession->getUser()->getUID();
|
|
$this->db->executeQuery("INSERT INTO *PREFIX*deckflow_workflows (user_id, deck_id, source_stack_id, target_stack_id)
|
|
VALUES (?, ?, ?, ?)", [
|
|
$userId,
|
|
$deckId,
|
|
$sourceStackId,
|
|
$targetStackId
|
|
]);
|
|
}
|
|
|
|
// Ruft alle Workflows für den aktuellen Benutzer ab
|
|
public function getWorkflows() {
|
|
$userId = $this->userSession->getUser()->getUID();
|
|
$result = $this->db->executeQuery("SELECT * FROM *PREFIX*deckflow_workflows WHERE user_id = ?", [$userId]);
|
|
return $result->fetchAll();
|
|
}
|
|
|
|
// Webhook zur Ausführung des Workflows, Karten zu verschieben
|
|
public function moveOverdueCards($deckId, $sourceStackId, $targetStackId) {
|
|
$deck = $this->deckService->getDeckById($deckId);
|
|
$sourceStack = $deck->getStackById($sourceStackId);
|
|
$targetStack = $deck->getStackById($targetStackId);
|
|
|
|
foreach ($sourceStack->getCards() as $card) {
|
|
if ($this->isCardOverdue($card)) {
|
|
$this->moveCardToTargetStack($card, $targetStack);
|
|
}
|
|
}
|
|
}
|
|
|
|
private function isCardOverdue($card) {
|
|
$dueDate = $card->getDueDate();
|
|
return $dueDate && $dueDate < time();
|
|
}
|
|
|
|
private function moveCardToTargetStack($card, $targetStack) {
|
|
$this->deckService->moveCardToStack($card, $targetStack);
|
|
}
|
|
}
|