Skip to content

Commit

Permalink
Merge pull request #282 from DFE-Digital/fix/your-results-additional-…
Browse files Browse the repository at this point in the history
…reqs

fix for the additional requirements checks not matching up questions to answers properly
  • Loading branch information
DanielClarkeEducation authored Aug 1, 2024
2 parents d55b7fb + 43a152f commit 8902b25
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,35 +94,51 @@ public async Task<IActionResult> Index(string qualificationId)

return View(model);
}

private (bool isValid, IActionResult? actionResult) ValidateAdditionalQuestions(Qualification qualification,
QualificationDetailsModel model)
{
// If the qualification has no additional requirements then skip this check
if (qualification.AdditionalRequirementQuestions != null &&
qualification.AdditionalRequirementQuestions.Count != 0)
// If the qualification has no additional requirements then skip all checks and return.
if (qualification.AdditionalRequirementQuestions == null ||
qualification.AdditionalRequirementQuestions.Count == 0) return (true, null);

var additionalRequirementsAnswers = userJourneyCookieService.GetAdditionalQuestionsAnswers();

// If there is a mismatch between the questions answered, then clear the answers and navigate back to the additional requirements check page
if (additionalRequirementsAnswers == null ||
qualification.AdditionalRequirementQuestions.Count != additionalRequirementsAnswers.Count)
{
var additionalRequirementsAnswers = userJourneyCookieService.GetAdditionalQuestionsAnswers();
return (false,
RedirectToAction("Index", "CheckAdditionalRequirements",
new { qualification.QualificationId }));
}

// If there is a mismatch between the questions answered, then clear the answers and navigate back to the additional requirements check page
if (additionalRequirementsAnswers == null ||
qualification.AdditionalRequirementQuestions.Count != additionalRequirementsAnswers.Count)
{
return (false, RedirectToAction("Index", "CheckAdditionalRequirements", new { qualification.QualificationId }));
}
// If there are not any answers to the questions that are not full and relevant we can continue back to check the ratios.
if (!CheckForAnyNonFAndRAnswers(qualification.AdditionalRequirementQuestions,
additionalRequirementsAnswers)) return (true, null);

// At this point, there will be at least one question answered in a non full and relevant way.
// we mark the ratios as not full and relevant and return.
model.RatioRequirements = MarkAsNotFullAndRelevant(model.RatioRequirements);
return (false, View(model));

if ((from question in qualification.AdditionalRequirementQuestions
}

/// <summary>
/// A function to take in the additional requirement questions and answers, match them up and check to see if the
/// user has answered any in a non full and relevant way.
/// </summary>
/// <param name="additionalRequirementQuestions">This should come from the qualification model</param>
/// <param name="additionalRequirementsAnswers">This should come from the user's selected answers</param>
/// <returns>True if we find any question answered in a non full and relevant way, false if none are found</returns>
private static bool CheckForAnyNonFAndRAnswers(List<AdditionalRequirementQuestion> additionalRequirementQuestions,
Dictionary<string, string> additionalRequirementsAnswers)
{
return (from question in additionalRequirementQuestions
from answer in additionalRequirementsAnswers
where (question.AnswerToBeFullAndRelevant && answer.Value == "no") ||
(!question.AnswerToBeFullAndRelevant && answer.Value == "yes")
select question).Any())
{
model.RatioRequirements = MarkAsNotFullAndRelevant(model.RatioRequirements);
return (false, View(model));
}
}

return (true, null);
where question.Question == answer.Key && ((question.AnswerToBeFullAndRelevant && answer.Value == "no") ||
(!question.AnswerToBeFullAndRelevant && answer.Value == "yes"))
select question).Any();
}

private void CheckRatioRequirements(int startDateYear, Qualification qualification, QualificationDetailsModel model)
Expand All @@ -133,13 +149,13 @@ private void CheckRatioRequirements(int startDateYear, Qualification qualificati

model.RatioRequirements.ApprovedForLevel2 =
CheckRatio(propertyToCheck, RatioRequirements.Level2RatioRequirementName, qualification);

model.RatioRequirements.ApprovedForLevel3 =
CheckRatio(propertyToCheck, RatioRequirements.Level3RatioRequirementName, qualification);

model.RatioRequirements.ApprovedForLevel6 =
CheckRatio(propertyToCheck, RatioRequirements.Level6RatioRequirementName, qualification);

model.RatioRequirements.ApprovedForUnqualified = true;
}

Expand All @@ -154,7 +170,7 @@ private bool CheckRatio(string propertyToCheck, string ratioName, Qualification
catch
{
logger.LogError("Could not find property: {PropertyToCheck} within {RatioName} for qualification: {QualificationId}",
propertyToCheck, ratioName, qualification.QualificationId);
propertyToCheck, ratioName, qualification.QualificationId);
throw;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,19 @@ public async Task Index_QualificationHasAdditionalQuestionsButNoneAnswered_Redir
}

[TestMethod]
[DataRow("no", "no")]
[DataRow("yes", "yes")]
[DataRow("no", "yes")]
public async Task
Index_Index_QualificationHasAdditionalQuestionsButAnswersAreNotCorrect_MarkAsNotRelevantAndReturn()
Index_Index_QualificationHasAdditionalQuestionsButAnswersAreNotCorrect_MarkAsNotRelevantAndReturn(string answer1, string answer2)
{
var mockLogger = new Mock<ILogger<QualificationDetailsController>>();
var mockContentService = new Mock<IContentService>();
var mockContentFilterService = new Mock<IContentFilterService>();
var mockInsetTextRenderer = new Mock<IGovUkInsetTextRenderer>();
var mockHtmlRenderer = new Mock<IHtmlRenderer>();
var mockUserJourneyCookieService = new Mock<IUserJourneyCookieService>();

const string qualificationId = "eyq-145";

var listOfAdditionalReqs = new List<AdditionalRequirementQuestion>
Expand All @@ -230,13 +233,19 @@ public async Task
{
Question = "Some Question",
AnswerToBeFullAndRelevant = true
},
new()
{
Question = "Another Question",
AnswerToBeFullAndRelevant = false
}
};

// Question has been answered, but the answer is not what we want for the qualification to be full and relevant
var listOfAdditionalReqsAnswered = new Dictionary<string, string>()
{
{ "Some Question", "no" }
{ "Some Question", answer1 },
{ "Another Question", answer2 }
};

var qualificationResult = new Qualification(qualificationId, "Qualification Name", "NCFE", 2, "2014", "2019",
Expand Down

0 comments on commit 8902b25

Please sign in to comment.