Implemented that chores can be assigned to users (closes #253)

This commit is contained in:
Bernd Bestel
2019-09-17 13:13:26 +02:00
parent 3dcd513094
commit 74f9470769
65 changed files with 987 additions and 75 deletions

View File

@@ -4,11 +4,16 @@ namespace Grocy\Services;
class ChoresService extends BaseService
{
const CHORE_TYPE_MANUALLY = 'manually';
const CHORE_TYPE_DYNAMIC_REGULAR = 'dynamic-regular';
const CHORE_TYPE_DAILY = 'daily';
const CHORE_TYPE_weekly = 'weekly';
const CHORE_TYPE_monthly = 'monthly';
const CHORE_PERIOD_TYPE_MANUALLY = 'manually';
const CHORE_PERIOD_TYPE_DYNAMIC_REGULAR = 'dynamic-regular';
const CHORE_PERIOD_TYPE_DAILY = 'daily';
const CHORE_PERIOD_TYPE_WEEKLY = 'weekly';
const CHORE_PERIOD_TYPE_MONTHLY = 'monthly';
const CHORE_ASSIGNMENT_TYPE_NO_ASSIGNMENT = 'no-assignment';
const CHORE_ASSIGNMENT_TYPE_WHO_LEAST_DID_FIRST = 'who-least-did-first';
const CHORE_ASSIGNMENT_TYPE_RANDOM = 'random';
const CHORE_ASSIGNMENT_TYPE_IN_ALPHABETICAL_ORDER = 'in-alphabetical-order';
public function GetCurrent()
{
@@ -23,26 +28,34 @@ class ChoresService extends BaseService
throw new \Exception('Chore does not exist');
}
$usersService = new UsersService();
$users = $usersService->GetUsersAsDto();
$chore = $this->Database->chores($choreId);
$choreTrackedCount = $this->Database->chores_log()->where('chore_id = :1 AND undone = 0', $choreId)->count();
$choreLastTrackedTime = $this->Database->chores_log()->where('chore_id = :1 AND undone = 0', $choreId)->max('tracked_time');
$nextExeuctionTime = $this->Database->chores_current()->where('chore_id', $choreId)->min('next_estimated_execution_time');
$nextExecutionTime = $this->Database->chores_current()->where('chore_id', $choreId)->min('next_estimated_execution_time');
$lastChoreLogRow = $this->Database->chores_log()->where('chore_id = :1 AND tracked_time = :2 AND undone = 0', $choreId, $choreLastTrackedTime)->fetch();
$lastDoneByUser = null;
if ($lastChoreLogRow !== null && !empty($lastChoreLogRow))
{
$usersService = new UsersService();
$users = $usersService->GetUsersAsDto();
$lastDoneByUser = FindObjectInArrayByPropertyValue($users, 'id', $lastChoreLogRow->done_by_user_id);
}
$nextExecutionAssignedUser = null;
if (!empty($chore->next_execution_assigned_to_user_id))
{
$nextExecutionAssignedUser = FindObjectInArrayByPropertyValue($users, 'id', $chore->next_execution_assigned_to_user_id);
}
return array(
'chore' => $chore,
'last_tracked' => $choreLastTrackedTime,
'tracked_count' => $choreTrackedCount,
'last_done_by' => $lastDoneByUser,
'next_estimated_execution_time' => $nextExeuctionTime
'next_estimated_execution_time' => $nextExecutionTime,
'next_execution_assigned_user' => $nextExecutionAssignedUser
);
}
@@ -72,7 +85,9 @@ class ChoresService extends BaseService
));
$logRow->save();
return $this->Database->lastInsertId();
$lastInsertId = $this->Database->lastInsertId();
$this->CalculateNextExecutionAssignment($choreId);
return $lastInsertId;
}
private function ChoreExists($choreId)
@@ -95,4 +110,86 @@ class ChoresService extends BaseService
'undone_timestamp' => date('Y-m-d H:i:s')
));
}
public function CalculateNextExecutionAssignment($choreId)
{
if (!$this->ChoreExists($choreId))
{
throw new \Exception('Chore does not exist');
}
$chore = $this->Database->chores($choreId);
$choreLastTrackedTime = $this->Database->chores_log()->where('chore_id = :1 AND undone = 0', $choreId)->max('tracked_time');
$lastChoreLogRow = $this->Database->chores_log()->where('chore_id = :1 AND tracked_time = :2 AND undone = 0', $choreId, $choreLastTrackedTime)->fetch();
$lastDoneByUserId = $lastChoreLogRow->done_by_user_id;
$usersService = new UsersService();
$users = $usersService->GetUsersAsDto();
$assignedUsers = array();
foreach ($users as $user)
{
if (in_array($user->id, explode(',', $chore->assignment_config)))
{
$assignedUsers[] = $user;
}
}
$nextExecutionUserId = null;
if ($chore->assignment_type == self::CHORE_ASSIGNMENT_TYPE_RANDOM)
{
// Random assignment and only 1 user in the group? Well, ok - will be hard to guess the next one...
if (count($assignedUsers) == 1)
{
$nextExecutionUserId = array_shift($assignedUsers)->id;
}
else
{
// Randomness in small groups will likely often result in the same user, so try it as long as this is the case
while ($nextExecutionUserId == null || $nextExecutionUserId == $lastDoneByUserId)
{
$nextExecutionUserId = $assignedUsers[array_rand($assignedUsers)]->id;
}
}
}
else if ($chore->assignment_type == self::CHORE_ASSIGNMENT_TYPE_IN_ALPHABETICAL_ORDER)
{
usort($assignedUsers, function($a, $b)
{
return strcmp($a->display_name, $b->display_name);
});
$nextRoundMatches = false;
foreach ($assignedUsers as $user)
{
if ($nextRoundMatches)
{
$nextExecutionUserId = $user->id;
break;
}
if ($user->id == $lastDoneByUserId)
{
$nextRoundMatches = true;
}
}
// If nothing has matched, probably it was the last user in the sorted list -> the first one is the next one
if ($nextExecutionUserId == null)
{
$nextExecutionUserId = array_shift($assignedUsers)->id;
}
}
else if ($chore->assignment_type == self::CHORE_ASSIGNMENT_TYPE_WHO_LEAST_DID_FIRST)
{
$row = $this->Database->chores_execution_users_statistics()->where('chore_id = :1', $choreId)->orderBy('execution_count')->limit(1)->fetch();
if ($row != null)
{
$nextExecutionUserId = $row->user_id;
}
}
$chore->update(array(
'next_execution_assigned_to_user_id' => $nextExecutionUserId
));
}
}

