From 64280c893683dec81596c86198d267c4e799dfc0 Mon Sep 17 00:00:00 2001 From: Qais Yousuf Date: Tue, 26 Mar 2024 16:45:27 +0100 Subject: [PATCH] Creating multiple question in the edit mode comppleted --- .../Controllers/QuestionnaireController.cs | 96 ++++-- .../Admin/Views/Questionnaire/Create.cshtml | 50 ++- .../Admin/Views/Questionnaire/Delete.cshtml | 21 +- .../Admin/Views/Questionnaire/Details.cshtml | 123 ++++++- .../Admin/Views/Questionnaire/Edit.cshtml | 303 ++++++++++-------- .../Admin/Views/Questionnaire/Index.cshtml | 68 +++- .../EditQuestionnaireViewModel.cs | 1 + .../QuestionnaireVM/QuestionnaireViewModel.cs | 1 + Web/wwwroot/css/site.css | 56 +++- 9 files changed, 516 insertions(+), 203 deletions(-) diff --git a/Web/Areas/Admin/Controllers/QuestionnaireController.cs b/Web/Areas/Admin/Controllers/QuestionnaireController.cs index 1380c56..1dbe025 100644 --- a/Web/Areas/Admin/Controllers/QuestionnaireController.cs +++ b/Web/Areas/Admin/Controllers/QuestionnaireController.cs @@ -221,75 +221,103 @@ namespace Web.Areas.Admin.Controllers } // Update the questionnaire with the modified list of questions - + var newQuestions = new List(); + // Update or add new questions foreach (var questionViewModel in viewModel.Questions) { var existingQuestion = existingQuestionnaire.Questions.FirstOrDefault(q => q.Id == questionViewModel.Id); - - if (existingQuestion != null) + if(questionViewModel.Id !=0) { - var answersToRemove = new List(); - existingQuestion.Text = questionViewModel.Text; - existingQuestion.Type = questionViewModel.Type; - - - foreach (var answerViewModel in questionViewModel.Answers) + if (existingQuestion != null) { - // Check if the answer already exists - var existingAnswer = existingQuestion.Answers.FirstOrDefault(a => a.Id == answerViewModel.Id); + var answersToRemove = new List(); + existingQuestion.Text = questionViewModel.Text; + existingQuestion.Type = questionViewModel.Type; - if (answerViewModel.Id == 0) + + foreach (var answerViewModel in questionViewModel.Answers) { + // Check if the answer already exists + var existingAnswer = existingQuestion.Answers.FirstOrDefault(a => a.Id == answerViewModel.Id); - existingQuestion.Answers.Add(new Answer { Text = answerViewModel.Text }); + if (answerViewModel.Id == 0) + { + + existingQuestion.Answers.Add(new Answer { Text = answerViewModel.Text }); - } - else if (answerViewModel.Text == null) - { - existingQuestion.Answers.Remove(existingAnswer); - await _questionnaire.Update(existingQuestionnaire); + } + else if (answerViewModel.Text == null) + { + existingQuestion.Answers.Remove(existingAnswer); + await _questionnaire.Update(existingQuestionnaire); + } + + + else if (existingAnswer != null) + { + + existingAnswer.Text = answerViewModel.Text; + } + } - else if (existingAnswer != null) - { - - existingAnswer.Text = answerViewModel.Text; - } } - - - } + + else { - // Add new question + // Create a new question var newQuestion = new Question { - Text = questionViewModel.Text, // Make sure question text is not null - Type = questionViewModel.Type, // Make sure question type is not null - Answers = new List() // Initialize answers list + Text = questionViewModel.Text, + Type = questionViewModel.Type, + Answers = new List() }; foreach (var answerViewModel in questionViewModel.Answers) { - // Add new answer - newQuestion.Answers.Add(new Answer { Text = answerViewModel.Text }); + if (!string.IsNullOrEmpty(answerViewModel.Text)) + { + // Add new answer if text is not null or empty + newQuestion.Answers.Add(new Answer { Text = answerViewModel.Text }); + } } - // Add new question to questionnaire - existingQuestionnaire.Questions.Add(newQuestion); + // Add new question to the list of new questions + newQuestions.Add(newQuestion); } + existingQuestionnaire.Questions.AddRange(newQuestions); + //else + //{ + // // Add new question + // var newQuestion = new Question + // { + // Text = questionViewModel.Text, // Make sure question text is not null + // Type = questionViewModel.Type, // Make sure question type is not null + // Answers = new List() // Initialize answers list + // }; + + // foreach (var answerViewModel in questionViewModel.Answers) + // { + // // Add new answer + // newQuestion.Answers.Add(new Answer { Text = answerViewModel.Text }); + // } + + // // Add new question to questionnaire + // existingQuestionnaire.Questions.Add(newQuestion); + //} } diff --git a/Web/Areas/Admin/Views/Questionnaire/Create.cshtml b/Web/Areas/Admin/Views/Questionnaire/Create.cshtml index d407ea8..d2a9161 100644 --- a/Web/Areas/Admin/Views/Questionnaire/Create.cshtml +++ b/Web/Areas/Admin/Views/Questionnaire/Create.cshtml @@ -51,17 +51,17 @@ | - - + + } - + @@ -73,14 +73,16 @@ @section Scripts { - + + @{ - + } @@ -88,12 +90,11 @@ diff --git a/Web/Areas/Admin/Views/Questionnaire/Delete.cshtml b/Web/Areas/Admin/Views/Questionnaire/Delete.cshtml index d486fa2..87c8c70 100644 --- a/Web/Areas/Admin/Views/Questionnaire/Delete.cshtml +++ b/Web/Areas/Admin/Views/Questionnaire/Delete.cshtml @@ -7,7 +7,16 @@
Delete questionnaire
-
Are you sure you want to delete this questionnaire: @Model.Title
+ @*
Are you sure you want to delete this questionnaire: @Model.Title
*@ +
+ Are you sure you want to delete: + + @Html.Raw(Model.Title.Length >= 30 ? Model.Title.Substring(0, 30) : Model.Title) + + + +
+
@@ -55,7 +64,7 @@
@@ -143,6 +152,14 @@ } }); }); + $(document).ready(function () { + $("#readMoreBtn").click(function () { + $("#readMore").toggle(); + $(this).text(function (_, text) { + return text === "Read More" ? "Read Less" : "Read More"; + }); + }); + }); }); diff --git a/Web/Areas/Admin/Views/Questionnaire/Details.cshtml b/Web/Areas/Admin/Views/Questionnaire/Details.cshtml index 5e93647..249faaa 100644 --- a/Web/Areas/Admin/Views/Questionnaire/Details.cshtml +++ b/Web/Areas/Admin/Views/Questionnaire/Details.cshtml @@ -9,7 +9,6 @@
-
Details @@ -18,8 +17,19 @@
-
Details of questionnaire @Model.Title
- + +

