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