1
|
|
|
<?php namespace Arcanesoft\Tracker\ViewComposers\Dashboard; |
2
|
|
|
|
3
|
|
|
use Arcanesoft\Tracker\Models\Visitor; |
4
|
|
|
use Arcanesoft\Tracker\Support\DateRange; |
5
|
|
|
use Arcanesoft\Tracker\ViewComposers\AbstractViewComposer; |
6
|
|
|
use Illuminate\Contracts\View\View; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class LanguagesListComposer |
11
|
|
|
* |
12
|
|
|
* @package Arcanesoft\Tracker\ViewComposers\Dashboard |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class LanguagesListComposer extends AbstractViewComposer |
16
|
|
|
{ |
17
|
|
|
/* ----------------------------------------------------------------- |
18
|
|
|
| Constants |
19
|
|
|
| ----------------------------------------------------------------- |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
const VIEW = 'tracker::admin._composers.dashboard.languages-ratio-list'; |
23
|
|
|
|
24
|
|
|
/* ----------------------------------------------------------------- |
25
|
|
|
| Main Methods |
26
|
|
|
| ----------------------------------------------------------------- |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Compose the view. |
31
|
|
|
* |
32
|
|
|
* @param \Illuminate\Contracts\View\View $view |
33
|
|
|
*/ |
34
|
|
|
public function compose(View $view) |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var \Carbon\Carbon $start |
38
|
|
|
* @var \Carbon\Carbon $end |
39
|
|
|
* @var \Illuminate\Support\Collection $range |
40
|
|
|
*/ |
41
|
|
|
extract(DateRange::getCurrentMonthDaysRange()); |
|
|
|
|
42
|
|
|
|
43
|
|
|
$view->with('languagesRatio', $this->calculateLanguagesPercentage( |
44
|
|
|
$this->getLanguagesCountFromSessions($start, $end) |
45
|
|
|
)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/* ------------------------------------------------------------------------------------------------ |
49
|
|
|
| Other Functions |
50
|
|
|
| ------------------------------------------------------------------------------------------------ |
51
|
|
|
*/ |
52
|
|
|
/** |
53
|
|
|
* Get the languages count from sessions. |
54
|
|
|
* |
55
|
|
|
* @param \Carbon\Carbon $start |
56
|
|
|
* @param \Carbon\Carbon $end |
57
|
|
|
* |
58
|
|
|
* @return \Illuminate\Support\Collection |
59
|
|
|
*/ |
60
|
|
|
private function getLanguagesCountFromSessions($start, $end) |
61
|
|
|
{ |
62
|
|
|
return $this->getCachedVisitors() |
63
|
|
|
->filter(function (Visitor $visitor) use ($start, $end) { |
64
|
|
|
return $visitor->updated_at->between($start, $end) && ! is_null($visitor->language); |
65
|
|
|
}) |
66
|
|
|
->transform(function (Visitor $visitor) { |
67
|
|
|
return $visitor->language; |
68
|
|
|
}) |
69
|
|
|
->groupBy('preference') |
70
|
|
|
->transform(function (Collection $items, $key) { |
71
|
|
|
return [ |
72
|
|
|
'name' => $key, |
73
|
|
|
'count' => $items->count(), |
74
|
|
|
]; |
75
|
|
|
}) |
76
|
|
|
->sortByDesc('count'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Calculate the languages percentage. |
81
|
|
|
* |
82
|
|
|
* @param \Illuminate\Support\Collection $languages |
83
|
|
|
* |
84
|
|
|
* @return \Illuminate\Support\Collection |
85
|
|
|
*/ |
86
|
|
|
private function calculateLanguagesPercentage($languages) |
87
|
|
|
{ |
88
|
|
|
$total = $languages->sum('count'); |
89
|
|
|
|
90
|
|
|
return $languages->transform(function ($item) use ($total) { |
91
|
|
|
return $item + [ |
92
|
|
|
'percentage' => round(($item['count'] / $total) * 100, 2) |
93
|
|
|
]; |
94
|
|
|
}); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|