Questionnaire details

+
+ +
+ Questionnaire +
+
+ Question +
+
+ Answer +
@@ -35,8 +45,29 @@ - - + + - - + @* *@ + + + @* *@ +
@Model.Id@Model.Title@Html.Raw(@Model.Description) +
+
+ + @Html.Raw(Model.Title.Length >= 30 ? Model.Title.Substring(0, 30) : Model.Title) + + Read More + + +
+
+
+
+
+ + @Html.Raw(Model.Description.Length >= 30 ? Model.Description.Substring(0, 30) : Model.Title) + + + Read More +
+
+
@@ -52,7 +83,18 @@
@question.Id - @question.Text +
+
+ + @Html.Raw(question.Text.Length >= 30 ? question.Text.Substring(0, 30) : question.Text) + + Read More + + + + +
+
@question.Type @@ -91,13 +133,76 @@
- - + + + + +@section Scripts { + + +} \ No newline at end of file diff --git a/Web/Areas/Admin/Views/Questionnaire/Edit.cshtml b/Web/Areas/Admin/Views/Questionnaire/Edit.cshtml index 5afb20c..94091f4 100644 --- a/Web/Areas/Admin/Views/Questionnaire/Edit.cshtml +++ b/Web/Areas/Admin/Views/Questionnaire/Edit.cshtml @@ -24,7 +24,13 @@ +
+
+

Edit existing questions or add new question

