1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitPrepared\Bundle\D1b0Workspace\Controller\V1; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
7
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
8
|
|
|
use Silex\Application; |
9
|
|
|
use Silex\Api\ControllerProviderInterface; |
10
|
|
|
use RedBeanPHP\Facade as R; |
11
|
|
|
|
12
|
|
|
class WorkspaceController implements ControllerProviderInterface |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
public $POINT_FOR_USING_A_CONQUERED_BADGE = 200; |
16
|
|
|
public $POINT_FOR_USING_A_BADGE = 100; |
17
|
|
|
public $POINT_DEFAULT = 50; |
18
|
|
|
public $DATE_FORMAT = 'Y-m-d\TH:i:s\Z'; |
19
|
|
|
private $app; |
20
|
|
|
|
21
|
|
|
public function connect(Application $app) |
22
|
|
|
{ |
23
|
|
|
$this->app = $app; |
24
|
|
|
$factory = $app['controllers_factory']; |
25
|
|
|
# il mount point e' precedente e non serve prima |
26
|
|
|
$this->app['db']; |
27
|
|
|
R::fancyDebug(TRUE); |
28
|
|
|
$factory->get('/', array($this, 'getWorkspaceList')); |
29
|
|
|
$factory->post('/', array($this, 'createWorkspace')); |
30
|
|
|
$factory->get('/{id}', array($this, 'getWorkspace')); |
31
|
|
|
$factory->put('/{id}', array($this, 'putWorkspace')); |
32
|
|
|
$factory->delete('/{id}', array($this, 'deleteWorkspace')); |
33
|
|
|
$factory->get('/{id}/share', array($this, 'share')); |
34
|
|
|
$factory->post('/join', array($this, 'join')); |
35
|
|
|
$factory->post('/{id}/part', array($this, 'postPart')); |
36
|
|
|
$factory->get('/{id}/part/{part_id}', array($this, 'getPart')); |
37
|
|
|
$factory->put('/{id}/part/{part_id}', array($this, 'putPart')); |
38
|
|
|
$factory->delete('/{id}/part/{part_id}', array($this, 'deletePart')); |
39
|
|
|
$factory->post('/{id}/part/{part_id}/checkin', array($this, 'checkin')); |
40
|
|
|
$factory->delete('/{id}/part/{part_id}/checkin', array($this, 'deleteCheckin')); |
41
|
|
|
return $factory; |
42
|
|
|
} |
43
|
|
|
public function getSessionId() { |
44
|
|
|
$user_id = $this->app['session']->get('user')['id']; |
45
|
|
|
return $user_id; |
46
|
|
|
} |
47
|
|
|
public function getWorkspaceList(Request $request) |
48
|
|
|
{ |
49
|
|
|
//print_r("sono qui"); |
50
|
|
|
$user_id = $this->getSessionId(); |
51
|
|
|
$workspaces = R::getAll("SELECT ws.id, |
52
|
|
|
ws.title, |
53
|
|
|
ws.description, |
54
|
|
|
ws.environment, |
55
|
|
|
ws.completed |
56
|
|
|
FROM userworkspace AS uws |
57
|
|
|
LEFT JOIN workspace AS ws |
58
|
|
|
ON uws.workspace = ws.id |
59
|
|
|
WHERE uws.user = ?",[$user_id]); |
60
|
|
|
$list = []; |
61
|
|
|
foreach ($workspaces as $ws) { |
62
|
|
|
array_push($list, [ |
63
|
|
|
"id"=>intval($ws['id']), |
64
|
|
|
"title"=>$ws['title'], |
65
|
|
|
"description"=>$ws['description'], |
66
|
|
|
"environment"=>intval($ws['environment']), |
67
|
|
|
"point"=>0, //TODO fare una view con i point già calcolati per il ws |
68
|
|
|
"completed"=>boolval($ws['completed']), |
69
|
|
|
]); |
70
|
|
|
} |
71
|
|
|
$headers = []; |
72
|
|
|
return JsonResponse::create($list, 200, $headers)->setSharedMaxAge(300); |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
public function createWorkspace(Request $request) |
76
|
|
|
{ |
77
|
|
|
$user_id = $this->getSessionId(); |
78
|
|
|
$data = json_decode($request->getContent(), true); |
79
|
|
|
//TODO validate json_decode |
80
|
|
|
$title = $data['title']; |
81
|
|
|
$description = $data['description']; |
82
|
|
|
$environment = $data['environment']; |
83
|
|
|
|
84
|
|
|
$patrol = $data['team']['patrol']; |
85
|
|
|
$unit = $data['team']['unit']; |
86
|
|
|
$group = $data['team']['group']; |
87
|
|
|
|
88
|
|
|
//save the workspace get id |
89
|
|
|
$ws = R::dispense("workspace"); |
90
|
|
|
$ws->title = $title; |
91
|
|
|
$ws->description = $description; |
92
|
|
|
$ws->environment = $environment; |
93
|
|
|
$ws->completed = false; |
94
|
|
|
$ws->inserttime = date($this->DATE_FORMAT); |
95
|
|
|
$ws->lastupdatetime = date($this->DATE_FORMAT); |
96
|
|
|
$id = R::store($ws); |
97
|
|
|
|
98
|
|
|
//save the team |
99
|
|
|
$team = R::dispense("team"); |
100
|
|
|
$team->workspace = $id; |
101
|
|
|
$team->patrol = $patrol; |
102
|
|
|
$team->unit = $unit; |
103
|
|
|
$team->group = $group; |
104
|
|
|
$team_id = R::store($team); |
|
|
|
|
105
|
|
|
|
106
|
|
|
//create a phantom part to add badge |
107
|
|
|
$part = R::dispense("part"); |
108
|
|
|
$part->workspace = $id; |
109
|
|
|
$part->user = $user_id; |
110
|
|
|
$part->inserttime = date($this->DATE_FORMAT); |
111
|
|
|
$part->lastupdatetime = date($this->DATE_FORMAT); |
112
|
|
|
$part->totalpoint = 0; |
113
|
|
|
$part_id = R::store($part); |
114
|
|
|
|
115
|
|
|
//add the badge to the project |
116
|
|
View Code Duplication |
foreach ($data['badges'] as $badge_id) { |
|
|
|
|
117
|
|
|
$pb = R::dispense("partbadge"); |
118
|
|
|
$pb->badge = $badge_id; |
119
|
|
|
$pb->part = $part_id; |
120
|
|
|
$pb->inserttime = date($this->DATE_FORMAT); |
121
|
|
|
$tmp = R::store($pb); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
//add the workspace created to the user as owner |
125
|
|
|
$usw = R::dispense("userworkspace"); |
126
|
|
|
$usw->user = $user_id; |
127
|
|
|
$usw->workspace = $id; |
128
|
|
|
$usw->inserttime = date($this->DATE_FORMAT); |
129
|
|
|
R::store($usw); |
130
|
|
|
|
131
|
|
|
$res = ["id" => $id]; |
132
|
|
|
$headers = []; |
133
|
|
|
return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function putWorkspace($id,Request $request) |
137
|
|
|
{ |
138
|
|
|
$user_id = $this->getSessionId(); |
|
|
|
|
139
|
|
|
$data = json_decode($request->getContent(), true); |
140
|
|
|
//TODO validate json_decode |
141
|
|
|
|
142
|
|
|
$title = $data['title']; |
143
|
|
|
$description = $data['description']; |
144
|
|
|
$environment = $data['environment']; |
145
|
|
|
|
146
|
|
|
$patrol = $data['team']['patrol']; |
147
|
|
|
$unit = $data['team']['unit']; |
148
|
|
|
$group = $data['team']['group']; |
149
|
|
|
|
150
|
|
|
$ws = R::load("workspace",intval($id)); |
151
|
|
|
$ws->title = $title; |
152
|
|
|
$ws->description = $description; |
153
|
|
|
$ws->environment = $environment; |
154
|
|
|
$ws->completed = false; |
155
|
|
|
$ws->lastupdatetime = date($this->DATE_FORMAT); |
156
|
|
|
$id = R::store($ws); |
157
|
|
|
|
158
|
|
|
//save the team |
159
|
|
|
$team = R::findOne("team","workspace = ?",[$id]); |
160
|
|
|
$team->patrol = $patrol; |
161
|
|
|
$team->unit = $unit; |
162
|
|
|
$team->group = $group; |
163
|
|
|
$team_id = R::store($team); |
|
|
|
|
164
|
|
|
|
165
|
|
|
//TODO WE DELIBERATLY IGNORE ANY CHANGE IN BADGES AND PARTS, THEY MUST NOT BE EDITED HERE!!!! AND IF YOU DID WE DONT CARE! |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
$response = new Response(); |
169
|
|
|
$response->headers->set('Content-Type', 'text/html'); |
170
|
|
|
$response->setStatusCode(Response::HTTP_NO_CONTENT); |
171
|
|
|
$response->setSharedMaxAge(300); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function getWorkspace($id, Request $request) { |
175
|
|
|
$user_id = $this->getSessionId(); |
|
|
|
|
176
|
|
|
//TODO controllare che l'utente abbia diritto a vedere questo workspace |
177
|
|
|
|
178
|
|
|
$workspace = R::findOne("workspace", "id = ?", [$id]); |
179
|
|
|
$team = R::findOne("team", "workspace = ?", [$id]); |
180
|
|
|
$part = R::findAll("part", "workspace = ?", [$id]); |
181
|
|
|
|
182
|
|
|
$badges = R::findAll("workspacebadge", "workspace = ?", [$id]); |
183
|
|
|
|
184
|
|
|
$l_part = []; |
185
|
|
|
foreach ($part as $p) { |
186
|
|
|
array_push($l_part, intval($p['id'])); |
187
|
|
|
} |
188
|
|
|
$l_badges = []; |
189
|
|
|
foreach ($badges as $b) { |
190
|
|
|
array_push($l_badges, intval($b['badge'])); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$res = [ |
194
|
|
|
'id'=> intval($workspace['id']), |
195
|
|
|
'title'=> $workspace['title'], |
196
|
|
|
'description'=> $workspace['description'], |
197
|
|
|
'environment'=> intval($workspace['environment']), |
198
|
|
|
'completed'=> $workspace['completed'], |
199
|
|
|
'parts'=>$l_part, |
200
|
|
|
'badges'=>$l_badges, |
201
|
|
|
'team'=>[ |
202
|
|
|
'patrol'=>$team['patrol'], |
203
|
|
|
'unit'=>$team['unit'], |
204
|
|
|
'group'=>$team['group'] |
205
|
|
|
] |
206
|
|
|
]; |
207
|
|
|
$headers = []; |
208
|
|
|
return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function deleteWorkspace($id,Request $request) |
212
|
|
|
{ |
213
|
|
|
//Disassocia un utente da un workspace |
214
|
|
|
$user_id = $this->getSessionId(); |
215
|
|
|
|
216
|
|
|
$wp = R::findOne("userworkspace","workspace = ? AND user = ?",[$id,$user_id]); |
217
|
|
|
R::trash($wp); |
218
|
|
|
|
219
|
|
|
$response = new Response(); |
220
|
|
|
$response->headers->set('Content-Type', 'text/html'); |
221
|
|
|
$response->setStatusCode(Response::HTTP_NO_CONTENT); |
222
|
|
|
$response->setSharedMaxAge(300); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function share($id, Request $request) { |
226
|
|
|
$generatedKey = hash("sha256", (mt_rand(10000, 99999).time().$id)); |
227
|
|
|
//TODO verificare documentazione realtiva sulla reale entropia generata da questo sistema |
228
|
|
|
$user_id = $this->getSessionId(); |
229
|
|
|
$share = R::dispense("share"); |
230
|
|
|
$share->user = $user_id; |
231
|
|
|
$share->workspace = $id; |
232
|
|
|
$share->key = $generatedKey; |
233
|
|
|
$share->inserttime = date($this->DATE_FORMAT); |
234
|
|
|
$share_id = R::store($share); |
235
|
|
|
|
236
|
|
|
$date = new \DateTime(); |
237
|
|
|
date_add($date, date_interval_create_from_date_string('15 minutes')); |
238
|
|
|
|
239
|
|
|
$res = [ |
240
|
|
|
"id"=>$share_id, |
241
|
|
|
"key"=>$generatedKey, |
242
|
|
|
"expire"=>$date->format($this->DATE_FORMAT) |
243
|
|
|
]; |
244
|
|
|
|
245
|
|
|
$headers = []; |
246
|
|
|
return JsonResponse::create($res, 200, $headers)->setSharedMaxAge(300); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
public function join(Request $request) { |
250
|
|
|
|
251
|
|
|
|
252
|
|
|
$headers = []; |
253
|
|
|
$response = JsonResponse::create(["message"=>"No key found"], 400, $headers)->setSharedMaxAge(300); |
254
|
|
|
|
255
|
|
|
//TODO verificare documentazione realtiva sulla reale entropia generata da questo sistema |
256
|
|
|
$user_id = $this->getSessionId(); |
257
|
|
|
$data = json_decode($request->getContent(), true); |
258
|
|
|
|
259
|
|
|
$key = $data['key']; |
260
|
|
|
|
261
|
|
|
$share = R::findOne("share","key = ?",[$key]); |
262
|
|
|
echo $share->inserttime; |
263
|
|
|
if($share !== NULL){ |
264
|
|
|
$date = new \DateTime(); |
265
|
|
|
date_sub($date, date_interval_create_from_date_string('15 minutes')); |
266
|
|
|
|
267
|
|
|
$wp_id=$share['workspace']; |
268
|
|
|
|
269
|
|
|
$dateOld = new \DateTime($share->inserttime); |
270
|
|
|
if($dateOld > $date){ |
271
|
|
|
$usw = R::dispense("userworkspace"); |
272
|
|
|
$usw->user = $user_id; |
273
|
|
|
$usw->workspace = $wp_id; |
274
|
|
|
$usw->inserttime = date($this->DATE_FORMAT); |
275
|
|
|
R::store($usw); |
276
|
|
|
$headers = []; |
277
|
|
|
$response = JsonResponse::create(["id"=>$wp_id], 200, $headers)->setSharedMaxAge(300); |
278
|
|
|
|
279
|
|
View Code Duplication |
}else{ |
|
|
|
|
280
|
|
|
$headers = []; |
281
|
|
|
$response = JsonResponse::create(["message"=>"Key no more valid"], 498, $headers)->setSharedMaxAge(300); |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
return $response; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
public function postPart($id, Request $request) { |
289
|
|
|
$user_id = $this->getSessionId(); |
290
|
|
|
|
291
|
|
|
$data = json_decode($request->getContent(), true); |
292
|
|
|
//var_dump($data); |
293
|
|
|
$part = R::dispense("part"); |
294
|
|
|
$part->workspace = $id; |
295
|
|
|
$part->user = $user_id; |
296
|
|
|
$part->inserttime = date($this->DATE_FORMAT); |
297
|
|
|
$part->lastupdatetime = date($this->DATE_FORMAT); |
298
|
|
|
$part->totalpoint = 0; |
299
|
|
|
$part_id = R::store($part); |
300
|
|
|
|
301
|
|
|
|
302
|
|
|
foreach($data['part'] as $r){ //TODO va fixato nelle api |
303
|
|
|
$resource = R::dispense("resource"); |
304
|
|
|
$resource->part = $part_id; |
305
|
|
|
$resource->inserttime = date($this->DATE_FORMAT); |
306
|
|
|
$resource->updatetime = date($this->DATE_FORMAT); |
307
|
|
|
$resource->type = $r['type']; |
308
|
|
|
$resource->ref = $r['ref']; |
309
|
|
|
$resource->hash = $r['hash']; |
310
|
|
|
$resource->available = false; |
311
|
|
|
$resource->totalpoint = 0; |
312
|
|
|
$resource_id = R::store($resource); |
|
|
|
|
313
|
|
|
} |
314
|
|
|
|
315
|
|
View Code Duplication |
foreach($data['badges'] as $badge_id){ //TODO va fixato nelle api |
|
|
|
|
316
|
|
|
$pb = R::dispense("partbadge"); |
317
|
|
|
$pb->badge = $badge_id; |
318
|
|
|
$pb->part = $part_id; |
319
|
|
|
$pb->inserttime = date($this->DATE_FORMAT); |
320
|
|
|
$tmp = R::store($pb); |
|
|
|
|
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
$res = ["id"=>$part_id]; |
324
|
|
|
$headers = []; |
325
|
|
|
return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
public function getPart($id,$part_id, Request $request) { |
329
|
|
|
$user_id = $this->getSessionId(); |
330
|
|
|
|
331
|
|
|
$data = json_decode($request->getContent(), true); |
|
|
|
|
332
|
|
|
|
333
|
|
|
$part = R::findOne("part","id = ?",[$part_id]); |
334
|
|
|
|
335
|
|
|
$resource = R::findAll("resource","part = ?",[$part_id]); |
336
|
|
|
|
337
|
|
|
$partecipants = R::findAll("cero","part = ?",[$part_id]); |
338
|
|
|
|
339
|
|
|
$badges = R::findAll("partbadge","part = ? AND deleted = 0",[$part_id]); |
340
|
|
|
|
341
|
|
|
$checked = false; |
|
|
|
|
342
|
|
|
$res= [ |
343
|
|
|
"id"=>intval($part->id), |
344
|
|
|
"creation"=>$part->inserttime, |
345
|
|
|
"points"=>intval($part->points), |
346
|
|
|
"badges"=>[], |
347
|
|
|
"part"=>[], |
348
|
|
|
"partecipants"=>[] |
349
|
|
|
]; |
350
|
|
|
|
351
|
|
|
foreach($badges as $b){ |
352
|
|
|
array_push($res['badges'],$b->id); |
353
|
|
|
} |
354
|
|
|
foreach($resource as $r){ |
355
|
|
|
array_push($res['part'],[ |
356
|
|
|
"type"=>$r->type, |
357
|
|
|
"hash"=>$r->hash, |
358
|
|
|
"ref"=>$r->ref |
359
|
|
|
]); |
360
|
|
|
|
361
|
|
|
} |
362
|
|
|
foreach($partecipants as $p){ |
363
|
|
|
array_push($res['partecipants'],$p->user);//TODO forse va usato l'id del c'ero e non l'id dell'utente |
364
|
|
|
if($user_id == $r['id']) |
|
|
|
|
365
|
|
|
$checked = true; |
|
|
|
|
366
|
|
|
} |
367
|
|
|
$res['present'] = true; |
368
|
|
|
|
369
|
|
|
$headers = []; |
370
|
|
|
return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
private function getPositionInArray($array,$id){ |
374
|
|
|
$count =0; |
375
|
|
|
foreach($array as $a){ |
376
|
|
|
if($a->id === $id){ |
377
|
|
|
return $count; |
378
|
|
|
} |
379
|
|
|
$count = $count + 1; |
380
|
|
|
} |
381
|
|
|
return -1; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
public function putPart($id,$part_id, Request $request) { |
385
|
|
|
$user_id = $this->getSessionId(); |
386
|
|
|
|
387
|
|
|
$data = json_decode($request->getContent(), true); |
388
|
|
|
|
389
|
|
|
|
390
|
|
|
$part = R::load("part",$part_id); |
391
|
|
|
$part->workspace = $id; |
392
|
|
|
$part->user = $user_id; |
393
|
|
|
$part->lastupdatetime = date($this->DATE_FORMAT); |
394
|
|
|
$part->totalpoint = 0; |
395
|
|
|
$part_id = R::store($part); |
396
|
|
|
|
397
|
|
|
$delete_res=R::findAll("resource","WHERE part = ?",[$part_id]); |
398
|
|
|
|
399
|
|
|
foreach($data['part'] as $r){ //TODO va fixato nelle api |
400
|
|
|
$resource = R::findOne("resource","WHERE hash = ? AND deleted = 0",[$r['hash']]);//TODO BISOGNA FARE IL DIFF TRA QUELLE PRESENTI E QUELLE NON PRESENTI |
401
|
|
|
if($resource == 0){ |
402
|
|
|
$resource = R::dispense("resource"); |
403
|
|
|
$resource->available = false; |
404
|
|
|
$resource->inserttime = date($this->DATE_FORMAT); |
405
|
|
|
} |
406
|
|
|
$resource->part = $part_id; |
407
|
|
|
$resource->updatetime = date($this->DATE_FORMAT); |
408
|
|
|
$resource->type = $r['type']; |
409
|
|
|
$resource->ref = $r['ref']; |
410
|
|
|
$resource->hash = $r['hash']; |
411
|
|
|
$resource->totalpoint = 0; |
412
|
|
|
$resource_id = R::store($resource); |
413
|
|
|
$rem_id=$this->getPositionInArray($delete_res,$resource_id); |
414
|
|
|
if($rem_id != 0) |
415
|
|
|
array_splice($delete_res,$rem_id,1); //RIMUOVO GLI ELEMENTI CHE HO MODIFICATO |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
foreach($delete_res as $d){ |
419
|
|
|
//RIMUOVO REALMENTE DAL DB LE COSE CHE HO LASCIATO FUORI DALLA PUT (PRESENTI NEL DB MA NON NELLA NUOVA VERSIONE ODIO LE PUT) |
420
|
|
|
$resource = R::load("resource",$d->id); |
421
|
|
|
$resource->deleted=true; |
422
|
|
|
R::store($resource); |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
$delete_badge=R::findAll("partbadge","WHERE part = ? AND deleted = 0",[$part_id]); |
426
|
|
|
|
427
|
|
|
foreach($data['badges'] as $badge_id){ |
428
|
|
|
$pb = R::load("partbadge",$badge_id); |
429
|
|
|
$pb->badge = $badge_id; |
430
|
|
|
$pb->part = $part_id; |
431
|
|
|
$tmp = R::store($pb); |
432
|
|
|
$rem_id=$this->getPositionInArray($delete_badge,$tmp); |
433
|
|
|
if($rem_id != 0) |
434
|
|
|
array_splice($delete_badge,$rem_id,1); //RIMUOVO GLI ELEMENTI CHE HO MODIFICATO |
435
|
|
|
} |
436
|
|
|
|
437
|
|
View Code Duplication |
foreach($delete_badge as $d){ |
|
|
|
|
438
|
|
|
//RIMUOVO REALMENTE DAL DB LE COSE CHE HO LASCIATO FUORI DALLA PUT (PRESENTI NEL DB MA NON NELLA NUOVA VERSIONE ODIO LE PUT) |
439
|
|
|
$badge = R::load("partbadge",$d['id']);//FORSE RILOADARLI NON È NECESSARIO |
440
|
|
|
$badge->deleted=true; |
441
|
|
|
R::store($badge); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
$res = ["id"=>$part_id]; |
445
|
|
|
$headers = []; |
446
|
|
|
return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300); |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
public function deletePart($id,$part_id, Request $request) { |
450
|
|
|
$user_id = $this->getSessionId(); |
|
|
|
|
451
|
|
|
$part = R::load("part",$part_id); |
452
|
|
|
$part->deleted=true; |
453
|
|
|
R::store($part); |
454
|
|
|
|
455
|
|
|
|
456
|
|
|
$delete_badge=R::findAll("partbadge","WHERE part = ?",[$part_id]); |
457
|
|
View Code Duplication |
foreach($delete_badge as $d){ |
|
|
|
|
458
|
|
|
$badge = R::load("partbadge",[$d->id]);//FORSE RILOADARLI NON È NECESSARIO BASTA FARE $d->deleted=true; e store($d) |
459
|
|
|
$badge->deleted=true; |
460
|
|
|
R::store($badge); |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
//TODO soft delete resource! |
464
|
|
|
|
465
|
|
|
$response = new Response(); |
466
|
|
|
$response->headers->set('Content-Type', 'text/html'); |
467
|
|
|
$response->setStatusCode(Response::HTTP_NO_CONTENT); |
468
|
|
|
$response->setSharedMaxAge(300); |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
private function getPoint($badge_id,$badges){ |
472
|
|
|
foreach($badges as $b){ |
473
|
|
|
if($b->id === $badge_id){ |
474
|
|
|
if($b->completed === True){ |
475
|
|
|
echo "CASO 1;<BR />"; |
476
|
|
|
return $this->POINT_FOR_USING_A_CONQUERED_BADGE; |
477
|
|
|
}else{ |
478
|
|
|
echo "CASO 2;<BR />"; |
479
|
|
|
return $this->POINT_FOR_USING_A_BADGE; |
480
|
|
|
} |
481
|
|
|
} |
482
|
|
|
} |
483
|
|
|
echo "CASO 3;<BR />"; |
484
|
|
|
return $this->POINT_DEFAULT; |
485
|
|
|
} |
486
|
|
|
public function checkin($id,$part_id, Request $request) { |
487
|
|
|
$user_id = $this->getSessionId(); |
488
|
|
|
|
489
|
|
|
$badges = R::findAll("partbadge","part = ? AND deleted = 0",[$part_id]); |
490
|
|
|
$u_badges = R::findAll("userbadge","user = ? AND deleted = 0",[$user_id]); |
491
|
|
|
|
492
|
|
|
$point_earned = 0; |
493
|
|
|
foreach($badges as $b){ //SE CI SONO DEI BADGE |
494
|
|
|
$point = $this->getPoint($b->id,$u_badges); |
495
|
|
|
if($point != $this->POINT_DEFAULT){ //SE SEI IN CAMMINO PER QUEI BADGE O SE LI POSSIEDI GIÀ |
496
|
|
|
echo "PUNTI:".$point; |
497
|
|
|
$point_earned = $point_earned + $point; |
498
|
|
|
$pb = R::dispense("cero"); |
499
|
|
|
$pb->user = $user_id; |
500
|
|
|
$pb->part = $part_id; |
501
|
|
|
$pb->badge = $b->id; |
502
|
|
|
$pb->inserttime = date($this->DATE_FORMAT); |
503
|
|
|
$pb->points = $point; |
504
|
|
|
$tmp = R::store($pb); |
|
|
|
|
505
|
|
|
|
506
|
|
View Code Duplication |
if($point === $this->POINT_FOR_USING_A_BADGE){ //SE SEI IN CAMMINO MA NON LI HAI ANCORA RAGGIUNTI |
|
|
|
|
507
|
|
|
$ubc = R::dispense("userbadgeclove"); |
508
|
|
|
$ubc->user = $user_id; |
509
|
|
|
$ubc->badge = $b->id; |
510
|
|
|
$ubc->part = $part_id; |
511
|
|
|
$ubc->inserttime = date($this->DATE_FORMAT); |
512
|
|
|
$tmp = R::store($ubc); |
|
|
|
|
513
|
|
|
} |
514
|
|
|
} |
515
|
|
|
} |
516
|
|
|
|
517
|
|
View Code Duplication |
if($point_earned <= 0){ //SE NON CI SONO BADGE O SE TU NON SEI IN CAMMINO PER NESSUNO DI LORO |
|
|
|
|
518
|
|
|
$pb = R::dispense("cero"); |
519
|
|
|
$pb->user = $user_id; |
520
|
|
|
$pb->part = $part_id; |
521
|
|
|
$pb->inserttime = date($this->DATE_FORMAT); |
522
|
|
|
$pb->points = $this->POINT_DEFAULT; |
523
|
|
|
$tmp = R::store($pb); |
|
|
|
|
524
|
|
|
} |
525
|
|
|
$res = ["points"=>$point_earned]; |
526
|
|
|
$headers = []; |
527
|
|
|
return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300); |
528
|
|
|
|
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
public function deleteCheckin($id,$part_id, Request $request) { |
532
|
|
|
$user_id = $this->getSessionId(); |
533
|
|
|
|
534
|
|
|
$u_badges = R::findAll("userbadge","user = ? AND part = ?",[$user_id,$part_id]); |
535
|
|
|
R::trashAll($u_badges); |
536
|
|
|
|
537
|
|
|
$cero = R::findAll("cero","user = ? AND part = ?",[$user_id,$part_id]); |
538
|
|
|
R::trashAll($cero); |
539
|
|
|
|
540
|
|
|
$response = new Response(); |
541
|
|
|
$response->headers->set('Content-Type', 'text/html'); |
542
|
|
|
$response->setStatusCode(Response::HTTP_NO_CONTENT); |
543
|
|
|
$response->setSharedMaxAge(300); |
544
|
|
|
return $response; |
545
|
|
|
|
546
|
|
|
} |
547
|
|
|
} |
548
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.