|
|
|
@ -13,18 +13,18 @@ namespace WF_WebAdmin.Pages
|
|
|
|
|
public partial class AddQuiz
|
|
|
|
|
{
|
|
|
|
|
[Inject]
|
|
|
|
|
public ILogger<AddQuiz> Logger { get; set; }
|
|
|
|
|
public ILogger<AddQuiz>? Logger { get; set; }
|
|
|
|
|
|
|
|
|
|
[Inject]
|
|
|
|
|
public IStringLocalizer<AddQuiz> Localizer { get; set; }
|
|
|
|
|
public IStringLocalizer<AddQuiz>? Localizer { get; set; }
|
|
|
|
|
|
|
|
|
|
[Inject]
|
|
|
|
|
private IQuizService quizService { get; set; }
|
|
|
|
|
private IQuizService? QuizService { get; set; }
|
|
|
|
|
|
|
|
|
|
[Inject]
|
|
|
|
|
public NavigationManager NavigationManager { get; set; }
|
|
|
|
|
public NavigationManager? NavigationManager { get; set; }
|
|
|
|
|
|
|
|
|
|
private QuizModel QuizModel = new();
|
|
|
|
|
private readonly QuizModel _quizModel = new();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles the valid submission of a quiz form.
|
|
|
|
@ -39,21 +39,21 @@ namespace WF_WebAdmin.Pages
|
|
|
|
|
int id;
|
|
|
|
|
|
|
|
|
|
// Get the current number of quizzes from the quiz service.
|
|
|
|
|
id = await quizService.getNbQuiz();
|
|
|
|
|
id = await QuizService.getNbQuiz();
|
|
|
|
|
|
|
|
|
|
// Increment the quiz ID for the new quiz.
|
|
|
|
|
id++;
|
|
|
|
|
|
|
|
|
|
// Create a new quiz and add it using the quiz service.
|
|
|
|
|
LoggerSaveStub.Log(Logger, LogLevel.Information, $"Creation of the {QuizModel.Question} question");
|
|
|
|
|
await quizService.addQuiz(new Quiz(
|
|
|
|
|
LoggerSaveStub.Log(Logger, LogLevel.Information, $"Creation of the {_quizModel.Question} question");
|
|
|
|
|
await QuizService.addQuiz(new Quiz(
|
|
|
|
|
id, // New quiz ID
|
|
|
|
|
validateInformation(QuizModel.Question), // Validated question
|
|
|
|
|
validateInformation(QuizModel.AnswerA), // Validated answer A
|
|
|
|
|
validateInformation(QuizModel.AnswerB), // Validated answer B
|
|
|
|
|
validateInformation(QuizModel.AnswerC), // Validated answer C
|
|
|
|
|
validateInformation(QuizModel.AnswerD), // Validated answer D
|
|
|
|
|
validateReponse(QuizModel.CAnswer) // Validated correct answer
|
|
|
|
|
ValidateInformation(_quizModel.Question), // Validated question
|
|
|
|
|
ValidateInformation(_quizModel.AnswerA), // Validated answer A
|
|
|
|
|
ValidateInformation(_quizModel.AnswerB), // Validated answer B
|
|
|
|
|
ValidateInformation(_quizModel.AnswerC), // Validated answer C
|
|
|
|
|
ValidateInformation(_quizModel.AnswerD), // Validated answer D
|
|
|
|
|
ValidateReponse(_quizModel.CAnswer) // Validated correct answer
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
// Navigate to the "modifquiz" page after adding the quiz.
|
|
|
|
@ -69,10 +69,10 @@ namespace WF_WebAdmin.Pages
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The selected answer that will be marked as the correct answer.</param>
|
|
|
|
|
/// <param name="checkedValue">The value of the selected option, typically used for validation or additional logic.</param>
|
|
|
|
|
private void OnCAwnserChange(string item, object checkedValue)
|
|
|
|
|
private void OnCAwnserChange(string item)
|
|
|
|
|
{
|
|
|
|
|
// Update the correct answer in the QuizModel with the selected answer.
|
|
|
|
|
QuizModel.CAnswer = item;
|
|
|
|
|
_quizModel.CAnswer = item;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -84,9 +84,9 @@ namespace WF_WebAdmin.Pages
|
|
|
|
|
/// <returns>
|
|
|
|
|
/// Returns the input string as it is for now. The validation logic is yet to be implemented.
|
|
|
|
|
/// </returns>
|
|
|
|
|
private static string validateInformation(string item)
|
|
|
|
|
private string ValidateInformation(string? item)
|
|
|
|
|
{
|
|
|
|
|
return item; // VALIDATION A FAIRE
|
|
|
|
|
return string.IsNullOrWhiteSpace(item) ? "Valeur par défaut" : item;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -98,43 +98,31 @@ namespace WF_WebAdmin.Pages
|
|
|
|
|
/// <returns>
|
|
|
|
|
/// Returns the input item if valid (A, B, C, or D). If the item is invalid or null, it returns a default value ("A").
|
|
|
|
|
/// </returns>
|
|
|
|
|
private static string validateReponse(string item)
|
|
|
|
|
private static string ValidateReponse(string item)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
// Check if the item is null or empty
|
|
|
|
|
if (string.IsNullOrEmpty(item))
|
|
|
|
|
{
|
|
|
|
|
// Check if the item is not null or empty
|
|
|
|
|
if (!string.IsNullOrEmpty(item))
|
|
|
|
|
{
|
|
|
|
|
// Validate that the item is one of the allowed values: A, B, C, or D
|
|
|
|
|
switch (item)
|
|
|
|
|
{
|
|
|
|
|
case "A":
|
|
|
|
|
break;
|
|
|
|
|
case "B":
|
|
|
|
|
break;
|
|
|
|
|
case "C":
|
|
|
|
|
break;
|
|
|
|
|
case "D":
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
// Throw exception if the item is not one of the allowed answers
|
|
|
|
|
throw new InvalidDataException("Invalid item (validateReponse) : item must be A,B,C or D " + item + " given.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Throw exception if the item is null or empty
|
|
|
|
|
throw new ArgumentNullException("Invalid item (validateReponse): null given.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return the validated item
|
|
|
|
|
return item;
|
|
|
|
|
// Throw exception if the item is null or empty
|
|
|
|
|
throw new ArgumentNullException(nameof(item), "The item cannot be null or empty.");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
|
|
// Validate that the item is one of the allowed values: A, B, C, or D
|
|
|
|
|
switch (item)
|
|
|
|
|
{
|
|
|
|
|
// In case of an exception, return a default answer ("A")
|
|
|
|
|
return "A"; // Default Argument
|
|
|
|
|
case "A":
|
|
|
|
|
case "B":
|
|
|
|
|
case "C":
|
|
|
|
|
case "D":
|
|
|
|
|
// Valid values, no action needed
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
// Throw exception if the item is not one of the allowed answers
|
|
|
|
|
throw new InvalidDataException($"Invalid item '{item}' provided. Item must be one of: A, B, C, or D.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return the validated item
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|