|
@@ -1,4 +1,5 @@
|
1
|
1
|
<?php
|
|
2
|
+
|
2
|
3
|
namespace Logipro\Bundle\PhoenixJupyterBundle\Services\ApiLayer;
|
3
|
4
|
|
4
|
5
|
use Symfony\Component\HttpClient\HttpClient;
|
|
@@ -13,7 +14,7 @@ use Logipro\Phoenix\Component\PhxFileGuesser;
|
13
|
14
|
class ApiLayerService
|
14
|
15
|
{
|
15
|
16
|
private static $instance = null;
|
16
|
|
-
|
|
17
|
+
|
17
|
18
|
/**
|
18
|
19
|
* url du jupyterhub
|
19
|
20
|
*/
|
|
@@ -24,7 +25,7 @@ class ApiLayerService
|
24
|
25
|
*/
|
25
|
26
|
private $notebookApiUrl = null;
|
26
|
27
|
|
27
|
|
- /**
|
|
28
|
+ /**
|
28
|
29
|
* url de l'api jupyterhub notebook
|
29
|
30
|
*/
|
30
|
31
|
private $notebookUrl = null;
|
|
@@ -41,12 +42,12 @@ class ApiLayerService
|
41
|
42
|
|
42
|
43
|
public function __construct()
|
43
|
44
|
{
|
44
|
|
- $server = $_SERVER['JUPYTER_URL'].trim('/');
|
|
45
|
+ $server = $_SERVER['JUPYTER_URL'] . trim('/');
|
45
|
46
|
|
46
|
|
- $this->hubUrl = $server . $this->buildPath(array('hub','api'));
|
47
|
|
- $this->notebookApiUrl = $server . $this->buildPath(array('user','%s','api'));
|
48
|
|
- $this->notebookUrl = $server . $this->buildPath(array('user','%s','notebooks'));
|
49
|
|
- $this->apiUrl = $_SERVER['JUPYTER_API_URL'].trim('/') . '/';
|
|
47
|
+ $this->hubUrl = $server . $this->buildPath(array('hub', 'api'));
|
|
48
|
+ $this->notebookApiUrl = $server . $this->buildPath(array('user', '%s', 'api'));
|
|
49
|
+ $this->notebookUrl = $server . $this->buildPath(array('user', '%s', 'notebooks'));
|
|
50
|
+ $this->apiUrl = $_SERVER['JUPYTER_API_URL'] . trim('/') . '/';
|
50
|
51
|
$this->token = $_SERVER['JUPYTER_API_TOKEN'];
|
51
|
52
|
}
|
52
|
53
|
|
|
@@ -60,41 +61,35 @@ class ApiLayerService
|
60
|
61
|
*
|
61
|
62
|
* @return array
|
62
|
63
|
*/
|
63
|
|
- private function doQuery($url,$method,$path,$code = 200,$options = array())
|
|
64
|
+ private function doQuery($url, $method, $path, $code = 200, $options = array())
|
64
|
65
|
{
|
65
|
|
- if (!is_array($code))
|
66
|
|
- {
|
|
66
|
+ if (!is_array($code)) {
|
67
|
67
|
$code = array($code);
|
68
|
68
|
}
|
69
|
69
|
|
70
|
|
- $serviceUrl = trim($url,'/');
|
71
|
|
- if ($path != '')
|
72
|
|
- {
|
|
70
|
+ $serviceUrl = trim($url, '/');
|
|
71
|
+ if ($path != '') {
|
73
|
72
|
$serviceUrl .= '/' . $path;
|
74
|
73
|
}
|
75
|
74
|
|
76
|
75
|
$httpClient = HttpClient::create();
|
77
|
|
- try
|
78
|
|
- {
|
|
76
|
+ try {
|
79
|
77
|
/*if (!empty($parameters))
|
80
|
78
|
{
|
81
|
79
|
$options['query'] = $parameters;
|
82
|
80
|
}*/
|
83
|
81
|
|
84
|
82
|
$options['timeout'] = 2000000;
|
85
|
|
- $this->response = $httpClient->request($method,$serviceUrl,$options);
|
86
|
|
- if (!in_array($this->response->getStatusCode(),$code))
|
87
|
|
- {
|
|
83
|
+ $this->response = $httpClient->request($method, $serviceUrl, $options);
|
|
84
|
+ if (!in_array($this->response->getStatusCode(), $code)) {
|
88
|
85
|
/*echo 'erreur :';
|
89
|
86
|
dump($response->getContent());*/
|
90
|
87
|
// FIXME peut être mettre une erreur
|
91
|
88
|
return null;
|
92
|
89
|
}
|
93
|
|
- return \json_decode($this->response->getContent(),true);
|
|
90
|
+ return \json_decode($this->response->getContent(), true);
|
94
|
91
|
//return $response->toArray();
|
95
|
|
- }
|
96
|
|
- catch (\Exception $exp)
|
97
|
|
- {
|
|
92
|
+ } catch (\Exception $exp) {
|
98
|
93
|
return null;
|
99
|
94
|
}
|
100
|
95
|
}
|
|
@@ -107,14 +102,13 @@ class ApiLayerService
|
107
|
102
|
* @param int $code
|
108
|
103
|
* @param array $options
|
109
|
104
|
*/
|
110
|
|
- private function doApiQuery($method,$path,$code = 200,$options = array())
|
|
105
|
+ private function doApiQuery($method, $path, $code = 200, $options = array())
|
111
|
106
|
{
|
112
|
|
- if (!isset($options['headers']['Authorization']))
|
113
|
|
- {
|
|
107
|
+ if (!isset($options['headers']['Authorization'])) {
|
114
|
108
|
$options['headers']['Authorization'] = 'token ' . $_SERVER['JUPYTER_API_TOKEN'];
|
115
|
109
|
}
|
116
|
|
-
|
117
|
|
- return $this->doQuery($this->apiUrl,$method,$path,$code,$options);
|
|
110
|
+
|
|
111
|
+ return $this->doQuery($this->apiUrl, $method, $path, $code, $options);
|
118
|
112
|
}
|
119
|
113
|
|
120
|
114
|
/**
|
|
@@ -126,9 +120,9 @@ class ApiLayerService
|
126
|
120
|
*
|
127
|
121
|
* @return array
|
128
|
122
|
*/
|
129
|
|
- private function doApiGet($path,$returnCode = 200,$parameters = array())
|
|
123
|
+ private function doApiGet($path, $returnCode = 200, $parameters = array())
|
130
|
124
|
{
|
131
|
|
- return $this->doApiQuery('GET',$path,$returnCode,$parameters);
|
|
125
|
+ return $this->doApiQuery('GET', $path, $returnCode, $parameters);
|
132
|
126
|
}
|
133
|
127
|
|
134
|
128
|
/**
|
|
@@ -140,9 +134,9 @@ class ApiLayerService
|
140
|
134
|
*
|
141
|
135
|
* @return array
|
142
|
136
|
*/
|
143
|
|
- private function doApiPost($path,$returnCode = 200,$parameters = array())
|
|
137
|
+ private function doApiPost($path, $returnCode = 200, $parameters = array())
|
144
|
138
|
{
|
145
|
|
- return $this->doApiQuery('POST',$path,$returnCode,$parameters);
|
|
139
|
+ return $this->doApiQuery('POST', $path, $returnCode, $parameters);
|
146
|
140
|
}
|
147
|
141
|
|
148
|
142
|
/**
|
|
@@ -155,14 +149,13 @@ class ApiLayerService
|
155
|
149
|
*
|
156
|
150
|
* @return array
|
157
|
151
|
*/
|
158
|
|
- private function doHubQuery($method,$path,$code = 200,$options = array())
|
|
152
|
+ private function doHubQuery($method, $path, $code = 200, $options = array())
|
159
|
153
|
{
|
160
|
|
- if (!isset($options['headers']['Authorization']))
|
161
|
|
- {
|
|
154
|
+ if (!isset($options['headers']['Authorization'])) {
|
162
|
155
|
$options['headers']['Authorization'] = 'token ' . $_SERVER['JUPYTER_API_TOKEN'];
|
163
|
156
|
}
|
164
|
|
-
|
165
|
|
- return $this->doQuery($this->hubUrl,$method,$path,$code,$options);
|
|
157
|
+
|
|
158
|
+ return $this->doQuery($this->hubUrl, $method, $path, $code, $options);
|
166
|
159
|
}
|
167
|
160
|
|
168
|
161
|
/**
|
|
@@ -174,9 +167,9 @@ class ApiLayerService
|
174
|
167
|
*
|
175
|
168
|
* @return array
|
176
|
169
|
*/
|
177
|
|
- private function doHubGet($path,$returnCode = 200,$parameters = array())
|
|
170
|
+ private function doHubGet($path, $returnCode = 200, $parameters = array())
|
178
|
171
|
{
|
179
|
|
- return $this->doHubQuery('GET',$path,$returnCode,$parameters);
|
|
172
|
+ return $this->doHubQuery('GET', $path, $returnCode, $parameters);
|
180
|
173
|
}
|
181
|
174
|
|
182
|
175
|
/**
|
|
@@ -188,9 +181,9 @@ class ApiLayerService
|
188
|
181
|
*
|
189
|
182
|
* @return array
|
190
|
183
|
*/
|
191
|
|
- private function doHubPost($path,$returnCode = 200,$parameters = array())
|
|
184
|
+ private function doHubPost($path, $returnCode = 200, $parameters = array())
|
192
|
185
|
{
|
193
|
|
- return $this->doHubQuery('POST',$path,$returnCode,$parameters);
|
|
186
|
+ return $this->doHubQuery('POST', $path, $returnCode, $parameters);
|
194
|
187
|
}
|
195
|
188
|
|
196
|
189
|
/**
|
|
@@ -204,13 +197,12 @@ class ApiLayerService
|
204
|
197
|
*
|
205
|
198
|
* @return array
|
206
|
199
|
*/
|
207
|
|
- private function doNotebookQuery(JupyterToken $token,$method,$path,$code = 200,$options = array())
|
|
200
|
+ private function doNotebookQuery(JupyterToken $token, $method, $path, $code = 200, $options = array())
|
208
|
201
|
{
|
209
|
|
- if (!isset($options['headers']['Authorization']))
|
210
|
|
- {
|
|
202
|
+ if (!isset($options['headers']['Authorization'])) {
|
211
|
203
|
$options['headers']['Authorization'] = 'token ' . $token->getToken();
|
212
|
204
|
}
|
213
|
|
- return $this->doQuery($this->getUserNotebookUrl($token),$method,$path,$code,$options);
|
|
205
|
+ return $this->doQuery($this->getUserNotebookUrl($token), $method, $path, $code, $options);
|
214
|
206
|
}
|
215
|
207
|
|
216
|
208
|
/**
|
|
@@ -222,7 +214,7 @@ class ApiLayerService
|
222
|
214
|
*/
|
223
|
215
|
private function getUserNotebookUrl($token)
|
224
|
216
|
{
|
225
|
|
- return str_replace('%s',$token->getUuid(),$this->notebookApiUrl);
|
|
217
|
+ return str_replace('%s', $token->getUuid(), $this->notebookApiUrl);
|
226
|
218
|
}
|
227
|
219
|
|
228
|
220
|
|
|
@@ -236,9 +228,9 @@ class ApiLayerService
|
236
|
228
|
*
|
237
|
229
|
* @return array
|
238
|
230
|
*/
|
239
|
|
- private function doNotebookGet(JupyterToken $token,$path,$returnCode = 200,$parameters = array())
|
|
231
|
+ private function doNotebookGet(JupyterToken $token, $path, $returnCode = 200, $parameters = array())
|
240
|
232
|
{
|
241
|
|
- return $this->doNotebookQuery($token,'GET',$path,$returnCode,$parameters);
|
|
233
|
+ return $this->doNotebookQuery($token, 'GET', $path, $returnCode, $parameters);
|
242
|
234
|
}
|
243
|
235
|
|
244
|
236
|
/**
|
|
@@ -251,9 +243,9 @@ class ApiLayerService
|
251
|
243
|
*
|
252
|
244
|
* @return array
|
253
|
245
|
*/
|
254
|
|
- private function doNotebookPost(JupyterToken $token,$path,$returnCode = 200,$parameters = array())
|
|
246
|
+ private function doNotebookPost(JupyterToken $token, $path, $returnCode = 200, $parameters = array())
|
255
|
247
|
{
|
256
|
|
- return $this->doNotebookQuery($token,'POST',$path,$returnCode,$parameters);
|
|
248
|
+ return $this->doNotebookQuery($token, 'POST', $path, $returnCode, $parameters);
|
257
|
249
|
}
|
258
|
250
|
|
259
|
251
|
/**
|
|
@@ -266,9 +258,9 @@ class ApiLayerService
|
266
|
258
|
*
|
267
|
259
|
* @return array
|
268
|
260
|
*/
|
269
|
|
- private function doNotebookPatch(JupyterToken $token,$path,$returnCode = 200,$parameters = array())
|
|
261
|
+ private function doNotebookPatch(JupyterToken $token, $path, $returnCode = 200, $parameters = array())
|
270
|
262
|
{
|
271
|
|
- return $this->doNotebookQuery($token,'PATCH',$path,$returnCode,$parameters);
|
|
263
|
+ return $this->doNotebookQuery($token, 'PATCH', $path, $returnCode, $parameters);
|
272
|
264
|
}
|
273
|
265
|
|
274
|
266
|
/**
|
|
@@ -281,9 +273,9 @@ class ApiLayerService
|
281
|
273
|
*
|
282
|
274
|
* @return array
|
283
|
275
|
*/
|
284
|
|
- private function doNotebookPut(JupyterToken $token,$path,$returnCode = 200,$parameters = array())
|
|
276
|
+ private function doNotebookPut(JupyterToken $token, $path, $returnCode = 200, $parameters = array())
|
285
|
277
|
{
|
286
|
|
- return $this->doNotebookQuery($token,'PUT',$path,$returnCode,$parameters);
|
|
278
|
+ return $this->doNotebookQuery($token, 'PUT', $path, $returnCode, $parameters);
|
287
|
279
|
}
|
288
|
280
|
|
289
|
281
|
/**
|
|
@@ -293,14 +285,13 @@ class ApiLayerService
|
293
|
285
|
*/
|
294
|
286
|
public function serviceIsAvalaible()
|
295
|
287
|
{
|
296
|
|
- $response = $this->doApiGet('phoenix/',200);
|
297
|
|
- if (!is_null($response) && $response['result'] == 'true')
|
298
|
|
- {
|
|
288
|
+ $response = $this->doApiGet('phoenix/', 200);
|
|
289
|
+ if (!is_null($response) && $response['result'] == 'true') {
|
299
|
290
|
return true;
|
300
|
291
|
}
|
301
|
292
|
return false;
|
302
|
293
|
}
|
303
|
|
-
|
|
294
|
+
|
304
|
295
|
/**
|
305
|
296
|
* échappe le texte
|
306
|
297
|
* et retourne le contenu échappé
|
|
@@ -324,14 +315,12 @@ class ApiLayerService
|
324
|
315
|
*/
|
325
|
316
|
private function buildPath(array $params)
|
326
|
317
|
{
|
327
|
|
- foreach ($params as $key => $param)
|
328
|
|
- {
|
329
|
|
- if ($param != "")
|
330
|
|
- {
|
|
318
|
+ foreach ($params as $key => $param) {
|
|
319
|
+ if ($param != "") {
|
331
|
320
|
$params[$key] = $this->sanitizeUrlParamPath($param);
|
332
|
321
|
}
|
333
|
322
|
}
|
334
|
|
- return implode('/',$params);
|
|
323
|
+ return implode('/', $params);
|
335
|
324
|
}
|
336
|
325
|
|
337
|
326
|
private function sanitize($name)
|
|
@@ -348,7 +337,7 @@ class ApiLayerService
|
348
|
337
|
*
|
349
|
338
|
* @return null/string
|
350
|
339
|
*/
|
351
|
|
- public function createTrainerServerIfNoneAndSetContent(JupyterVirtualClassroom $classroom,JupyterToken $token,$fromurl = null,$notebookContent = null,$useUrl = true)
|
|
340
|
+ public function createTrainerServerIfNoneAndSetContent(JupyterVirtualClassroom $classroom, JupyterToken $token, $fromurl = null, $notebookContent = null, $useUrl = true)
|
352
|
341
|
{
|
353
|
342
|
$notebookName = $this->getNoteBookName($classroom);
|
354
|
343
|
$path = $this->buildPath(
|
|
@@ -356,36 +345,33 @@ class ApiLayerService
|
356
|
345
|
'phoenix',
|
357
|
346
|
'trainer',
|
358
|
347
|
$token->getUuid(),
|
359
|
|
- $this->getFolderName($classroom,$useUrl),
|
360
|
|
- $this->getNoteBookNameByString($classroom->getSequenceItemId(),$classroom->getTitle(),true,$useUrl)
|
|
348
|
+ $this->getFolderName($classroom, $useUrl),
|
|
349
|
+ $this->getNoteBookNameByString($classroom->getSequenceItemId(), $classroom->getTitle(), true, $useUrl)
|
361
|
350
|
)
|
362
|
351
|
);
|
363
|
352
|
|
364
|
353
|
$contentData = array();
|
365
|
|
- if (!is_null($notebookContent))
|
366
|
|
- {
|
|
354
|
+ if (!is_null($notebookContent)) {
|
367
|
355
|
$contentData['content'] = $notebookContent;
|
368
|
356
|
}
|
369
|
357
|
$contentData['fromurl'] = $fromurl;
|
370
|
358
|
|
371
|
359
|
$options['headers']['Content-Type'] = 'application/json';
|
372
|
|
- $options['body'] = json_encode($contentData);//'{"toto":"content"}';
|
|
360
|
+ $options['body'] = json_encode($contentData); //'{"toto":"content"}';
|
373
|
361
|
|
374
|
|
- $response = $this->doApiPost($path,200,$options);
|
|
362
|
+ $response = $this->doApiPost($path, 200, $options);
|
375
|
363
|
|
376
|
|
- if ($response['result'] == 'true' && isset($response['url']))
|
377
|
|
- {
|
|
364
|
+ if ($response['result'] == 'true' && isset($response['url'])) {
|
378
|
365
|
// url du notebook
|
379
|
366
|
$url = $response['url'];
|
380
|
367
|
$classroom->setUrl($url);
|
381
|
368
|
|
382
|
369
|
// met à jour le token dans le cas d'une création de compte
|
383
|
|
- if (isset($response['token']))
|
384
|
|
- {
|
|
370
|
+ if (isset($response['token'])) {
|
385
|
371
|
$token->setToken($response['token']);
|
386
|
372
|
$token->setApiId($response['tokenId']);
|
387
|
373
|
}
|
388
|
|
-
|
|
374
|
+
|
389
|
375
|
|
390
|
376
|
return $url;
|
391
|
377
|
}
|
|
@@ -401,7 +387,7 @@ class ApiLayerService
|
401
|
387
|
*
|
402
|
388
|
* @return string url du notebook
|
403
|
389
|
*/
|
404
|
|
- public function createStudentServerIfNoneAndSetContent(JupyterVirtualClassroom $classroom,JupyterToken $trainerToken,JupyterToken $studentToken,$useUrl = true)
|
|
390
|
+ public function createStudentServerIfNoneAndSetContent(JupyterVirtualClassroom $classroom, JupyterToken $trainerToken, JupyterToken $studentToken, $useUrl = true)
|
405
|
391
|
{
|
406
|
392
|
$path = $this->buildPath(
|
407
|
393
|
array(
|
|
@@ -409,8 +395,8 @@ class ApiLayerService
|
409
|
395
|
'student',
|
410
|
396
|
$trainerToken->getUuid(),
|
411
|
397
|
$studentToken->getUuid(),
|
412
|
|
- $this->getFolderName($classroom,$useUrl),
|
413
|
|
- $this->getNoteBookNameByString($classroom->getSequenceItemId(),$classroom->getTitle(),true,$useUrl)
|
|
398
|
+ $this->getFolderName($classroom, $useUrl),
|
|
399
|
+ $this->getNoteBookNameByString($classroom->getSequenceItemId(), $classroom->getTitle(), true, $useUrl)
|
414
|
400
|
)
|
415
|
401
|
);
|
416
|
402
|
|
|
@@ -423,20 +409,18 @@ class ApiLayerService
|
423
|
409
|
$options['headers']['Content-Type'] = 'application/json';
|
424
|
410
|
$options['body'] = json_encode($contentData);
|
425
|
411
|
|
426
|
|
- $response = $this->doApiPost($path,200,$options);
|
427
|
|
- if ($response['result'] == 'true' && isset($response['url']))
|
428
|
|
- {
|
|
412
|
+ $response = $this->doApiPost($path, 200, $options);
|
|
413
|
+ if ($response['result'] == 'true' && isset($response['url'])) {
|
429
|
414
|
// url du notebook
|
430
|
415
|
$url = $response['url'];
|
431
|
416
|
//$classroom->setUrl($url);
|
432
|
417
|
|
433
|
418
|
// met à jour le token dans le cas d'une création de compte
|
434
|
|
- if (isset($response['token']))
|
435
|
|
- {
|
|
419
|
+ if (isset($response['token'])) {
|
436
|
420
|
$studentToken->setToken($response['token']);
|
437
|
421
|
$studentToken->setApiId($response['tokenId']);
|
438
|
422
|
}
|
439
|
|
-
|
|
423
|
+
|
440
|
424
|
|
441
|
425
|
return $url;
|
442
|
426
|
}
|
|
@@ -453,7 +437,7 @@ class ApiLayerService
|
453
|
437
|
*
|
454
|
438
|
* @return string url du notebook
|
455
|
439
|
*/
|
456
|
|
- public function createStudentAssesmentServerIfNone(JupyterVirtualClassroom $classroom,JupyterToken $trainerToken,JupyterToken $studentToken,$useUrl = true)
|
|
440
|
+ public function createStudentAssesmentServerIfNone(JupyterVirtualClassroom $classroom, JupyterToken $trainerToken, JupyterToken $studentToken, $useUrl = true)
|
457
|
441
|
{
|
458
|
442
|
$path = $this->buildPath(
|
459
|
443
|
array(
|
|
@@ -462,27 +446,26 @@ class ApiLayerService
|
462
|
446
|
'student',
|
463
|
447
|
$trainerToken->getUuid(),
|
464
|
448
|
$studentToken->getUuid(),
|
465
|
|
- $this->getFolderName($classroom,$useUrl),
|
466
|
|
- $this->getNoteBookNameByString($classroom->getSequenceItemId(),$classroom->getTitle(),true,$useUrl)
|
|
449
|
+ $this->getFolderName($classroom, $useUrl),
|
|
450
|
+ $this->getNoteBookNameByString($classroom->getSequenceItemId(), $classroom->getTitle(), true, $useUrl)
|
467
|
451
|
)
|
468
|
452
|
);
|
469
|
453
|
|
470
|
454
|
$options['headers']['Content-Type'] = 'application/json';
|
471
|
455
|
|
472
|
|
- $response = $this->doApiPost($path,200,$options);
|
473
|
|
- if ($response['result'] == 'true' && isset($response['url']))
|
474
|
|
- {
|
|
456
|
+ $response = $this->doApiPost($path, 200, $options);
|
|
457
|
+
|
|
458
|
+ if ($response['result'] == 'true' && isset($response['url'])) {
|
475
|
459
|
// url du notebook
|
476
|
460
|
$url = $response['url'];
|
477
|
|
- //$classroom->setUrl($url);
|
|
461
|
+
|
|
462
|
+ //$classroom->setUrl($url);
|
478
|
463
|
|
479
|
464
|
// met à jour le token dans le cas d'une création de compte
|
480
|
|
- if (isset($response['token']))
|
481
|
|
- {
|
|
465
|
+ if (isset($response['token'])) {
|
482
|
466
|
$studentToken->setToken($response['token']);
|
483
|
467
|
$studentToken->setApiId($response['tokenId']);
|
484
|
468
|
}
|
485
|
|
-
|
486
|
469
|
|
487
|
470
|
return $url;
|
488
|
471
|
}
|
|
@@ -499,7 +482,7 @@ class ApiLayerService
|
499
|
482
|
*
|
500
|
483
|
* @return string score
|
501
|
484
|
*/
|
502
|
|
- public function sendStudentNotebook(JupyterToken $trainerToken,JupyterToken $studentToken,JupyterVirtualclassroom $classroom,$useUrl = true)
|
|
485
|
+ public function sendStudentNotebook(JupyterToken $trainerToken, JupyterToken $studentToken, JupyterVirtualclassroom $classroom, $useUrl = true)
|
503
|
486
|
{
|
504
|
487
|
$path = $this->buildPath(
|
505
|
488
|
array(
|
|
@@ -508,18 +491,13 @@ class ApiLayerService
|
508
|
491
|
'send',
|
509
|
492
|
$trainerToken->getUuid(),
|
510
|
493
|
$studentToken->getUuid(),
|
511
|
|
- $this->getFolderName($classroom,$useUrl)
|
|
494
|
+ $this->getFolderName($classroom, $useUrl)
|
512
|
495
|
)
|
513
|
496
|
);
|
514
|
497
|
|
515
|
498
|
$options['headers']['Content-Type'] = 'application/json';
|
516
|
499
|
|
517
|
|
- $response = $this->doApiPost($path,200,$options);
|
518
|
|
- if ($response['result'] == 'true' && isset($response['score']))
|
519
|
|
- {
|
520
|
|
- return array('score' => $response['score'],'max-score' => $response['max-score']);
|
521
|
|
- }
|
522
|
|
- return array('score' => '','max-score' => '');
|
|
500
|
+ return $this->doApiPost($path, 200, $options);
|
523
|
501
|
}
|
524
|
502
|
|
525
|
503
|
/**
|
|
@@ -549,10 +527,9 @@ class ApiLayerService
|
549
|
527
|
$token->getToken()
|
550
|
528
|
)
|
551
|
529
|
);
|
552
|
|
- $response = $this->doApiGet($path,200);
|
|
530
|
+ $response = $this->doApiGet($path, 200);
|
553
|
531
|
|
554
|
|
- if ($response['result'] == 'true')
|
555
|
|
- {
|
|
532
|
+ if ($response['result'] == 'true') {
|
556
|
533
|
return true;
|
557
|
534
|
}
|
558
|
535
|
return null;
|
|
@@ -571,10 +548,9 @@ class ApiLayerService
|
571
|
548
|
$token->getToken()
|
572
|
549
|
)
|
573
|
550
|
);
|
574
|
|
- $response = $this->doApiPost($path,200);
|
|
551
|
+ $response = $this->doApiPost($path, 200);
|
575
|
552
|
|
576
|
|
- if ($response['result'] == 'true' && isset($response['token']))
|
577
|
|
- {
|
|
553
|
+ if ($response['result'] == 'true' && isset($response['token'])) {
|
578
|
554
|
$token->setToken($response['token']);
|
579
|
555
|
return true;
|
580
|
556
|
}
|
|
@@ -598,9 +574,9 @@ class ApiLayerService
|
598
|
574
|
)
|
599
|
575
|
);
|
600
|
576
|
|
601
|
|
- $response = $this->doHubPost($path,array(201,202,400));
|
|
577
|
+ $response = $this->doHubPost($path, array(201, 202, 400));
|
602
|
578
|
|
603
|
|
- return array('isStarted' => true,'result' => true);
|
|
579
|
+ return array('isStarted' => true, 'result' => true);
|
604
|
580
|
}
|
605
|
581
|
|
606
|
582
|
/**
|
|
@@ -627,7 +603,7 @@ class ApiLayerService
|
627
|
603
|
*
|
628
|
604
|
* @return string nouvel url
|
629
|
605
|
*/
|
630
|
|
- public function renameNotebook(JupyterToken $token,JupyterVirtualClassroom $jupyter,$newname)
|
|
606
|
+ public function renameNotebook(JupyterToken $token, JupyterVirtualClassroom $jupyter, $newname)
|
631
|
607
|
{
|
632
|
608
|
// vieux
|
633
|
609
|
$oldName = $this->getNotebookName($jupyter);
|
|
@@ -639,7 +615,7 @@ class ApiLayerService
|
639
|
615
|
);
|
640
|
616
|
|
641
|
617
|
// nouveau
|
642
|
|
- $newname = $this->getNoteBookNameByString($jupyter->getSequenceItemId(),$newname);
|
|
618
|
+ $newname = $this->getNoteBookNameByString($jupyter->getSequenceItemId(), $newname);
|
643
|
619
|
$newPath = $this->buildPath(
|
644
|
620
|
array(
|
645
|
621
|
$newname
|
|
@@ -647,11 +623,10 @@ class ApiLayerService
|
647
|
623
|
);
|
648
|
624
|
|
649
|
625
|
$options["body"] = json_encode(array('path' => $newPath));
|
650
|
|
-
|
651
|
|
- $response = $this->doNotebookPatch($token,$path,array(200,400),$options);
|
652
|
|
- if (isset($response['name']))
|
653
|
|
- {
|
654
|
|
- return str_replace($oldName,$newname,$jupyter->getUrl());
|
|
626
|
+
|
|
627
|
+ $response = $this->doNotebookPatch($token, $path, array(200, 400), $options);
|
|
628
|
+ if (isset($response['name'])) {
|
|
629
|
+ return str_replace($oldName, $newname, $jupyter->getUrl());
|
655
|
630
|
}
|
656
|
631
|
return null;
|
657
|
632
|
}
|
|
@@ -664,7 +639,7 @@ class ApiLayerService
|
664
|
639
|
*
|
665
|
640
|
* @return string
|
666
|
641
|
*/
|
667
|
|
- public function getNotebookContent(JupyterToken $token,JupyterVirtualClassroom $jupyter)
|
|
642
|
+ public function getNotebookContent(JupyterToken $token, JupyterVirtualClassroom $jupyter)
|
668
|
643
|
{
|
669
|
644
|
$path = $this->buildPath(
|
670
|
645
|
array(
|
|
@@ -675,7 +650,7 @@ class ApiLayerService
|
675
|
650
|
$this->getNotebookName($jupyter)
|
676
|
651
|
)
|
677
|
652
|
);
|
678
|
|
- $this->doApiGet($path,array(200,404));
|
|
653
|
+ $this->doApiGet($path, array(200, 404));
|
679
|
654
|
return $this->response->getContent();
|
680
|
655
|
/*$path = $this->buildPath(
|
681
|
656
|
array(
|
|
@@ -702,7 +677,7 @@ class ApiLayerService
|
702
|
677
|
*
|
703
|
678
|
* @return bool
|
704
|
679
|
*/
|
705
|
|
- public function setNotebookContent(JupyterToken $token,JupyterVirtualClassroom $jupyter,$content)
|
|
680
|
+ public function setNotebookContent(JupyterToken $token, JupyterVirtualClassroom $jupyter, $content)
|
706
|
681
|
{
|
707
|
682
|
$path = $this->buildPath(
|
708
|
683
|
array(
|
|
@@ -712,7 +687,7 @@ class ApiLayerService
|
712
|
687
|
)
|
713
|
688
|
);
|
714
|
689
|
|
715
|
|
- return $this->setNotebookContentByPath($path,$token,$jupyter,$content);
|
|
690
|
+ return $this->setNotebookContentByPath($path, $token, $jupyter, $content);
|
716
|
691
|
}
|
717
|
692
|
|
718
|
693
|
/**
|
|
@@ -725,16 +700,15 @@ class ApiLayerService
|
725
|
700
|
*
|
726
|
701
|
* @return bool
|
727
|
702
|
*/
|
728
|
|
- private function setNotebookContentByPath($path,JupyterToken $token,JupyterVirtualClassroom $jupyter,$content)
|
|
703
|
+ private function setNotebookContentByPath($path, JupyterToken $token, JupyterVirtualClassroom $jupyter, $content)
|
729
|
704
|
{
|
730
|
705
|
$data['content'] = ($content);
|
731
|
706
|
$data['type'] = 'file';
|
732
|
707
|
$data['format'] = 'text';
|
733
|
708
|
$options['body'] = json_encode($data);
|
734
|
709
|
|
735
|
|
- $response = $this->doNotebookPut($token,$path,array(201,400,404),$options);
|
736
|
|
- if (isset($response['name']))
|
737
|
|
- {
|
|
710
|
+ $response = $this->doNotebookPut($token, $path, array(201, 400, 404), $options);
|
|
711
|
+ if (isset($response['name'])) {
|
738
|
712
|
return true;
|
739
|
713
|
}
|
740
|
714
|
return false;
|
|
@@ -748,7 +722,7 @@ class ApiLayerService
|
748
|
722
|
*
|
749
|
723
|
* @return
|
750
|
724
|
*/
|
751
|
|
- public function deleteNotebook(JupyterToken $token,JupyterVirtualClassroom $jupyter)
|
|
725
|
+ public function deleteNotebook(JupyterToken $token, JupyterVirtualClassroom $jupyter)
|
752
|
726
|
{
|
753
|
727
|
// suppression du fichier
|
754
|
728
|
$path = $this->buildPath(
|
|
@@ -760,7 +734,7 @@ class ApiLayerService
|
760
|
734
|
)
|
761
|
735
|
);
|
762
|
736
|
|
763
|
|
- $response = $this->doApiQuery('DELETE',$path,array(201,404));
|
|
737
|
+ $response = $this->doApiQuery('DELETE', $path, array(201, 404));
|
764
|
738
|
|
765
|
739
|
return $response;
|
766
|
740
|
}
|
|
@@ -774,16 +748,13 @@ class ApiLayerService
|
774
|
748
|
*
|
775
|
749
|
* @return string
|
776
|
750
|
*/
|
777
|
|
- private function getNoteBookName(JupyterVirtualClassroom $jupyter,$withExt = true,$useUrl = true)
|
|
751
|
+ private function getNoteBookName(JupyterVirtualClassroom $jupyter, $withExt = true, $useUrl = true)
|
778
|
752
|
{
|
779
|
753
|
$url = $jupyter->getUrl();
|
780
|
|
- if ($useUrl && $url)
|
781
|
|
- {
|
782
|
|
- return $this->getNoteBookNameByUrl($url);
|
783
|
|
- }
|
784
|
|
- else
|
785
|
|
- {
|
786
|
|
- $name = $this->getNoteBookNameByString($jupyter->getSequenceItemId(),$jupyter->getTitle(),$withExt);
|
|
754
|
+ if ($useUrl && $url) {
|
|
755
|
+ return $this->getNoteBookNameByUrl($url);
|
|
756
|
+ } else {
|
|
757
|
+ $name = $this->getNoteBookNameByString($jupyter->getSequenceItemId(), $jupyter->getTitle(), $withExt);
|
787
|
758
|
return $name;
|
788
|
759
|
}
|
789
|
760
|
}
|
|
@@ -796,14 +767,13 @@ class ApiLayerService
|
796
|
767
|
*
|
797
|
768
|
* @return string
|
798
|
769
|
*/
|
799
|
|
- private function getNoteBookNameByUrl($url,$withExt = true)
|
|
770
|
+ private function getNoteBookNameByUrl($url, $withExt = true)
|
800
|
771
|
{
|
801
|
772
|
$mask = PATHINFO_FILENAME;
|
802
|
|
- if ($withExt)
|
803
|
|
- {
|
|
773
|
+ if ($withExt) {
|
804
|
774
|
$mask = PATHINFO_BASENAME;
|
805
|
775
|
}
|
806
|
|
- return pathinfo($url,$mask);
|
|
776
|
+ return pathinfo($url, $mask);
|
807
|
777
|
}
|
808
|
778
|
|
809
|
779
|
/**
|
|
@@ -815,7 +785,7 @@ class ApiLayerService
|
815
|
785
|
*/
|
816
|
786
|
private function getIdMask($id)
|
817
|
787
|
{
|
818
|
|
- return hash('crc32',$id);
|
|
788
|
+ return hash('crc32', $id);
|
819
|
789
|
}
|
820
|
790
|
|
821
|
791
|
/**
|
|
@@ -824,40 +794,33 @@ class ApiLayerService
|
824
|
794
|
* @param JupyterVirtualClassroom $jupyter
|
825
|
795
|
* @return bool $useUrl
|
826
|
796
|
*/
|
827
|
|
- public function getFolderName(JupyterVirtualClassroom $jupyter,$useUrl = true)
|
|
797
|
+ public function getFolderName(JupyterVirtualClassroom $jupyter, $useUrl = true)
|
828
|
798
|
{
|
829
|
799
|
$url = $jupyter->getUrl();
|
830
|
|
- if (!$useUrl || !$url)
|
831
|
|
- {
|
|
800
|
+ if (!$useUrl || !$url) {
|
832
|
801
|
$learningPath = $jupyter->getLearningPath();
|
833
|
|
- if ($learningPath->isTemplate())
|
834
|
|
- {
|
835
|
|
- $title = substr($learningPath->getTitle() . " - " . $jupyter->getTitle(),0,70);
|
836
|
|
- return sprintf('%s-%s',$this->getIdMask($jupyter->getSequenceItemId()),$title);
|
837
|
|
- }
|
838
|
|
- else
|
839
|
|
- {
|
|
802
|
+ if ($learningPath->isTemplate()) {
|
|
803
|
+ $title = substr($learningPath->getTitle() . " - " . $jupyter->getTitle(), 0, 70);
|
|
804
|
+ return sprintf('%s-%s', $this->getIdMask($jupyter->getSequenceItemId()), $title);
|
|
805
|
+ } else {
|
840
|
806
|
$session = $jupyter->getSession();
|
841
|
|
- $title = substr($session->getTitle() . " - " . $jupyter->getTitle(),0,70);
|
842
|
|
- return sprintf('%s-%s',$this->getIdMask($jupyter->getSequenceItemId()),$title);
|
|
807
|
+ $title = substr($session->getTitle() . " - " . $jupyter->getTitle(), 0, 70);
|
|
808
|
+ return sprintf('%s-%s', $this->getIdMask($jupyter->getSequenceItemId()), $title);
|
843
|
809
|
}
|
844
|
|
- }
|
845
|
|
- else
|
846
|
|
- {
|
847
|
|
- return $this->getFolderNameByUrl($url,true);
|
|
810
|
+ } else {
|
|
811
|
+ return $this->getFolderNameByUrl($url, true);
|
848
|
812
|
}
|
849
|
813
|
}
|
850
|
814
|
|
851
|
|
- private function getFolderNameByUrl($url,$sanitize = true)
|
|
815
|
+ private function getFolderNameByUrl($url, $sanitize = true)
|
852
|
816
|
{
|
853
|
|
- $parts = explode('/',$url);
|
|
817
|
+ $parts = explode('/', $url);
|
854
|
818
|
|
855
|
819
|
$result = $parts[count($parts) - 2];
|
856
|
820
|
|
857
|
821
|
// normalement pas de sanitize, car à ce niveau la, si le nom du dossier est incorrecte
|
858
|
822
|
// c'est que le problème s'est produit en amon
|
859
|
|
- if ($sanitize)
|
860
|
|
- {
|
|
823
|
+ if ($sanitize) {
|
861
|
824
|
return $this->sanitize($result);
|
862
|
825
|
}
|
863
|
826
|
return $result;
|
|
@@ -872,11 +835,10 @@ class ApiLayerService
|
872
|
835
|
*
|
873
|
836
|
* @return string
|
874
|
837
|
*/
|
875
|
|
- private function getNoteBookNameByString($id,$string,$withExt = true)
|
|
838
|
+ private function getNoteBookNameByString($id, $string, $withExt = true)
|
876
|
839
|
{
|
877
|
|
- $name = substr($this->sanitize($string),0,70);//sprintf('%s-%s',$this->getIdMask($id),$string));
|
878
|
|
- if ($withExt)
|
879
|
|
- {
|
|
840
|
+ $name = substr($this->sanitize($string), 0, 70); //sprintf('%s-%s',$this->getIdMask($id),$string));
|
|
841
|
+ if ($withExt) {
|
880
|
842
|
$name .= '.ipynb';
|
881
|
843
|
}
|
882
|
844
|
|
|
@@ -892,16 +854,16 @@ class ApiLayerService
|
892
|
854
|
*
|
893
|
855
|
* @return void
|
894
|
856
|
*/
|
895
|
|
- public function copyNotebookUser(JupyterToken $tokenIn,JupyterToken $tokenOut,JupyterVirtualClassroom $jupyter)
|
|
857
|
+ public function copyNotebookUser(JupyterToken $tokenIn, JupyterToken $tokenOut, JupyterVirtualClassroom $jupyter)
|
896
|
858
|
{
|
897
|
859
|
// récupère le contenu
|
898
|
860
|
// get
|
899
|
|
- $content = $this->getNotebookContent($tokenIn,$jupyter);
|
|
861
|
+ $content = $this->getNotebookContent($tokenIn, $jupyter);
|
900
|
862
|
|
901
|
863
|
|
902
|
864
|
// upload le contenu
|
903
|
865
|
// post
|
904
|
|
- $this->setNotebookContent($tokenOut,$jupyter,json_encode($content));
|
|
866
|
+ $this->setNotebookContent($tokenOut, $jupyter, json_encode($content));
|
905
|
867
|
}
|
906
|
868
|
|
907
|
869
|
/**
|
|
@@ -913,25 +875,23 @@ class ApiLayerService
|
913
|
875
|
*
|
914
|
876
|
* @return void
|
915
|
877
|
*/
|
916
|
|
- public function changeNotebookUser(JupyterToken $tokenIn,JupyterToken $tokenOut,JupyterVirtualClassroom $jupyter)
|
|
878
|
+ public function changeNotebookUser(JupyterToken $tokenIn, JupyterToken $tokenOut, JupyterVirtualClassroom $jupyter)
|
917
|
879
|
{
|
918
|
880
|
$inResponse = $this->startServer($tokenIn);
|
919
|
881
|
$outResponse = $this->startServer($tokenOut);
|
920
|
882
|
|
921
|
883
|
// copie le notebook d'un utilisateur à l'autre
|
922
|
|
- $this->copyNotebookUser($tokenIn,$tokenOut,$jupyter);
|
|
884
|
+ $this->copyNotebookUser($tokenIn, $tokenOut, $jupyter);
|
923
|
885
|
|
924
|
886
|
// supprime le fichier
|
925
|
887
|
// delete
|
926
|
|
- $this->deleteNotebook($tokenIn,$jupyter);
|
|
888
|
+ $this->deleteNotebook($tokenIn, $jupyter);
|
927
|
889
|
|
928
|
890
|
// stop les serveurs s'il étaient pas démarrés
|
929
|
|
- if (!$inResponse['isStarted'])
|
930
|
|
- {
|
|
891
|
+ if (!$inResponse['isStarted']) {
|
931
|
892
|
$this->stopServer($tokenIn);
|
932
|
893
|
}
|
933
|
|
- if (!$outResponse['isStarted'])
|
934
|
|
- {
|
|
894
|
+ if (!$outResponse['isStarted']) {
|
935
|
895
|
$this->stopServer($tokenOut);
|
936
|
896
|
}
|
937
|
897
|
}
|
|
@@ -944,10 +904,10 @@ class ApiLayerService
|
944
|
904
|
*
|
945
|
905
|
* @return void
|
946
|
906
|
*/
|
947
|
|
- public function cloneNotebook(JupyterToken $token,JupyterVirtualClassroom $fromClassroom)
|
|
907
|
+ public function cloneNotebook(JupyterToken $token, JupyterVirtualClassroom $fromClassroom)
|
948
|
908
|
{
|
949
|
|
- $folderFrom = $this->getFolderNameByUrl($fromClassroom->getUrl(),false);
|
950
|
|
- $folderTo = $this->getFolderName($fromClassroom,false);
|
|
909
|
+ $folderFrom = $this->getFolderNameByUrl($fromClassroom->getUrl(), false);
|
|
910
|
+ $folderTo = $this->getFolderName($fromClassroom, false);
|
951
|
911
|
$path = $this->buildPath(
|
952
|
912
|
array(
|
953
|
913
|
'phoenix',
|
|
@@ -960,10 +920,9 @@ class ApiLayerService
|
960
|
920
|
)
|
961
|
921
|
);
|
962
|
922
|
|
963
|
|
- $response = $this->doApiPost($path,array(200,404));
|
964
|
|
- if (isset($response['result']) && $response['result'] == 'True')
|
965
|
|
- {
|
966
|
|
- $fromClassroom->setUrl(str_replace($folderFrom,$response['folder'],$fromClassroom->getUrl()));
|
|
923
|
+ $response = $this->doApiPost($path, array(200, 404));
|
|
924
|
+ if (isset($response['result']) && $response['result'] == 'true') {
|
|
925
|
+ $fromClassroom->setUrl(str_replace($folderFrom, $response['folder'], $fromClassroom->getUrl()));
|
967
|
926
|
|
968
|
927
|
return true;
|
969
|
928
|
}
|
|
@@ -977,7 +936,7 @@ class ApiLayerService
|
977
|
936
|
*
|
978
|
937
|
* @return bool
|
979
|
938
|
*/
|
980
|
|
- public function notebookExist(JupyterToken $token,$url)
|
|
939
|
+ public function notebookExist(JupyterToken $token, $url)
|
981
|
940
|
{
|
982
|
941
|
$path = $this->buildPath(
|
983
|
942
|
array(
|
|
@@ -989,9 +948,8 @@ class ApiLayerService
|
989
|
948
|
)
|
990
|
949
|
);
|
991
|
950
|
|
992
|
|
- $response = $this->doApiGet($path,array(200,404));
|
993
|
|
- if (isset($response['result']) && $response['result'] == 'True')
|
994
|
|
- {
|
|
951
|
+ $response = $this->doApiGet($path, array(200, 404));
|
|
952
|
+ if (isset($response['result']) && $response['result'] == 'true') {
|
995
|
953
|
return true;
|
996
|
954
|
}
|
997
|
955
|
return false;
|
|
@@ -1007,12 +965,12 @@ class ApiLayerService
|
1007
|
965
|
public function getFolderUrlByNotebookUrl($url)
|
1008
|
966
|
{
|
1009
|
967
|
// redirige l'utilisateur sur le dossier du notebook
|
1010
|
|
- $urlParts = explode('/',$url);
|
|
968
|
+ $urlParts = explode('/', $url);
|
1011
|
969
|
array_pop($urlParts);
|
1012
|
|
- return join('/',$urlParts);
|
|
970
|
+ return join('/', $urlParts);
|
1013
|
971
|
}
|
1014
|
972
|
|
1015
|
|
- public function resetNotebook(JupyterToken $trainerToken,JupyterToken $studentToken,$urlFrom,$urlTo)
|
|
973
|
+ public function resetNotebook(JupyterToken $trainerToken, JupyterToken $studentToken, $urlFrom, $urlTo)
|
1016
|
974
|
{
|
1017
|
975
|
$path = $this->buildPath(
|
1018
|
976
|
array(
|
|
@@ -1020,17 +978,37 @@ class ApiLayerService
|
1020
|
978
|
'clone',
|
1021
|
979
|
$trainerToken->getUuid(),
|
1022
|
980
|
$studentToken->getUuid(),
|
1023
|
|
- $this->getFolderNameByUrl($urlFrom,false),
|
|
981
|
+ $this->getFolderNameByUrl($urlFrom, false),
|
1024
|
982
|
$this->getFolderNameByUrl($urlTo),
|
1025
|
983
|
true
|
1026
|
984
|
)
|
1027
|
985
|
);
|
1028
|
986
|
|
1029
|
|
- $response = $this->doApiPost($path,array(200,404));
|
1030
|
|
- if (isset($response['result']) && $response['result'] == 'True')
|
1031
|
|
- {
|
|
987
|
+ $response = $this->doApiPost($path, array(200, 404));
|
|
988
|
+ if (isset($response['result']) && $response['result'] == 'true') {
|
1032
|
989
|
return true;
|
1033
|
990
|
}
|
1034
|
991
|
return false;
|
1035
|
992
|
}
|
1036
|
|
-}
|
|
993
|
+
|
|
994
|
+ public function resetAssesmentNotebook(JupyterVirtualClassroom $sequenceItem,JupyterToken $trainerToken, JupyterToken $studentToken, $urlFrom): string
|
|
995
|
+ {
|
|
996
|
+ $path = $this->buildPath(
|
|
997
|
+ array(
|
|
998
|
+ 'phoenix',
|
|
999
|
+ 'fetch',
|
|
1000
|
+ $trainerToken->getUuid(),
|
|
1001
|
+ $studentToken->getUuid(),
|
|
1002
|
+ $this->getFolderNameByUrl($urlFrom, false),
|
|
1003
|
+ $this->getNoteBookName($sequenceItem)
|
|
1004
|
+ )
|
|
1005
|
+ );
|
|
1006
|
+
|
|
1007
|
+ $response = $this->doApiPost($path, array(200, 404));
|
|
1008
|
+
|
|
1009
|
+ if (isset($response['result']) && $response['result'] == 'true') {
|
|
1010
|
+ return $response['url'];
|
|
1011
|
+ }
|
|
1012
|
+ return '';
|
|
1013
|
+ }
|
|
1014
|
+}
|