From 6fdbe8923fd1bce0193b3c7919544f217ef24f47 Mon Sep 17 00:00:00 2001 From: Tobias Sekan Date: Wed, 6 Nov 2019 10:33:26 +0100 Subject: [PATCH] Fix mixed order of events with same time --- htdocs/comm/action/index.php | 47 ++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/htdocs/comm/action/index.php b/htdocs/comm/action/index.php index 031918b194b..9b35f21a0b2 100644 --- a/htdocs/comm/action/index.php +++ b/htdocs/comm/action/index.php @@ -1719,7 +1719,6 @@ function dol_color_minus($color, $minus, $minusunit = 16) return $newcolor; } - /** * Sort events by date * @@ -1729,24 +1728,52 @@ function dol_color_minus($color, $minus, $minusunit = 16) */ function sort_events_by_date($a, $b) { - if($a->datep != $b->datep) + // datep => Event start time + // datef => Event end time + + // Events have different start time + if ($a->datep !== $b->datep) { return $a->datep - $b->datep; } - // If both events have the same start time, longest first - - if(! is_numeric($b->datef)) + // Events have same start time and no end time + if ((! is_numeric($b->datef)) || (! is_numeric($a->datef))) { - // when event B have no end timestamp, event B should sort be before event A (All day events on top) - return 1; + return sort_events_by_percentage($a, $b); } - if(! is_numeric($a->datef)) + // Events have the same start time and same end time + if ($b->datef === $a->datef) + { + return sort_events_by_percentage($a, $b); + } + + // Events have the same start time, but have different end time -> longest event first + return $b->datef - $a->datef; +} + +/** + * Sort events by percentage + * + * @param object $a Event A + * @param object $b Event B + * @return int < 0 if event A should be before event B, > 0 otherwise, 0 if they have the exact same percentage + */ +function sort_events_by_percentage($a, $b) +{ + // Sort events with no percentage before each other + // (usefull to sort holidays, sick days or similar on the top) + + if ($a->percentage < 0) { - // when event A have no end timestamp , event A should sort be before event B (All day events on top) return -1; } - return $b->datef - $a->datef; + if ($b->percentage < 0) + { + return 1; + } + + return $b->percentage - $a->percentage; }