+
+ +
@for (int i = 0; i < Model.Questions.Count; i++) { @@ -44,10 +50,15 @@
-
- @Model.Questions[i].Text - | - + @*
+ @Model.Questions[i].Text + | +
*@ +
+ @Html.Raw(Model.Questions[i].Text.Length >= 30 ? Model.Questions[i].Text.Substring(0, 30) : Model.Questions[i].Text) + + | | +
- + -
- @Model.Questions[i].Text - -
- +
+ + + + +
+
- + } - +
@@ -102,7 +115,23 @@
- + @section Scripts { @@ -127,11 +156,11 @@ var newAnswerIndex = questionContainer.find('.answers .form-group').length; var answerHtml = ` -
- - - -
`; +
+ + + +
`; // Append the new answer to the specific question questionContainer.find('.answers').append(answerHtml); @@ -149,6 +178,8 @@ questionContainer.find('.question-details').show(); questionContainer.find('.editQuestion').hide(); questionContainer.find('.saveQuestion').show(); + questionContainer.find('.removeQuestion').show(); + questionContainer.find('.removeQuestion1').show(); }); // Function to save question details @@ -159,10 +190,10 @@ var questionText = questionContainer.find('.question-details input[name^="Questions"]').val(); // Apply styles for success (green text color) - titleTextElement.css('color', '#28a745'); // Set the success text color + // titleTextElement.css('color', '#28a745'); // Set the success text color - // Add checkmark icon before the text - titleTextElement.prepend(''); + // // Add checkmark icon before the text + // titleTextElement.prepend(''); // Show the original question title titleTextElement.show(); @@ -173,143 +204,139 @@ // Show the edit button and hide the save button questionContainer.find('.editQuestion').show(); questionContainer.find('.saveQuestion').hide(); + questionContainer.find('.removeQuestion1').hide(); + // // questionContainer.find('.removeQuestion3').show(); + // var successMessage = $(''); + // $(this).closest('.question').find('.question-title').prepend(successMessage); + // successMessage.fadeIn(); + + }); // Function to remove a question - $(document).on('click', '.removeQuestion', function () { - $(this).closest('.question').remove(); - }); + $(document).on('click', '.removeQuestion, .removeQuestion1, .removeQuestion3', function () { + // Store a reference to the question being removed + var questionToRemove = $(this).closest('.question'); + // Show the confirmation modal + $('#confirmDeleteModal').modal('show'); + + // Handle deletion confirmation + $('.confirm-delete').click(function () { + // Remove the question + questionToRemove.remove(); + + // Hide the modal after deletion + $('#confirmDeleteModal').modal('hide'); + }); + }); + // $(document).on('click', '.removeQuestion1', function () { + // $(this).closest('.question').remove(); + // }); + // $(document).on('click', '.removeQuestion3', function () { + // $(this).closest('.question').remove(); + // }); + + // Function to add a new question + // Function to add a new question + // Function to add a new question + + + $('.read-more-btn').click(function () { + var $questionTitle = $(this).closest('.question-title'); + var $moreQuestion = $questionTitle.find('.more-question'); + var $readMoreBtn = $(this); + + if ($moreQuestion.is(':visible')) { + $moreQuestion.hide(); // Hide the "Read More" text + $readMoreBtn.text('Read More'); // Change button text to "Read More" + } else { + $moreQuestion.show(); // Show the "Read More" text + $readMoreBtn.text('Read Less'); // Change button text to "Read Less" + } + }); // Function to add a new question $('#addNewQuestion').click(function () { var newQuestionIndex = $('.question').length; var questionHtml = ` -
-
- -
-
-
- -
-

NEXT QUESTION

-
- -
-
-
-
- -
-
- - -
-
- - -
-
-
- - - +
+ +
+
+
+
+
+

NEXT QUESTION

+
+
+
- -
- -
- -
-
`; + + + +
+
+ + +
+
+ + +
+
+
+ + + +
+
+ +
+ +
+ +
+ +
+
+
`; + // Append the new question HTML to the container $('#questionsContainer').append(questionHtml); + + // Show success message + // var successMessage = $(''); + // $('.question-title').prepend(successMessage); + // successMessage.fadeIn().delay(2000).fadeOut(); }); + + }); - @* *@ - - - } diff --git a/Web/Areas/Admin/Views/Questionnaire/Index.cshtml b/Web/Areas/Admin/Views/Questionnaire/Index.cshtml index 63dfb0a..6131ab7 100644 --- a/Web/Areas/Admin/Views/Questionnaire/Index.cshtml +++ b/Web/Areas/Admin/Views/Questionnaire/Index.cshtml @@ -3,6 +3,8 @@ @{ ViewData["Title"] = "Questionnaire"; } + +
@@ -33,8 +35,28 @@
@item.Id @item.Title@Html.Raw(item.Description) @item.Title +
+
+ @Html.Raw(item.Title.Length >= 30 ? item.Title.Substring(0, 30) : item.Title) + + Read More +
+
+
@Html.Raw(item.Description) + +
+
+ @Html.Raw(item.Description.Length >= 30 ? item.Description.Substring(0, 30) : item.Title) + + Read More +
+
+
@*