diff --git a/LearningHub.Nhs.WebUI/Controllers/Api/ContributeController.cs b/LearningHub.Nhs.WebUI/Controllers/Api/ContributeController.cs index b5c555b08..6bfbcca6c 100644 --- a/LearningHub.Nhs.WebUI/Controllers/Api/ContributeController.cs +++ b/LearningHub.Nhs.WebUI/Controllers/Api/ContributeController.cs @@ -343,17 +343,17 @@ public ActionResult GetSettings() [Route("PublishResourceVersion")] public async Task PublishResourceVersionAsync([FromBody] PublishViewModel publishViewModel) { - var associatedResource = await this.resourceService.GetResourceVersionExtendedAsync(publishViewModel.ResourceVersionId); + var associatedResource = await this.resourceService.GetResourceVersionAsync(publishViewModel.ResourceVersionId); var validationResult = await this.contributeService.SubmitResourceVersionForPublishAsync(publishViewModel); if (validationResult.IsValid) { - if (associatedResource.ResourceTypeEnum != ResourceTypeEnum.Scorm && associatedResource.ResourceTypeEnum != ResourceTypeEnum.Html) + if (associatedResource.ResourceType != ResourceTypeEnum.Scorm && associatedResource.ResourceType != ResourceTypeEnum.Html) { - var obsoleteFiles = await this.resourceService.GetObsoleteResourceFile(publishViewModel.ResourceVersionId); - if (obsoleteFiles != null && obsoleteFiles.Any()) - { - await this.fileService.PurgeResourceFile(null, obsoleteFiles); - } + var obsoleteFiles = await this.resourceService.GetObsoleteResourceFile(publishViewModel.ResourceVersionId); + if (obsoleteFiles != null && obsoleteFiles.Any()) + { + await this.fileService.PurgeResourceFile(null, obsoleteFiles); + } } } @@ -668,7 +668,7 @@ private async Task RemoveBlockCollectionFiles(int resourceVersionId, BlockCollec { foreach (var oldblock in existingAttachements) { - var entry = newBlocks.FirstOrDefault(x => x.BlockType == BlockType.Media && x.MediaBlock != null && x.MediaBlock.MediaType == MediaType.Attachment && x.MediaBlock.Attachment != null && x.MediaBlock.Attachment.File?.FileId == oldblock.MediaBlock.Attachment?.File?.FileId); + var entry = newBlocks.FirstOrDefault(x => x.BlockType == BlockType.Media && x.MediaBlock != null && x.MediaBlock.MediaType == MediaType.Attachment && x.MediaBlock.Attachment != null && (x.MediaBlock.Attachment.File?.FileId == oldblock.MediaBlock.Attachment?.File?.FileId || x.MediaBlock.Attachment.File?.FilePath == oldblock.MediaBlock.Attachment?.File?.FilePath)); if (entry == null) { filePaths.Add(oldblock.MediaBlock.Attachment?.File?.FilePath); @@ -681,7 +681,7 @@ private async Task RemoveBlockCollectionFiles(int resourceVersionId, BlockCollec { foreach (var oldblock in existingVideos) { - var entry = newBlocks.FirstOrDefault(x => x.BlockType == BlockType.Media && x.MediaBlock != null && x.MediaBlock.MediaType == MediaType.Video && x.MediaBlock.Video != null && x.MediaBlock.Video.VideoFile?.File?.FileId == oldblock.MediaBlock?.Video?.VideoFile?.File?.FileId); + var entry = newBlocks.FirstOrDefault(x => x.BlockType == BlockType.Media && x.MediaBlock != null && x.MediaBlock.MediaType == MediaType.Video && x.MediaBlock.Video != null && (x.MediaBlock.Video.VideoFile?.File?.FileId == oldblock.MediaBlock?.Video?.VideoFile?.File?.FileId || x.MediaBlock.Video.VideoFile?.File?.FilePath == oldblock.MediaBlock?.Video?.VideoFile?.File?.FilePath)); if (entry == null) { if (!string.IsNullOrWhiteSpace(oldblock.MediaBlock.Video.File.FilePath)) @@ -707,7 +707,7 @@ private async Task RemoveBlockCollectionFiles(int resourceVersionId, BlockCollec { foreach (var oldblock in existingImages) { - var entry = newBlocks.FirstOrDefault(x => x.BlockType == BlockType.Media && x.MediaBlock != null && x.MediaBlock.MediaType == MediaType.Image && x.MediaBlock.Image != null && x.MediaBlock?.Image?.File?.FileId == oldblock.MediaBlock?.Image?.File?.FileId); + var entry = newBlocks.FirstOrDefault(x => x.BlockType == BlockType.Media && x.MediaBlock != null && x.MediaBlock.MediaType == MediaType.Image && x.MediaBlock.Image != null && (x.MediaBlock?.Image?.File?.FileId == oldblock.MediaBlock?.Image?.File?.FileId || x.MediaBlock?.Image?.File?.FilePath == oldblock.MediaBlock?.Image?.File?.FilePath)); if (entry == null) { filePaths.Add(oldblock?.MediaBlock?.Image?.File?.FilePath); @@ -722,7 +722,7 @@ private async Task RemoveBlockCollectionFiles(int resourceVersionId, BlockCollec { foreach (var oldblock in imageBlock?.ImageCarouselBlock?.ImageBlockCollection?.Blocks) { - var entry = newBlocks.FirstOrDefault(x => x.BlockType == BlockType.ImageCarousel && x.ImageCarouselBlock != null && x.ImageCarouselBlock.ImageBlockCollection != null && x.ImageCarouselBlock.ImageBlockCollection.Blocks != null && x.ImageCarouselBlock.ImageBlockCollection.Blocks.Where(x => x.MediaBlock?.Image?.File?.FileId == oldblock.MediaBlock?.Image?.File?.FileId).Any()); + var entry = newBlocks.FirstOrDefault(x => x.BlockType == BlockType.ImageCarousel && x.ImageCarouselBlock != null && x.ImageCarouselBlock.ImageBlockCollection != null && x.ImageCarouselBlock.ImageBlockCollection.Blocks != null && x.ImageCarouselBlock.ImageBlockCollection.Blocks.Where(x => x.MediaBlock?.Image?.File?.FileId == oldblock.MediaBlock?.Image?.File?.FileId || x.MediaBlock?.Image?.File?.FilePath == oldblock.MediaBlock?.Image?.File?.FilePath).Any()); if (entry == null) { filePaths.Add(oldblock.MediaBlock?.Image?.File?.FilePath); @@ -738,10 +738,13 @@ private async Task RemoveBlockCollectionFiles(int resourceVersionId, BlockCollec { foreach (var oldblock in wsi?.WholeSlideImageBlock?.WholeSlideImageBlockItems) { - var entry = newBlocks.FirstOrDefault(x => x.WholeSlideImageBlock != null && x.WholeSlideImageBlock.WholeSlideImageBlockItems.Where(x => x.WholeSlideImage?.File?.FileId == oldblock.WholeSlideImage?.File?.FileId).Any()); - if (entry == null) + if (oldblock != null && (oldblock.WholeSlideImage.File.WholeSlideImageFile.Status == WholeSlideImageFileStatus.ProcessingComplete || oldblock.WholeSlideImage.File.WholeSlideImageFile.Status == WholeSlideImageFileStatus.ProcessingFailed)) { - filePaths.Add(oldblock.WholeSlideImage?.File?.FilePath); + var entry = newBlocks.FirstOrDefault(x => x.WholeSlideImageBlock != null && x.WholeSlideImageBlock.WholeSlideImageBlockItems.Where(x => x.WholeSlideImage?.File?.FileId == oldblock.WholeSlideImage?.File?.FileId || x.WholeSlideImage?.File?.FilePath == oldblock.WholeSlideImage?.File?.FilePath).Any()); + if (entry == null) + { + filePaths.Add(oldblock.WholeSlideImage?.File?.FilePath); + } } } } @@ -754,16 +757,17 @@ private async Task RemoveBlockCollectionFiles(int resourceVersionId, BlockCollec { if (!newQuestionFiles.Any()) { - filePaths.AddRange(existingQuestionFiles); + filePaths.AddRange(existingQuestionFiles.Values.ToList()); } else { foreach (var file in existingQuestionFiles) { - var entry = newQuestionFiles.FirstOrDefault(x => x.Equals(file)); - if (entry == null) + bool found = false; + var entry = newQuestionFiles.FirstOrDefault(x => (x.Key == file.Key || x.Value == file.Value) && (found = true)); + if (!found) { - filePaths.Add(file); + filePaths.Add(file.Value); } } } @@ -786,114 +790,117 @@ private async Task RemoveBlockCollectionFiles(int resourceVersionId, BlockCollec _ = Task.Run(async () => { await this.fileService.PurgeResourceFile(null, deleteList); }); } } - catch (Exception ex) + catch { } } - private List CheckQuestionBlock(BlockCollectionViewModel model) + private Dictionary CheckQuestionBlock(BlockCollectionViewModel model) { - var filePath = new List(); - foreach (var block in model.Blocks) + var filePath = new Dictionary(); + if (model != null && model.Blocks.Any()) { - if (block.BlockType == BlockType.Question && block.QuestionBlock != null) + foreach (var block in model.Blocks) { - if (block.QuestionBlock.QuestionType == QuestionTypeEnum.MatchGame && block.QuestionBlock.Answers != null) + if (block.BlockType == BlockType.Question && block.QuestionBlock != null) { - foreach (var answerBlock in block.QuestionBlock.Answers) + if (block.QuestionBlock.QuestionType == QuestionTypeEnum.MatchGame && block.QuestionBlock.Answers != null) { - if (answerBlock.BlockCollection != null && answerBlock.BlockCollection.Blocks != null) + foreach (var answerBlock in block.QuestionBlock.Answers) { - foreach (var imageBlock in answerBlock.BlockCollection.Blocks) + if (answerBlock.BlockCollection != null && answerBlock.BlockCollection.Blocks != null && answerBlock.BlockCollection.Blocks.Any()) { - if (imageBlock.BlockType == BlockType.Media && imageBlock.MediaBlock != null) + foreach (var imageBlock in answerBlock.BlockCollection.Blocks) { - filePath.Add(imageBlock.MediaBlock.Image.File.FilePath); + if (imageBlock.BlockType == BlockType.Media && imageBlock.MediaBlock != null && imageBlock.MediaBlock.Image.File != null) + { + filePath.Add(imageBlock.MediaBlock.Image.File.FileId, imageBlock.MediaBlock.Image.File.FilePath); + } } } } } - } - var questionBlockCollection = block.QuestionBlock.QuestionBlockCollection; - if (questionBlockCollection != null && questionBlockCollection.Blocks != null) - { - foreach (var questionBlock in questionBlockCollection.Blocks) + var questionBlockCollection = block.QuestionBlock.QuestionBlockCollection; + if (questionBlockCollection != null && questionBlockCollection.Blocks != null && questionBlockCollection.Blocks.Any()) { - if (questionBlock.BlockType == BlockType.Media && questionBlock.MediaBlock != null) + foreach (var questionBlock in questionBlockCollection.Blocks) { - if (questionBlock.MediaBlock.Image != null) + if (questionBlock.BlockType == BlockType.Media && questionBlock.MediaBlock != null) { - filePath.Add(questionBlock.MediaBlock.Image.File.FilePath); - } + if (questionBlock.MediaBlock.Image != null) + { + filePath.Add(questionBlock.MediaBlock.Image.File.FileId, questionBlock.MediaBlock.Image.File.FilePath); + } - if (questionBlock.MediaBlock.Video != null) - { - if (questionBlock.MediaBlock.Video.VideoFile != null) + if (questionBlock.MediaBlock.Video != null) { if (questionBlock.MediaBlock.Video.File != null) { - filePath.Add(questionBlock.MediaBlock.Video.File.FilePath); - } - - if (questionBlock.MediaBlock.Video.VideoFile.TranscriptFile != null) - { - filePath.Add(questionBlock.MediaBlock.Video.VideoFile.TranscriptFile.File.FilePath); + filePath.Add(questionBlock.MediaBlock.Video.File.FileId, questionBlock.MediaBlock.Video.File.FilePath); } - if (questionBlock.MediaBlock.Video.VideoFile.CaptionsFile != null) + if (questionBlock.MediaBlock.Video.VideoFile != null) { - filePath.Add(questionBlock.MediaBlock.Video.VideoFile.CaptionsFile.File.FilePath); + if (questionBlock.MediaBlock.Video.VideoFile.TranscriptFile != null) + { + filePath.Add(questionBlock.MediaBlock.Video.VideoFile.TranscriptFile.File.FileId, questionBlock.MediaBlock.Video.VideoFile.TranscriptFile.File.FilePath); + } + + if (questionBlock.MediaBlock.Video.VideoFile.CaptionsFile != null) + { + filePath.Add(questionBlock.MediaBlock.Video.VideoFile.CaptionsFile.File.FileId, questionBlock.MediaBlock.Video.VideoFile.CaptionsFile.File.FilePath); + } } } } - } - else if (questionBlock.BlockType == BlockType.WholeSlideImage && questionBlock.WholeSlideImageBlock != null) - { - var existingWholeSlideImages = questionBlock.WholeSlideImageBlock?.WholeSlideImageBlockItems; - if (existingWholeSlideImages != null && existingWholeSlideImages.Any()) + else if (questionBlock.BlockType == BlockType.WholeSlideImage && questionBlock.WholeSlideImageBlock != null && questionBlock.WholeSlideImageBlock.WholeSlideImageBlockItems.Any()) { - foreach (var wsi in existingWholeSlideImages) + var existingWholeSlideImages = questionBlock.WholeSlideImageBlock?.WholeSlideImageBlockItems; + if (existingWholeSlideImages != null && existingWholeSlideImages.Any()) { - if (wsi.WholeSlideImage != null && wsi.WholeSlideImage.File != null && wsi.WholeSlideImage.File.WholeSlideImageFile != null && (wsi.WholeSlideImage.File.WholeSlideImageFile.Status == WholeSlideImageFileStatus.ProcessingComplete || wsi.WholeSlideImage.File.WholeSlideImageFile.Status == WholeSlideImageFileStatus.ProcessingFailed)) + foreach (var wsi in existingWholeSlideImages) { - filePath.Add(wsi.WholeSlideImage.File.FilePath); + if (wsi.WholeSlideImage != null && wsi.WholeSlideImage.File != null && wsi.WholeSlideImage.File.FileId > 0) + { + filePath.Add(wsi.WholeSlideImage.File.FileId, wsi.WholeSlideImage.File.FilePath); + } } } } } } - } - var feedbackBlockCollection = block.QuestionBlock.FeedbackBlockCollection; - if (feedbackBlockCollection != null && feedbackBlockCollection.Blocks != null) - { - foreach (var feedbackBlock in feedbackBlockCollection.Blocks) + var feedbackBlockCollection = block.QuestionBlock.FeedbackBlockCollection; + if (feedbackBlockCollection != null && feedbackBlockCollection.Blocks != null && feedbackBlockCollection.Blocks.Any()) { - if (feedbackBlock.BlockType == BlockType.Media && feedbackBlock.MediaBlock != null) + foreach (var feedbackBlock in feedbackBlockCollection.Blocks) { - if (feedbackBlock.MediaBlock.Image != null) + if (feedbackBlock.BlockType == BlockType.Media && feedbackBlock.MediaBlock != null) { - filePath.Add(feedbackBlock.MediaBlock.Image.File.FilePath); - } + if (feedbackBlock.MediaBlock.Image != null) + { + filePath.Add(feedbackBlock.MediaBlock.Image.File.FileId, feedbackBlock.MediaBlock.Image.File.FilePath); + } - if (feedbackBlock.MediaBlock.Video != null) - { - if (feedbackBlock.MediaBlock.Video.VideoFile != null) + if (feedbackBlock.MediaBlock.Video != null) { if (feedbackBlock.MediaBlock.Video.File != null) { - filePath.Add(feedbackBlock.MediaBlock.Video.File.FilePath); - } - - if (feedbackBlock.MediaBlock.Video.VideoFile.TranscriptFile != null) - { - filePath.Add(feedbackBlock.MediaBlock.Video.VideoFile.TranscriptFile.File.FilePath); + filePath.Add(feedbackBlock.MediaBlock.Video.File.FileId, feedbackBlock.MediaBlock.Video.File.FilePath); } - if (feedbackBlock.MediaBlock.Video.VideoFile.CaptionsFile != null) + if (feedbackBlock.MediaBlock.Video.VideoFile != null) { - filePath.Add(feedbackBlock.MediaBlock.Video.VideoFile.CaptionsFile.File.FilePath); + if (feedbackBlock.MediaBlock.Video.VideoFile.TranscriptFile != null) + { + filePath.Add(feedbackBlock.MediaBlock.Video.VideoFile.TranscriptFile.File.FileId, feedbackBlock.MediaBlock.Video.VideoFile.TranscriptFile.File.FilePath); + } + + if (feedbackBlock.MediaBlock.Video.VideoFile.CaptionsFile != null) + { + filePath.Add(feedbackBlock.MediaBlock.Video.VideoFile.CaptionsFile.File.FileId, feedbackBlock.MediaBlock.Video.VideoFile.CaptionsFile.File.FilePath); + } } } } @@ -903,7 +910,7 @@ private List CheckQuestionBlock(BlockCollectionViewModel model) } } - return filePath.Where(x => x != null).ToList(); + return filePath; } } } diff --git a/WebAPI/LearningHub.Nhs.Database/LearningHub.Nhs.Database.sqlproj b/WebAPI/LearningHub.Nhs.Database/LearningHub.Nhs.Database.sqlproj index bc730fb58..2613eb68c 100644 --- a/WebAPI/LearningHub.Nhs.Database/LearningHub.Nhs.Database.sqlproj +++ b/WebAPI/LearningHub.Nhs.Database/LearningHub.Nhs.Database.sqlproj @@ -513,6 +513,7 @@ + diff --git a/WebAPI/LearningHub.Nhs.Database/Stored Procedures/Resources/BlockCollectionFileSearch.sql b/WebAPI/LearningHub.Nhs.Database/Stored Procedures/Resources/BlockCollectionFileSearch.sql new file mode 100644 index 000000000..fcf2e5a4a --- /dev/null +++ b/WebAPI/LearningHub.Nhs.Database/Stored Procedures/Resources/BlockCollectionFileSearch.sql @@ -0,0 +1,715 @@ +------------------------------------------------------------------------------- +-- Author Tobi Awe +-- Created 30-05-2024 +-- Purpose search all assessment/case block collections files + + +-- Modification History +-- +-- 31-05-2024 TD-3023 Initial Revision +-- 04-06-2024 TD-3023 Included answer blocks to the search +------------------------------------------------------------------------------- + + +CREATE PROCEDURE [resources].[BlockCollectionFileSearch] ( + @excludeResourceVersionId INT, + @resourceType INT, + @filePath nvarchar(max) output +) +AS +BEGIN + -- Table to hold BlockCollectionIds + DECLARE @BlockCollectionId TABLE (Id INT); + DECLARE @AllBlockAssociatedFiles TABLE (FileId INT, FilePath NVARCHAR(200)); + SET @filePath = NULL; + + -- Insert BlockCollectionIds based on the provided query + IF @resourceType = 10 +BEGIN + --case +INSERT INTO @BlockCollectionId (Id) +SELECT DISTINCT c.BlockCollectionId +FROM [resources].[CaseResourceVersion] AS c +INNER JOIN [resources].[ResourceVersion] AS r + ON c.ResourceVersionId = r.Id +INNER JOIN [resources].[Resource] AS r0 + ON r.ResourceId = r0.Id +WHERE + c.Deleted = 0 + AND r.Deleted = 0 + AND r0.Deleted = 0 + AND c.ResourceVersionId <> @excludeResourceVersionId + AND (r.VersionStatusId <> 2 OR (r.VersionStatusId = 2 AND r0.CurrentResourceVersionId = c.ResourceVersionId)) + AND c.BlockCollectionId IS NOT NULL; + +END +ELSE IF @resourceType = 11 +BEGIN + --assessment + INSERT INTO @BlockCollectionId (Id) +SELECT DISTINCT Id +FROM ( + SELECT + c.AssessmentContentId AS Id + FROM + [resources].AssessmentResourceVersion AS c + INNER JOIN + [resources].ResourceVersion AS r ON c.ResourceVersionId = r.Id + INNER JOIN + [resources].Resource AS r0 ON r.ResourceId = r0.Id + WHERE + c.Deleted = CAST(0 AS bit) + AND r.Deleted = CAST(0 AS bit) + AND r0.Deleted = CAST(0 AS bit) + AND c.ResourceVersionId <> @excludeResourceVersionId + AND (r.VersionStatusId <> 2 OR (r.VersionStatusId = 2 AND r0.CurrentResourceVersionId = c.ResourceVersionId)) + AND c.AssessmentContentId IS NOT NULL + + UNION ALL + + SELECT + c.EndGuidanceId AS Id + FROM + [resources].AssessmentResourceVersion AS c + INNER JOIN + [resources].ResourceVersion AS r ON c.ResourceVersionId = r.Id + INNER JOIN + [resources].Resource AS r0 ON r.ResourceId = r0.Id + WHERE + c.Deleted = CAST(0 AS bit) + AND r.Deleted = CAST(0 AS bit) + AND r0.Deleted = CAST(0 AS bit) + AND c.ResourceVersionId <> @excludeResourceVersionId + AND (r.VersionStatusId <> 2 OR (r.VersionStatusId = 2 AND r0.CurrentResourceVersionId = c.ResourceVersionId)) + AND c.EndGuidanceId IS NOT NULL +) as assessmentBlockCollections; +END + + -- Table to hold temp results for blocks + DECLARE @TempBlockResult TABLE ( + Id INT, + [Order] INT, + Title NVARCHAR(200), + BlockType INT, + BlockCollectionId INT + ); + + DECLARE @QABlock TABLE ( + Id INT, + [Order] INT, + Title NVARCHAR(200), + BlockType INT, + BlockCollectionId INT + ); + + -- Table to hold results for blocks + DECLARE @BlockResult TABLE ( + Id INT, + [Order] INT, + Title NVARCHAR(200), + BlockType INT, + BlockCollectionId INT + ); + + -- Insert temp block data + INSERT INTO @TempBlockResult + SELECT Id, [Order], Title, BlockType, BlockCollectionId + FROM resources.Block + WHERE Deleted = 0 AND BlockCollectionId IN (SELECT Id FROM @BlockCollectionId); + + -- Insert ImageCarouselBlockCollectionId + INSERT INTO @BlockCollectionId (Id) + SELECT icb.ImageBlockCollectionId + FROM resources.ImageCarouselBlock icb + INNER JOIN @TempBlockResult b ON icb.BlockId = b.Id + WHERE icb.Deleted = 0; + +--Insert QuestionBlockCollectionId and FeedbackBlockCollectionId + INSERT INTO @BlockCollectionId (Id) + SELECT qb.QuestionBlockCollectionId + FROM resources.QuestionBlock qb + INNER JOIN @TempBlockResult b ON qb.BlockId = b.Id + WHERE qb.Deleted = 0 + + UNION + + SELECT qb.FeedbackBlockCollectionId + FROM resources.QuestionBlock qb + INNER JOIN @TempBlockResult b ON qb.BlockId = b.Id + WHERE qb.Deleted = 0; + + -- Insert answer question block data + INSERT INTO @QABlock + SELECT qb.Id, [Order], Title, BlockType, BlockCollectionId + FROM resources.QuestionBlock qb + INNER JOIN @TempBlockResult b ON qb.BlockId = b.Id + WHERE qb.Deleted = 0; + + --Insert AnswerBlockcollectionId + INSERT INTO @BlockCollectionId (Id) + SELECT qa.BlockCollectionId + FROM resources.QuestionAnswer qa + INNER JOIN @QABlock b ON qa.QuestionBlockId = b.Id + WHERE qa.Deleted = 0; + + + -- Insert block data + INSERT INTO @BlockResult + SELECT Id, [Order], Title, BlockType, BlockCollectionId + FROM resources.Block + WHERE Deleted = 0 AND BlockCollectionId IN (SELECT Id FROM @BlockCollectionId); + + + + -- WholeSlide Image Block + DECLARE @WsibResult TABLE ( + Id INT, + BlockId INT + ); + + INSERT INTO @WsibResult + SELECT w.Id, w.BlockId + FROM resources.WholeSlideImageBlock w + INNER JOIN @BlockResult b ON b.Id = w.BlockId + WHERE w.Deleted = 0; + + -- SELECT * FROM @WsibResult; + + -- WholeSlide Image Block Items + DECLARE @WsibiResult TABLE ( + Id INT, + WholeSlideImageBlockId INT, + WholeSlideImageId INT, + PlaceholderText NVARCHAR(255), + [Order] INT + ); + + INSERT INTO @WsibiResult + SELECT wi.Id, wi.WholeSlideImageBlockId, wi.WholeSlideImageId, wi.PlaceholderText, wi.[Order] + FROM resources.WholeSlideImageBlockItem wi + INNER JOIN @WsibResult w ON wi.WholeSlideImageBlockId = w.Id + WHERE wi.Deleted = 0; + + --SELECT * FROM @WsibiResult; + + -- WholeSlide Image + DECLARE @WsiResult TABLE ( + Id INT, + Title NVARCHAR(1000), + FileId INT + ); + + INSERT INTO @WsiResult + SELECT wsi.Id, wsi.Title, wsi.FileId + FROM resources.WholeSlideImage wsi + INNER JOIN @WsibiResult wi ON wsi.Id = wi.WholeSlideImageId + WHERE wsi.Deleted = 0; + + + --SELECT * FROM @WsiResult; + + -- WholeSlide Image File + DECLARE @WsiFileResult TABLE ( + Id INT, + FileTypeId INT, + FileChunkDetailId INT, + [FileName] NVARCHAR(255), + FilePath NVARCHAR(1024), + FileSizekb INT + ); + + INSERT INTO @WsiFileResult + SELECT f.Id, f.FileTypeId, f.FileChunkDetailId, f.[FileName], f.FilePath, f.FileSizeKb + FROM resources.[File] f + INNER JOIN @WsiResult wsi ON f.Id = wsi.FileId + WHERE f.Deleted = 0; + + INSERT INTO @AllBlockAssociatedFiles + SELECT Id,FilePath FROM @WsiFileResult; + + + + -- Media Block + DECLARE @MediaBlockResult TABLE ( + Id INT, + BlockId INT, + MediaType INT, + AttachmentId INT, + ImageId INT, + VideoId INT + ); + + INSERT INTO @MediaBlockResult + SELECT mb.Id, mb.BlockId, mb.MediaType, mb.AttachmentId, mb.ImageId, mb.VideoId + FROM resources.MediaBlock mb + INNER JOIN @BlockResult b ON mb.BlockId = b.Id + WHERE mb.Deleted = 0; + + + --SELECT * FROM @MediaBlockResult; + + -- Attachment + DECLARE @AttachmentResult TABLE ( + Id INT, + FileId INT + ); + + INSERT INTO @AttachmentResult + SELECT a.Id, a.FileId + FROM resources.Attachment a + INNER JOIN @MediaBlockResult mb ON a.Id = mb.AttachmentId + WHERE a.Deleted = 0; + + --SELECT * FROM @AttachmentResult; + + -- Attachment File + DECLARE @AttachmentFileResult TABLE ( + Id INT, + FileTypeId INT, + FileChunkDetailId INT, + [FileName] NVARCHAR(255), + FilePath NVARCHAR(1024), + FileSizekb INT + ); + + INSERT INTO @AttachmentFileResult + SELECT f.Id, f.FileTypeId, f.FileChunkDetailId, f.[FileName], f.FilePath, f.FileSizeKb + FROM resources.[File] f + INNER JOIN @AttachmentResult ar ON f.Id = ar.FileId + WHERE f.Deleted = 0; + + INSERT INTO @AllBlockAssociatedFiles + SELECT Id,FilePath FROM @AttachmentFileResult; + + -- Attachment File - Partial File + INSERT INTO @AllBlockAssociatedFiles + SELECT pf.FileId,af.FilePath + FROM resources.PartialFile pf + INNER JOIN @AttachmentFileResult af ON pf.FileId = af.Id + WHERE pf.Deleted = 0; + + -- Image + DECLARE @ImageResult TABLE ( + Id INT, + FileId INT, + AltText NVARCHAR(125), + Description NVARCHAR(250) + ); + + INSERT INTO @ImageResult + SELECT i.Id, i.FileId, i.AltText, i.Description + FROM resources.Image i + INNER JOIN @MediaBlockResult mb ON i.Id = mb.ImageId + WHERE i.Deleted = 0; + + --SELECT * FROM @ImageResult; + + -- Image File + DECLARE @ImageFileResult TABLE ( + Id INT, + FileTypeId INT, + FileChunkDetailId INT, + [FileName] NVARCHAR(255), + FilePath NVARCHAR(1024), + FileSizekb INT + ); + + INSERT INTO @ImageFileResult + SELECT f.Id, f.FileTypeId, f.FileChunkDetailId, f.[FileName], f.FilePath, f.FileSizeKb + FROM resources.[File] f + INNER JOIN @ImageResult ir ON f.Id = ir.FileId + WHERE f.Deleted = 0; + + INSERT INTO @AllBlockAssociatedFiles + SELECT Id,FilePath FROM @ImageFileResult; + + + -- Video + DECLARE @VideoResult TABLE ( + Id INT, + FileId INT + ); + + INSERT INTO @VideoResult + SELECT v.Id, v.FileId + FROM resources.Video v + INNER JOIN @MediaBlockResult mb ON v.Id = mb.VideoId + WHERE v.Deleted = 0; + + -- SELECT * FROM @VideoResult; + + -- Video File + DECLARE @VideoFileResult TABLE ( + Id INT, + FileTypeId INT, + FileChunkDetailId INT, + [FileName] NVARCHAR(255), + FilePath NVARCHAR(1024), + FileSizekb INT + ); + + INSERT INTO @VideoFileResult + SELECT f.Id, f.FileTypeId, f.FileChunkDetailId, f.[FileName], f.FilePath, f.FileSizeKb + FROM resources.[File] f + INNER JOIN @VideoResult vr ON f.Id = vr.FileId + WHERE f.Deleted = 0; + + INSERT INTO @AllBlockAssociatedFiles + SELECT Id,FilePath FROM @VideoFileResult; + + --SELECT * FROM @AllBlockAssociatedFiles; +---------------------------------------------------------------------------------------------------------------------- + + + + -- Table to hold BlockCollectionIds + DECLARE @_BlockCollectionId TABLE (Id INT); + DECLARE @_QuestionBlockCollectionId TABLE (Id INT); + DECLARE @_ImageCarouselBlockCollectionId TABLE (Id INT); + DECLARE @_AllBlockAssociatedFiles TABLE (FileId INT, FilePath NVARCHAR(200)); + DECLARE @_QABlock TABLE (Id INT,[Order] INT,Title NVARCHAR(200),BlockType INT,BlockCollectionId INT); + + -- Insert BlockCollectionIds based on the provided query +IF @resourceType = 10 +BEGIN + --case +INSERT INTO @_BlockCollectionId (Id) +SELECT DISTINCT c.BlockCollectionId +FROM [resources].[CaseResourceVersion] AS c +INNER JOIN [resources].[ResourceVersion] AS r + ON c.ResourceVersionId = r.Id +INNER JOIN [resources].[Resource] AS r0 + ON r.ResourceId = r0.Id +WHERE + c.Deleted = 0 + AND c.ResourceVersionId = @excludeResourceVersionId + +END +ELSE IF @resourceType = 11 +BEGIN + --assessment +INSERT INTO @_BlockCollectionId (Id) +SELECT DISTINCT Id +FROM ( + SELECT + c.AssessmentContentId AS Id + FROM + [resources].AssessmentResourceVersion AS c + INNER JOIN + [resources].ResourceVersion AS r ON c.ResourceVersionId = r.Id + INNER JOIN + [resources].Resource AS r0 ON r.ResourceId = r0.Id + WHERE + c.Deleted = CAST(0 AS bit) + AND c.ResourceVersionId = @excludeResourceVersionId + + UNION ALL + + SELECT + c.EndGuidanceId AS Id + FROM + [resources].AssessmentResourceVersion AS c + INNER JOIN + [resources].ResourceVersion AS r ON c.ResourceVersionId = r.Id + INNER JOIN + [resources].Resource AS r0 ON r.ResourceId = r0.Id + WHERE + c.Deleted = CAST(0 AS bit) + AND r.Deleted = CAST(0 AS bit) + AND r0.Deleted = CAST(0 AS bit) + AND c.ResourceVersionId <> @excludeResourceVersionId +) as assessmentBlockCollections; +END + + + -- Table to hold temp results for blocks + DECLARE @_TempBlockResult TABLE ( + Id INT, + [Order] INT, + Title NVARCHAR(200), + BlockType INT, + BlockCollectionId INT + ); + + -- Table to hold results for blocks + DECLARE @_BlockResult TABLE ( + Id INT, + [Order] INT, + Title NVARCHAR(200), + BlockType INT, + BlockCollectionId INT + ); + + -- Insert temp block data + INSERT INTO @_TempBlockResult + SELECT Id, [Order], Title, BlockType, BlockCollectionId + FROM resources.Block + WHERE Deleted = 0 AND BlockCollectionId IN (SELECT Id FROM @_BlockCollectionId); + + -- Insert ImageCarouselBlockCollectionId + INSERT INTO @_BlockCollectionId (Id) + SELECT icb.ImageBlockCollectionId + FROM resources.ImageCarouselBlock icb + INNER JOIN @_TempBlockResult b ON icb.BlockId = b.Id + WHERE icb.Deleted = 0; + + --Insert QuestionBlockCollectionId and FeedbackBlockCollectionId + INSERT INTO @_BlockCollectionId (Id) + SELECT qb.QuestionBlockCollectionId + FROM resources.QuestionBlock qb + INNER JOIN @_TempBlockResult b ON qb.BlockId = b.Id + WHERE qb.Deleted = 0 + + UNION + + SELECT qb.FeedbackBlockCollectionId + FROM resources.QuestionBlock qb + INNER JOIN @_TempBlockResult b ON qb.BlockId = b.Id + WHERE qb.Deleted = 0; + + -- Insert answer question block data + INSERT INTO @_QABlock + SELECT qb.Id, [Order], Title, BlockType, BlockCollectionId + FROM resources.QuestionBlock qb + INNER JOIN @_TempBlockResult b ON qb.BlockId = b.Id + WHERE qb.Deleted = 0; + + --Insert AnswerBlockcollectionId + INSERT INTO @_BlockCollectionId (Id) + SELECT qa.BlockCollectionId + FROM resources.QuestionAnswer qa + INNER JOIN @_QABlock b ON qa.QuestionBlockId = b.Id + WHERE qa.Deleted = 0; + + + + -- Insert block data + INSERT INTO @_BlockResult + SELECT Id, [Order], Title, BlockType, BlockCollectionId + FROM resources.Block + WHERE Deleted = 0 AND BlockCollectionId IN (SELECT Id FROM @_BlockCollectionId); + + + + -- WholeSlide Image Block + DECLARE @_WsibResult TABLE ( + Id INT, + BlockId INT + ); + + INSERT INTO @_WsibResult + SELECT w.Id, w.BlockId + FROM resources.WholeSlideImageBlock w + INNER JOIN @_BlockResult b ON b.Id = w.BlockId + WHERE w.Deleted = 0; + + -- SELECT * FROM @_WsibResult; + + -- WholeSlide Image Block Items + DECLARE @_WsibiResult TABLE ( + Id INT, + WholeSlideImageBlockId INT, + WholeSlideImageId INT, + PlaceholderText NVARCHAR(255), + [Order] INT + ); + + INSERT INTO @_WsibiResult + SELECT wi.Id, wi.WholeSlideImageBlockId, wi.WholeSlideImageId, wi.PlaceholderText, wi.[Order] + FROM resources.WholeSlideImageBlockItem wi + INNER JOIN @_WsibResult w ON wi.WholeSlideImageBlockId = w.Id + WHERE wi.Deleted = 0; + + --SELECT * FROM @_WsibiResult; + + -- WholeSlide Image + DECLARE @_WsiResult TABLE ( + Id INT, + Title NVARCHAR(1000), + FileId INT + ); + + INSERT INTO @_WsiResult + SELECT wsi.Id, wsi.Title, wsi.FileId + FROM resources.WholeSlideImage wsi + INNER JOIN @_WsibiResult wi ON wsi.Id = wi.WholeSlideImageId + WHERE wsi.Deleted = 0; + + + --SELECT * FROM @_WsiResult; + + -- WholeSlide Image File + DECLARE @_WsiFileResult TABLE ( + Id INT, + FileTypeId INT, + FileChunkDetailId INT, + [FileName] NVARCHAR(255), + FilePath NVARCHAR(1024), + FileSizekb INT + ); + + INSERT INTO @_WsiFileResult + SELECT f.Id, f.FileTypeId, f.FileChunkDetailId, f.[FileName], f.FilePath, f.FileSizeKb + FROM resources.[File] f + INNER JOIN @_WsiResult wsi ON f.Id = wsi.FileId + WHERE f.Deleted = 0; + + INSERT INTO @_AllBlockAssociatedFiles + SELECT Id,FilePath FROM @_WsiFileResult; + + + + -- Media Block + DECLARE @_MediaBlockResult TABLE ( + Id INT, + BlockId INT, + MediaType INT, + AttachmentId INT, + ImageId INT, + VideoId INT + ); + + INSERT INTO @_MediaBlockResult + SELECT mb.Id, mb.BlockId, mb.MediaType, mb.AttachmentId, mb.ImageId, mb.VideoId + FROM resources.MediaBlock mb + INNER JOIN @_BlockResult b ON mb.BlockId = b.Id + WHERE mb.Deleted = 0; + + + --SELECT * FROM @_MediaBlockResult; + + -- Attachment + DECLARE @_AttachmentResult TABLE ( + Id INT, + FileId INT + ); + + INSERT INTO @_AttachmentResult + SELECT a.Id, a.FileId + FROM resources.Attachment a + INNER JOIN @_MediaBlockResult mb ON a.Id = mb.AttachmentId + WHERE a.Deleted = 0; + + --SELECT * FROM @_AttachmentResult; + + -- Attachment File + DECLARE @_AttachmentFileResult TABLE ( + Id INT, + FileTypeId INT, + FileChunkDetailId INT, + [FileName] NVARCHAR(255), + FilePath NVARCHAR(1024), + FileSizekb INT + ); + + INSERT INTO @_AttachmentFileResult + SELECT f.Id, f.FileTypeId, f.FileChunkDetailId, f.[FileName], f.FilePath, f.FileSizeKb + FROM resources.[File] f + INNER JOIN @_AttachmentResult ar ON f.Id = ar.FileId + WHERE f.Deleted = 0; + + INSERT INTO @_AllBlockAssociatedFiles + SELECT Id,FilePath FROM @_AttachmentFileResult; + + -- Attachment File - Partial File + INSERT INTO @_AllBlockAssociatedFiles + SELECT pf.FileId,af.FilePath + FROM resources.PartialFile pf + INNER JOIN @_AttachmentFileResult af ON pf.FileId = af.Id + WHERE pf.Deleted = 0; + + -- Image + DECLARE @_ImageResult TABLE ( + Id INT, + FileId INT, + AltText NVARCHAR(125), + Description NVARCHAR(250) + ); + + INSERT INTO @_ImageResult + SELECT i.Id, i.FileId, i.AltText, i.Description + FROM resources.Image i + INNER JOIN @_MediaBlockResult mb ON i.Id = mb.ImageId + WHERE i.Deleted = 0; + + --SELECT * FROM @_ImageResult; + + -- Image File + DECLARE @_ImageFileResult TABLE ( + Id INT, + FileTypeId INT, + FileChunkDetailId INT, + [FileName] NVARCHAR(255), + FilePath NVARCHAR(1024), + FileSizekb INT + ); + + INSERT INTO @_ImageFileResult + SELECT f.Id, f.FileTypeId, f.FileChunkDetailId, f.[FileName], f.FilePath, f.FileSizeKb + FROM resources.[File] f + INNER JOIN @_ImageResult ir ON f.Id = ir.FileId + WHERE f.Deleted = 0; + + INSERT INTO @_AllBlockAssociatedFiles + SELECT Id,FilePath FROM @_ImageFileResult; + + + -- Video + DECLARE @_VideoResult TABLE ( + Id INT, + FileId INT + ); + + INSERT INTO @_VideoResult + SELECT v.Id, v.FileId + FROM resources.Video v + INNER JOIN @_MediaBlockResult mb ON v.Id = mb.VideoId + WHERE v.Deleted = 0; + + -- SELECT * FROM @_VideoResult; + + -- Video File + DECLARE @_VideoFileResult TABLE ( + Id INT, + FileTypeId INT, + FileChunkDetailId INT, + [FileName] NVARCHAR(255), + FilePath NVARCHAR(1024), + FileSizekb INT + ); + + INSERT INTO @_VideoFileResult + SELECT f.Id, f.FileTypeId, f.FileChunkDetailId, f.[FileName], f.FilePath, f.FileSizeKb + FROM resources.[File] f + INNER JOIN @_VideoResult vr ON f.Id = vr.FileId + WHERE f.Deleted = 0; + + INSERT INTO @_AllBlockAssociatedFiles + SELECT Id,FilePath FROM @_VideoFileResult; + + --SELECT * FROM @_AllBlockAssociatedFiles; +---------------------------------------------------------------------------------------------------------------------- + + DECLARE @SearchResult TABLE ( + Id INT, + FilePath NVARCHAR(1024), + MatchId INT, + MatchedPath NVARCHAR(1024) + ); + + INSERT INTO @SearchResult + SELECT f.FileId, f.FilePath, isMatched.FileId, isMatched.FilePath + FROM @_AllBlockAssociatedFiles f + LEFT JOIN @AllBlockAssociatedFiles isMatched ON (f.FileId = isMatched.FileId or f.FilePath = isMatched.FilePath) + + + DECLARE @result VARCHAR(MAX); + + SELECT @result= STUFF(( + SELECT ', ' + FilePath + FROM @SearchResult WHERE MatchId is NULL + FOR XML PATH(''), TYPE + ).value('.', 'NVARCHAR(MAX)'), 1, 2, ''); + + SET @filePath = @result; + END \ No newline at end of file diff --git a/WebAPI/LearningHub.Nhs.Repository.Interface/Resources/IBlockCollectionRepository.cs b/WebAPI/LearningHub.Nhs.Repository.Interface/Resources/IBlockCollectionRepository.cs index 0a1a52618..2a5bbb916 100644 --- a/WebAPI/LearningHub.Nhs.Repository.Interface/Resources/IBlockCollectionRepository.cs +++ b/WebAPI/LearningHub.Nhs.Repository.Interface/Resources/IBlockCollectionRepository.cs @@ -1,8 +1,10 @@ namespace LearningHub.Nhs.Repository.Interface.Resources { + using System; using System.Collections.Generic; using System.Threading.Tasks; using LearningHub.Nhs.Models.Entities.Resource.Blocks; + using LearningHub.Nhs.Models.Enums; /// /// The BlockRepository interface. @@ -30,5 +32,13 @@ public interface IBlockCollectionRepository : IGenericRepositoryThe Block Collection Id. /// The . Task> GetQuestionBlocks(int blockCollectionId); + + /// + /// Gets the Case, AssessmentContent, AssessmentGuidance Block Collections (including child Blocks, TextBlocks, WholeSlideImageBlocks and Files) except that of the provided resource version. + /// + /// The excluded ResourceVersion Id. + /// The resource type. + /// The . + Task GetResourceBlockCollectionsFileAsync(int excludeResourceVersionId, ResourceTypeEnum resourceTypeEnum); } } diff --git a/WebAPI/LearningHub.Nhs.Repository/Resources/BlockCollectionRepository.cs b/WebAPI/LearningHub.Nhs.Repository/Resources/BlockCollectionRepository.cs index be2bc4f98..c18b90187 100644 --- a/WebAPI/LearningHub.Nhs.Repository/Resources/BlockCollectionRepository.cs +++ b/WebAPI/LearningHub.Nhs.Repository/Resources/BlockCollectionRepository.cs @@ -166,6 +166,28 @@ await Task.WhenAll(blockCollection.Blocks return blockCollection; } + /// + /// Gets the Case, AssessmentContent, AssessmentGuidance Block Collections (including child Blocks, TextBlocks, WholeSlideImageBlocks and Files) except that of the provided resource version. + /// + /// The excluded ResourceVersion Id. + /// The resource type. + /// The . + public async Task GetResourceBlockCollectionsFileAsync(int excludeResourceVersionId, ResourceTypeEnum resourceTypeEnum) + { + if (excludeResourceVersionId > 0) + { + var param0 = new SqlParameter("@excludeResourceVersionId", SqlDbType.Int) { Value = excludeResourceVersionId }; + var param1 = new SqlParameter("@resourceType", SqlDbType.Int) { Value = (int)resourceTypeEnum }; + var param2 = new SqlParameter("@filePath", SqlDbType.NVarChar) { Direction = ParameterDirection.Output, Size = -1 }; + await this.DbContext.Database.ExecuteSqlRawAsync("[resources].[BlockCollectionFileSearch] @excludeResourceVersionId, @resourceType, @filePath output", param0, param1, param2); + return param2.Value.ToString(); + } + else + { + return string.Empty; + } + } + /// /// Gets the Question blocks for a particular blockCollectionId. /// diff --git a/WebAPI/LearningHub.Nhs.Services.Interface/IResourceService.cs b/WebAPI/LearningHub.Nhs.Services.Interface/IResourceService.cs index 511aa8500..0ce7f5c00 100644 --- a/WebAPI/LearningHub.Nhs.Services.Interface/IResourceService.cs +++ b/WebAPI/LearningHub.Nhs.Services.Interface/IResourceService.cs @@ -5,11 +5,13 @@ namespace LearningHub.Nhs.Services.Interface using System.Threading.Tasks; using LearningHub.Nhs.Models.Common; using LearningHub.Nhs.Models.Entities.Resource; + using LearningHub.Nhs.Models.Enums; using LearningHub.Nhs.Models.Paging; using LearningHub.Nhs.Models.Provider; using LearningHub.Nhs.Models.Resource; using LearningHub.Nhs.Models.Resource.Admin; using LearningHub.Nhs.Models.Resource.AzureMediaAsset; + using LearningHub.Nhs.Models.Resource.Blocks; using LearningHub.Nhs.Models.Resource.Contribute; using LearningHub.Nhs.Models.Resource.ResourceDisplay; using LearningHub.Nhs.Models.Validation; @@ -436,6 +438,14 @@ public interface IResourceService /// The . Task GetCaseDetailsByIdAsync(int resourceVersionId); + /// + /// The get case resource version async. + /// + /// The resource version id. + /// The resource type. + /// The . + Task> GetResourceBlockCollectionsFilePathAsync(int excludeResourceVersionId, ResourceTypeEnum resourceType); + /// /// The update case resource version async. /// diff --git a/WebAPI/LearningHub.Nhs.Services/ResourceService.cs b/WebAPI/LearningHub.Nhs.Services/ResourceService.cs index 67f233123..a97546303 100644 --- a/WebAPI/LearningHub.Nhs.Services/ResourceService.cs +++ b/WebAPI/LearningHub.Nhs.Services/ResourceService.cs @@ -9,6 +9,7 @@ namespace LearningHub.Nhs.Services using System.Threading.Tasks; using AutoMapper; using Azure.Storage.Blobs; + using Azure.Storage.Blobs.Models; using Azure.Storage.Blobs.Specialized; using LearningHub.Nhs.Api.Shared.Configuration; using LearningHub.Nhs.Entities.Resource; @@ -1326,288 +1327,308 @@ public async Task> GetResourceVersionsAsync(int r public async Task> GetObsoleteResourceFile(int resourceVersionId, bool deletedResource = false) { var retVal = new List(); - var resource = await this.GetResourceVersionExtendedViewModelAsync(resourceVersionId); - var rvs = await this.resourceVersionRepository.GetResourceVersionsAsync(resource.ResourceId); - rvs = rvs.Where(x => x.Id != resourceVersionId && x.PublicationId > 0).OrderByDescending(x => x.PublicationId).ToList(); - var rv = rvs.FirstOrDefault(); - if (rv != null) + var resourceVersion = await this.GetResourceVersionByIdAsync(resourceVersionId); + if (resourceVersion != null) { - var extendedResourceVersion = await this.GetResourceVersionExtendedViewModelAsync(rv.Id); - if (extendedResourceVersion.ResourceTypeEnum == ResourceTypeEnum.Scorm) - { - if (resource.ScormDetails?.ContentFilePath != extendedResourceVersion.ScormDetails.ContentFilePath) - { - if (!deletedResource) - { - retVal.Add(extendedResourceVersion.ScormDetails.ContentFilePath); - } - else - { - retVal.Add(resource.ScormDetails?.File?.FilePath); - } - } - } - else if (extendedResourceVersion.ResourceTypeEnum == ResourceTypeEnum.Html) - { - if (resource.HtmlDetails?.ContentFilePath != extendedResourceVersion.HtmlDetails.ContentFilePath) - { - if (!deletedResource) - { - retVal.Add(extendedResourceVersion.HtmlDetails.ContentFilePath); - } - else - { - retVal.Add(resource.HtmlDetails?.File?.FilePath); - } - } - } - else if (extendedResourceVersion.ResourceTypeEnum == ResourceTypeEnum.GenericFile) + if (resourceVersion.ResourceType == ResourceTypeEnum.Assessment) { - if (resource.GenericFileDetails.File.FilePath != extendedResourceVersion.GenericFileDetails.File.FilePath) + if (deletedResource) { - if (!deletedResource) - { - retVal.Add(extendedResourceVersion.GenericFileDetails.File.FilePath); - } - else + var assessmentFiles = await this.GetResourceBlockCollectionsFilePathAsync(resourceVersionId, ResourceTypeEnum.Assessment); + if (assessmentFiles.Any()) { - retVal.Add(resource.GenericFileDetails.File.FilePath); + retVal.AddRange(assessmentFiles); } } - } - else if (extendedResourceVersion.ResourceTypeEnum == ResourceTypeEnum.Image) - { - if (resource.ImageDetails.File.FilePath != extendedResourceVersion.ImageDetails.File.FilePath) + else { - if (!deletedResource) + var assessmentDetails = await this.GetAssessmentViewModel(resourceVersionId); + var rvs = await this.resourceVersionRepository.GetResourceVersionsAsync(resourceVersion.ResourceId); + rvs = rvs.Where(x => x.Id != resourceVersionId && x.PublicationId > 0).OrderByDescending(x => x.PublicationId).ToList(); + var rv = rvs.FirstOrDefault(); + var currentAssessmentVersion = await this.GetAssessmentViewModel(rv.Id); + List deletableFiles = null; + if (assessmentDetails is { EndGuidance: { } } && assessmentDetails.EndGuidance.Blocks != null) { - retVal.Add(extendedResourceVersion.ImageDetails.File.FilePath); + var endGuidanceFiles = this.CheckBlockFile(assessmentDetails.EndGuidance, currentAssessmentVersion.EndGuidance); + if (endGuidanceFiles.Any()) + { + deletableFiles = await this.GetResourceBlockCollectionsFilePathAsync(rv.Id, ResourceTypeEnum.Assessment); + retVal.AddRange(from entry in endGuidanceFiles + where deletableFiles.Contains(entry) + select entry); + } } - else + + if (assessmentDetails is { AssessmentContent: { } } && assessmentDetails.AssessmentContent.Blocks != null) { - retVal.Add(resource.ImageDetails.File.FilePath); + var assessmentContentFiles = this.CheckBlockFile(assessmentDetails.AssessmentContent, currentAssessmentVersion.AssessmentContent); + if (assessmentContentFiles.Any()) + { + deletableFiles ??= await this.GetResourceBlockCollectionsFilePathAsync(rv.Id, ResourceTypeEnum.Assessment); + retVal.AddRange(from entry in assessmentContentFiles + where deletableFiles.Contains(entry) + select entry); + } } } } - else if (extendedResourceVersion.ResourceTypeEnum == ResourceTypeEnum.Audio) + else if (resourceVersion.ResourceType == ResourceTypeEnum.Case) { - if (resource.AudioDetails?.File?.FilePath != extendedResourceVersion.AudioDetails.File.FilePath) - { - if (!deletedResource) - { - retVal.Add(extendedResourceVersion.AudioDetails.File.FilePath); - } - else - { - retVal.Add(resource.AudioDetails.File.FilePath); - } - } - - if (resource.AudioDetails?.ResourceAzureMediaAsset?.FilePath != extendedResourceVersion.AudioDetails.ResourceAzureMediaAsset.FilePath) + var caseFiles = new List(); + if (deletedResource) { - if (!deletedResource) - { - retVal.Add(extendedResourceVersion.AudioDetails.ResourceAzureMediaAsset.FilePath); - } - else - { - retVal.Add(resource.AudioDetails.ResourceAzureMediaAsset.FilePath); - } + caseFiles = await this.GetResourceBlockCollectionsFilePathAsync(resourceVersionId, ResourceTypeEnum.Case); } - } - else if (extendedResourceVersion.ResourceTypeEnum == ResourceTypeEnum.Video) - { - if (resource.VideoDetails?.File?.FilePath != extendedResourceVersion.VideoDetails.File.FilePath) + else { - if (!deletedResource) + var caseDetails = await this.GetCaseDetailsByIdAsync(resourceVersionId); + var rvs = await this.resourceVersionRepository.GetResourceVersionsAsync(resourceVersion.ResourceId); + rvs = rvs.Where(x => x.Id != resourceVersionId && x.PublicationId > 0).OrderByDescending(x => x.PublicationId).ToList(); + var rv = rvs.FirstOrDefault(); + if (rv != null) { - retVal.Add(extendedResourceVersion.VideoDetails.File.FilePath); - } - else - { - retVal.Add(resource.VideoDetails.File.FilePath); + var currentCaseVersion = await this.GetCaseDetailsByIdAsync(rv.Id); + var nonpublishedFiles = this.CheckBlockFile(caseDetails.BlockCollection, currentCaseVersion.BlockCollection); + if (nonpublishedFiles.Any()) + { + var deletableCaseFiles = await this.GetResourceBlockCollectionsFilePathAsync(rv.Id, ResourceTypeEnum.Case); + caseFiles.AddRange(from entry in nonpublishedFiles + where deletableCaseFiles.Contains(entry) + select entry); + } } } - if (resource.VideoDetails?.ResourceAzureMediaAsset?.FilePath != extendedResourceVersion.VideoDetails.ResourceAzureMediaAsset.FilePath) + if (caseFiles.Any()) { - if (!deletedResource) - { - retVal.Add(extendedResourceVersion.VideoDetails.ResourceAzureMediaAsset.FilePath); - } - else - { - retVal.Add(resource.VideoDetails.ResourceAzureMediaAsset.FilePath); - } + retVal.AddRange(caseFiles); } } - else if (extendedResourceVersion.ResourceTypeEnum == ResourceTypeEnum.Article) + else { - var inputResourceFiles = resource.ArticleDetails.Files.ToList(); - var previousPublishedfiles = extendedResourceVersion.ArticleDetails?.Files?.ToList(); - if (!deletedResource) + // get latest published resource version + var rvs = await this.resourceVersionRepository.GetResourceVersionsAsync(resourceVersion.ResourceId); + rvs = rvs.Where(x => x.Id != resourceVersionId && x.PublicationId > 0).OrderByDescending(x => x.PublicationId).ToList(); + var rv = rvs.FirstOrDefault(); + var extendedResourceVersion = await this.GetResourceVersionExtendedViewModelAsync(rv.Id); + switch (resourceVersion.ResourceType) { - if (previousPublishedfiles.Any()) - { - foreach (var file in previousPublishedfiles) + case ResourceTypeEnum.Scorm: + var scormResource = await this.GetScormDetailsByIdAsync(resourceVersionId); + if (rv != null) { - if (!inputResourceFiles.Where(x => x.FilePath == file.FilePath).Any()) + if (scormResource?.ContentFilePath != extendedResourceVersion.ScormDetails.ContentFilePath) { - retVal.Add(file.FilePath); + if (!deletedResource) + { + retVal.Add(extendedResourceVersion.ScormDetails.ContentFilePath); + } + else + { + retVal.Add(scormResource?.File?.FilePath); + } } } - } - } - else - { - if (inputResourceFiles.Any()) - { - foreach (var file in inputResourceFiles) + else if (rv == null && deletedResource) { - if (!previousPublishedfiles.Where(x => x.FilePath == file.FilePath).Any()) - { - retVal.Add(file.FilePath); - } + retVal.Add(scormResource.ContentFilePath); } - } - } - } - else if (extendedResourceVersion.ResourceTypeEnum == ResourceTypeEnum.Assessment) - { - var endGuidanceFiles = new List(); - var assessmentContentFiles = new List(); - if (resource.AssessmentDetails is { EndGuidance: { } } && resource.AssessmentDetails.EndGuidance.Blocks != null) - { - if (deletedResource) + break; + case ResourceTypeEnum.Html: + var htmlResource = await this.GetHtmlDetailsByIdAsync(resourceVersionId); + if (rv != null) { - endGuidanceFiles = this.CheckBlockFile(extendedResourceVersion.AssessmentDetails.EndGuidance, resource.AssessmentDetails.EndGuidance); + if (htmlResource?.ContentFilePath != extendedResourceVersion.HtmlDetails.ContentFilePath) + { + if (!deletedResource) + { + retVal.Add(extendedResourceVersion.HtmlDetails.ContentFilePath); + } + else + { + retVal.Add(htmlResource?.File?.FilePath); + } + } } - else + else if (rv == null && deletedResource) { - endGuidanceFiles = this.CheckBlockFile(resource.AssessmentDetails.EndGuidance, extendedResourceVersion.AssessmentDetails.EndGuidance); + retVal.Add(htmlResource.ContentFilePath); } - if (endGuidanceFiles.Any()) + break; + case ResourceTypeEnum.GenericFile: + var fileResource = await this.GetGenericFileDetailsByIdAsync(resourceVersionId); + if (rv != null) { - retVal.AddRange(endGuidanceFiles); + if (fileResource?.File.FilePath != extendedResourceVersion.GenericFileDetails.File.FilePath) + { + if (!deletedResource) + { + retVal.Add(extendedResourceVersion.GenericFileDetails.File.FilePath); + } + else + { + retVal.Add(fileResource?.File?.FilePath); + } + } + } + else if (rv == null && deletedResource) + { + retVal.Add(fileResource.File.FilePath); } - } - if (resource.AssessmentDetails is { AssessmentContent: { } } && resource.AssessmentDetails.AssessmentContent.Blocks != null) - { - if (deletedResource) + break; + case ResourceTypeEnum.Image: + var imageResource = await this.GetImageDetailsByIdAsync(resourceVersionId); + if (rv != null) { - assessmentContentFiles = this.CheckBlockFile(extendedResourceVersion.AssessmentDetails.AssessmentContent, resource.AssessmentDetails.AssessmentContent); + if (imageResource?.File.FilePath != extendedResourceVersion.ImageDetails.File.FilePath) + { + if (!deletedResource) + { + retVal.Add(extendedResourceVersion.ImageDetails.File.FilePath); + } + else + { + retVal.Add(imageResource.File.FilePath); + } + } } - else + else if (rv == null && deletedResource) { - assessmentContentFiles = this.CheckBlockFile(resource.AssessmentDetails.AssessmentContent, extendedResourceVersion.AssessmentDetails.AssessmentContent); + retVal.Add(imageResource.File.FilePath); } - if (assessmentContentFiles.Any()) + break; + case ResourceTypeEnum.Audio: + var audioResource = await this.GetAudioDetailsByIdAsync(resourceVersionId); + if (rv != null) { - retVal.AddRange(assessmentContentFiles); + if (audioResource?.File?.FilePath != extendedResourceVersion.AudioDetails.File.FilePath) + { + if (!deletedResource) + { + retVal.Add(extendedResourceVersion.AudioDetails.File.FilePath); + } + else + { + retVal.Add(audioResource.File.FilePath); + } + } + + if (audioResource?.ResourceAzureMediaAsset?.FilePath != extendedResourceVersion.AudioDetails.ResourceAzureMediaAsset.FilePath) + { + if (!deletedResource) + { + retVal.Add(extendedResourceVersion.AudioDetails.ResourceAzureMediaAsset.FilePath); + } + else + { + retVal.Add(audioResource.ResourceAzureMediaAsset.FilePath); + } + } + } + else if (rv == null && deletedResource) + { + retVal.Add(audioResource?.File?.FilePath); + if (audioResource?.ResourceAzureMediaAsset?.FilePath != null) + { + retVal.Add(audioResource.ResourceAzureMediaAsset.FilePath); + } } - } - } - else if (extendedResourceVersion.ResourceTypeEnum == ResourceTypeEnum.Case) - { - var caseFiles = new List(); - if (deletedResource) - { - caseFiles = this.CheckBlockFile(extendedResourceVersion.CaseDetails.BlockCollection, resource.CaseDetails.BlockCollection); - } - else - { - caseFiles = this.CheckBlockFile(resource.CaseDetails.BlockCollection, extendedResourceVersion.CaseDetails.BlockCollection); - } - if (caseFiles.Any()) - { - retVal.AddRange(caseFiles); - } - } - } - else if (rv == null && deletedResource) - { - if (resource.ResourceTypeEnum == ResourceTypeEnum.Scorm) - { - retVal.Add(resource.ScormDetails.ContentFilePath); - } - else if (resource.ResourceTypeEnum == ResourceTypeEnum.Html) - { - retVal.Add(resource.HtmlDetails.ContentFilePath); - } - else if (resource.ResourceTypeEnum == ResourceTypeEnum.GenericFile) - { - retVal.Add(resource.GenericFileDetails.File.FilePath); - } - else if (resource.ResourceTypeEnum == ResourceTypeEnum.Image) - { - retVal.Add(resource.ImageDetails.File.FilePath); - } - else if (resource.ResourceTypeEnum == ResourceTypeEnum.Audio) - { - retVal.Add(resource.AudioDetails?.File?.FilePath); - if (resource.AudioDetails?.ResourceAzureMediaAsset?.FilePath != null) - { - retVal.Add(resource.AudioDetails.ResourceAzureMediaAsset.FilePath); - } - } - else if (resource.ResourceTypeEnum == ResourceTypeEnum.Video) - { - retVal.Add(resource.VideoDetails?.File?.FilePath); - if (resource.VideoDetails?.ResourceAzureMediaAsset?.FilePath != null) - { - retVal.Add(resource.VideoDetails.ResourceAzureMediaAsset.FilePath); - } - } - else if (resource.ResourceTypeEnum == ResourceTypeEnum.Article) - { - var inputResourceFiles = resource.ArticleDetails.Files.ToList(); - if (inputResourceFiles.Any()) - { - foreach (var file in inputResourceFiles) + break; + case ResourceTypeEnum.Video: + var videoResource = await this.GetVideoDetailsByIdAsync(resourceVersionId); + if (rv != null) { - retVal.Add(file.FilePath); + if (videoResource?.File?.FilePath != extendedResourceVersion.VideoDetails.File.FilePath) + { + if (!deletedResource) + { + retVal.Add(extendedResourceVersion.VideoDetails.File.FilePath); + } + else + { + retVal.Add(videoResource.File.FilePath); + } + } + + if (videoResource?.ResourceAzureMediaAsset?.FilePath != extendedResourceVersion.VideoDetails.ResourceAzureMediaAsset.FilePath) + { + if (!deletedResource) + { + retVal.Add(extendedResourceVersion.VideoDetails.ResourceAzureMediaAsset.FilePath); + } + else + { + retVal.Add(videoResource.ResourceAzureMediaAsset.FilePath); + } + } } - } - } - else if (resource.ResourceTypeEnum == ResourceTypeEnum.Assessment) - { - if (deletedResource) - { - if (resource.AssessmentDetails is { EndGuidance: { } } && resource.AssessmentDetails.EndGuidance.Blocks != null) - { - var assessmentFiles = this.CheckBlockFile(null, resource.AssessmentDetails.EndGuidance); - if (assessmentFiles.Any()) + else if (rv == null && deletedResource) { - retVal.AddRange(assessmentFiles); + retVal.Add(videoResource?.File?.FilePath); + if (videoResource?.ResourceAzureMediaAsset?.FilePath != null) + { + retVal.Add(videoResource.ResourceAzureMediaAsset.FilePath); + } } - } - if (resource.AssessmentDetails is { AssessmentContent: { } } && resource.AssessmentDetails.AssessmentContent.Blocks != null) - { - var assessmentFiles = this.CheckBlockFile(null, resource.AssessmentDetails.AssessmentContent); - if (assessmentFiles.Any()) + break; + case ResourceTypeEnum.Article: + var articleResource = await this.GetArticleDetailsByIdAsync(resourceVersionId); + if (rv != null) + { + var inputResourceFiles = articleResource.Files.ToList(); + var previousPublishedfiles = extendedResourceVersion.ArticleDetails?.Files?.ToList(); + if (!deletedResource) + { + if (previousPublishedfiles.Any()) + { + foreach (var file in previousPublishedfiles) + { + if (!inputResourceFiles.Where(x => x.FilePath == file.FilePath).Any()) + { + retVal.Add(file.FilePath); + } + } + } + } + else + { + if (inputResourceFiles.Any()) + { + foreach (var file in inputResourceFiles) + { + if (!previousPublishedfiles.Where(x => x.FilePath == file.FilePath).Any()) + { + retVal.Add(file.FilePath); + } + } + } + } + } + else if (rv == null && deletedResource) { - retVal.AddRange(assessmentFiles); + var inputResourceFiles = articleResource.Files.ToList(); + if (inputResourceFiles.Any()) + { + foreach (var file in inputResourceFiles) + { + retVal.Add(file.FilePath); + } + } } - } - } - } - else if (resource.ResourceTypeEnum == ResourceTypeEnum.Case) - { - var caseFiles = this.CheckBlockFile(null, resource.CaseDetails.BlockCollection); - if (caseFiles.Any()) - { - retVal.AddRange(caseFiles); + + break; } } } - return retVal; + return retVal.Distinct().ToList(); } /// @@ -2190,6 +2211,27 @@ public async Task GetCaseDetailsByIdAsync(int resourceVersionId) return caseViewModel; } + /// + /// The get case resource version async. + /// + /// The resource version id. + /// The resource type. + /// The . + public async Task> GetResourceBlockCollectionsFilePathAsync(int excludeResourceVersionId, ResourceTypeEnum resourceType) + { + var pathList = new List(); + var collection = await this.blockCollectionRepository.GetResourceBlockCollectionsFileAsync(excludeResourceVersionId, resourceType); + if (!string.IsNullOrWhiteSpace(collection)) + { + pathList = collection.Split(',') + .Select(item => item.Trim()) + .Where(item => !string.IsNullOrEmpty(item)) + .ToList(); + } + + return pathList; + } + /// /// The update case resource version async. /// @@ -4921,7 +4963,7 @@ private List CheckBlockFile(BlockCollectionViewModel? publishedBlock, Bl { foreach (var oldblock in existingAttachements) { - var publishedEntry = (publishedBlock != null && publishedBlock.Blocks.Any()) ? publishedBlock.Blocks.FirstOrDefault(x => x.BlockType == BlockType.Media && x.MediaBlock != null && x.MediaBlock.MediaType == MediaType.Attachment && x.MediaBlock.Attachment != null && x.MediaBlock.Attachment.File?.FileId == oldblock.MediaBlock.Attachment?.File?.FileId) : null; + var publishedEntry = (publishedBlock != null && publishedBlock.Blocks.Any()) ? publishedBlock.Blocks.FirstOrDefault(x => x.BlockType == BlockType.Media && x.MediaBlock != null && x.MediaBlock.MediaType == MediaType.Attachment && x.MediaBlock.Attachment != null && (x.MediaBlock.Attachment.File?.FilePath == oldblock.MediaBlock.Attachment?.File?.FilePath || x.MediaBlock.Attachment.File?.FileId == oldblock.MediaBlock.Attachment?.File?.FileId)) : null; if (publishedEntry == null) { retVal.Add(oldblock.MediaBlock.Attachment?.File?.FilePath); @@ -4934,7 +4976,7 @@ private List CheckBlockFile(BlockCollectionViewModel? publishedBlock, Bl { foreach (var oldblock in existingVideos) { - var publishedEntry = (publishedBlock != null && publishedBlock.Blocks.Any()) ? publishedBlock.Blocks.FirstOrDefault(x => x.BlockType == BlockType.Media && x.MediaBlock != null && x.MediaBlock.MediaType == MediaType.Video && x.MediaBlock.Video != null && x.MediaBlock.Video.VideoFile?.File?.FileId == oldblock.MediaBlock?.Video?.VideoFile?.File?.FileId) : null; + var publishedEntry = (publishedBlock != null && publishedBlock.Blocks.Any()) ? publishedBlock.Blocks.FirstOrDefault(x => x.BlockType == BlockType.Media && x.MediaBlock != null && x.MediaBlock.MediaType == MediaType.Video && x.MediaBlock.Video != null && (x.MediaBlock.Video.VideoFile?.File?.FilePath == oldblock.MediaBlock?.Video?.VideoFile?.File?.FilePath || x.MediaBlock.Video.VideoFile?.File?.FileId == oldblock.MediaBlock?.Video?.VideoFile?.File?.FileId)) : null; if (publishedEntry == null) { if (!string.IsNullOrWhiteSpace(oldblock.MediaBlock.Video.File.FilePath)) @@ -4960,7 +5002,7 @@ private List CheckBlockFile(BlockCollectionViewModel? publishedBlock, Bl { foreach (var oldblock in existingImages) { - var publishedEntry = (publishedBlock != null && publishedBlock.Blocks.Any()) ? publishedBlock.Blocks.FirstOrDefault(x => x.BlockType == BlockType.Media && x.MediaBlock != null && x.MediaBlock.MediaType == MediaType.Image && x.MediaBlock.Image != null && x.MediaBlock?.Image?.File?.FileId == oldblock.MediaBlock?.Image?.File?.FileId) : null; + var publishedEntry = (publishedBlock != null && publishedBlock.Blocks.Any()) ? publishedBlock.Blocks.FirstOrDefault(x => x.BlockType == BlockType.Media && x.MediaBlock != null && x.MediaBlock.MediaType == MediaType.Image && x.MediaBlock.Image != null && (x.MediaBlock?.Image?.File?.FilePath == oldblock.MediaBlock?.Image?.File?.FilePath || x.MediaBlock?.Image?.File?.FileId == oldblock.MediaBlock?.Image?.File?.FileId)) : null; if (publishedEntry == null) { retVal.Add(oldblock.MediaBlock?.Image?.File?.FilePath); @@ -4975,7 +5017,7 @@ private List CheckBlockFile(BlockCollectionViewModel? publishedBlock, Bl { foreach (var oldblock in imageBlock.ImageCarouselBlock.ImageBlockCollection.Blocks) { - var publishedEntry = (publishedBlock != null && publishedBlock.Blocks.Any()) ? publishedBlock.Blocks.FirstOrDefault(x => x.BlockType == BlockType.ImageCarousel && x.ImageCarouselBlock != null && x.ImageCarouselBlock.ImageBlockCollection != null && x.ImageCarouselBlock.ImageBlockCollection.Blocks != null && x.ImageCarouselBlock.ImageBlockCollection.Blocks.Where(x => x.MediaBlock?.Image?.File?.FileId == oldblock.MediaBlock?.Image?.File?.FileId).Any()) : null; + var publishedEntry = (publishedBlock != null && publishedBlock.Blocks.Any()) ? publishedBlock.Blocks.FirstOrDefault(x => x.BlockType == BlockType.ImageCarousel && x.ImageCarouselBlock != null && x.ImageCarouselBlock.ImageBlockCollection != null && x.ImageCarouselBlock.ImageBlockCollection.Blocks != null && x.ImageCarouselBlock.ImageBlockCollection.Blocks.Where(x => x.MediaBlock?.Image?.File?.FilePath == oldblock.MediaBlock?.Image?.File?.FilePath || x.MediaBlock?.Image?.File?.FileId == oldblock.MediaBlock?.Image?.File?.FileId).Any()) : null; if (publishedEntry == null) { retVal.Add(oldblock.MediaBlock?.Image?.File?.FilePath); @@ -4991,10 +5033,13 @@ private List CheckBlockFile(BlockCollectionViewModel? publishedBlock, Bl { foreach (var oldblock in wsi?.WholeSlideImageBlock?.WholeSlideImageBlockItems) { - var publishedEntry = (publishedBlock != null && publishedBlock.Blocks.Any()) ? publishedBlock.Blocks.FirstOrDefault(x => x.WholeSlideImageBlock != null && x.WholeSlideImageBlock.WholeSlideImageBlockItems.Where(x => x.WholeSlideImage?.File?.FileId == oldblock.WholeSlideImage?.File?.FileId).Any()) : null; - if (publishedEntry == null) + if (oldblock != null && (oldblock.WholeSlideImage.File.WholeSlideImageFile.Status == WholeSlideImageFileStatus.ProcessingComplete || oldblock.WholeSlideImage.File.WholeSlideImageFile.Status == WholeSlideImageFileStatus.ProcessingFailed)) { - retVal.Add(oldblock.WholeSlideImage?.File?.FilePath); + var publishedEntry = (publishedBlock != null && publishedBlock.Blocks.Any()) ? publishedBlock.Blocks.FirstOrDefault(x => x.WholeSlideImageBlock != null && x.WholeSlideImageBlock.WholeSlideImageBlockItems.Where(x => x.WholeSlideImage?.File?.FilePath == oldblock.WholeSlideImage?.File?.FilePath || x.WholeSlideImage?.File?.FileId == oldblock.WholeSlideImage?.File?.FileId).Any()) : null; + if (publishedEntry == null) + { + retVal.Add(oldblock.WholeSlideImage?.File?.FilePath); + } } } } @@ -5002,19 +5047,20 @@ private List CheckBlockFile(BlockCollectionViewModel? publishedBlock, Bl } var questionFiles = this.CheckQuestionBlock(caseBlockCollection); - var publishedQuestionFiles = (publishedBlock != null && publishedBlock.Blocks.Any()) ? this.CheckQuestionBlock(publishedBlock) : new List(); + var publishedQuestionFiles = (publishedBlock != null && publishedBlock.Blocks.Any()) ? this.CheckQuestionBlock(publishedBlock) : new Dictionary(); if (questionFiles.Any() && !publishedQuestionFiles.Any()) { - retVal.AddRange(questionFiles); + retVal.AddRange(questionFiles.Values.ToList()); } else if (questionFiles.Any() && publishedQuestionFiles.Any()) { foreach (var file in questionFiles) { - var publishedEntry = publishedQuestionFiles.FirstOrDefault(x => x.Equals(file)); - if (publishedEntry == null) + bool found = false; + var publishedEntry = publishedQuestionFiles.FirstOrDefault(x => (x.Key == file.Key || x.Value == file.Value) && (found = true)); + if (!found) { - retVal.Add(file); + retVal.Add(file.Value); } } } @@ -5023,109 +5069,112 @@ private List CheckBlockFile(BlockCollectionViewModel? publishedBlock, Bl return retVal.Where(x => x != null).ToList(); } - private List CheckQuestionBlock(BlockCollectionViewModel model) + private Dictionary CheckQuestionBlock(BlockCollectionViewModel model) { - var filePath = new List(); - foreach (var block in model.Blocks) + var filePath = new Dictionary(); + if (model != null && model.Blocks.Any()) { - if (block.BlockType == BlockType.Question && block.QuestionBlock != null) + foreach (var block in model.Blocks) { - if (block.QuestionBlock.QuestionType == QuestionTypeEnum.MatchGame && block.QuestionBlock.Answers != null) + if (block.BlockType == BlockType.Question && block.QuestionBlock != null) { - foreach (var answerBlock in block.QuestionBlock.Answers) + if (block.QuestionBlock.QuestionType == QuestionTypeEnum.MatchGame && block.QuestionBlock.Answers != null) { - if (answerBlock.BlockCollection != null && answerBlock.BlockCollection.Blocks != null) + foreach (var answerBlock in block.QuestionBlock.Answers) { - foreach (var imageBlock in answerBlock.BlockCollection.Blocks) + if (answerBlock.BlockCollection != null && answerBlock.BlockCollection.Blocks != null && answerBlock.BlockCollection.Blocks.Any()) { - if (imageBlock.BlockType == BlockType.Media && imageBlock.MediaBlock != null) + foreach (var imageBlock in answerBlock.BlockCollection.Blocks) { - filePath.Add(imageBlock.MediaBlock.Image.File.FilePath); + if (imageBlock.BlockType == BlockType.Media && imageBlock.MediaBlock != null && imageBlock.MediaBlock.Image.File != null) + { + filePath.Add(imageBlock.MediaBlock.Image.File.FileId, imageBlock.MediaBlock.Image.File.FilePath); + } } } } } - } - var questionBlockCollection = block.QuestionBlock.QuestionBlockCollection; - if (questionBlockCollection != null && questionBlockCollection.Blocks != null) - { - foreach (var questionBlock in questionBlockCollection.Blocks) + var questionBlockCollection = block.QuestionBlock.QuestionBlockCollection; + if (questionBlockCollection != null && questionBlockCollection.Blocks != null && questionBlockCollection.Blocks.Any()) { - if (questionBlock.BlockType == BlockType.Media && questionBlock.MediaBlock != null) + foreach (var questionBlock in questionBlockCollection.Blocks) { - if (questionBlock.MediaBlock.Image != null) + if (questionBlock.BlockType == BlockType.Media && questionBlock.MediaBlock != null) { - filePath.Add(questionBlock.MediaBlock.Image.File.FilePath); - } + if (questionBlock.MediaBlock.Image != null) + { + filePath.Add(questionBlock.MediaBlock.Image.File.FileId, questionBlock.MediaBlock.Image.File.FilePath); + } - if (questionBlock.MediaBlock.Video != null) - { - if (questionBlock.MediaBlock.Video.VideoFile != null) + if (questionBlock.MediaBlock.Video != null) { if (questionBlock.MediaBlock.Video.File != null) { - filePath.Add(questionBlock.MediaBlock.Video.File.FilePath); - } - - if (questionBlock.MediaBlock.Video.VideoFile.TranscriptFile != null) - { - filePath.Add(questionBlock.MediaBlock.Video.VideoFile.TranscriptFile.File.FilePath); + filePath.Add(questionBlock.MediaBlock.Video.File.FileId, questionBlock.MediaBlock.Video.File.FilePath); } - if (questionBlock.MediaBlock.Video.VideoFile.CaptionsFile != null) + if (questionBlock.MediaBlock.Video.VideoFile != null) { - filePath.Add(questionBlock.MediaBlock.Video.VideoFile.CaptionsFile.File.FilePath); + if (questionBlock.MediaBlock.Video.VideoFile.TranscriptFile != null) + { + filePath.Add(questionBlock.MediaBlock.Video.VideoFile.TranscriptFile.File.FileId, questionBlock.MediaBlock.Video.VideoFile.TranscriptFile.File.FilePath); + } + + if (questionBlock.MediaBlock.Video.VideoFile.CaptionsFile != null) + { + filePath.Add(questionBlock.MediaBlock.Video.VideoFile.CaptionsFile.File.FileId, questionBlock.MediaBlock.Video.VideoFile.CaptionsFile.File.FilePath); + } } } } - } - else if (questionBlock.BlockType == BlockType.WholeSlideImage && questionBlock.WholeSlideImageBlock != null) - { - var existingWholeSlideImages = questionBlock.WholeSlideImageBlock.WholeSlideImageBlockItems; - if (existingWholeSlideImages.Any()) + else if (questionBlock.BlockType == BlockType.WholeSlideImage && questionBlock.WholeSlideImageBlock != null && questionBlock.WholeSlideImageBlock.WholeSlideImageBlockItems.Any()) { - foreach (var wsi in existingWholeSlideImages) + var existingWholeSlideImages = questionBlock.WholeSlideImageBlock.WholeSlideImageBlockItems; + if (existingWholeSlideImages.Any()) { - if (wsi.WholeSlideImage != null && wsi.WholeSlideImage.File != null && wsi.WholeSlideImage.File.WholeSlideImageFile != null && (wsi.WholeSlideImage.File.WholeSlideImageFile.Status == WholeSlideImageFileStatus.ProcessingComplete || wsi.WholeSlideImage.File.WholeSlideImageFile.Status == WholeSlideImageFileStatus.ProcessingFailed)) + foreach (var wsi in existingWholeSlideImages) { - filePath.Add(wsi.WholeSlideImage.File.FilePath); + if (wsi.WholeSlideImage != null && wsi.WholeSlideImage.File != null && wsi.WholeSlideImage.File.FileId > 0) + { + filePath.Add(wsi.WholeSlideImage.File.FileId, wsi.WholeSlideImage.File.FilePath); + } } } } } } - } - var feedbackBlockCollection = block.QuestionBlock.FeedbackBlockCollection; - if (feedbackBlockCollection != null && feedbackBlockCollection.Blocks != null) - { - foreach (var feedbackBlock in feedbackBlockCollection.Blocks) + var feedbackBlockCollection = block.QuestionBlock.FeedbackBlockCollection; + if (feedbackBlockCollection != null && feedbackBlockCollection.Blocks != null && feedbackBlockCollection.Blocks.Any()) { - if (feedbackBlock.BlockType == BlockType.Media && feedbackBlock.MediaBlock != null) + foreach (var feedbackBlock in feedbackBlockCollection.Blocks) { - if (feedbackBlock.MediaBlock.Image != null) + if (feedbackBlock.BlockType == BlockType.Media && feedbackBlock.MediaBlock != null) { - filePath.Add(feedbackBlock.MediaBlock.Image.File.FilePath); - } + if (feedbackBlock.MediaBlock.Image != null) + { + filePath.Add(feedbackBlock.MediaBlock.Image.File.FileId, feedbackBlock.MediaBlock.Image.File.FilePath); + } - if (feedbackBlock.MediaBlock.Video != null) - { - if (feedbackBlock.MediaBlock.Video.VideoFile != null) + if (feedbackBlock.MediaBlock.Video != null) { if (feedbackBlock.MediaBlock.Video.File != null) { - filePath.Add(feedbackBlock.MediaBlock.Video.File.FilePath); - } - - if (feedbackBlock.MediaBlock.Video.VideoFile.TranscriptFile != null) - { - filePath.Add(feedbackBlock.MediaBlock.Video.VideoFile.TranscriptFile.File.FilePath); + filePath.Add(feedbackBlock.MediaBlock.Video.File.FileId, feedbackBlock.MediaBlock.Video.File.FilePath); } - if (feedbackBlock.MediaBlock.Video.VideoFile.CaptionsFile != null) + if (feedbackBlock.MediaBlock.Video.VideoFile != null) { - filePath.Add(feedbackBlock.MediaBlock.Video.VideoFile.CaptionsFile.File.FilePath); + if (feedbackBlock.MediaBlock.Video.VideoFile.TranscriptFile != null) + { + filePath.Add(feedbackBlock.MediaBlock.Video.VideoFile.TranscriptFile.File.FileId, feedbackBlock.MediaBlock.Video.VideoFile.TranscriptFile.File.FilePath); + } + + if (feedbackBlock.MediaBlock.Video.VideoFile.CaptionsFile != null) + { + filePath.Add(feedbackBlock.MediaBlock.Video.VideoFile.CaptionsFile.File.FileId, feedbackBlock.MediaBlock.Video.VideoFile.CaptionsFile.File.FilePath); + } } } } @@ -5135,7 +5184,7 @@ private List CheckQuestionBlock(BlockCollectionViewModel model) } } - return filePath.Where(x => x != null).ToList(); + return filePath; } } } \ No newline at end of file