From b9f46baefcfaf2890dc3cfa5d1d7fe3f547f0e19 Mon Sep 17 00:00:00 2001 From: Khaled Lela Date: Mon, 8 May 2023 14:50:05 +0300 Subject: handle scorm with identifer --- src/Manager/ScormManager.php | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/Manager/ScormManager.php b/src/Manager/ScormManager.php index d403d1e..bb502f4 100644 --- a/src/Manager/ScormManager.php +++ b/src/Manager/ScormManager.php @@ -42,16 +42,12 @@ class ScormManager public function uploadScormFromUri($file, $uuid = null) { // $uuid is meant for user to update scorm content. Hence, if user want to update content should parse in existing uuid - if (!empty($uuid)) - { + if (!empty($uuid)) { $this->uuid = $uuid; - } - else - { + } else { $this->uuid = Str::uuid(); } - $scorm = null; $this->scormDisk->readScormArchive($file, function ($path) use (&$scorm, $file, $uuid) { $filename = basename($file); @@ -69,12 +65,9 @@ class ScormManager public function uploadScormArchive(UploadedFile $file, $uuid = null) { // $uuid is meant for user to update scorm content. Hence, if user want to update content should parse in existing uuid - if (!empty($uuid)) - { + if (!empty($uuid)) { $this->uuid = $uuid; - } - else - { + } else { $this->uuid = Str::uuid(); } @@ -117,8 +110,7 @@ class ScormManager } // This uuid is use when the admin wants to edit existing scorm file. - if (!empty($uuid)) - { + if (!empty($uuid)) { $this->uuid = $uuid; // Overwrite system generated uuid } @@ -136,8 +128,11 @@ class ScormManager * Handle dynamic method calls into the method. * return $this->dynamicWhere($method, $parameters); **/ + + $scorm = ScormModel::whereOriginFile($filename); + // Uuid indicator is better than filename for update content or add new content. - $scorm = ScormModel::whereUuid($this->uuid); + // $scorm = ScormModel::whereUuid($this->uuid); // Check if scom package already exists to drop old one. if (!$scorm->exists()) { @@ -608,7 +603,8 @@ class ScormManager $tracking->setLessonStatus($lessonStatus); $bestStatus = $lessonStatus; - if (empty($tracking->getCompletionStatus()) + if ( + empty($tracking->getCompletionStatus()) || ($completionStatus !== $tracking->getCompletionStatus() && $statusPriority[$completionStatus] > $statusPriority[$tracking->getCompletionStatus()]) ) { // This is no longer needed as completionStatus and successStatus are merged together @@ -656,7 +652,8 @@ class ScormManager return $updateResult; } - public function resetUserData($scormId, $userId) { + public function resetUserData($scormId, $userId) + { $scos = ScormScoModel::where('scorm_id', $scormId)->get(); foreach ($scos as $sco) { -- cgit v1.2.3 From c8c335715f653fc67282aaa78892ba06133732a2 Mon Sep 17 00:00:00 2001 From: Khaled Lela Date: Mon, 8 May 2023 14:57:34 +0300 Subject: handle scorm with uuid --- src/Manager/ScormManager.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Manager/ScormManager.php b/src/Manager/ScormManager.php index bb502f4..cf9587c 100644 --- a/src/Manager/ScormManager.php +++ b/src/Manager/ScormManager.php @@ -128,11 +128,9 @@ class ScormManager * Handle dynamic method calls into the method. * return $this->dynamicWhere($method, $parameters); **/ - - $scorm = ScormModel::whereOriginFile($filename); - + // $scorm = ScormModel::whereOriginFile($filename); // Uuid indicator is better than filename for update content or add new content. - // $scorm = ScormModel::whereUuid($this->uuid); + $scorm = ScormModel::whereUuid($this->uuid); // Check if scom package already exists to drop old one. if (!$scorm->exists()) { -- cgit v1.2.3 From 70f8710355ec0a91d279b12a2c0e74083dc6e7b6 Mon Sep 17 00:00:00 2001 From: Khaled Lela Date: Mon, 8 May 2023 14:58:54 +0300 Subject: update where uuid --- src/Manager/ScormManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Manager/ScormManager.php b/src/Manager/ScormManager.php index cf9587c..2dcaa5e 100644 --- a/src/Manager/ScormManager.php +++ b/src/Manager/ScormManager.php @@ -130,7 +130,7 @@ class ScormManager **/ // $scorm = ScormModel::whereOriginFile($filename); // Uuid indicator is better than filename for update content or add new content. - $scorm = ScormModel::whereUuid($this->uuid); + $scorm = ScormModel::where('uuid', $this->uuid); // Check if scom package already exists to drop old one. if (!$scorm->exists()) { -- cgit v1.2.3 From 924c72de0af5fd00ae171189788b8a713472afba Mon Sep 17 00:00:00 2001 From: Khaled Lela Date: Thu, 14 Aug 2025 13:10:00 +0300 Subject: Enhance SCORM file handling with improved error logging and validation. Added checks for disk accessibility and file existence in readScormArchive method. Updated uuid assignment logic in uploadScormFromUri and uploadScormArchive methods. --- src/Manager/ScormDisk.php | 67 ++++++++++++++++++++++++++++++++++++++------ src/Manager/ScormManager.php | 20 ++++++------- 2 files changed, 69 insertions(+), 18 deletions(-) diff --git a/src/Manager/ScormDisk.php b/src/Manager/ScormDisk.php index 99988af..47fd3eb 100644 --- a/src/Manager/ScormDisk.php +++ b/src/Manager/ScormDisk.php @@ -47,16 +47,35 @@ class ScormDisk public function readScormArchive($file, callable $fn) { try { + $archiveDisk = $this->getArchiveDisk(); + + // Log the file path being processed for debugging + Log::info('Processing SCORM archive file: ' . $file); + + // Check if file exists on archive disk + if (!$archiveDisk->exists($file)) { + Log::error('File not found on archive disk: ' . $file); + throw new StorageNotFoundException('scorm_archive_not_found_on_archive_disk: ' . $file); + } + + // Get the stream from archive disk + $stream = $archiveDisk->readStream($file); + if (!is_resource($stream)) { + Log::error('Failed to read stream from archive disk for file: ' . $file . '. Stream type: ' . gettype($stream)); + throw new StorageNotFoundException('failed_to_read_scorm_archive_stream: ' . $file); + } + if (Storage::exists($file)) { Storage::delete($file); } - Storage::writeStream($file, $this->getArchiveDisk()->readStream($file)); + + Storage::writeStream($file, $stream); $path = Storage::path($file); call_user_func($fn, $path); // Clean local resources $this->clean($file); } catch (Exception $ex) { - Log::error($ex->getMessage()); + Log::error('Error in readScormArchive: ' . $ex->getMessage() . ' for file: ' . $file); throw new StorageNotFoundException('scorm_archive_not_found'); } } @@ -132,10 +151,26 @@ class ScormDisk */ private function getDisk() { - if (!config()->has('filesystems.disks.' . config('scorm.disk'))) { - throw new StorageNotFoundException('scorm_disk_not_define'); + $diskName = config('scorm.disk'); + if (empty($diskName)) { + throw new StorageNotFoundException('scorm_disk_not_configured'); } - return Storage::disk(config('scorm.disk')); + + if (!config()->has('filesystems.disks.' . $diskName)) { + throw new StorageNotFoundException('scorm_disk_not_define: ' . $diskName); + } + + $disk = Storage::disk($diskName); + + // Test if the disk is accessible + try { + $disk->exists('test'); + } catch (Exception $ex) { + Log::error('SCORM disk not accessible: ' . $ex->getMessage()); + throw new StorageNotFoundException('scorm_disk_not_accessible: ' . $diskName); + } + + return $disk; } /** @@ -143,9 +178,25 @@ class ScormDisk */ private function getArchiveDisk() { - if (!config()->has('filesystems.disks.' . config('scorm.archive'))) { - throw new StorageNotFoundException('scorm_archive_disk_not_define'); + $archiveDiskName = config('scorm.archive'); + if (empty($archiveDiskName)) { + throw new StorageNotFoundException('scorm_archive_disk_not_configured'); + } + + if (!config()->has('filesystems.disks.' . $archiveDiskName)) { + throw new StorageNotFoundException('scorm_archive_disk_not_define: ' . $archiveDiskName); + } + + $disk = Storage::disk($archiveDiskName); + + // Test if the disk is accessible + try { + $disk->exists('test'); + } catch (Exception $ex) { + Log::error('Archive disk not accessible: ' . $ex->getMessage()); + throw new StorageNotFoundException('scorm_archive_disk_not_accessible: ' . $archiveDiskName); } - return Storage::disk(config('scorm.archive')); + + return $disk; } } diff --git a/src/Manager/ScormManager.php b/src/Manager/ScormManager.php index 2dcaa5e..abf1e26 100644 --- a/src/Manager/ScormManager.php +++ b/src/Manager/ScormManager.php @@ -42,12 +42,16 @@ class ScormManager public function uploadScormFromUri($file, $uuid = null) { // $uuid is meant for user to update scorm content. Hence, if user want to update content should parse in existing uuid - if (!empty($uuid)) { - $this->uuid = $uuid; - } else { - $this->uuid = Str::uuid(); + $this->uuid = $uuid ?? Str::uuid()->toString(); + + // Validate that the file parameter is not empty + if (empty($file)) { + throw new InvalidScormArchiveException('file_parameter_empty'); } + // Log the file being processed for debugging + \Log::info('Uploading SCORM from URI: ' . $file); + $scorm = null; $this->scormDisk->readScormArchive($file, function ($path) use (&$scorm, $file, $uuid) { $filename = basename($file); @@ -65,11 +69,7 @@ class ScormManager public function uploadScormArchive(UploadedFile $file, $uuid = null) { // $uuid is meant for user to update scorm content. Hence, if user want to update content should parse in existing uuid - if (!empty($uuid)) { - $this->uuid = $uuid; - } else { - $this->uuid = Str::uuid(); - } + $this->uuid = $uuid ?? Str::uuid()->toString(); return $this->saveScorm($file, $file->getClientOriginalName(), $uuid); } @@ -401,7 +401,7 @@ class ScormManager 'user_id' => $userId, 'sco_id' => $sco->id ], [ - 'uuid' => Str::uuid(), + 'uuid' => Str::uuid()->toString(), 'progression' => $scoTracking->getProgression(), 'score_raw' => $scoTracking->getScoreRaw(), 'score_min' => $scoTracking->getScoreMin(), -- cgit v1.2.3 From e43bcd1830be760ee2b05c559c661710d107d826 Mon Sep 17 00:00:00 2001 From: Khaled Lela Date: Thu, 14 Aug 2025 14:41:34 +0300 Subject: Refactor SCORM disk handling by removing redundant disk accessibility checks and cleaning up whitespace. This improves code readability and maintains existing functionality. --- src/Manager/ScormDisk.php | 38 +++++++++++--------------------------- 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/src/Manager/ScormDisk.php b/src/Manager/ScormDisk.php index 47fd3eb..5b71904 100644 --- a/src/Manager/ScormDisk.php +++ b/src/Manager/ScormDisk.php @@ -48,27 +48,27 @@ class ScormDisk { try { $archiveDisk = $this->getArchiveDisk(); - + // Log the file path being processed for debugging Log::info('Processing SCORM archive file: ' . $file); - + // Check if file exists on archive disk if (!$archiveDisk->exists($file)) { Log::error('File not found on archive disk: ' . $file); throw new StorageNotFoundException('scorm_archive_not_found_on_archive_disk: ' . $file); } - + // Get the stream from archive disk $stream = $archiveDisk->readStream($file); if (!is_resource($stream)) { Log::error('Failed to read stream from archive disk for file: ' . $file . '. Stream type: ' . gettype($stream)); throw new StorageNotFoundException('failed_to_read_scorm_archive_stream: ' . $file); } - + if (Storage::exists($file)) { Storage::delete($file); } - + Storage::writeStream($file, $stream); $path = Storage::path($file); call_user_func($fn, $path); @@ -155,21 +155,13 @@ class ScormDisk if (empty($diskName)) { throw new StorageNotFoundException('scorm_disk_not_configured'); } - + if (!config()->has('filesystems.disks.' . $diskName)) { throw new StorageNotFoundException('scorm_disk_not_define: ' . $diskName); } - + $disk = Storage::disk($diskName); - - // Test if the disk is accessible - try { - $disk->exists('test'); - } catch (Exception $ex) { - Log::error('SCORM disk not accessible: ' . $ex->getMessage()); - throw new StorageNotFoundException('scorm_disk_not_accessible: ' . $diskName); - } - + return $disk; } @@ -182,21 +174,13 @@ class ScormDisk if (empty($archiveDiskName)) { throw new StorageNotFoundException('scorm_archive_disk_not_configured'); } - + if (!config()->has('filesystems.disks.' . $archiveDiskName)) { throw new StorageNotFoundException('scorm_archive_disk_not_define: ' . $archiveDiskName); } - + $disk = Storage::disk($archiveDiskName); - - // Test if the disk is accessible - try { - $disk->exists('test'); - } catch (Exception $ex) { - Log::error('Archive disk not accessible: ' . $ex->getMessage()); - throw new StorageNotFoundException('scorm_archive_disk_not_accessible: ' . $archiveDiskName); - } - + return $disk; } } -- cgit v1.2.3 From 1813b3a3ecca7e7bc1e9ae9d1baf1b559a70168b Mon Sep 17 00:00:00 2001 From: Khaled Lela Date: Thu, 14 Aug 2025 15:28:11 +0300 Subject: Refactor error handling in readScormArchive method to throw the original exception instead of a custom StorageNotFoundException --- src/Manager/ScormDisk.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Manager/ScormDisk.php b/src/Manager/ScormDisk.php index 5b71904..41de387 100644 --- a/src/Manager/ScormDisk.php +++ b/src/Manager/ScormDisk.php @@ -76,7 +76,7 @@ class ScormDisk $this->clean($file); } catch (Exception $ex) { Log::error('Error in readScormArchive: ' . $ex->getMessage() . ' for file: ' . $file); - throw new StorageNotFoundException('scorm_archive_not_found'); + throw $ex; } } -- cgit v1.2.3