Skip to content

Commit 9f68e71

Browse files
committed
The Broken commit: Need to pull replacement files live instead of storing data that could be out-of-date.
1 parent a0050d5 commit 9f68e71

File tree

4 files changed

+42
-25
lines changed

4 files changed

+42
-25
lines changed

assets/js/Components/ReviewFilesPage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ export default function ReviewFilesPage({
416416
}
417417

418418
const updatedFileData = response?.data?.lmsResponse?.content
419-
let metadataObj = tempFile.metadata ? JSON.parse(tempFile.metadata) : {}
419+
let metadataObj = tempFile?.metadata || {}
420420
console.log(updatedFileData)
421421
metadataObj.replacedByFileId = updatedFileData.id
422422

src/Controller/FileItemsController.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,19 @@ public function postFile(FileItem $file, Request $request, UtilityService $util,
9090
if (isset($responseContent['id'])) {
9191
$file->setReplacementFile($responseContent);
9292
}
93-
93+
94+
$this->doctrine->getManager()->flush();
95+
9496
// Update report stats
9597
$report = $course->getUpdatedReport();
98+
99+
$reportArr = $report->toArray();
100+
$reportArr['files'] = $course->getFileItems();
101+
$reportArr['issues'] = $course->getAllIssues();
102+
$reportArr['contentItems'] = $course->getContentItems();
103+
$reportArr['contentSections'] = $lmsFetch->getCourseSections($course, $user);
96104

97-
$this->doctrine->getManager()->flush();
105+
$response->setData($reportArr);
98106

99107
// Create response
100108
$apiResponse->addMessage('form.msg.success_replaced', 'success', 5000);
@@ -104,7 +112,7 @@ public function postFile(FileItem $file, Request $request, UtilityService $util,
104112
$apiResponse->setData([
105113
'lmsResponse' => $lmsResponse,
106114
'file' => ['pending' => false],
107-
'report' => $report,
115+
'report' => $reportArr,
108116
]);
109117
} catch (\Exception $e) {
110118
$apiResponse->addError($e->getMessage());

src/Entity/FileItem.php

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use App\Repository\FileItemRepository;
66
use App\Services\UtilityService;
77
use Doctrine\ORM\Mapping as ORM;
8-
8+
use Symfony\Component\Console\Output\ConsoleOutput;
99

1010
#[ORM\Entity(repositoryClass: FileItemRepository::class)]
1111
class FileItem implements \JsonSerializable
@@ -139,16 +139,11 @@ public function setMetadata(?string $metadata): self
139139

140140
public function setReplacementFile($fileData): self
141141
{
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));
142+
if(!isset($fileData['id'])) {
143+
$tempMetadata = json_decode($this->getMetadata(), true);
144+
$tempMetadata['replacement'] = $fileData['id'];
145+
$this->setMetadata(json_encode($tempMetadata));
146+
}
152147

153148
return $this;
154149
}
@@ -157,7 +152,21 @@ public function getReplacementFile(): ?array
157152
{
158153
$metadata = json_decode($this->getMetadata(), true);
159154
if (isset($metadata['replacement'])) {
160-
return $metadata['replacement'];
155+
// Fetch the replacement file from the database using the LMS File ID
156+
$lmsFileId = $metadata['replacement'];
157+
$output = new ConsoleOutput();
158+
$output->writeln("Fetching replacement file with LMS File ID: " . $lmsFileId);
159+
160+
// TODO: Get ANOTHER file item IF it still exists in the database.
161+
// This file does NOT have access to the ManagerRegistry (doctrine) but we need it to do
162+
// something like this...
163+
164+
// $fileItemRepo = $this->doctrine->getManager()->getRepository(FileItem::class);
165+
// $replacementFile = $fileItemRepo->findOneBy(['lmsFileId' => $lmsFileId]);
166+
167+
// if ($replacementFile) {
168+
// return $replacementFile;
169+
// }
161170
}
162171

163172
return [];
@@ -232,6 +241,13 @@ public function update($file): self
232241

233242
public function jsonSerialize(): array
234243
{
244+
// If there is a replacement file, verify that it still exists.
245+
$replacementFileData = $this->getReplacementFile();
246+
if(isset($replacementFileData['id'])) {
247+
$output = new ConsoleOutput();
248+
$output->writeln("File Item " . $this->getId() . " has a replacement file ID of " . $replacementFileData['id']);
249+
}
250+
235251
return [
236252
'id' => $this->getId(),
237253
'fileName' => $this->getFileName(),
@@ -246,6 +262,7 @@ public function jsonSerialize(): array
246262
'downloadUrl' => $this->getDownloadUrl(),
247263
'lmsUrl' => $this->getLmsUrl(),
248264
'metadata' => json_decode($this->getMetadata(), true),
265+
'replacement' => $replacementFileData,
249266
];
250267
}
251268

src/Services/LmsPostService.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,7 @@ public function saveFileToLms(FileItem $file, UploadedFile $uploadedFile, User $
7676
return;
7777
}
7878

79-
$lmsResponse = $lms->postFileItem($file, $uploadedFile->getClientOriginalName());
80-
81-
// If the file was successfully posted, create a new FileItem in the UDOIT database
82-
$lmsResponseContent = $lmsResponse->getContent();
83-
if(isset($lmsResponseContent['id'])) {
84-
$lms->updateFileItem($file->getCourse(), $lmsResponseContent);
85-
}
86-
87-
return $lmsResponse;
79+
return $lms->postFileItem($file, $uploadedFile->getClientOriginalName());
8880
}
8981

9082
public function replaceContent(Issue $issue, ContentItem $contentItem, $fullPageHtml = null)

0 commit comments

Comments
 (0)