View File

@@ -125,8 +125,8 @@ class DemoDataGeneratorService extends BaseService
INSERt INTO meal_plan(day, recipe_id) VALUES ('{$sundayThisWeek}', 4);
INSERT INTO chores (name, period_type, period_days) VALUES ('{$this->__t_sql('Changed towels in the bathroom')}', 'manually', 5); --1
INSERT INTO chores (name, period_type, period_days) VALUES ('{$this->__t_sql('Cleaned the kitchen floor')}', 'dynamic-regular', 7); --2
INSERT INTO chores (name, period_type, period_days) VALUES ('{$this->__t_sql('Lawn mowed in the garden')}', 'dynamic-regular', 21); --3
INSERT INTO chores (name, period_type, period_days, assignment_type, assignment_config, next_execution_assigned_to_user_id) VALUES ('{$this->__t_sql('Cleaned the kitchen floor')}', 'dynamic-regular', 7, 'random', '1,2,3,4', 1); --2
INSERT INTO chores (name, period_type, period_days, assignment_type, assignment_config, next_execution_assigned_to_user_id) VALUES ('{$this->__t_sql('Lawn mowed in the garden')}', 'dynamic-regular', 21, 'random', '1,2,3,4', 1); --3
INSERT INTO chores (name, period_type, period_days) VALUES ('{$this->__t_sql('The thing which happens on the 5th of every month')}', 'monthly', 5); --4
INSERT INTO chores (name, period_type) VALUES ('{$this->__t_sql('The thing which happens daily')}', 'daily'); --5
INSERT INTO chores (name, period_type, period_config) VALUES ('{$this->__t_sql('The thing which happens on Mondays and Wednesdays')}', 'weekly', 'monday,wednesday'); --6

View File

@@ -34,7 +34,8 @@ class LocalizationService
{
$this->PotMain = Translations::fromPoFile(__DIR__ . '/../localization/strings.pot');
$this->Pot = Translations::fromPoFile(__DIR__ . '/../localization/chore_types.pot');
$this->Pot = Translations::fromPoFile(__DIR__ . '/../localization/chore_period_types.pot');
$this->Pot = $this->Pot->mergeWith(Translations::fromPoFile(__DIR__ . '/../localization/chore_assignment_types.pot'));
$this->Pot = $this->Pot->mergeWith(Translations::fromPoFile(__DIR__ . '/../localization/component_translations.pot'));
$this->Pot = $this->Pot->mergeWith(Translations::fromPoFile(__DIR__ . '/../localization/demo_data.pot'));
$this->Pot = $this->Pot->mergeWith(Translations::fromPoFile(__DIR__ . '/../localization/stock_transaction_types.pot'));
@@ -45,7 +46,8 @@ class LocalizationService
$this->PoUserStrings = new Translations();
$this->PoUserStrings->setDomain('grocy/userstrings');
$this->Po = Translations::fromPoFile(__DIR__ . "/../localization/$culture/chore_types.po");
$this->Po = Translations::fromPoFile(__DIR__ . "/../localization/$culture/chore_period_types.po");
$this->Po = $this->Po->mergeWith(Translations::fromPoFile(__DIR__ . "/../localization/$culture/chore_assignment_types.po"));
$this->Po = $this->Po->mergeWith(Translations::fromPoFile(__DIR__ . "/../localization/$culture/component_translations.po"));
$this->Po = $this->Po->mergeWith(Translations::fromPoFile(__DIR__ . "/../localization/$culture/demo_data.po"));
$this->Po = $this->Po->mergeWith(Translations::fromPoFile(__DIR__ . "/../localization/$culture/stock_transaction_types.po"));