SendEmailCreator icon indicating copy to clipboard operation
SendEmailCreator copied to clipboard

TaskEmailDue: Hours before due date possible?

Open bhopmann opened this issue 5 years ago • 6 comments

First of all, thank you very much for your plugin, especially for the automatic action within the file https://github.com/creecros/SendEmailCreator/blob/master/Action/TaskEmailDue.php.

I wonder if it is possible to include hours in the script instead of (or in addition to) days. Unfortunately, so far I have not been able to fully understand the logic of the script. I would therefore be grateful for any starting point!

bhopmann avatar Jan 03 '21 16:01 bhopmann

yes. there are 24 hours in a day. therefore 1/24 is an hour.

creecros avatar Jan 03 '21 17:01 creecros

if you're looking for the logic, its here: https://github.com/creecros/SendEmailCreator/blob/425fd4e100e7a03c248e941bec99a09721c63fd5/Action/TaskEmailDue.php#L120

line 120, duration x 86400, so technically its in seconds. so if your duration was 1.041667, that would be a day and an hour.

bear in mind, this duration is only checked according to your crontab. so its basically irrelevant, unless your cron runs enough to check this. most crons run once a day, its at that time it checks if the duration condition is met. so if your cron runs once a day at 9am, and your condition isn't met until 10am, well, it won't trigger until the next day at 9am, because its not going to check at 10am.

creecros avatar Jan 03 '21 17:01 creecros

Thank you for your explanation! I'm currently running ./cli trigger:tasks every 15 minutes and want to push notifications one or two hours before a task is due. Therefore I'm currently experimenting with the following setting:

if ( $remaining > 0 ) {
            $hours_to_due = floor($remaining / 3600);
            $seconds_to_due = $remaining % 3600;
            $minutes_to_due = $seconds_to_due > 0 ? floor( $seconds_to_due / 60  ) : 0;
        }

        $time_part = '';
        if ( $hours_to_due > 0 ) {
            $time_part = $hours_to_due . ' hour' . ($hours_to_due == 1 ? '' : 's');
            if ( $hours_to_due < 2 && $minutes_to_due > 0 ) {
                $time_part .= ' and ' . $minutes_to_due . ' minute' . ($minutes_to_due == 1 ? '' : 's');
            }
        }
        else if ( $minutes_to_due > 0 ) {
            $time_part = $minutes_to_due . ' minute' . ($minutes_to_due == 1 ? '' : 's');
        }

[...]

$max_duration = $this->getParam('duration') * 7200;

[...]

$minimum_gotify_span = 86400;

bhopmann avatar Jan 03 '21 21:01 bhopmann

you seem to be on the right path, you could take it a step further, and offer an array of "duration in" days, hours, minutes, or seconds and then i.e., if day, multiply by 86400, hours by 3600, etc...

creecros avatar Jan 03 '21 23:01 creecros

Thank you for your advice, I will give it a try!

bhopmann avatar Jan 04 '21 20:01 bhopmann

I've tried out a little more and am now using the following function (with $task['date_due'] set to Hours before due date).

   public function makeDefaultSubject($task)
    {
        $project = $this->projectModel->getById($task['project_id']);

        $remaining = $task['date_due'] - time();
        $days_to_due = 0;
        $hours_to_due = 0;
        $minutes_to_due = 0;

        if ( $remaining > 0 )
        {
        	
            $days_to_due = floor($remaining / 86400);
            $hours_to_due = floor(($remaining % 86400) / 3600);
            $minutes_to_due = floor(($remaining % 3600) / 60);
        }

        $time_part = array();

        if ( $days_to_due > 0 )
        {
            $time_part[] = $days_to_due . t(' day') . ($days_to_due == 1 ? '' : t('s'));
        }

        if ( $hours_to_due > 0 )
        {
            $time_part[] = $hours_to_due . t(' hour') . ($hours_to_due == 1 ? '' : t('s '));
        }

        if ( $minutes_to_due > 0 )
        {
            $time_part[] = $minutes_to_due . t(' minute') . ($minutes_to_due == 1 ? '' : t('s '));
        }

        $time_part = implode(t(' and '), $time_part);

        $subject = '[' . $project['name']  . '][' . $task['title']  . '] ' . ($time_part ? t('Due in ') . $time_part : t('Task is due'));
        //print "\n".$subject."\n";

        return $subject;
    }

bhopmann avatar Jan 11 '21 16:01 bhopmann

I dont think there is any action I need to do on this, Stale

creecros avatar Feb 10 '23 18:02 creecros