Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 174 additions & 8 deletions LearningHub.Nhs.WebUI/Controllers/Api/ContributeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using LearningHub.Nhs.Models.Entities.Resource;
using LearningHub.Nhs.Models.Enums;
using LearningHub.Nhs.Models.Resource;
using LearningHub.Nhs.Models.Resource.Blocks;
Expand Down Expand Up @@ -493,7 +494,11 @@ public async Task<ActionResult> SaveCaseDetailAsync([FromBody] CaseViewModel req
{
var existingResourceState = await this.resourceService.GetResourceVersionExtendedAsync(request.ResourceVersionId);
int resourceVersionId = await this.contributeService.SaveCaseDetailAsync(request);
this.RemoveDeletedCaseFiles(existingResourceState?.CaseDetails, request);
if (existingResourceState.CaseDetails?.BlockCollection != null)
{
this.RemoveBlockCollectionFiles(existingResourceState.CaseDetails.BlockCollection, request.BlockCollection);
}

return this.Ok(resourceVersionId);
}

Expand All @@ -506,7 +511,13 @@ public async Task<ActionResult> SaveCaseDetailAsync([FromBody] CaseViewModel req
[Route("SaveAssessmentDetail")]
public async Task<ActionResult> SaveAssessmentDetailAsync([FromBody] AssessmentViewModel request)
{
var existingResourceState = await this.resourceService.GetResourceVersionExtendedAsync(request.ResourceVersionId);
int resourceVersionId = await this.contributeService.SaveAssessmentDetailAsync(request);
if (existingResourceState != null && existingResourceState.AssessmentDetails != null)
{
this.RemoveBlockCollectionFiles(existingResourceState.AssessmentDetails, request);
}

return this.Ok(resourceVersionId);
}

Expand Down Expand Up @@ -627,15 +638,28 @@ private async Task<bool> UserCanEditCatalogue(int catalogueId)
return await this.catalogueService.CanCurrentUserEditCatalogue(catalogueId);
}

private void RemoveDeletedCaseFiles(CaseViewModel existingResource, CaseViewModel newResource)
private void RemoveBlockCollectionFiles(AssessmentViewModel existingModel, AssessmentViewModel newModel)
{
if (existingModel is { EndGuidance: { } } && existingModel.EndGuidance.Blocks != null)
{
this.RemoveBlockCollectionFiles(existingModel.EndGuidance, newModel.EndGuidance);
}

if (existingModel is { AssessmentContent: { } } && existingModel.AssessmentContent.Blocks != null)
{
this.RemoveBlockCollectionFiles(existingModel.AssessmentContent, newModel.AssessmentContent);
}
}

private void RemoveBlockCollectionFiles(BlockCollectionViewModel existingResource, BlockCollectionViewModel newResource)
{
try
{
var filePaths = new List<string>();
if (existingResource != null && existingResource.BlockCollection != null)
if (existingResource != null)
{
var allBlocks = existingResource.BlockCollection.Blocks.ToList();
var newBlocks = newResource.BlockCollection.Blocks.ToList();
var allBlocks = existingResource.Blocks.ToList();
var newBlocks = newResource.Blocks.ToList();
if (allBlocks.Any())
{
var existingAttachements = allBlocks.Where(x => x.BlockType == BlockType.Media && x.MediaBlock != null && x.MediaBlock.MediaType == MediaType.Attachment && x.MediaBlock.Attachment != null).ToList();
Expand All @@ -659,10 +683,19 @@ private void RemoveDeletedCaseFiles(CaseViewModel existingResource, CaseViewMode
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);
if (entry == null)
{
filePaths.Add(oldblock.MediaBlock.Video?.VideoFile?.File?.FilePath);
if (oldblock.MediaBlock?.Video?.VideoFile?.TranscriptFile?.File?.FilePath != null)
if (!string.IsNullOrWhiteSpace(oldblock.MediaBlock.Video.File.FilePath))
{
filePaths.Add(oldblock.MediaBlock.Video.File.FilePath);
}

if (!string.IsNullOrWhiteSpace(oldblock.MediaBlock.Video?.File?.VideoFile?.File?.FilePath))
{
filePaths.Add(oldblock.MediaBlock.Video?.VideoFile?.TranscriptFile?.File?.FilePath);
filePaths.Add(oldblock.MediaBlock.Video.File.VideoFile.File.FilePath);
}

if (oldblock.MediaBlock?.Video?.File?.VideoFile?.TranscriptFile?.File?.FilePath != null)
{
filePaths.Add(oldblock.MediaBlock.Video.File.VideoFile.TranscriptFile.File.FilePath);
}
}
}
Expand Down Expand Up @@ -713,6 +746,27 @@ private void RemoveDeletedCaseFiles(CaseViewModel existingResource, CaseViewMode
}
}
}

var existingQuestionFiles = this.CheckQuestionBlock(existingResource);
var newQuestionFiles = this.CheckQuestionBlock(newResource);
if (existingQuestionFiles.Any())
{
if (!newQuestionFiles.Any())
{
filePaths.AddRange(existingQuestionFiles);
}
else
{
foreach (var file in existingQuestionFiles)
{
var entry = newQuestionFiles.FirstOrDefault(x => x.Equals(file));
if (entry == null)
{
filePaths.Add(file);
}
}
}
}
}

if (filePaths != null && filePaths.Any())
Expand All @@ -724,5 +778,117 @@ private void RemoveDeletedCaseFiles(CaseViewModel existingResource, CaseViewMode
{
}
}

private List<string> CheckQuestionBlock(BlockCollectionViewModel model)
{
var filePath = new List<string>();
foreach (var block in model.Blocks)
{
if (block.BlockType == BlockType.Question && block.QuestionBlock != null)
{
if (block.QuestionBlock.QuestionType == QuestionTypeEnum.MatchGame && block.QuestionBlock.Answers != null)
{
foreach (var answerBlock in block.QuestionBlock.Answers)
{
if (answerBlock.BlockCollection != null && answerBlock.BlockCollection.Blocks != null)
{
foreach (var imageBlock in answerBlock.BlockCollection.Blocks)
{
if (imageBlock.BlockType == BlockType.Media && imageBlock.MediaBlock != null)
{
filePath.Add(imageBlock.MediaBlock.Image.File.FilePath);
}
}
}
}
}

var questionBlockCollection = block.QuestionBlock.QuestionBlockCollection;
if (questionBlockCollection != null && questionBlockCollection.Blocks != null)
{
foreach (var questionBlock in questionBlockCollection.Blocks)
{
if (questionBlock.BlockType == BlockType.Media && questionBlock.MediaBlock != null)
{
if (questionBlock.MediaBlock.Image != null)
{
filePath.Add(questionBlock.MediaBlock.Image.File.FilePath);
}

if (questionBlock.MediaBlock.Video != null)
{
if (questionBlock.MediaBlock.Video.File != null)
{
filePath.Add(questionBlock.MediaBlock.Video.File.FilePath);
}

if (questionBlock.MediaBlock.Video.VideoFile != null)
{
if (questionBlock.MediaBlock.Video.VideoFile.TranscriptFile != null)
{
filePath.Add(questionBlock.MediaBlock.Video.VideoFile.TranscriptFile.File.FilePath);
}

if (questionBlock.MediaBlock.Video.VideoFile.CaptionsFile != null)
{
filePath.Add(questionBlock.MediaBlock.Video.VideoFile.CaptionsFile.File.FilePath);
}
}
}
}
else if (questionBlock.BlockType == BlockType.WholeSlideImage && questionBlock.WholeSlideImageBlock != null)
{
var existingWholeSlideImages = questionBlock.WholeSlideImageBlock.WholeSlideImageBlockItems.ToList();
if (existingWholeSlideImages.Any())
{
foreach (var wsi in existingWholeSlideImages)
{
filePath.Add(wsi.WholeSlideImage?.File?.FilePath);
}
}
}
}
}

var feedbackBlockCollection = block.QuestionBlock.FeedbackBlockCollection;
if (feedbackBlockCollection != null && feedbackBlockCollection.Blocks != null)
{
foreach (var feedbackBlock in feedbackBlockCollection.Blocks)
{
if (feedbackBlock.BlockType == BlockType.Media && feedbackBlock.MediaBlock != null)
{
if (feedbackBlock.MediaBlock.Image != null)
{
filePath.Add(feedbackBlock.MediaBlock.Image.File.FilePath);
}

if (feedbackBlock.MediaBlock.Video != null)
{
if (feedbackBlock.MediaBlock.Video.File != null)
{
filePath.Add(feedbackBlock.MediaBlock.Video.File.FilePath);
}

if (feedbackBlock.MediaBlock.Video.VideoFile != null)
{
if (feedbackBlock.MediaBlock.Video.VideoFile.TranscriptFile != null)
{
filePath.Add(feedbackBlock.MediaBlock.Video.VideoFile.TranscriptFile.File.FilePath);
}

if (feedbackBlock.MediaBlock.Video.VideoFile.CaptionsFile != null)
{
filePath.Add(feedbackBlock.MediaBlock.Video.VideoFile.CaptionsFile.File.FilePath);
}
}
}
}
}
}
}
}

return filePath;
}
}
}
40 changes: 0 additions & 40 deletions LearningHub.Nhs.WebUI/Services/FileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,50 +230,10 @@ public async Task PurgeResourceFile(ResourceVersionExtendedViewModel vm = null,
{
allContentPath.Add(vm.ScormDetails.ContentFilePath);
}
else if (vm.GenericFileDetails != null && !string.IsNullOrWhiteSpace(vm.GenericFileDetails.File.FilePath))
{
allFilePath.Add(vm.GenericFileDetails.File.FilePath);
}
else if (vm.HtmlDetails != null && !string.IsNullOrWhiteSpace(vm.HtmlDetails.ContentFilePath))
{
allContentPath.Add(vm.HtmlDetails.ContentFilePath);
}
else if (vm.ImageDetails != null && !string.IsNullOrWhiteSpace(vm.ImageDetails.File?.FilePath))
{
allFilePath.Add(vm.ImageDetails.File?.FilePath);
}
else if (vm.ArticleDetails != null)
{
var files = vm.ArticleDetails.Files.ToList();
if (files.Any())
{
foreach (var file in files)
{
allFilePath.Add(file.FilePath);
}
}
}
else if (vm.CaseDetails != null)
{
var blockCollection = vm.CaseDetails.BlockCollection;
foreach (var entry in blockCollection.Blocks)
{
if (entry.ImageCarouselBlock != null)
{
foreach (var item in entry.ImageCarouselBlock?.ImageBlockCollection?.Blocks)
{
allFilePath.Add(item?.MediaBlock?.Image?.File.FilePath);
}
}
else if (entry.WholeSlideImageBlock != null)
{
foreach (var item in entry.WholeSlideImageBlock.WholeSlideImageBlockItems)
{
allFilePath.Add(item?.WholeSlideImage?.File.FilePath);
}
}
}
}

// audio and video to be added
await this.MoveInPutDirectoryToArchive(allFilePath);
Expand Down
Loading