From d733b2cdb15b5b05a1999daec5f0e70f60c1ceca Mon Sep 17 00:00:00 2001 From: Qais Yousuf Date: Wed, 1 May 2024 17:51:38 +0200 Subject: [PATCH] Sending the survey to multiple users created --- .../Controllers/QuestionnaireController.cs | 159 ++++++------------ .../Controllers/SurveyAnalysisController.cs | 32 +++- .../Questionnaire/SendQuestionnaire.cshtml | 13 +- .../Views/SurveyAnalysis/Analysis.cshtml | 74 +++++++- .../SendQuestionnaireViewModel.cs | 6 +- Web/Web.csproj | 1 + 6 files changed, 167 insertions(+), 118 deletions(-) diff --git a/Web/Areas/Admin/Controllers/QuestionnaireController.cs b/Web/Areas/Admin/Controllers/QuestionnaireController.cs index 4f3d2cf..da13472 100644 --- a/Web/Areas/Admin/Controllers/QuestionnaireController.cs +++ b/Web/Areas/Admin/Controllers/QuestionnaireController.cs @@ -465,120 +465,69 @@ namespace Web.Areas.Admin.Controllers { if (ModelState.IsValid) { - - - var questionnairePath = _configuration["Email:Questionnaire"]; - int surveyId = viewModel.QuestionnaireId; - - var userEmailEncoded = HttpUtility.UrlEncode(viewModel.Email); - - - DateTime currentDateTime; - if (viewModel.ExpirationDateTime.HasValue) - { - currentDateTime = viewModel.ExpirationDateTime.Value; - } - else - { - - currentDateTime = DateTime.Now; - } - - // Calculate the expiration date and time by adding 5 minutes to the current date and time - DateTime expiryDateTime = currentDateTime; - - // Generate a unique token, for example, using a cryptographic library or a GUID + DateTime currentDateTime = viewModel.ExpirationDateTime.HasValue ? viewModel.ExpirationDateTime.Value : DateTime.Now; + DateTime expiryDateTime = currentDateTime; // This line might need adjustment if you are actually setting an expiry. string token = Guid.NewGuid().ToString(); - - // Append the expiration date and time to the token (you might want to encrypt it for security) string tokenWithExpiry = $"{token}|{expiryDateTime.ToString("yyyy-MM-ddTHH:mm:ssZ")}"; - - var completeUrl = $"{Request.Scheme}://{Request.Host}/{questionnairePath}/{viewModel.QuestionnaireId}?t={tokenWithExpiry}&E={userEmailEncoded}"; - - //var completeUrl = $"{Request.Scheme}://{Request.Host}/{questionnairePath}/{viewModel.QuestionnaireId}?t={tokenWithExpiry}&E={userEmail}"; - - //var completeUrl = $"{Request.Scheme}://{Request.Host}/{questionnairePath}/{viewModel.QuestionnaireId}"; - - - var toEmail = viewModel.Email; + var emailList = viewModel.Emails.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries) + .Select(email => email.Trim()) + .ToList(); var question = _questionnaire.GetQuesById(viewModel.Id); - var subject = question.Title; - // Construct the email body with HTML formatting - string emailBody = $@" - - - - - -
-

Hey {viewModel.Name},

-
{subject}
-

Thank you for participating in our survey. Your feedback is valuable to us.

-

Please click the button below to start the survey:

-

The survey will be expire :{expiryDateTime.ToLongDateString()} Time: {expiryDateTime.ToShortTimeString()}

-
- Start Survey -
- -

-

Søren Eggert Lundsteen Olsen
- Seosoft ApS
-


- Hovedgaden 3 - Jordrup
- Kolding 6064
- Denmark

- -
- - "; - - - // Call the SendConfirmationEmailAsync method to send the email - var emailSend = new EmailToSend(toEmail, subject, emailBody); - - bool emailSent = await _emailServices.SendConfirmationEmailAsync(emailSend); - - if (emailSent) + bool allEmailsSent = true; + foreach (var email in emailList) { - // Email sent successfully - // You can redirect to a success page or return a success message - TempData["Success"] = "Questionnaire sent successfully"; - return RedirectToAction(nameof(Index)); + var userName = email.Substring(0, email.IndexOf('@')); // This assumes the email is valid and contains an '@' + userName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(userName.Replace(".", " ")); // Optional: Improve formatting, replace dots and capitalize names + var userEmailEncoded = HttpUtility.UrlEncode(email); + var completeUrl = $"{Request.Scheme}://{Request.Host}/{questionnairePath}/{viewModel.QuestionnaireId}?t={tokenWithExpiry}&E={userEmailEncoded}"; + + string emailBody = $@" + + + + + +
+

Hey {userName},

+
{subject}
+

Thank you for participating in our survey. Your feedback is valuable to us.

+

Please click the button below to start the survey:

+

The survey will expire: {expiryDateTime.ToLongDateString()} Time: {expiryDateTime.ToShortTimeString()}

+
+ Start Survey +

+

Søren Eggert Lundsteen Olsen
+ Seosoft ApS
+


+ Hovedgaden 3 Jordrup
+ Kolding 6064
+ Denmark

+
+ + "; + + var emailSend = new EmailToSend(email, subject, emailBody); + bool emailSent = await _emailServices.SendConfirmationEmailAsync(emailSend); + if (!emailSent) + { + allEmailsSent = false; + ModelState.AddModelError(string.Empty, "Failed to send questionnaire via email to: " + email); + } } - else + + + if (allEmailsSent) { - // Email failed to send - // You can return an error message or handle it as needed - ModelState.AddModelError(string.Empty, "Failed to send questionnaire via email."); + TempData["Success"] = "Questionnaire sent successfully to all recipients."; + return RedirectToAction(nameof(Index)); } } diff --git a/Web/Areas/Admin/Controllers/SurveyAnalysisController.cs b/Web/Areas/Admin/Controllers/SurveyAnalysisController.cs index f3c902a..5f268c6 100644 --- a/Web/Areas/Admin/Controllers/SurveyAnalysisController.cs +++ b/Web/Areas/Admin/Controllers/SurveyAnalysisController.cs @@ -4,7 +4,8 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Model; using Web.ViewModel.QuestionnaireVM; -using Web.ViewModel.QuestionVM; + +using System.IO; namespace Web.Areas.Admin.Controllers { @@ -109,6 +110,35 @@ namespace Web.Areas.Admin.Controllers } + //[HttpGet] + //public IActionResult GenerateReport(int id) + //{ + // var viewModel = GetQuestionnaireData(id); + // if (viewModel == null) + // { + // return NotFound("No questionnaire found with the given ID."); + // } + + // var webReport = new WebReport(); + + // // Load your FastReport report design + // webReport.Report.Load(Path.Combine(env.WebRootPath, "Reports", "QuestionnaireReport.frx")); + + // // Register the data source + // webReport.Report.RegisterData(new[] { viewModel }, "Questionnaire"); + + // webReport.Report.Prepare(); + + // using (MemoryStream ms = new MemoryStream()) + // { + // webReport.Report.Export(new FastReport.Export.PdfSimple.PDFSimpleExport(), ms); + // return File(ms.ToArray(), "application/pdf", "QuestionnaireReport.pdf"); + // } + //} + + + + } } diff --git a/Web/Areas/Admin/Views/Questionnaire/SendQuestionnaire.cshtml b/Web/Areas/Admin/Views/Questionnaire/SendQuestionnaire.cshtml index 6889216..3880002 100644 --- a/Web/Areas/Admin/Views/Questionnaire/SendQuestionnaire.cshtml +++ b/Web/Areas/Admin/Views/Questionnaire/SendQuestionnaire.cshtml @@ -20,16 +20,13 @@ +
- - - -
-
- - - + + +
+
diff --git a/Web/Areas/Admin/Views/SurveyAnalysis/Analysis.cshtml b/Web/Areas/Admin/Views/SurveyAnalysis/Analysis.cshtml index 2d15fb8..7729ce7 100644 --- a/Web/Areas/Admin/Views/SurveyAnalysis/Analysis.cshtml +++ b/Web/Areas/Admin/Views/SurveyAnalysis/Analysis.cshtml @@ -110,6 +110,70 @@
+@* @section Scripts { + + +} + *@ + @section Scripts {