|
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/$id_part', array($this, 'getPart')); |
|
33
|
|
|
$factory->patch('/{id}/part/$id_part/checkin', array($this, 'checkin')); |
|
34
|
|
|
return $factory; |
|
35
|
|
|
} |
|
36
|
|
|
public function getSessionId() { |
|
37
|
|
|
$user_id = $this->app['session']->get('user')['id']; |
|
38
|
|
|
return $user_id; |
|
39
|
|
|
} |
|
40
|
|
|
public function getWorkspaceList(Request $request) |
|
41
|
|
|
{ |
|
42
|
|
|
$user_id = $this->getSessionId(); |
|
43
|
|
|
$workspaces = R::getAll("SELECT ws.id, |
|
44
|
|
|
ws.title, |
|
45
|
|
|
ws.description, |
|
46
|
|
|
ws.environment, |
|
47
|
|
|
ws.completed |
|
48
|
|
|
FROM userworkspace AS uws |
|
49
|
|
|
LEFT JOIN workspace AS ws |
|
50
|
|
|
ON uws.workspace = ws.id |
|
51
|
|
|
WHERE uws.user = ?",[$user_id]); |
|
52
|
|
|
$list = []; |
|
53
|
|
|
foreach ($workspaces as $ws) { |
|
54
|
|
|
array_push($list, [ |
|
55
|
|
|
"id"=>$ws['id'], |
|
56
|
|
|
"title"=>$ws['title'], |
|
57
|
|
|
"description"=>$ws['description'], |
|
58
|
|
|
"environment"=>$ws['environment'], |
|
59
|
|
|
"point"=>0, //TODO fare una view con i point già calcolati per il ws |
|
60
|
|
|
"completed"=>$ws['completed'], |
|
61
|
|
|
]); |
|
62
|
|
|
} |
|
63
|
|
|
$headers = []; |
|
64
|
|
|
return JsonResponse::create($list, 200, $headers)->setSharedMaxAge(300); |
|
65
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
public function createWorkspace(Request $request) |
|
68
|
|
|
{ |
|
69
|
|
|
$user_id = $this->getSessionId(); |
|
70
|
|
|
$counter = 0; |
|
|
|
|
|
|
71
|
|
|
$data = json_decode($request->getContent(), true); |
|
72
|
|
|
//TODO validate json_decode |
|
73
|
|
|
$title = $data['title']; |
|
74
|
|
|
$description = $data['description']; |
|
75
|
|
|
$environment = $data['environment']; |
|
76
|
|
|
|
|
77
|
|
|
$patrol = $data['team']['patrol']; |
|
78
|
|
|
$unit = $data['team']['unit']; |
|
79
|
|
|
$group = $data['team']['group']; |
|
80
|
|
|
|
|
81
|
|
|
//save the workspace get id |
|
82
|
|
|
$ws = R::dispense("workspace"); |
|
83
|
|
|
$ws->title = $title; |
|
84
|
|
|
$ws->description = $description; |
|
85
|
|
|
$ws->environment = $environment; |
|
86
|
|
|
$ws->completed = false; |
|
87
|
|
|
$ws->inserttime = date('Y-m-d H:i:s'); |
|
88
|
|
|
$ws->lastupdatetime = date('Y-m-d H:i:s'); |
|
89
|
|
|
$id = R::store($ws); |
|
90
|
|
|
|
|
91
|
|
|
//save the team |
|
92
|
|
|
$team = R::dispense("team"); |
|
93
|
|
|
$team->workspace = $id; |
|
94
|
|
|
$team->patrol = $patrol; |
|
95
|
|
|
$team->unit = $unit; |
|
96
|
|
|
$team->group = $group; |
|
97
|
|
|
$team_id = R::store($team); |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
//create a phantom part to add badge |
|
100
|
|
|
$part = R::dispense("part"); |
|
101
|
|
|
$part->workspace = $id; |
|
102
|
|
|
$part->user = $user_id; |
|
103
|
|
|
$part->inserttime = date('Y-m-d H:i:s'); |
|
104
|
|
|
$part->lastupdatetime = date('Y-m-d H:i:s'); |
|
105
|
|
|
$part->totalpoint = 0; |
|
106
|
|
|
$part_id = R::store($part); |
|
107
|
|
|
|
|
108
|
|
|
//add the badge to the project |
|
109
|
|
View Code Duplication |
foreach ($data['badges'] as $badge_id) { |
|
|
|
|
|
|
110
|
|
|
//TODO insert those badge as first hidden post |
|
111
|
|
|
$pb = R::dispense("partbadge"); |
|
112
|
|
|
$pb->badge = $badge_id; |
|
113
|
|
|
$pb->part = $part_id; |
|
114
|
|
|
$pb->inserttime = date('Y-m-d H:i:s'); |
|
115
|
|
|
$tmp = R::store($pb); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
//add the workspace created to the user as owner |
|
119
|
|
|
$usw = R::dispense("userworkspace"); |
|
120
|
|
|
$usw->user = $user_id; |
|
121
|
|
|
$usw->workspace = $id; |
|
122
|
|
|
$usw->inserttime = date('Y-m-d H:i:s'); |
|
123
|
|
|
R::store($usw); |
|
124
|
|
|
|
|
125
|
|
|
$res = ["id" => $id]; |
|
126
|
|
|
$headers = []; |
|
127
|
|
|
return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function getWorkspace($id, Request $request) { |
|
131
|
|
|
$user_id = $this->getSessionId(); |
|
|
|
|
|
|
132
|
|
|
//TODO controllare che l'utente abbia diritto a vedere questo workspace |
|
133
|
|
|
|
|
134
|
|
|
$workspace = R::findOne("workspace", "id = ?", [$id]); |
|
135
|
|
|
$part = R::findAll("part", "workspace = ?", [$id]); |
|
136
|
|
|
|
|
137
|
|
|
$badges = R::findAll("workspacebadge", "workspace = ?", [$id]); |
|
138
|
|
|
|
|
139
|
|
|
$l_part = []; |
|
140
|
|
|
foreach ($part as $p) { |
|
141
|
|
|
array_push($l_part, intval($p['id'])); |
|
142
|
|
|
} |
|
143
|
|
|
$l_badges = []; |
|
144
|
|
|
foreach ($badges as $b) { |
|
145
|
|
|
array_push($l_badges, intval($b['badge'])); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
$res = [ |
|
149
|
|
|
'id'=> $workspace['id'], |
|
150
|
|
|
'title'=> $workspace['title'], |
|
151
|
|
|
'description'=> $workspace['description'], |
|
152
|
|
|
'environment'=> $workspace['environment'], |
|
153
|
|
|
'environment'=> $workspace['environment'], |
|
154
|
|
|
'completed'=> $workspace['completed'], |
|
155
|
|
|
'parts'=>$l_part, |
|
156
|
|
|
'badges'=>$l_badges |
|
157
|
|
|
]; |
|
158
|
|
|
$headers = []; |
|
159
|
|
|
return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
public function share($id, Request $request) { |
|
163
|
|
|
$generatedKey = hash("sha256", (mt_rand(10000, 99999).time().$id)); |
|
164
|
|
|
//TODO verificare documentazione realtiva sulla reale entropia generata da questo sistema |
|
165
|
|
|
$user_id = $this->getSessionId(); |
|
166
|
|
|
$share = R::dispense("share"); |
|
167
|
|
|
$share->user = $user_id; |
|
168
|
|
|
$share->workspace = $id; |
|
169
|
|
|
$share->key = $generatedKey; |
|
170
|
|
|
$share->inserttime = date('Y-m-d H:i:s'); |
|
171
|
|
|
$share_id = R::store($share); |
|
172
|
|
|
|
|
173
|
|
|
$date = new \DateTime(); |
|
174
|
|
|
date_add($date, date_interval_create_from_date_string('15 minutes')); |
|
175
|
|
|
|
|
176
|
|
|
$res = [ |
|
177
|
|
|
"id"=>$share_id, |
|
178
|
|
|
"key"=>$generatedKey, |
|
179
|
|
|
"expire"=>$date->format('Y-m-d H:i:s') |
|
180
|
|
|
]; |
|
181
|
|
|
|
|
182
|
|
|
$headers = []; |
|
183
|
|
|
return JsonResponse::create($res, 200, $headers)->setSharedMaxAge(300); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function postPart($id, Request $request) { |
|
187
|
|
|
$user_id = $this->getSessionId(); |
|
188
|
|
|
|
|
189
|
|
|
$data = json_decode($request->getContent(), true); |
|
190
|
|
|
|
|
191
|
|
|
$part = R::dispense("part"); |
|
192
|
|
|
$part->workspace = $id; |
|
193
|
|
|
$part->user = $user_id; |
|
194
|
|
|
$part->inserttime = date('Y-m-d H:i:s'); |
|
195
|
|
|
$part->lastupdatetime = date('Y-m-d H:i:s'); |
|
196
|
|
|
$part->totalpoint = 0; |
|
197
|
|
|
$part_id = R::store($part); |
|
198
|
|
|
|
|
199
|
|
|
foreach($data['part'] as $r){ //TODO va fixato nelle api |
|
200
|
|
|
if($r->type != "badge"){ |
|
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
|
|
|
|
|
213
|
|
View Code Duplication |
foreach($data['badges'] as $badge_id){ //TODO va fixato nelle api |
|
|
|
|
|
|
214
|
|
|
$pb = R::dispense("partbadge"); |
|
215
|
|
|
$pb->badge = $badge_id; |
|
216
|
|
|
$pb->part = $part_id; |
|
217
|
|
|
$pb->inserttime = date('Y-m-d H:i:s'); |
|
218
|
|
|
$tmp = R::store($pb); |
|
|
|
|
|
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
$res = ["id"=>$part_id]; |
|
222
|
|
|
$headers = []; |
|
223
|
|
|
return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
public function getPart($id,$part_id,$request) { |
|
227
|
|
|
$user_id = $this->getSessionId(); |
|
|
|
|
|
|
228
|
|
|
|
|
229
|
|
|
$data = json_decode($request->getContent(), true); |
|
|
|
|
|
|
230
|
|
|
|
|
231
|
|
|
$part = R::finOne("part","id = ?",[$part_id]); |
|
232
|
|
|
|
|
233
|
|
|
$resource = R::findAll("resource","part = ?",[$part_id]); |
|
234
|
|
|
|
|
235
|
|
|
$partecipants = R::findAll("cero","part = ?",[$part_id]); |
|
236
|
|
|
|
|
237
|
|
|
$badges = R::findAll("partbadge","part = ?",[$part_id]); |
|
238
|
|
|
|
|
239
|
|
|
$res= [ |
|
240
|
|
|
"id"=>$part->id, |
|
241
|
|
|
"creation"=>$part->inserttime, |
|
242
|
|
|
"points"=>$part->points, |
|
243
|
|
|
"checked"=>$part->checked, |
|
244
|
|
|
"badges"=>[], |
|
245
|
|
|
"part"=>[], |
|
246
|
|
|
"partecipants"=>[] |
|
247
|
|
|
]; |
|
248
|
|
|
|
|
249
|
|
|
foreach($badges as $b){ |
|
250
|
|
|
array_push($res['badges'],$b->id); |
|
251
|
|
|
} |
|
252
|
|
|
foreach($resource as $r){ |
|
253
|
|
|
array_push($res['part'],[ |
|
254
|
|
|
"type"=>$r->type, |
|
255
|
|
|
"hash"=>$r->hash, |
|
256
|
|
|
"ref"=>$r->ref |
|
257
|
|
|
]); |
|
258
|
|
|
} |
|
259
|
|
|
foreach($partecipants as $p){ |
|
260
|
|
|
array_push($res['partecipants'],$p->user);//TODO forse va usato l'id del c'ero e non l'id dell'utente |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
$headers = []; |
|
264
|
|
|
return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300); |
|
265
|
|
|
} |
|
266
|
|
|
private function getPoint($badge_id,$badges){ |
|
|
|
|
|
|
267
|
|
|
foreach($badges as $b){ |
|
268
|
|
|
if($b->id === $badge_id){ |
|
269
|
|
|
if($b->completed === True) |
|
270
|
|
|
return POINT_FOR_USING_A_CONQUERED_BADGE; |
|
271
|
|
|
else |
|
272
|
|
|
return POINT_FOR_USING_A_BADGE; |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
return POINT_DEFAULT; |
|
276
|
|
|
} |
|
277
|
|
|
public function checkin($id,$part_id,$request) { |
|
278
|
|
|
$user_id = $this->getSessionId(); |
|
279
|
|
|
|
|
280
|
|
|
$badges = R::findAll("partbadge","part = ?",[$part_id]); |
|
281
|
|
|
$u_badges = R::findAll("userbadge","user = ?",[$user_id]); |
|
282
|
|
|
|
|
283
|
|
|
foreach($badges as $b){ |
|
284
|
|
|
$point = getPoint($b->id,$u_badges); |
|
285
|
|
|
$pb = R::dispense("cero"); |
|
286
|
|
|
$pb->user = $user_id; |
|
287
|
|
|
$pb->part = $part_id; |
|
288
|
|
|
$pb->badge = $b->id; |
|
289
|
|
|
$pb->inserttime = date('Y-m-d H:i:s'); |
|
290
|
|
|
$bp->points = $point; |
|
|
|
|
|
|
291
|
|
|
$tmp = R::store($pb); |
|
|
|
|
|
|
292
|
|
|
|
|
293
|
|
|
if($point === POINT_FOR_USING_A_BADGE){ |
|
294
|
|
|
$ubc = R::dispense("userbadgeclove"); |
|
295
|
|
|
$ubc->user = $user_id; |
|
296
|
|
|
$ubc->badge = $b->id; |
|
297
|
|
|
$ubc->part = $part_id; |
|
298
|
|
|
$ubc->inserttime = date('Y-m-d H:i:s'); |
|
299
|
|
|
$tmp = R::store($ubc); |
|
|
|
|
|
|
300
|
|
|
} |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
|
|
304
|
|
|
} |
|
305
|
|
|
} |
|
306
|
|
|
|
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.