2017-07-25 20:03:31 +02:00
< ? php
2018-04-10 20:30:11 +02:00
class HabitsService
2017-07-25 20:03:31 +02:00
{
const HABIT_TYPE_MANUALLY = 'manually' ;
const HABIT_TYPE_DYNAMIC_REGULAR = 'dynamic-regular' ;
public static function GetCurrentHabits ()
{
$sql = 'SELECT * from habits_current' ;
2018-04-10 20:30:11 +02:00
return DatabaseService :: ExecuteDbQuery ( DatabaseService :: GetDbConnectionRaw (), $sql ) -> fetchAll ( PDO :: FETCH_OBJ );
2017-07-25 20:03:31 +02:00
}
public static function GetNextHabitTime ( int $habitId )
{
2018-04-10 20:30:11 +02:00
$db = DatabaseService :: GetDbConnection ();
2017-07-25 20:03:31 +02:00
$habit = $db -> habits ( $habitId );
2018-04-10 20:30:11 +02:00
$habitLastLogRow = DatabaseService :: ExecuteDbQuery ( DatabaseService :: GetDbConnectionRaw (), " SELECT * from habits_current WHERE habit_id = $habitId LIMIT 1 " ) -> fetch ( PDO :: FETCH_OBJ );
2017-07-25 20:03:31 +02:00
switch ( $habit -> period_type )
{
case self :: HABIT_TYPE_MANUALLY :
return date ( 'Y-m-d H:i:s' );
case self :: HABIT_TYPE_DYNAMIC_REGULAR :
return date ( 'Y-m-d H:i:s' , strtotime ( '+' . $habit -> period_days . ' day' , strtotime ( $habitLastLogRow -> last_tracked_time )));
}
return null ;
}
public static function GetHabitDetails ( int $habitId )
{
2018-04-10 20:30:11 +02:00
$db = DatabaseService :: GetDbConnection ();
2017-07-25 20:03:31 +02:00
$habit = $db -> habits ( $habitId );
$habitTrackedCount = $db -> habits_log () -> where ( 'habit_id' , $habitId ) -> count ();
$habitLastTrackedTime = $db -> habits_log () -> where ( 'habit_id' , $habitId ) -> max ( 'tracked_time' );
return array (
'habit' => $habit ,
'last_tracked' => $habitLastTrackedTime ,
'tracked_count' => $habitTrackedCount
);
}
public static function TrackHabit ( int $habitId , string $trackedTime )
{
2018-04-10 20:30:11 +02:00
$db = DatabaseService :: GetDbConnection ();
2017-07-25 20:03:31 +02:00
$logRow = $db -> habits_log () -> createRow ( array (
'habit_id' => $habitId ,
'tracked_time' => $trackedTime
));
$logRow -> save ();
return true ;
}
}