Skip to content

Commit db3ddf3

Browse files
committed
Merge pull request #569 from LearningLocker/issue/attachments
Returns attachments when attachments set to true.
2 parents cdd7e93 + 437c4a3 commit db3ddf3

File tree

5 files changed

+108
-35
lines changed

5 files changed

+108
-35
lines changed

app/controllers/xapi/StatementController.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,30 @@ public function index() {
222222
}
223223

224224
// Returns the StatementResult object.
225-
return $this->makeStatementObject($statements, [
225+
$statement_result = json_encode($this->makeStatementObject($statements, [
226226
'total' => $total,
227227
'offset' => $options['offset'],
228228
'limit' => $options['limit']
229-
]);
229+
]));
230+
231+
if ($options['attachments'] === true) {
232+
$boundary = 'abcABC0123\'()+_,-./:=?';
233+
$content_type = 'multipart/mixed; boundary='.$boundary;
234+
$statement_result = "Content-Type:application/json\r\n\r\n".$statement_result;
235+
$body = "--$boundary\r\n".implode(
236+
"\r\n--$boundary\r\n",
237+
array_merge([$statement_result], $this->statement->getAttachments($statements, $this->lrs->_id))
238+
)."\r\n--$boundary--";
239+
} else {
240+
$content_type = 'application/json;';
241+
$body = $statement_result;
242+
}
243+
244+
// Creates the response.
245+
return \Response::make($body, BaseController::OK, [
246+
'Content-Type' => $content_type,
247+
'X-Experience-API-Consistent-Through' => $this->statement->getCurrentDate()
248+
]);;
230249
}
231250

