1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitPrepared\Bundle\D1b0Workspace\Controller\V1; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
7
|
|
|
use Silex\Application; |
8
|
|
|
use Silex\Api\ControllerProviderInterface; |
9
|
|
|
use RedBeanPHP\Facade as R; |
10
|
|
|
|
11
|
|
|
class WorkspaceController implements ControllerProviderInterface |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
public $POINT_FOR_USING_A_CONQUERED_BADGE = 200; |
15
|
|
|
public $POINT_FOR_USING_A_BADGE = 100; |
16
|
|
|
public $POINT_DEFAULT = 50; |
17
|
|
|
|
18
|
|
|
private $app; |
19
|
|
|
|
20
|
|
|
public function connect(Application $app) |
21
|
|
|
{ |
22
|
|
|
$this->app = $app; |
23
|
|
|
$factory = $app['controllers_factory']; |
24
|
|
|
# il mount point e' precedente e non serve prima |
25
|
|
|
$this->app['db']; |
26
|
|
|
R::fancyDebug(TRUE); |
27
|
|
|
$factory->get('/', array($this, 'getWorkspaceList')); |
28
|
|
|
$factory->post('/', array($this, 'createWorkspace')); |
29
|
|
|
$factory->get('/{id}', array($this, 'getWorkspace')); |
30
|
|
|
$factory->get('/{id}/share', array($this, 'share')); |
31
|
|
|
$factory->post('/{id}/part', array($this, 'postPart')); |
32
|
|
|
$factory->get('/{id}/part/{part_id}', array($this, 'getPart')); |
33
|
|
|
$factory->put('/{id}/part/{part_id}', array($this, 'putPart')); |
34
|
|
|
$factory->post('/{id}/part/{part_id}/checkin', array($this, 'checkin')); |
35
|
|
|
return $factory; |
36
|
|
|
} |
37
|
|
|
public function getSessionId() { |
38
|
|
|
$user_id = $this->app['session']->get('user')['id']; |
39
|
|
|
return $user_id; |
40
|
|
|
} |
41
|
|
|
public function getWorkspaceList(Request $request) |
42
|
|
|
{ |
43
|
|
|
$user_id = $this->getSessionId(); |
44
|
|
|
$workspaces = R::getAll("SELECT ws.id, |
45
|
|
|
ws.title, |
46
|
|
|
ws.description, |
47
|
|
|
ws.environment, |
48
|
|
|
ws.completed |
49
|
|
|
FROM userworkspace AS uws |
50
|
|
|
LEFT JOIN workspace AS ws |
51
|
|
|
ON uws.workspace = ws.id |
52
|
|
|
WHERE uws.user = ?",[$user_id]); |
53
|
|
|
$list = []; |
54
|
|
|
foreach ($workspaces as $ws) { |
55
|
|
|
array_push($list, [ |
56
|
|
|
"id"=>$ws['id'], |
57
|
|
|
"title"=>$ws['title'], |
58
|
|
|
"description"=>$ws['description'], |
59
|
|
|
"environment"=>$ws['environment'], |
60
|
|
|
"point"=>0, //TODO fare una view con i point già calcolati per il ws |
61
|
|
|
"completed"=>$ws['completed'], |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
$headers = []; |
65
|
|
|
return JsonResponse::create($list, 200, $headers)->setSharedMaxAge(300); |
66
|
|
|
|
67
|
|
|
} |
68
|
|
|
public function createWorkspace(Request $request) |
69
|
|
|
{ |
70
|
|
|
$user_id = $this->getSessionId(); |
71
|
|
|
$counter = 0; |
72
|
|
|
$data = json_decode($request->getContent(), true); |
73
|
|
|
//TODO validate json_decode |
74
|
|
|
$title = $data['title']; |
75
|
|
|
$description = $data['description']; |
76
|
|
|
$environment = $data['environment']; |
77
|
|
|
|
78
|
|
|
$patrol = $data['team']['patrol']; |
79
|
|
|
$unit = $data['team']['unit']; |
80
|
|
|
$group = $data['team']['group']; |
81
|
|
|
|
82
|
|
|
//save the workspace get id |
83
|
|
|
$ws = R::dispense("workspace"); |
84
|
|
|
$ws->title = $title; |
85
|
|
|
$ws->description = $description; |
86
|
|
|
$ws->environment = $environment; |
87
|
|
|
$ws->completed = false; |
88
|
|
|
$ws->inserttime = date('Y-m-d H:i:s'); |
89
|
|
|
$ws->lastupdatetime = date('Y-m-d H:i:s'); |
90
|
|
|
$id = R::store($ws); |
91
|
|
|
|
92
|
|
|
//save the team |
93
|
|
|
$team = R::dispense("team"); |
94
|
|
|
$team->workspace = $id; |
95
|
|
|
$team->patrol = $patrol; |
96
|
|
|
$team->unit = $unit; |
97
|
|
|
$team->group = $group; |
98
|
|
|
$team_id = R::store($team); |
99
|
|
|
|
100
|
|
|
//create a phantom part to add badge |
101
|
|
|
$part = R::dispense("part"); |
102
|
|
|
$part->workspace = $id; |
103
|
|
|
$part->user = $user_id; |
104
|
|
|
$part->inserttime = date('Y-m-d H:i:s'); |
105
|
|
|
$part->lastupdatetime = date('Y-m-d H:i:s'); |
106
|
|
|
$part->totalpoint = 0; |
107
|
|
|
$part_id = R::store($part); |
108
|
|
|
|
109
|
|
|
//add the badge to the project |
110
|
|
|
foreach ($data['badges'] as $badge_id) { |
111
|
|
|
//TODO insert those badge as first hidden post |
112
|
|
|
$pb = R::dispense("partbadge"); |
113
|
|
|
$pb->badge = $badge_id; |
114
|
|
|
$pb->part = $part_id; |
115
|
|
|
$pb->inserttime = date('Y-m-d H:i:s'); |
116
|
|
|
$tmp = R::store($pb); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
//add the workspace created to the user as owner |
120
|
|
|
$usw = R::dispense("userworkspace"); |
121
|
|
|
$usw->user = $user_id; |
122
|
|
|
$usw->workspace = $id; |
123
|
|
|
$usw->inserttime = date('Y-m-d H:i:s'); |
124
|
|
|
R::store($usw); |
125
|
|
|
|
126
|
|
|
$res = ["id" => $id]; |
127
|
|
|
$headers = []; |
128
|
|
|
return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function getWorkspace($id, Request $request) { |
132
|
|
|
$user_id = $this->getSessionId(); |
133
|
|
|
//TODO controllare che l'utente abbia diritto a vedere questo workspace |
134
|
|
|
|
135
|
|
|
$workspace = R::findOne("workspace", "id = ?", [$id]); |
136
|
|
|
$part = R::findAll("part", "workspace = ?", [$id]); |
137
|
|
|
|
138
|
|
|
$badges = R::findAll("workspacebadge", "workspace = ?", [$id]); |
139
|
|
|
|
140
|
|
|
$l_part = []; |
141
|
|
|
foreach ($part as $p) { |
142
|
|
|
array_push($l_part, intval($p['id'])); |
143
|
|
|
} |
144
|
|
|
$l_badges = []; |
145
|
|
|
foreach ($badges as $b) { |
146
|
|
|
array_push($l_badges, intval($b['badge'])); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$res = [ |
150
|
|
|
'id'=> $workspace['id'], |
151
|
|
|
'title'=> $workspace['title'], |
152
|
|
|
'description'=> $workspace['description'], |
153
|
|
|
'environment'=> $workspace['environment'], |
154
|
|
|
'environment'=> $workspace['environment'], |
155
|
|
|
'completed'=> $workspace['completed'], |
156
|
|
|
'parts'=>$l_part, |
157
|
|
|
'badges'=>$l_badges |
158
|
|
|
]; |
159
|
|
|
$headers = []; |
160
|
|
|
return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function share($id, Request $request) { |
164
|
|
|
$generatedKey = hash("sha256", (mt_rand(10000, 99999).time().$id)); |
165
|
|
|
//TODO verificare documentazione realtiva sulla reale entropia generata da questo sistema |
166
|
|
|
$user_id = $this->getSessionId(); |
167
|
|
|
$share = R::dispense("share"); |
168
|
|
|
$share->user = $user_id; |
169
|
|
|
$share->workspace = $id; |
170
|
|
|
$share->key = $generatedKey; |
171
|
|
|
$share->inserttime = date('Y-m-d H:i:s'); |
172
|
|
|
$share_id = R::store($share); |
173
|
|
|
|
174
|
|
|
$date = new \DateTime(); |
175
|
|
|
date_add($date, date_interval_create_from_date_string('15 minutes')); |
176
|
|
|
|
177
|
|
|
$res = [ |
178
|
|
|
"id"=>$share_id, |
179
|
|
|
"key"=>$generatedKey, |
180
|
|
|
"expire"=>$date->format('Y-m-d H:i:s') |
181
|
|
|
]; |
182
|
|
|
|
183
|
|
|
$headers = []; |
184
|
|
|
return JsonResponse::create($res, 200, $headers)->setSharedMaxAge(300); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function postPart($id, Request $request) { |
188
|
|
|
$user_id = $this->getSessionId(); |
189
|
|
|
|
190
|
|
|
$data = json_decode($request->getContent(), true); |
191
|
|
|
|
192
|
|
|
$part = R::dispense("part"); |
193
|
|
|
$part->workspace = $id; |
194
|
|
|
$part->user = $user_id; |
195
|
|
|
$part->inserttime = date('Y-m-d H:i:s'); |
196
|
|
|
$part->lastupdatetime = date('Y-m-d H:i:s'); |
197
|
|
|
$part->totalpoint = 0; |
198
|
|
|
$part_id = R::store($part); |
199
|
|
|
|
200
|
|
|
foreach($data['part'] as $r){ //TODO va fixato nelle api |
201
|
|
|
$resource = R::dispense("resource"); |
202
|
|
|
$resource->part = $part_id; |
203
|
|
|
$resource->inserttime = date('Y-m-d H:i:s'); |
204
|
|
|
$resource->updatetime = date('Y-m-d H:i:s'); |
205
|
|
|
$resource->type = $r->type; |
206
|
|
|
$resource->ref = $r->ref; |
207
|
|
|
$resource->hash = $r->hash; |
208
|
|
|
$resource->totalpoint = 0; |
209
|
|
|
$resource_id = R::store($resource); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
foreach($data['badges'] as $badge_id){ //TODO va fixato nelle api |
213
|
|
|
$pb = R::dispense("partbadge"); |
214
|
|
|
$pb->badge = $badge_id; |
215
|
|
|
$pb->part = $part_id; |
216
|
|
|
$pb->inserttime = date('Y-m-d H:i:s'); |
217
|
|
|
$tmp = R::store($pb); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$res = ["id"=>$part_id]; |
221
|
|
|
$headers = []; |
222
|
|
|
return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function getPart($id,$part_id, Request $request) { |
226
|
|
|
$user_id = $this->getSessionId(); |
227
|
|
|
|
228
|
|
|
$data = json_decode($request->getContent(), true); |
229
|
|
|
|
230
|
|
|
$part = R::findOne("part","id = ?",[$part_id]); |
231
|
|
|
|
232
|
|
|
$resource = R::findAll("resource","part = ?",[$part_id]); |
233
|
|
|
|
234
|
|
|
$partecipants = R::findAll("cero","part = ?",[$part_id]); |
235
|
|
|
|
236
|
|
|
$badges = R::findAll("partbadge","part = ?",[$part_id]); |
237
|
|
|
|
238
|
|
|
$res= [ |
239
|
|
|
"id"=>$part->id, |
240
|
|
|
"creation"=>$part->inserttime, |
241
|
|
|
"points"=>$part->points, |
242
|
|
|
"checked"=>$part->checked, |
243
|
|
|
"badges"=>[], |
244
|
|
|
"part"=>[], |
245
|
|
|
"partecipants"=>[] |
246
|
|
|
]; |
247
|
|
|
|
248
|
|
|
foreach($badges as $b){ |
249
|
|
|
array_push($res['badges'],$b->id); |
250
|
|
|
} |
251
|
|
|
foreach($resource as $r){ |
252
|
|
|
array_push($res['part'],[ |
253
|
|
|
"type"=>$r->type, |
254
|
|
|
"hash"=>$r->hash, |
255
|
|
|
"ref"=>$r->ref |
256
|
|
|
]); |
257
|
|
|
} |
258
|
|
|
foreach($partecipants as $p){ |
259
|
|
|
array_push($res['partecipants'],$p->user);//TODO forse va usato l'id del c'ero e non l'id dell'utente |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
$headers = []; |
263
|
|
|
return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
private function getPositionInArray($array,$id){ |
267
|
|
|
$count =0; |
268
|
|
|
foreach($array as $a){ |
269
|
|
|
if($a->id === $id){ |
270
|
|
|
return $count; |
271
|
|
|
} |
272
|
|
|
$count = $count + 1; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
public function putPart($id,$part_id, Request $request) { |
277
|
|
|
$user_id = $this->getSessionId(); |
278
|
|
|
|
279
|
|
|
$data = json_decode($request->getContent(), true); |
280
|
|
|
|
281
|
|
|
$part = R::load("part",$part_id); |
282
|
|
|
$part->workspace = $id; |
283
|
|
|
$part->user = $user_id; |
284
|
|
|
$part->lastupdatetime = date('Y-m-d H:i:s'); |
285
|
|
|
$part->totalpoint = 0; |
286
|
|
|
$part_id = R::store($part); |
287
|
|
|
|
288
|
|
|
$delete_res=R::findAll("resource","WHERE part = ?",[$part_id]); |
289
|
|
|
|
290
|
|
|
foreach($data['part'] as $r){ //TODO va fixato nelle api |
291
|
|
|
$resource = R::findOne("resource","WHERE hash =?",[$r->hash]);//TODO BISOGNA FARE IL DIFF TRA QUELLE PRESENTI E QUELLE NON PRESENTI |
292
|
|
|
$resource->part = $part_id; |
293
|
|
|
$resource->updatetime = date('Y-m-d H:i:s'); |
294
|
|
|
$resource->type = $r->type; |
295
|
|
|
$resource->ref = $r->ref; |
296
|
|
|
$resource->hash = $r->hash; |
297
|
|
|
$resource->totalpoint = 0; |
298
|
|
|
$resource_id = R::store($resource); |
299
|
|
|
array_splice($delete_res,getPositionInArray($delete_res,$resource_id),1); //RIMUOVO GLI ELEMENTI CHE HO MODIFICATO |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
foreach($delete_res as $d){ |
303
|
|
|
//RIMUOVO REALMENTE DAL DB LE COSE CHE HO LASCIATO FUORI DALLA PUT (PRESENTI NEL DB MA NON NELLA NUOVA VERSIONE ODIO LE PUT) |
304
|
|
|
$resource = R::load("resource",[$r->id]); |
305
|
|
|
R::trash($resource); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
foreach($data['badges'] as $badge_id){ //TODO VANNO CANCELLATI I BADGE RIMOSSI IN QUESTO MODO |
309
|
|
|
$pb = R::load("partbadge",$badge_id); |
310
|
|
|
$pb->badge = $badge_id; |
311
|
|
|
$pb->part = $part_id; |
312
|
|
|
$tmp = R::store($pb); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
$res = ["id"=>$part_id]; |
316
|
|
|
$headers = []; |
317
|
|
|
return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
private function getPoint($badge_id,$badges){ |
321
|
|
|
foreach($badges as $b){ |
322
|
|
|
if($b->id === $badge_id){ |
323
|
|
|
if($b->completed === True){ |
324
|
|
|
echo "CASO 1;<BR />"; |
325
|
|
|
return $this->$POINT_FOR_USING_A_CONQUERED_BADGE; |
326
|
|
|
}else{ |
327
|
|
|
echo "CASO 2;<BR />"; |
328
|
|
|
return $this->POINT_FOR_USING_A_BADGE; |
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
echo "CASO 3;<BR />"; |
333
|
|
|
return $this->POINT_DEFAULT; |
334
|
|
|
} |
335
|
|
|
public function checkin($id,$part_id, Request $request) { |
336
|
|
|
$user_id = $this->getSessionId(); |
337
|
|
|
|
338
|
|
|
$badges = R::findAll("partbadge","part = ?",[$part_id]); |
339
|
|
|
$u_badges = R::findAll("userbadge","user = ?",[$user_id]); |
340
|
|
|
|
341
|
|
|
$point_earned = 0 |
342
|
|
|
foreach($badges as $b){ //SE CI SONO DEI BADGE |
|
|
|
|
343
|
|
|
$point = $this->getPoint($b->id,$u_badges); |
344
|
|
|
if($point != $this->POINT_DEFAULT){ //SE SEI IN CAMMINO PER QUEI BADGE O SE LI POSSIEDI GIÀ |
345
|
|
|
echo "PUNTI:".$point; |
346
|
|
|
$point_earned = $point_earned + $point |
347
|
|
|
$pb = R::dispense("cero"); |
348
|
|
|
$pb->user = $user_id; |
349
|
|
|
$pb->part = $part_id; |
350
|
|
|
$pb->badge = $b->id; |
351
|
|
|
$pb->inserttime = date('Y-m-d H:i:s'); |
352
|
|
|
$pb->points = $point; |
353
|
|
|
$tmp = R::store($pb); |
354
|
|
|
|
355
|
|
|
if($point === $this->POINT_FOR_USING_A_BADGE){ //SE SEI IN CAMMINO MA NON LI HAI ANCORA RAGGIUNTI |
356
|
|
|
$ubc = R::dispense("userbadgeclove"); |
357
|
|
|
$ubc->user = $user_id; |
358
|
|
|
$ubc->badge = $b->id; |
359
|
|
|
$ubc->part = $part_id; |
360
|
|
|
$ubc->inserttime = date('Y-m-d H:i:s'); |
361
|
|
|
$tmp = R::store($ubc); |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
if($point_earned <= 0){ //SE NON CI SONO BADGE O SE TU NON SEI IN CAMMINO PER NESSUNO DI LORO |
367
|
|
|
$pb = R::dispense("cero"); |
368
|
|
|
$pb->user = $user_id; |
369
|
|
|
$pb->part = $part_id; |
370
|
|
|
$pb->inserttime = date('Y-m-d H:i:s'); |
371
|
|
|
$pb->points = $this->POINT_DEFAULT; |
372
|
|
|
$tmp = R::store($pb); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
|
376
|
|
|
} |
377
|
|
|
} |
378
|
|
|
|