aswi2020merlot-gitlab/app/Http/Controllers/CategoriesController.php @ 977060b9
1 |
<?php
|
---|---|
2 |
|
3 |
namespace App\Http\Controllers; |
4 |
|
5 |
use App\ArtefactCategory; |
6 |
use App\Category; |
7 |
use Illuminate\Support\Facades\Auth; |
8 |
use App\User; |
9 |
|
10 |
class CategoriesController extends Controller |
11 |
{
|
12 |
public function __construct() |
13 |
{
|
14 |
$this->middleware('auth'); |
15 |
}
|
16 |
|
17 |
public function calculateClusters($categoriesCount) |
18 |
{
|
19 |
if(isset($categoriesCount)) |
20 |
{
|
21 |
$min = PHP_INT_MAX; |
22 |
$max = PHP_INT_MIN; |
23 |
foreach($categoriesCount as $category) |
24 |
{
|
25 |
$c = count($category); |
26 |
if($c < $min) |
27 |
{
|
28 |
$min = $c; |
29 |
}
|
30 |
|
31 |
if($c > $max) |
32 |
{
|
33 |
$max = $c; |
34 |
}
|
35 |
}
|
36 |
$center = $min + (($max - $min)/2); |
37 |
return array($min, $center, $max); |
38 |
}
|
39 |
else
|
40 |
{
|
41 |
return null; |
42 |
}
|
43 |
}
|
44 |
|
45 |
/**
|
46 |
* Display a listing of the resource.
|
47 |
*
|
48 |
* @return \Illuminate\Http\Response
|
49 |
*/
|
50 |
public function index() |
51 |
{
|
52 |
if(isset(Auth::user()->id)) |
53 |
{
|
54 |
$userId = Auth::user()->id; |
55 |
|
56 |
$categoryNames = Category::all(); |
57 |
$countCategory = count($categoryNames); |
58 |
$categories = array(); |
59 |
for($i = 1; $i <= $countCategory;$i++) |
60 |
{
|
61 |
array_push($categories, Category::find($i)->artefacts()->get()); |
62 |
}
|
63 |
$clusters = $this->calculateClusters($categories); |
64 |
|
65 |
$categorySizes = array(); |
66 |
if(isset($clusters)) |
67 |
{
|
68 |
foreach($categories as $category) |
69 |
{
|
70 |
$c = count($category); |
71 |
$type1 = abs($clusters['0'] - $c); |
72 |
$type2 = abs($clusters['1'] - $c); |
73 |
$type3 = abs($clusters['2'] - $c); |
74 |
|
75 |
if($type1 <= $type2 && $type1 <= $type3) |
76 |
{
|
77 |
array_push($categorySizes, 1); |
78 |
}
|
79 |
else if($type2 <= $type1 && $type2 <= $type3) |
80 |
{
|
81 |
array_push($categorySizes, 2); |
82 |
}
|
83 |
else
|
84 |
{
|
85 |
array_push($categorySizes, 3); |
86 |
}
|
87 |
}
|
88 |
}
|
89 |
|
90 |
$data = array( |
91 |
'title' => 'Categories', |
92 |
'user' => User::find($userId), |
93 |
'count' => $countCategory, |
94 |
'categories' => $categories, |
95 |
'categorySizes' => $categorySizes, |
96 |
'categoryNames' => $categoryNames |
97 |
);
|
98 |
return view('categories.index') -> with($data); |
99 |
}
|
100 |
else
|
101 |
{
|
102 |
$data = array( |
103 |
'title' => 'Welcome to the MERLOT page', |
104 |
);
|
105 |
return view('pages.index') -> with($data); |
106 |
}
|
107 |
}
|
108 |
}
|