Skip to content

Commit a0050d5

Browse files
committed
Remove Async EqualAccess, files can now save to LMS.
1 parent da42f63 commit a0050d5

File tree

12 files changed

+78
-303
lines changed

12 files changed

+78
-303
lines changed

assets/js/Components/ReviewFilesPage.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,9 @@ export default function ReviewFilesPage({
398398
* handleFileUpload is called when a new file has already been selected by the user
399399
* and is ready to be uploaded to the server and verified.
400400
*/
401-
const handleFileUpload = (newFileData) => {
401+
const handleFileUpload = (newFileData, changeReferences = false) => {
402402

403-
const tempFile = Object.assign({}, activeIssue.fileData)
403+
const tempFile = Object.assign({}, activeIssue.fileData, { changeReferences: changeReferences } )
404404

405405
updateActiveSessionIssue("file-" + tempFile.id, settings.ISSUE_STATE.SAVING)
406406

@@ -409,8 +409,17 @@ export default function ReviewFilesPage({
409409
api.postFile(tempFile, newFileData)
410410
.then((responsStr) => responsStr.json())
411411
.then((response) => {
412-
const updatedFileData = { ...tempFile, ...response.data.file }
412+
if(response.errors && response.errors.length > 0) {
413+
response.errors.forEach((err) => addMessage({ message: t(err), severity: 'error', visible: true }))
414+
updateActiveSessionIssue("file-" + tempFile.id, settings.ISSUE_STATE.ERROR)
415+
return
416+
}
413417

418+
const updatedFileData = response?.data?.lmsResponse?.content
419+
let metadataObj = tempFile.metadata ? JSON.parse(tempFile.metadata) : {}
420+
console.log(updatedFileData)
421+
metadataObj.replacedByFileId = updatedFileData.id
422+
414423
// Set messages
415424
response.messages.forEach((msg) => addMessage(msg))
416425

assets/js/Services/Report.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ export function analyzeReport(report, ISSUE_STATE) {
194194
fileReferences[fileId].push({
195195
contentItemTitle: contentItem.title,
196196
contentItemUrl: contentItem.url,
197+
contentItemLmsId: contentItem.lmsContentId,
197198
contentType: contentItem.contentType,
198199
sectionIds: contentItem.sections,
199200
})

src/Controller/FileItemsController.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Symfony\Component\HttpFoundation\JsonResponse;
1111
use Symfony\Component\HttpFoundation\Request;
1212
use Symfony\Component\Routing\Annotation\Route;
13+
use Symfony\Component\Console\Output\ConsoleOutput;
1314

1415
class FileItemsController extends ApiController
1516
{
@@ -65,6 +66,7 @@ public function reviewFile(FileItem $file, Request $request, UtilityService $uti
6566
#[Route('/api/files/{file}/post', methods: ['POST'], name: 'file_post')]
6667
public function postFile(FileItem $file, Request $request, UtilityService $util, LmsPostService $lmsPost)
6768
{
69+
$output = new ConsoleOutput();
6870
$apiResponse = new ApiResponse();
6971
$user = $this->getUser();
7072

@@ -77,10 +79,17 @@ public function postFile(FileItem $file, Request $request, UtilityService $util,
7779

7880
$uploadedFile = $request->files->get('file');
7981

82+
$output->writeln("Uploading file: " . $uploadedFile->getClientOriginalName());
8083
// Save content to LMS
8184
$lmsResponse = $lmsPost->saveFileToLms($file, $uploadedFile, $user);
85+
$responseContent = $lmsResponse->getContent();
8286

83-
$apiResponse->setData($lmsResponse);
87+
$output->writeln("Response Content: " . print_r($responseContent, true));
88+
89+
// If the new file was successfully posted, update the FileItem metadata to point to this replacement
90+
if (isset($responseContent['id'])) {
91+
$file->setReplacementFile($responseContent);
92+
}
8493

8594
// Update report stats
8695
$report = $course->getUpdatedReport();
@@ -91,7 +100,9 @@ public function postFile(FileItem $file, Request $request, UtilityService $util,
91100
$apiResponse->addMessage('form.msg.success_replaced', 'success', 5000);
92101

93102
$apiResponse->addLogMessages($util->getUnreadMessages());
103+
94104
$apiResponse->setData([
105+
'lmsResponse' => $lmsResponse,
95106
'file' => ['pending' => false],
96107
'report' => $report,
97108
]);

src/Entity/Course.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,9 @@ public function getFileItems($activeOnly = true): array
299299
$files = [];
300300

301301
foreach ($this->fileItems as $file) {
302-
$files[] = $file;
302+
if (!$activeOnly || $file->isActive()) {
303+
$files[] = $file;
304+
}
303305
}
304306

305307
return $files;

src/Entity/FileItem.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,32 @@ public function setMetadata(?string $metadata): self
137137
return $this;
138138
}
139139

140+
public function setReplacementFile($fileData): self
141+
{
142+
$replacement = [
143+
'id' => $fileData['id'],
144+
'fileName' => $fileData['display_name'],
145+
'fileType' => $fileData['mime_class'],
146+
'url' => $fileData['url'],
147+
];
148+
149+
$tempMetadata = json_decode($this->getMetadata(), true);
150+
$tempMetadata['replacement'] = $replacement;
151+
$this->setMetadata(json_encode($tempMetadata));
152+
153+
return $this;
154+
}
155+
156+
public function getReplacementFile(): ?array
157+
{
158+
$metadata = json_decode($this->getMetadata(), true);
159+
if (isset($metadata['replacement'])) {
160+
return $metadata['replacement'];
161+
}
162+
163+
return [];
164+
}
165+
140166
public function getStatus(): ?bool
141167
{
142168
return $this->status;
@@ -188,6 +214,8 @@ public function setReviewed(?bool $reviewed): self
188214
public function update($file): self
189215
{
190216
$updatedDate = new \DateTime($file['updated'], UtilityService::$timezone);
217+
$replacementFile = $this->getReplacementFile();
218+
$file['replacement'] = $replacementFile;
191219

192220
$this->setUpdated($updatedDate);
193221
$this->setActive(true);
@@ -217,6 +245,7 @@ public function jsonSerialize(): array
217245
'reviewed' => $this->getReviewed(),
218246
'downloadUrl' => $this->getDownloadUrl(),
219247
'lmsUrl' => $this->getLmsUrl(),
248+
'metadata' => json_decode($this->getMetadata(), true),
220249
];
221250
}
222251

src/Lms/Canvas/CanvasApi.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Symfony\Component\HttpClient\HttpClient;
77
use Symfony\Component\Mime\Part\DataPart;
88
use Symfony\Component\Mime\Part\Multipart\FormDataPart;
9+
use Symfony\Component\Console\Output\ConsoleOutput;
910

1011
class CanvasApi {
1112

@@ -111,28 +112,22 @@ public function apiPost($url, $options, $sendAuthorized = true)
111112
// Posts a file to Canvas
112113
public function apiFilePost(string $url, array $options, string $filepath, string $newFileName): LmsResponse
113114
{
115+
// Since files are posted to REPLACE existing files, they should be placed in the same folder
114116
$fileResponse = $this->apiGet($url);
115117
$file = $fileResponse->getContent();
116-
117-
// TODO: handle failed call
118-
119118
$endpointOptions = [
120119
'name' => $newFileName,
121120
'parent_folder_id' => $file['folder_id'],
122-
'content_type' => $file['content-type'],
123121
];
124122

123+
// Get the upload endpoint from Canvas for the new file
125124
$endpointResponse = $this->apiPost($options['postUrl'], ['query' => $endpointOptions], true);
126125
$endpointContent = $endpointResponse->getContent();
127126

128-
$this->apiDelete($url);
129-
130-
// TODO: handle failed call
131-
127+
// Attach the file and send it to the upload URL
132128
$formFields = $endpointContent['upload_params'];
133129
$formFields['file'] = DataPart::fromPath($filepath);
134130
$formData = new FormDataPart($formFields);
135-
136131
$fileResponse = $this->apiPost($endpointContent['upload_url'], [
137132
'headers' => $formData->getPreparedHeaders()->toArray(),
138133
'body' => $formData->bodyToIterable(),

src/Lms/Canvas/CanvasLms.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ public function postContentItem(ContentItem $contentItem)
461461

462462
public function postFileItem(FileItem $file, string $newFileName)
463463
{
464+
$output = new ConsoleOutput();
464465
$user = $this->security->getUser();
465466
$apiDomain = $this->getApiDomain($user);
466467
$apiToken = $this->getApiToken($user);
@@ -471,13 +472,9 @@ public function postFileItem(FileItem $file, string $newFileName)
471472
'postUrl' => "courses/{$file->getCourse()->getLmsCourseId()}/files"
472473
];
473474

475+
$output->writeln("Posting file to LMS: " . $url . " with file path: " . $filepath);
474476
$fileResponse = $canvasApi->apiFilePost($url, $options, $filepath, $newFileName);
475-
$fileObj = $fileResponse->getContent();
476-
477-
if (isset($fileObj['id'])) {
478-
$file->setLmsFileId($fileObj['id']);
479-
$this->entityManager->flush();
480-
}
477+
481478

482479
return $fileResponse;
483480
}

0 commit comments

Comments
 (0)