|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace KI\PonthubBundle\Helper; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityRepository; |
|
6
|
|
|
use KI\PonthubBundle\Entity\Episode; |
|
7
|
|
|
use KI\PonthubBundle\Entity\Game; |
|
8
|
|
|
use KI\PonthubBundle\Entity\Movie; |
|
9
|
|
|
use KI\PonthubBundle\Entity\Other; |
|
10
|
|
|
use KI\PonthubBundle\Entity\Software; |
|
11
|
|
|
use KI\UserBundle\Entity\User; |
|
12
|
|
|
|
|
13
|
|
|
class StatisticsHelper |
|
14
|
|
|
{ |
|
15
|
|
|
protected $ponthubFileUserRepository; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(EntityRepository $ponthubFileUserRepository) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->ponthubFileUserRepository = $ponthubFileUserRepository; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param User $user |
|
24
|
|
|
* @return array |
|
25
|
|
|
*/ |
|
26
|
|
|
public function getUserStatistics(User $user) |
|
27
|
|
|
{ |
|
28
|
|
|
$downloads = $this->ponthubFileUserRepository->findBy(['user' => $user], ['date' => 'ASC']); |
|
29
|
|
|
$totalFiles = count($downloads); |
|
30
|
|
|
|
|
31
|
|
|
if ($totalFiles == 0) { |
|
32
|
|
|
return ['repartition' => [], 'timeline' => [], 'totalSize' => 0, 'totalFiles' => 0, 'hipster' => 0]; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
// Récupération de la date javascript du premier download |
|
36
|
|
|
$date = 1000*($downloads[0]->getDate() - 10*3600); |
|
37
|
|
|
|
|
38
|
|
|
// Initialisation des tableaux à retourner |
|
39
|
|
|
$repartition = [ |
|
40
|
|
|
['Films', 0], |
|
41
|
|
|
['Épisodes', 0], |
|
42
|
|
|
['Jeux', 0], |
|
43
|
|
|
['Logiciels', 0], |
|
44
|
|
|
['Autres', 0] |
|
45
|
|
|
]; |
|
46
|
|
|
$timeline = [ |
|
47
|
|
|
['name' => 'Films', 'data' => [[$date, 0]]], |
|
48
|
|
|
['name' => 'Épisodes', 'data' => [[$date, 0]]], |
|
49
|
|
|
['name' => 'Jeux', 'data' => [[$date, 0]]], |
|
50
|
|
|
['name' => 'Logiciels', 'data' => [[$date, 0]]], |
|
51
|
|
|
['name' => 'Autres', 'data' => [[$date, 0]]] |
|
52
|
|
|
]; |
|
53
|
|
|
$totalSize = 0; |
|
54
|
|
|
$hipster = 0; |
|
55
|
|
|
|
|
56
|
|
|
// On complète les tableaux au fur et à mesure |
|
57
|
|
|
foreach ($downloads as $download) { |
|
58
|
|
|
$file = $download->getFile(); |
|
59
|
|
|
// Conversion en millisecondes, unité javascript de base |
|
60
|
|
|
$date = $download->getDate()*1000; |
|
61
|
|
|
|
|
62
|
|
|
if ($file instanceof Movie) { |
|
63
|
|
|
$repartition[0][1]++; |
|
64
|
|
|
$this->updateSeries($timeline, $date, 0); |
|
65
|
|
|
} |
|
66
|
|
|
if ($file instanceof Episode) { |
|
67
|
|
|
$repartition[1][1]++; |
|
68
|
|
|
$this->updateSeries($timeline, $date, 1); |
|
69
|
|
|
} |
|
70
|
|
|
if ($file instanceof Game) { |
|
71
|
|
|
$repartition[2][1]++; |
|
72
|
|
|
$this->updateSeries($timeline, $date, 3); |
|
73
|
|
|
} |
|
74
|
|
|
if ($file instanceof Software) { |
|
75
|
|
|
$repartition[3][1]++; |
|
76
|
|
|
$this->updateSeries($timeline, $date, 4); |
|
77
|
|
|
} |
|
78
|
|
|
if ($file instanceof Other) { |
|
79
|
|
|
$repartition[4][1]++; |
|
80
|
|
|
$this->updateSeries($timeline, $date, 5); |
|
81
|
|
|
} |
|
82
|
|
|
$totalSize += $file->getSize(); |
|
83
|
|
|
|
|
84
|
|
|
// Gain de points hipsteritude en fonction du nombre d'autres |
|
85
|
|
|
// personnes qui ont téléchargé le fichier |
|
86
|
|
|
$this->computeHipsteritude($hipster, $file->downloads() - 1); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// Dans le chart stacké, on met la date actuelle comme point de fin |
|
90
|
|
|
$this->updateSeries($timeline, time()*1000, -1); |
|
91
|
|
|
|
|
92
|
|
|
return [ |
|
93
|
|
|
'repartition' => $repartition, |
|
94
|
|
|
'timeline' => $timeline, |
|
95
|
|
|
'totalSize' => $totalSize, |
|
96
|
|
|
'totalFiles' => $totalFiles, |
|
97
|
|
|
'hipster' => (int)(10*$hipster/$totalFiles) |
|
98
|
|
|
]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Met à jour une série de données pour le graphe de téléchargements cumulés |
|
103
|
|
|
* @param array &$series |
|
104
|
|
|
* @param integer $date |
|
105
|
|
|
* @param integer $id |
|
106
|
|
|
*/ |
|
107
|
|
|
private function updateSeries(&$series, $date, $id) { |
|
108
|
|
|
foreach ($series as $key => &$value) { |
|
109
|
|
|
if ($key != $id) { |
|
110
|
|
|
$value['data'][] = [$date, $value['data'][count($value['data']) - 1][1]]; |
|
111
|
|
|
} else { |
|
112
|
|
|
$value['data'][] = [$date, $value['data'][count($value['data']) - 1][1] + 1]; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Met à jour la hipsteritude |
|
119
|
|
|
* @param integer &$hipster La hipsteritude à mettre à jour |
|
120
|
|
|
* @param integer $count Le nombre d'autres personnes ayant téléchargé le fichier |
|
121
|
|
|
*/ |
|
122
|
|
|
private function computeHipsteritude(&$hipster, $count) |
|
123
|
|
|
{ |
|
124
|
|
|
if ($count == 0) { |
|
125
|
|
|
$hipster += 20; |
|
126
|
|
|
} else if ($count < 2) { |
|
127
|
|
|
$hipster += 15; |
|
128
|
|
|
} else if ($count < 4) { |
|
129
|
|
|
$hipster += 10; |
|
130
|
|
|
} else if ($count < 9) { |
|
131
|
|
|
$hipster += 5; |
|
132
|
|
|
} else if ($count < 19) { |
|
133
|
|
|
$hipster += 3; |
|
134
|
|
|
} else { |
|
135
|
|
|
$hipster++; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|