- группировка посещаемости по каждому дню
- ранжирование каждой посещаемости по тому, насколько рано/поздно , в зависимости от параметра, используя свой индекс в отсортированном списке.
- суммируют свои индексы, чтобы найти наибольшее значение в каждом случае.
Изначально у меня был код
Код: Выделить всё
protected function punctualChart ($attendanceCollection, ?string $puncMode) {
$puncMode = $puncMode?? "ear";
// group them into each day. rank by employee with most days
$attendanceDays = $attendanceCollection->groupBy(function ($attendance) {
return $attendance->created_at->format("m-d");
})
->map(function ($daysCollection) use ($puncMode) { // then sum their value for each day? add their indexes. depending on the mode, we know whether highest or lowest is the winner
return $daysCollection->sort(function ($a, $b) use ($puncMode) {
$date1 = $a->created_at;
$date2 = $b->created_at;
if ($puncMode == "lat") {
if ($b > $a) return 1;
if ($a > $b) return -1;
return 0;
}
else {
if ($a > $b) return 1;
if ($b > $a) return -1;
return 0;
}
});
})->values();
// dd($attendanceDays->get(1), $attendanceDays->get(2), $puncMode);
$scoreEmployee = [];
foreach ($attendanceDays as $attendanceCollection) {
foreach ($attendanceCollection as $index => $attendance) {
dump($index);
$employeeId = /*$attendance->employee->last_name.*/$attendance->employee_id;
if (!array_key_exists($employeeId, $scoreEmployee))
$scoreEmployee[$employeeId] = /*0;*/[
"value" => 0,
"model" => $attendance->employee->last_name
];
$scoreEmployee[$employeeId]["value"] += $index+1; // offset 0-based index
}
}
$sortedEmployeeScores = \Illuminate\Support\Arr::sort($scoreEmployee, "value");
dd($scoreEmployee, $sortedEmployeeScores);
return $scoreEmployee;
}
Код: Выделить всё
protected function punctualChart ($attendanceCollection, ?string $puncMode) {
$puncMode = $puncMode?? "ear";
// group them into each day. rank by employee with most days
$attendanceDays = $attendanceCollection->groupBy(function ($attendance) {
return $attendance->created_at->format("m-d");
})
->map(function ($daysCollection) use ($puncMode) { // then sum their value for each day? add their indexes. depending on the mode, we know whether highest or lowest is the winner
return $puncMode == "ear"? $daysCollection->sortBy("created_at"):
$daysCollection->sortByDesc("created_at");
})/*->values()*/;
// dd($attendanceDays->get(1), $attendanceDays->get(2), $puncMode);
$scoreEmployee = [];
foreach ($attendanceDays as $attendanceCollection) {
foreach ($attendanceCollection as $index => $attendance) {
dump($index);
$employeeId = /*$attendance->employee->last_name.*/$attendance->employee_id;
if (!array_key_exists($employeeId, $scoreEmployee))
$scoreEmployee[$employeeId] = /*0;*/[
"value" => 0,
"model" => $attendance->employee->last_name
];
$scoreEmployee[$employeeId]["value"] += $index+1; // offset 0-based index
}
}
$sortedEmployeeScores = \Illuminate\Support\Arr::sort($scoreEmployee, "value");
dd($scoreEmployee, $sortedEmployeeScores);
return $scoreEmployee;
}
Так что же я делаю не так и как мне поменять их индексы, чтобы они отражали их позицию? Затем просуммируйте эту позицию для рендеринга на моем графике (эту часть добавлять не нужно. Просто раскрываем для полноты картины)?
Подробнее здесь: https://stackoverflow.com/questions/791 ... ex-by-time
Мобильная версия