232251
/**
@@ -274,19 +293,12 @@ private function makeStatementObject(array $statements, array $options) {
274293
}
275294

276295
// Creates the statement result.
277-
$statementResult = [
296+
$statement_result = [
278297
'more' => $this->getMoreLink($options['total'], $options['limit'], $options['offset']),
279298
'statements' => $statements
280299
];
281300

282-
// Creates the response.
283-
$response = \Response::make($statementResult, BaseController::OK);
284-
$response->headers->set(
285-
'X-Experience-API-Consistent-Through',
286-
$this->statement->getCurrentDate()
287-
);
288-
289-
return $response;
301+
return $statement_result;
290302
}
291303

292304
/**

app/database/migrations/2015_02_13_100706_statement_id_index.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,4 @@ public function down() {
1616
$table->dropIndex(['statement.id', 'lrs._id']);
1717
});
1818
}
19-
2019
}

app/database/migrations/2015_02_25_154357_document_sha.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ public function up() {
1111
$document->sha = trim($document->sha, '"');
1212
$document->save();
1313
}
14-
echo(count($documents) . ' converted.');
14+
echo(count($documents) . ' converted.').PHP_EOL;
1515
});
1616

17-
echo('All finished, hopefully!');
17+
echo('All finished, hopefully!').PHP_EOL;
1818
}
1919

2020
public function down() {
@@ -23,10 +23,10 @@ public function down() {
2323
$document->sha = '"'.trim($document->sha, '"').'"';
2424
$document->save();
2525
}
26-
echo(count($documents) . ' converted.');
26+
echo(count($documents) . ' converted.').PHP_EOL;
2727
});
2828

29-
echo('All finished, hopefully!');
29+
echo('All finished, hopefully!').PHP_EOL;
3030
}
3131
}
3232

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
use \Locker\Helpers\Helpers as Helpers;
6+
7+
class AttachmentRename extends Migration {
8+
9+
public function up() {
10+
$uploads = Helpers::getEnvVar('LOCAL_FILESTORE');
11+
$LRSs = $this->getDirectores($uploads);
12+
13+
// Gets the attachments.
14+
$attachments = [];
15+
foreach ($LRSs as $lrs) {
16+
$attachments = array_merge($attachments, array_map(function ($dir) use ($uploads, $lrs) {
17+
return $uploads.'/'.$lrs.'/attachments/'.$dir;
18+
}, $this->getDirectores(
19+
$uploads.'/'.$lrs.'/attachments'
20+
)));
21+
}
22+
23+
// Migrates the attachments.
24+
foreach ($attachments as $attachment) {
25+
$file = $attachment.'/'.scandir($attachment)[2];
26+
$ext = pathinfo($file, PATHINFO_EXTENSION);
27+
file_put_contents($attachment.'.'.$ext, file_get_contents($file));
28+
}
29+
30+
echo 'Migrated '.count($attachments)." attachments.".PHP_EOL;
31+
}
32+
33+
private function getDirectores($path) {
34+
return array_values(array_filter(scandir($path), function ($name) use ($path) {
35+
return $name[0] !== '.' && is_dir($path.'/'.$name);
36+
}));
37+
}
38+
39+
public function down() {
40+
41+
}
42+
}

app/locker/repository/Statement/EloquentStatementRepository.php

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -851,13 +851,34 @@ private function doesStatementIdExist($lrsId, array $statement) {
851851
return null;
852852
}
853853

854+
public function getAttachments($statements, $lrs) {
855+
$destination_path = Helpers::getEnvVar('LOCAL_FILESTORE').'/'.$lrs.'/attachments/';
856+
$attachments = [];
857+
858+
foreach ($statements as $statement) {
859+
$statement = $statement['statement'];
860+
$attachments = array_merge($attachments, array_map(function ($attachment) use ($destination_path) {
861+
$ext = array_search($attachment['contentType'], FileTypes::getMap());
862+
$filename = $attachment['sha2'].'.'.$ext;
863+
return (
864+
'Content-Type:'.$attachment['contentType']."\r\n".
865+
'Content-Transfer-Encoding:binary'."\r\n".
866+
'X-Experience-API-Hash:'.$attachment['sha2'].
867+
"\r\n\r\n".
868+
file_get_contents($destination_path.$filename)
869+
);
870+
}, isset($statement['attachments']) ? $statement['attachments'] : []));
871+
}
872+
873+
return $attachments;
874+
}
875+
854876
/**
855877
* Store any attachments
856878
*
857879
**/
858880
private function storeAttachments( $attachments, $lrs ){
859-
860-
foreach( $attachments as $attachment ){
881+
foreach ($attachments as $attachment) {
861882
// Determines the delimiter.
862883
$delim = "\n";
863884
if (strpos($attachment, "\r".$delim) !== false) $delim = "\r".$delim;
@@ -868,35 +889,34 @@ private function storeAttachments( $attachments, $lrs ){
868889

869890
// Parse headers and separate so we can access
870891
$raw_headers = explode($delim, $raw_headers);
871-
$headers = array();
892+
$headers = [];
872893
foreach ($raw_headers as $header) {
873894
list($name, $value) = explode(':', $header);
874895
$headers[strtolower($name)] = ltrim($value, ' ');
875896
}
876897

877898
//get the correct ext if valid
878-
$ext = array_search( $headers['content-type'], FileTypes::getMap() );
879-
if( $ext === false ){
880-
\App::abort(400, 'This file type cannot be supported');
881-
}
899+
$ext = array_search($headers['content-type'], FileTypes::getMap());
900+
if ($ext === false) throw new Exceptions\Exception(
901+
'This file type cannot be supported'
902+
);
882903

883-
$filename = str_random(12) . "." . $ext;
904+
$destination_path = Helpers::getEnvVar('LOCAL_FILESTORE').'/'.$lrs.'/attachments/';
905+
$filename = $headers['x-experience-api-hash'];
884906

885907
//create directory if it doesn't exist
886-
if (!\File::exists(Helpers::getEnvVar('LOCAL_FILESTORE').'/'.$lrs.'/attachments/' . $headers['x-experience-api-hash'] . '/')) {
887-
\File::makeDirectory(Helpers::getEnvVar('LOCAL_FILESTORE').'/'.$lrs.'/attachments/' . $headers['x-experience-api-hash'] . '/', 0775, true);
908+
if (!\File::exists($destination_path)) {
909+
\File::makeDirectory($destination_path, 0775, true);
888910
}
889911

890-
$destinationPath = Helpers::getEnvVar('LOCAL_FILESTORE').'/'.$lrs.'/attachments/' . $headers['x-experience-api-hash'] . '/';
891-
892-
$filename = $destinationPath.$filename;
893-
$file = fopen( $filename, 'wb'); //opens the file for writing with a BINARY (b) fla
894-
$size = fwrite( $file, $body ); //write the data to the file
895-
fclose( $file );
912+
$filename = $destination_path.$filename.'.'.$ext;
913+
$file = fopen($filename, 'wb'); //opens the file for writing with a BINARY (b) fla
914+
$size = fwrite($file, $body); //write the data to the file
915+
fclose($file);
896916

897-
if( $size === false ){
898-
\App::abort( 400, 'There was an issue saving the attachment');
899-
}
917+
if($size === false) throw new Exceptions\Exception(
918+
'There was an issue saving the attachment'
919+
);
900920
}
901921

902922
}

0 commit comments

Comments
 (0)