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
|
|
View Code Duplication |
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->deleted = false; |
114
|
|
|
$part_id = R::store($part); |
115
|
|
|
|
116
|
|
|
//add the badge to the project |
117
|
|
View Code Duplication |
foreach ($data['badges'] as $badge_id) { |
|
|
|
|
118
|
|
|
$pb = R::dispense("partbadge"); |
119
|
|
|
$pb->badge = $badge_id; |
120
|
|
|
$pb->part = $part_id; |
121
|
|
|
$pb->inserttime = date($this->DATE_FORMAT); |
122
|
|
|
$tmp = R::store($pb); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
//add the workspace created to the user as owner |
126
|
|
|
$usw = R::dispense("userworkspace"); |
127
|
|
|
$usw->user = $user_id; |
128
|
|
|
$usw->workspace = $id; |
129
|
|
|
$usw->inserttime = date($this->DATE_FORMAT); |
130
|
|
|
R::store($usw); |
131
|
|
|
|
132
|
|
|
$res = ["id" => $id]; |
133
|
|
|
$headers = []; |
134
|
|
|
return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function putWorkspace($id, Request $request) |
138
|
|
|
{ |
139
|
|
|
$user_id = $this->getSessionId(); |
|
|
|
|
140
|
|
|
$data = json_decode($request->getContent(), true); |
141
|
|
|
//TODO validate json_decode |
142
|
|
|
|
143
|
|
|
$title = $data['title']; |
144
|
|
|
$description = $data['description']; |
145
|
|
|
$environment = $data['environment']; |
146
|
|
|
|
147
|
|
|
$patrol = $data['team']['patrol']; |
148
|
|
|
$unit = $data['team']['unit']; |
149
|
|
|
$group = $data['team']['group']; |
150
|
|
|
|
151
|
|
|
$ws = R::load("workspace", intval($id)); |
152
|
|
|
$ws->title = $title; |
153
|
|
|
$ws->description = $description; |
154
|
|
|
$ws->environment = $environment; |
155
|
|
|
$ws->completed = false; |
156
|
|
|
$ws->lastupdatetime = date($this->DATE_FORMAT); |
157
|
|
|
$id = R::store($ws); |
158
|
|
|
|
159
|
|
|
//save the team |
160
|
|
|
$team = R::findOne("team", "workspace = ?", [$id]); |
161
|
|
|
$team->patrol = $patrol; |
162
|
|
|
$team->unit = $unit; |
163
|
|
|
$team->group = $group; |
164
|
|
|
$team_id = R::store($team); |
|
|
|
|
165
|
|
|
|
166
|
|
|
//TODO WE DELIBERATLY IGNORE ANY CHANGE IN BADGES AND PARTS, THEY MUST NOT BE EDITED HERE!!!! AND IF YOU DID WE DONT CARE! |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
$response = new Response(); |
170
|
|
|
$response->headers->set('Content-Type', 'text/html'); |
171
|
|
|
$response->setStatusCode(Response::HTTP_NO_CONTENT); |
172
|
|
|
$response->setSharedMaxAge(300); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function getWorkspace($id, Request $request) { |
176
|
|
|
$user_id = $this->getSessionId(); |
|
|
|
|
177
|
|
|
//TODO controllare che l'utente abbia diritto a vedere questo workspace |
178
|
|
|
|
179
|
|
|
$workspace = R::findOne("workspace", "id = ?", [$id]); |
180
|
|
|
$team = R::findOne("team", "workspace = ?", [$id]); |
181
|
|
|
$part = R::findAll("part", "workspace = ? AND DELETED = 0", [$id]); |
182
|
|
|
|
183
|
|
|
$badges = R::findAll("workspacebadge", "workspace = ?", [$id]); //TODO controllare i deleted |
184
|
|
|
|
185
|
|
|
$l_part = []; |
186
|
|
|
foreach ($part as $p) { |
187
|
|
|
array_push($l_part, intval($p['id'])); |
188
|
|
|
} |
189
|
|
|
$l_badges = []; |
190
|
|
|
foreach ($badges as $b) { |
191
|
|
|
array_push($l_badges, intval($b['badge'])); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$res = [ |
195
|
|
|
'id'=> intval($workspace['id']), |
196
|
|
|
'title'=> $workspace['title'], |
197
|
|
|
'description'=> $workspace['description'], |
198
|
|
|
'environment'=> intval($workspace['environment']), |
199
|
|
|
'completed'=> $workspace['completed'], |
200
|
|
|
'parts'=>$l_part, |
201
|
|
|
'badges'=>$l_badges, |
202
|
|
|
'team'=>[ |
203
|
|
|
'patrol'=>$team['patrol'], |
204
|
|
|
'unit'=>$team['unit'], |
205
|
|
|
'group'=>$team['group'] |
206
|
|
|
] |
207
|
|
|
]; |
208
|
|
|
$headers = []; |
209
|
|
|
return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function deleteWorkspace($id, Request $request) |
213
|
|
|
{ |
214
|
|
|
//Disassocia un utente da un workspace |
215
|
|
|
$user_id = $this->getSessionId(); |
216
|
|
|
|
217
|
|
|
$wp = R::findOne("userworkspace", "workspace = ? AND user = ?", [$id, $user_id]); |
218
|
|
|
R::trash($wp); |
219
|
|
|
|
220
|
|
|
$response = new Response(); |
221
|
|
|
$response->headers->set('Content-Type', 'text/html'); |
222
|
|
|
$response->setStatusCode(Response::HTTP_NO_CONTENT); |
223
|
|
|
$response->setSharedMaxAge(300); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function share($id, Request $request) { |
227
|
|
|
$generatedKey = hash("sha256", (mt_rand(10000, 99999).time().$id)); |
228
|
|
|
//TODO verificare documentazione realtiva sulla reale entropia generata da questo sistema |
229
|
|
|
$user_id = $this->getSessionId(); |
230
|
|
|
$share = R::dispense("share"); |
231
|
|
|
$share->user = $user_id; |
232
|
|
|
$share->workspace = $id; |
233
|
|
|
$share->key = $generatedKey; |
234
|
|
|
$share->inserttime = date($this->DATE_FORMAT); |
235
|
|
|
$share_id = R::store($share); |
236
|
|
|
|
237
|
|
|
$date = new \DateTime(); |
238
|
|
|
date_add($date, date_interval_create_from_date_string('15 minutes')); |
239
|
|
|
|
240
|
|
|
$res = [ |
241
|
|
|
"id"=>$share_id, |
242
|
|
|
"key"=>$generatedKey, |
243
|
|
|
"expire"=>$date->format($this->DATE_FORMAT) |
244
|
|
|
]; |
245
|
|
|
|
246
|
|
|
$headers = []; |
247
|
|
|
return JsonResponse::create($res, 200, $headers)->setSharedMaxAge(300); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function join(Request $request) { |
251
|
|
|
|
252
|
|
|
|
253
|
|
|
$headers = []; |
254
|
|
|
$response = JsonResponse::create(["message"=>"No key found"], 400, $headers)->setSharedMaxAge(300); |
255
|
|
|
|
256
|
|
|
//TODO verificare documentazione realtiva sulla reale entropia generata da questo sistema |
257
|
|
|
$user_id = $this->getSessionId(); |
258
|
|
|
$data = json_decode($request->getContent(), true); |
259
|
|
|
|
260
|
|
|
$key = $data['key']; |
261
|
|
|
|
262
|
|
|
$share = R::findOne("share", "key = ?", [$key]); |
263
|
|
|
echo $share->inserttime; |
264
|
|
|
if ($share !== NULL) { |
265
|
|
|
$date = new \DateTime(); |
266
|
|
|
date_sub($date, date_interval_create_from_date_string('15 minutes')); |
267
|
|
|
|
268
|
|
|
$wp_id = $share['workspace']; |
269
|
|
|
|
270
|
|
|
$dateOld = new \DateTime($share->inserttime); |
271
|
|
|
if ($dateOld > $date) { |
272
|
|
|
$usw = R::dispense("userworkspace"); |
273
|
|
|
$usw->user = $user_id; |
274
|
|
|
$usw->workspace = $wp_id; |
275
|
|
|
$usw->inserttime = date($this->DATE_FORMAT); |
276
|
|
|
R::store($usw); |
277
|
|
|
$headers = []; |
278
|
|
|
$response = JsonResponse::create(["id"=>$wp_id], 200, $headers)->setSharedMaxAge(300); |
279
|
|
|
|
280
|
|
View Code Duplication |
}else { |
|
|
|
|
281
|
|
|
$headers = []; |
282
|
|
|
$response = JsonResponse::create(["message"=>"Key no more valid"], 498, $headers)->setSharedMaxAge(300); |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
return $response; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
public function postPart($id, Request $request) {//TODO quando uno crea una parte bisognerebbe dire che lui c'era in quella parte |
290
|
|
|
$user_id = $this->getSessionId(); |
291
|
|
|
|
292
|
|
|
$data = json_decode($request->getContent(), true); |
293
|
|
|
//var_dump($data); |
294
|
|
|
$part = R::dispense("part"); |
295
|
|
|
$part->workspace = $id; |
296
|
|
|
$part->user = $user_id; |
297
|
|
|
$part->inserttime = date($this->DATE_FORMAT); |
298
|
|
|
$part->lastupdatetime = date($this->DATE_FORMAT); |
299
|
|
|
$part->totalpoint = 0; |
300
|
|
|
$part->deleted = false; |
301
|
|
|
$part_id = R::store($part); |
302
|
|
|
|
303
|
|
|
|
304
|
|
|
foreach ($data['part'] as $r) { //TODO va fixato nelle api |
305
|
|
|
$resource = R::dispense("resource"); |
306
|
|
|
$resource->part = $part_id; |
307
|
|
|
$resource->inserttime = date($this->DATE_FORMAT); |
308
|
|
|
$resource->updatetime = date($this->DATE_FORMAT); |
309
|
|
|
$resource->type = $r['type']; |
310
|
|
|
$resource->ref = $r['ref']; |
311
|
|
|
$resource->hash = $r['hash']; |
312
|
|
|
$resource->available = false; |
313
|
|
|
$resource->totalpoint = 0; |
314
|
|
|
$resource_id = R::store($resource); |
|
|
|
|
315
|
|
|
} |
316
|
|
|
|
317
|
|
View Code Duplication |
foreach ($data['badges'] as $badge_id) { //TODO va fixato nelle api |
|
|
|
|
318
|
|
|
$pb = R::dispense("partbadge"); |
319
|
|
|
$pb->badge = $badge_id; |
320
|
|
|
$pb->part = $part_id; |
321
|
|
|
$pb->inserttime = date($this->DATE_FORMAT); |
322
|
|
|
$tmp = R::store($pb); |
|
|
|
|
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
$res = ["id"=>$part_id]; |
326
|
|
|
$headers = []; |
327
|
|
|
return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
public function getPart($id, $part_id, Request $request) { |
331
|
|
|
$user_id = $this->getSessionId(); |
332
|
|
|
|
333
|
|
|
$data = json_decode($request->getContent(), true); |
|
|
|
|
334
|
|
|
|
335
|
|
|
$part = R::findOne("part", "id = ?", [$part_id]); |
336
|
|
|
|
337
|
|
|
$resource = R::findAll("resource", "part = ?", [$part_id]); |
338
|
|
|
|
339
|
|
|
$partecipants = R::findAll("cero", "part = ?", [$part_id]); |
340
|
|
|
|
341
|
|
|
$badges = R::findAll("partbadge", "part = ? AND deleted = 0", [$part_id]); |
342
|
|
|
|
343
|
|
|
$checked = false; |
|
|
|
|
344
|
|
|
$res = [ |
345
|
|
|
"id"=>intval($part->id), |
346
|
|
|
"creation"=>$part->inserttime, |
347
|
|
|
"points"=>intval($part->points), |
348
|
|
|
"badges"=>[], |
349
|
|
|
"part"=>[], |
350
|
|
|
"partecipants"=>[] |
351
|
|
|
]; |
352
|
|
|
|
353
|
|
|
foreach ($badges as $b) { |
354
|
|
|
array_push($res['badges'], $b->id); |
355
|
|
|
} |
356
|
|
|
foreach ($resource as $r) { |
357
|
|
|
array_push($res['part'], [ |
358
|
|
|
"type"=>$r->type, |
359
|
|
|
"hash"=>$r->hash, |
360
|
|
|
"ref"=>$r->ref |
361
|
|
|
]); |
362
|
|
|
|
363
|
|
|
} |
364
|
|
|
foreach ($partecipants as $p) { |
365
|
|
|
array_push($res['partecipants'], $p->user); //TODO forse va usato l'id del c'ero e non l'id dell'utente |
366
|
|
|
if ($user_id == $r['id']) { |
|
|
|
|
367
|
|
|
$checked = true; |
|
|
|
|
368
|
|
|
} |
369
|
|
|
} |
370
|
|
|
$res['present'] = true; |
371
|
|
|
|
372
|
|
|
$headers = []; |
373
|
|
|
return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
private function getPositionInArray($array, $id) { |
377
|
|
|
$count = 0; |
378
|
|
|
foreach ($array as $a) { |
379
|
|
|
if ($a->id === $id) { |
380
|
|
|
return $count; |
381
|
|
|
} |
382
|
|
|
$count = $count + 1; |
383
|
|
|
} |
384
|
|
|
return -1; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
public function putPart($id, $part_id, Request $request) { |
388
|
|
|
$user_id = $this->getSessionId(); |
389
|
|
|
|
390
|
|
|
$data = json_decode($request->getContent(), true); |
391
|
|
|
|
392
|
|
|
|
393
|
|
|
$part = R::load("part", $part_id); |
394
|
|
|
$part->workspace = $id; |
395
|
|
|
$part->user = $user_id; |
396
|
|
|
$part->lastupdatetime = date($this->DATE_FORMAT); |
397
|
|
|
$part->totalpoint = 0; |
398
|
|
|
$part->deleted = false; |
399
|
|
|
$part_id = R::store($part); |
400
|
|
|
|
401
|
|
|
$delete_res = R::findAll("resource", "WHERE part = ?", [$part_id]); |
402
|
|
|
|
403
|
|
|
foreach ($data['part'] as $r) { //TODO va fixato nelle api |
404
|
|
|
$resource = R::findOne("resource", "WHERE hash = ? AND deleted = 0", [$r['hash']]); //TODO BISOGNA FARE IL DIFF TRA QUELLE PRESENTI E QUELLE NON PRESENTI |
405
|
|
|
if ($resource == 0) { |
406
|
|
|
$resource = R::dispense("resource"); |
407
|
|
|
$resource->available = false; |
408
|
|
|
$resource->inserttime = date($this->DATE_FORMAT); |
409
|
|
|
} |
410
|
|
|
$resource->part = $part_id; |
411
|
|
|
$resource->updatetime = date($this->DATE_FORMAT); |
412
|
|
|
$resource->type = $r['type']; |
413
|
|
|
$resource->ref = $r['ref']; |
414
|
|
|
$resource->hash = $r['hash']; |
415
|
|
|
$resource->totalpoint = 0; |
416
|
|
|
$resource_id = R::store($resource); |
417
|
|
|
$rem_id = $this->getPositionInArray($delete_res, $resource_id); |
418
|
|
|
if ($rem_id != 0) { |
419
|
|
|
array_splice($delete_res, $rem_id, 1); |
420
|
|
|
} |
421
|
|
|
//RIMUOVO GLI ELEMENTI CHE HO MODIFICATO |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
foreach ($delete_res as $d) { |
425
|
|
|
//RIMUOVO REALMENTE DAL DB LE COSE CHE HO LASCIATO FUORI DALLA PUT (PRESENTI NEL DB MA NON NELLA NUOVA VERSIONE ODIO LE PUT) |
426
|
|
|
$resource = R::load("resource", $d->id); |
427
|
|
|
$resource->deleted = true; |
428
|
|
|
R::store($resource); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
$delete_badge = R::findAll("partbadge", "WHERE part = ? AND deleted = 0", [$part_id]); |
432
|
|
|
|
433
|
|
|
foreach ($data['badges'] as $badge_id) { |
434
|
|
|
$pb = R::load("partbadge", $badge_id); |
435
|
|
|
$pb->badge = $badge_id; |
436
|
|
|
$pb->part = $part_id; |
437
|
|
|
$tmp = R::store($pb); |
438
|
|
|
$rem_id = $this->getPositionInArray($delete_badge, $tmp); |
439
|
|
|
if ($rem_id != 0) { |
440
|
|
|
array_splice($delete_badge, $rem_id, 1); |
441
|
|
|
} |
442
|
|
|
//RIMUOVO GLI ELEMENTI CHE HO MODIFICATO |
443
|
|
|
} |
444
|
|
|
|
445
|
|
View Code Duplication |
foreach ($delete_badge as $d) { |
|
|
|
|
446
|
|
|
//RIMUOVO REALMENTE DAL DB LE COSE CHE HO LASCIATO FUORI DALLA PUT (PRESENTI NEL DB MA NON NELLA NUOVA VERSIONE ODIO LE PUT) |
447
|
|
|
$badge = R::load("partbadge", $d['id']); //FORSE RILOADARLI NON È NECESSARIO |
448
|
|
|
$badge->deleted = true; |
449
|
|
|
R::store($badge); |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
$res = ["id"=>$part_id]; |
453
|
|
|
$headers = []; |
454
|
|
|
return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300); |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
public function deletePart($id, $part_id, Request $request) { |
458
|
|
|
$user_id = $this->getSessionId(); |
|
|
|
|
459
|
|
|
$part = R::load("part", $part_id); |
460
|
|
|
$part->deleted = true; |
461
|
|
|
R::store($part); |
462
|
|
|
|
463
|
|
|
|
464
|
|
|
$delete_badge = R::findAll("partbadge", "WHERE part = ?", [$part_id]); |
465
|
|
View Code Duplication |
foreach ($delete_badge as $d) { |
|
|
|
|
466
|
|
|
$badge = R::load("partbadge", [$d->id]); //FORSE RILOADARLI NON È NECESSARIO BASTA FARE $d->deleted=true; e store($d) |
467
|
|
|
$badge->deleted = true; |
468
|
|
|
R::store($badge); |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
//TODO soft delete resource! |
472
|
|
|
|
473
|
|
|
$response = new Response(); |
474
|
|
|
$response->headers->set('Content-Type', 'text/html'); |
475
|
|
|
$response->setStatusCode(Response::HTTP_NO_CONTENT); |
476
|
|
|
$response->setSharedMaxAge(300); |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
private function getPoint($badge_id, $badges) { |
480
|
|
|
foreach ($badges as $b) { |
481
|
|
|
if ($b->id === $badge_id) { |
482
|
|
|
if ($b->completed === True) { |
483
|
|
|
echo "CASO 1;<BR />"; |
484
|
|
|
return $this->POINT_FOR_USING_A_CONQUERED_BADGE; |
485
|
|
|
}else { |
486
|
|
|
echo "CASO 2;<BR />"; |
487
|
|
|
return $this->POINT_FOR_USING_A_BADGE; |
488
|
|
|
} |
489
|
|
|
} |
490
|
|
|
} |
491
|
|
|
echo "CASO 3;<BR />"; |
492
|
|
|
return $this->POINT_DEFAULT; |
493
|
|
|
} |
494
|
|
|
public function checkin($id, $part_id, Request $request) { |
495
|
|
|
$user_id = $this->getSessionId(); |
496
|
|
|
|
497
|
|
|
$badges = R::findAll("partbadge", "part = ? AND deleted = 0", [$part_id]); |
498
|
|
|
$u_badges = R::findAll("userbadge", "user = ? AND deleted = 0", [$user_id]); |
499
|
|
|
|
500
|
|
|
$point_earned = 0; |
501
|
|
|
foreach ($badges as $b) { //SE CI SONO DEI BADGE |
502
|
|
|
$point = $this->getPoint($b->id, $u_badges); |
503
|
|
|
if ($point != $this->POINT_DEFAULT) { //SE SEI IN CAMMINO PER QUEI BADGE O SE LI POSSIEDI GIÀ |
504
|
|
|
echo "PUNTI:".$point; |
505
|
|
|
$point_earned = $point_earned + $point; |
506
|
|
|
$pb = R::dispense("cero"); |
507
|
|
|
$pb->user = $user_id; |
508
|
|
|
$pb->part = $part_id; |
509
|
|
|
$pb->badge = $b->id; |
510
|
|
|
$pb->inserttime = date($this->DATE_FORMAT); |
511
|
|
|
$pb->points = $point; |
512
|
|
|
$tmp = R::store($pb); |
|
|
|
|
513
|
|
|
|
514
|
|
View Code Duplication |
if ($point === $this->POINT_FOR_USING_A_BADGE) { //SE SEI IN CAMMINO MA NON LI HAI ANCORA RAGGIUNTI |
|
|
|
|
515
|
|
|
$ubc = R::dispense("userbadgeclove"); |
516
|
|
|
$ubc->user = $user_id; |
517
|
|
|
$ubc->badge = $b->id; |
518
|
|
|
$ubc->part = $part_id; |
519
|
|
|
$ubc->inserttime = date($this->DATE_FORMAT); |
520
|
|
|
$tmp = R::store($ubc); |
|
|
|
|
521
|
|
|
} |
522
|
|
|
} |
523
|
|
|
} |
524
|
|
|
|
525
|
|
View Code Duplication |
if ($point_earned <= 0) { //SE NON CI SONO BADGE O SE TU NON SEI IN CAMMINO PER NESSUNO DI LORO |
|
|
|
|
526
|
|
|
$pb = R::dispense("cero"); |
527
|
|
|
$pb->user = $user_id; |
528
|
|
|
$pb->part = $part_id; |
529
|
|
|
$pb->inserttime = date($this->DATE_FORMAT); |
530
|
|
|
$pb->points = $this->POINT_DEFAULT; |
531
|
|
|
$tmp = R::store($pb); |
|
|
|
|
532
|
|
|
} |
533
|
|
|
$res = ["points"=>intval($point_earned)]; |
534
|
|
|
$headers = []; |
535
|
|
|
return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300); |
536
|
|
|
|
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
public function deleteCheckin($id, $part_id, Request $request) { |
540
|
|
|
$user_id = $this->getSessionId(); |
541
|
|
|
|
542
|
|
|
$u_badges = R::findAll("userbadge", "user = ? AND part = ?", [$user_id, $part_id]); |
543
|
|
|
R::trashAll($u_badges); |
544
|
|
|
|
545
|
|
|
$cero = R::findAll("cero", "user = ? AND part = ?", [$user_id, $part_id]); |
546
|
|
|
R::trashAll($cero); |
547
|
|
|
|
548
|
|
|
$response = new Response(); |
549
|
|
|
$response->headers->set('Content-Type', 'text/html'); |
550
|
|
|
$response->setStatusCode(Response::HTTP_NO_CONTENT); |
551
|
|
|
$response->setSharedMaxAge(300); |
552
|
|
|
return $response; |
553
|
|
|
|
554
|
|
|
} |
555
|
|
|
} |
556
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.