940 lines
34 KiB
C#
940 lines
34 KiB
C#
using Data;
|
|
using Mailjet.Client.Resources;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
using Microsoft.AspNetCore.WebUtilities;
|
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
using Microsoft.CodeAnalysis.Elfie.Extensions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Model;
|
|
using Services.EmailSend;
|
|
using Services.Interaces;
|
|
using System.Globalization;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Web;
|
|
using Web.ViewModel.QuestionnaireVM;
|
|
|
|
|
|
namespace Web.Areas.Admin.Controllers
|
|
{
|
|
|
|
|
|
|
|
public class QuestionnaireController : Controller
|
|
{
|
|
private readonly IQuestionnaireRepository _questionnaire;
|
|
private readonly SurveyContext _context;
|
|
private readonly IQuestionRepository _question;
|
|
private readonly IConfiguration _configuration;
|
|
private readonly IEmailServices _emailServices;
|
|
|
|
public QuestionnaireController(IQuestionnaireRepository Questionnaire, SurveyContext Context, IQuestionRepository Question, IConfiguration configuration, IEmailServices emailServices)
|
|
{
|
|
_questionnaire = Questionnaire;
|
|
_context = Context;
|
|
_question = Question;
|
|
_configuration = configuration;
|
|
_emailServices = emailServices;
|
|
}
|
|
public IActionResult Index()
|
|
{
|
|
|
|
var questionnaire = _questionnaire.GetQuestionnairesWithQuestion();
|
|
|
|
var question = _question.GetQuestionsWithAnswers();
|
|
|
|
|
|
|
|
List<QuestionnaireViewModel> viewmodel = new List<QuestionnaireViewModel>();
|
|
|
|
|
|
foreach (var item in questionnaire)
|
|
{
|
|
viewmodel.Add(new QuestionnaireViewModel
|
|
{
|
|
Id = item.Id,
|
|
Description = item.Description,
|
|
Title = item.Title,
|
|
Questions = item.Questions,
|
|
|
|
|
|
|
|
});
|
|
}
|
|
|
|
return View(viewmodel);
|
|
}
|
|
[HttpGet]
|
|
public IActionResult Create()
|
|
|
|
|
|
{
|
|
|
|
var questionTypes = Enum.GetValues(typeof(QuestionType)).Cast<QuestionType>();
|
|
|
|
ViewBag.QuestionTypes = new SelectList(questionTypes);
|
|
|
|
var questionnaire = new QuestionnaireViewModel
|
|
{
|
|
|
|
Questions = new List<Question>(),
|
|
Answers = new List<Answer>()
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return View(questionnaire);
|
|
}
|
|
[HttpPost]
|
|
public async Task<IActionResult> Create(QuestionnaireViewModel viewmodel)
|
|
{
|
|
if (ModelState.IsValid)
|
|
{
|
|
var questionnaire = new Questionnaire
|
|
{
|
|
Id = viewmodel.Id,
|
|
Title = viewmodel.Title,
|
|
Description = viewmodel.Description,
|
|
};
|
|
|
|
var questions = viewmodel.Questions;
|
|
|
|
foreach (var questionViewModel in viewmodel.Questions)
|
|
{
|
|
var question = new Question
|
|
{
|
|
QuestionnaireId = questionViewModel.QuestionnaireId,
|
|
Text = questionViewModel.Text,
|
|
Type = questionViewModel.Type,
|
|
Answers = new List<Answer>()
|
|
};
|
|
|
|
foreach (var answerViewModel in questionViewModel.Answers)
|
|
{
|
|
// Skip empty answers
|
|
if (string.IsNullOrWhiteSpace(answerViewModel.Text))
|
|
continue;
|
|
|
|
var answer = new Answer
|
|
{
|
|
Text = answerViewModel.Text,
|
|
QuestionId = answerViewModel.QuestionId,
|
|
IsOtherOption = answerViewModel.IsOtherOption // NEW: Handle IsOtherOption property
|
|
};
|
|
|
|
question.Answers.Add(answer);
|
|
}
|
|
|
|
questionnaire.Questions.Add(question);
|
|
}
|
|
|
|
_questionnaire.Add(questionnaire);
|
|
await _questionnaire.commitAsync();
|
|
TempData["Success"] = "Questionnaire created successfully";
|
|
|
|
return RedirectToAction("Index");
|
|
}
|
|
return View(viewmodel);
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult Edit(int? id)
|
|
{
|
|
var questionTypes = Enum.GetValues(typeof(QuestionType))
|
|
.Cast<QuestionType>()
|
|
.Select(e => new SelectListItem { Value = e.ToString(), Text = e.ToString() });
|
|
ViewBag.QuestionTypes = questionTypes;
|
|
|
|
var questionnaire = _questionnaire.GetQuestionnaireWithQuestionAndAnswer(id);
|
|
|
|
if (questionnaire == null)
|
|
{
|
|
return NotFound(); // Or handle not found case appropriately
|
|
}
|
|
|
|
var viewModel = new EditQuestionnaireViewModel
|
|
{
|
|
Id = questionnaire.Id,
|
|
Title = questionnaire.Title,
|
|
Description = questionnaire.Description,
|
|
|
|
|
|
Questions = questionnaire.Questions
|
|
.Select(q => new Question
|
|
{
|
|
Id = q.Id,
|
|
Text = q.Text,
|
|
Type = q.Type,
|
|
QuestionnaireId = q.QuestionnaireId,
|
|
|
|
|
|
Answers = q.Answers.Select(a => new Answer
|
|
{
|
|
Id = a.Id,
|
|
Text = a.Text,
|
|
Question = a.Question,
|
|
QuestionId = a.QuestionId
|
|
|
|
|
|
|
|
|
|
}).ToList()
|
|
}).ToList()
|
|
};
|
|
|
|
return View(viewModel);
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> Edit(EditQuestionnaireViewModel viewModel)
|
|
{
|
|
var questionTypes = Enum.GetValues(typeof(QuestionType))
|
|
.Cast<QuestionType>()
|
|
.Select(e => new SelectListItem { Value = e.ToString(), Text = e.ToString() });
|
|
ViewBag.QuestionTypes = questionTypes;
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
try
|
|
{
|
|
using var transaction = await _context.Database.BeginTransactionAsync();
|
|
|
|
try
|
|
{
|
|
// Step 1: Update the questionnaire basic info
|
|
var existingQuestionnaire = await _context.Questionnaires
|
|
.FirstOrDefaultAsync(q => q.Id == viewModel.Id);
|
|
|
|
if (existingQuestionnaire == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
existingQuestionnaire.Title = viewModel.Title;
|
|
existingQuestionnaire.Description = viewModel.Description;
|
|
|
|
// Step 2: Get all existing questions for this questionnaire
|
|
var existingQuestions = await _context.Questions
|
|
.Where(q => q.QuestionnaireId == viewModel.Id)
|
|
.ToListAsync();
|
|
|
|
// Step 3: Delete ALL answers first (foreign key constraint)
|
|
if (existingQuestions.Any())
|
|
{
|
|
var questionIds = existingQuestions.Select(q => q.Id).ToList();
|
|
var existingAnswers = await _context.Answers
|
|
.Where(a => questionIds.Contains(a.QuestionId))
|
|
.ToListAsync();
|
|
|
|
_context.Answers.RemoveRange(existingAnswers);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
// Step 4: Delete ALL questions
|
|
_context.Questions.RemoveRange(existingQuestions);
|
|
await _context.SaveChangesAsync();
|
|
|
|
// Step 5: Add new questions (only if provided and valid)
|
|
int newQuestionsAdded = 0;
|
|
if (viewModel.Questions != null && viewModel.Questions.Count > 0)
|
|
{
|
|
var validQuestions = viewModel.Questions
|
|
.Where(q => !string.IsNullOrWhiteSpace(q.Text))
|
|
.ToList();
|
|
|
|
foreach (var questionViewModel in validQuestions)
|
|
{
|
|
var newQuestion = new Question
|
|
{
|
|
Text = questionViewModel.Text.Trim(),
|
|
Type = questionViewModel.Type,
|
|
QuestionnaireId = viewModel.Id
|
|
};
|
|
|
|
_context.Questions.Add(newQuestion);
|
|
await _context.SaveChangesAsync(); // Save to get the ID
|
|
|
|
// Add answers for this question
|
|
if (questionViewModel.Answers != null)
|
|
{
|
|
var validAnswers = questionViewModel.Answers
|
|
.Where(a => !string.IsNullOrWhiteSpace(a.Text))
|
|
.ToList();
|
|
|
|
foreach (var answerViewModel in validAnswers)
|
|
{
|
|
var newAnswer = new Answer
|
|
{
|
|
Text = answerViewModel.Text.Trim(),
|
|
IsOtherOption = answerViewModel.IsOtherOption,
|
|
QuestionId = newQuestion.Id
|
|
};
|
|
|
|
_context.Answers.Add(newAnswer);
|
|
}
|
|
|
|
if (validAnswers.Any())
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
}
|
|
|
|
newQuestionsAdded++;
|
|
}
|
|
}
|
|
|
|
// Step 6: Final save and commit
|
|
await _context.SaveChangesAsync();
|
|
await transaction.CommitAsync();
|
|
|
|
// Step 7: Get final count for success message
|
|
var finalQuestionCount = await _context.Questions
|
|
.Where(q => q.QuestionnaireId == viewModel.Id)
|
|
.CountAsync();
|
|
|
|
// Success message
|
|
if (finalQuestionCount == 0)
|
|
{
|
|
TempData["Success"] = "Questionnaire updated successfully. All questions have been removed.";
|
|
}
|
|
else
|
|
{
|
|
TempData["Success"] = $"Questionnaire updated successfully with {finalQuestionCount} question(s).";
|
|
}
|
|
|
|
return RedirectToAction(nameof(Index));
|
|
}
|
|
catch (Exception)
|
|
{
|
|
await transaction.RollbackAsync();
|
|
throw;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ModelState.AddModelError("", "An error occurred while updating the questionnaire. Please try again.");
|
|
return View(viewModel);
|
|
}
|
|
}
|
|
|
|
return View(viewModel);
|
|
}
|
|
[HttpGet]
|
|
public IActionResult Delete(int id)
|
|
{
|
|
var questionTypes = Enum.GetValues(typeof(QuestionType)).Cast<QuestionType>();
|
|
|
|
ViewBag.QuestionTypes = new SelectList(questionTypes);
|
|
var questionnaire = _questionnaire.GetQuestionnaireWithQuestionAndAnswer(id);
|
|
|
|
if (questionnaire == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
var viewModel = new QuestionnaireViewModel
|
|
{
|
|
Id = questionnaire.Id,
|
|
Title = questionnaire.Title,
|
|
Description = questionnaire.Description,
|
|
Questions = questionnaire.Questions
|
|
.Select(q => new Question
|
|
{
|
|
Id = q.Id,
|
|
Text = q.Text,
|
|
Type = q.Type,
|
|
Answers = q.Answers.Select(a => new Answer
|
|
{
|
|
Id = a.Id,
|
|
Text = a.Text,
|
|
IsOtherOption = a.IsOtherOption // NEW: Include IsOtherOption property
|
|
}).ToList()
|
|
}).ToList()
|
|
};
|
|
|
|
return View(viewModel);
|
|
}
|
|
|
|
[HttpPost]
|
|
[ActionName("Delete")]
|
|
public async Task<IActionResult> DeleteConfirm(int id)
|
|
{
|
|
try
|
|
{
|
|
// Your Delete method is async Task, so you need to await it
|
|
// It doesn't return anything, so don't assign it to a variable
|
|
await _questionnaire.Delete(id);
|
|
|
|
// If we reach here, deletion was successful (no exception thrown)
|
|
return Json(new { success = true, message = "Item deleted successfully" });
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
return Json(new { success = false, message = "Invalid ID provided" });
|
|
}
|
|
catch (ArgumentException ex)
|
|
{
|
|
return Json(new { success = false, message = "Questionnaire not found" });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Log the actual exception to see what's wrong
|
|
System.Diagnostics.Debug.WriteLine($"Delete error: {ex.Message}");
|
|
return Json(new { success = false, message = "An error occurred while deleting the questionnaire" });
|
|
}
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult Details(int id)
|
|
{
|
|
var questionTypes = Enum.GetValues(typeof(QuestionType)).Cast<QuestionType>();
|
|
|
|
ViewBag.QuestionTypes = new SelectList(questionTypes);
|
|
var questionnaire = _questionnaire.GetQuestionnaireWithQuestionAndAnswer(id);
|
|
|
|
if (questionnaire == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
var viewModel = new QuestionnaireViewModel
|
|
{
|
|
Id = questionnaire.Id,
|
|
Title = questionnaire.Title,
|
|
Description = questionnaire.Description,
|
|
Questions = questionnaire.Questions
|
|
.Select(q => new Question
|
|
{
|
|
Id = q.Id,
|
|
Text = q.Text,
|
|
Type = q.Type,
|
|
Answers = q.Answers.Select(a => new Answer
|
|
{
|
|
Id = a.Id,
|
|
Text = a.Text,
|
|
IsOtherOption = a.IsOtherOption // NEW: Include IsOtherOption property
|
|
}).ToList()
|
|
}).ToList()
|
|
};
|
|
|
|
return View(viewModel);
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult SendQuestionnaire(int id)
|
|
{
|
|
var quesstionnaireFromDb = _questionnaire.GetQuestionnaireWithQuestionAndAnswer(id);
|
|
var sendquestionviewmodel = new SendQuestionnaireViewModel();
|
|
|
|
sendquestionviewmodel.QuestionnaireId = id;
|
|
ViewBag.questionnaireName = quesstionnaireFromDb.Title;
|
|
|
|
return View(sendquestionviewmodel);
|
|
|
|
}
|
|
|
|
|
|
[HttpPost]
|
|
|
|
public async Task<IActionResult> SendQuestionnaire(SendQuestionnaireViewModel viewModel)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
return View(viewModel);
|
|
|
|
var questionnairePath = _configuration["Email:Questionnaire"];
|
|
var subject = _questionnaire.GetQuesById(viewModel.Id)?.Title ?? "Survey Invitation";
|
|
|
|
var currentDateTime = viewModel.ExpirationDateTime ?? DateTime.Now;
|
|
string token = Guid.NewGuid().ToString();
|
|
string tokenWithExpiry = $"{token}|{currentDateTime:yyyy-MM-ddTHH:mm:ssZ}";
|
|
|
|
var emailList = viewModel.Emails.Split(',', StringSplitOptions.RemoveEmptyEntries)
|
|
.Select(e => e.Trim())
|
|
.ToList();
|
|
|
|
bool allEmailsSent = true;
|
|
|
|
foreach (var email in emailList)
|
|
{
|
|
string userName = FormatUserNameFromEmail(email);
|
|
string userEmailEncoded = HttpUtility.UrlEncode(email);
|
|
string completeUrl = $"{Request.Scheme}://{Request.Host}/{questionnairePath}/{viewModel.QuestionnaireId}?t={tokenWithExpiry}&E={userEmailEncoded}";
|
|
|
|
string emailBody = GenerateEmailBody(userName, subject, completeUrl, currentDateTime);
|
|
|
|
var emailSend = new EmailToSend(email, subject, emailBody)
|
|
{
|
|
Headers = new Dictionary<string, string>
|
|
{
|
|
{ "X-Priority", "1" },
|
|
{ "Importance", "High" },
|
|
{ "List-Unsubscribe", "<mailto:kontakt@nvkn.dk?subject=Unsubscribe>" },
|
|
{ "List-Unsubscribe-Post", "List-Unsubscribe=One-Click" },
|
|
{ "X-Microsoft-Classification", "Personal" }
|
|
}
|
|
};
|
|
|
|
bool emailSent = await _emailServices.SendConfirmationEmailAsync(emailSend);
|
|
|
|
if (!emailSent)
|
|
{
|
|
allEmailsSent = false;
|
|
ModelState.AddModelError(string.Empty, $"Failed to send questionnaire to: {email}");
|
|
}
|
|
}
|
|
|
|
if (allEmailsSent)
|
|
{
|
|
TempData["Success"] = "Questionnaire sent successfully to all recipients.";
|
|
return RedirectToAction(nameof(Index));
|
|
}
|
|
|
|
return View(viewModel);
|
|
}
|
|
private string FormatUserNameFromEmail(string email)
|
|
{
|
|
var usernamePart = email.Split('@')[0];
|
|
return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(usernamePart.Replace('.', ' '));
|
|
}
|
|
|
|
|
|
// ✅ Replace your GenerateEmailBody method with this less promotional version:
|
|
|
|
// ✅ Replace your GenerateEmailBody with this VERY simple version:
|
|
|
|
private static string GenerateEmailBody(string userName, string subject, string url, DateTime expiry)
|
|
{
|
|
var danishCulture = new CultureInfo("da-DK");
|
|
string expiryDate = expiry.ToString("dd. MMMM yyyy", danishCulture);
|
|
string expiryTime = expiry.ToString("HH:mm", danishCulture);
|
|
|
|
return $@"
|
|
<!DOCTYPE html>
|
|
<html lang='da'>
|
|
<head>
|
|
<meta charset='UTF-8'>
|
|
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
|
|
<title>Spørgeskema</title>
|
|
<style>
|
|
body {{
|
|
font-family: Arial, sans-serif;
|
|
font-size: 14px;
|
|
line-height: 1.4;
|
|
color: #333;
|
|
background-color: #ffffff;
|
|
margin: 0;
|
|
padding: 0;
|
|
}}
|
|
|
|
.container {{
|
|
max-width: 600px;
|
|
margin: 0 auto;
|
|
padding: 20px;
|
|
background-color: #ffffff;
|
|
}}
|
|
|
|
.header {{
|
|
border-bottom: 1px solid #ccc;
|
|
padding-bottom: 10px;
|
|
margin-bottom: 20px;
|
|
}}
|
|
|
|
.content {{
|
|
margin-bottom: 20px;
|
|
}}
|
|
|
|
.content p {{
|
|
margin: 10px 0;
|
|
}}
|
|
|
|
.link {{
|
|
color: #0066cc;
|
|
text-decoration: underline;
|
|
}}
|
|
|
|
.footer {{
|
|
border-top: 1px solid #ccc;
|
|
padding-top: 15px;
|
|
margin-top: 20px;
|
|
font-size: 12px;
|
|
color: #666;
|
|
}}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class='container'>
|
|
<div class='header'>
|
|
<strong>Nærværskonsulenterne ApS</strong>
|
|
</div>
|
|
|
|
<div class='content'>
|
|
<p>Hej {userName},</p>
|
|
|
|
<p>Vi håber du har det godt.</p>
|
|
|
|
<p>Vi gennemfører en kort undersøgelse om arbejdsmiljø og trivsel på arbejdspladsen. Din erfaring og feedback er meget værdifuld for os.</p>
|
|
|
|
<p>Undersøgelsen tager kun 3-5 minutter at besvare:</p>
|
|
|
|
<p><a href='{url}' class='link'>{url}</a></p>
|
|
|
|
<p><strong>Vigtigt:</strong> Undersøgelsen skal besvares inden {expiryDate} kl. {expiryTime}</p>
|
|
|
|
<p>Alle svar behandles fortroligt og anonymt.</p>
|
|
|
|
<p>Hvis du har spørgsmål, er du velkommen til at kontakte os på kontakt@nvkn.dk</p>
|
|
|
|
<p>På forhånd tak for din tid.</p>
|
|
|
|
<p>Med venlig hilsen,<br/>
|
|
Nærværskonsulenterne ApS</p>
|
|
</div>
|
|
|
|
<div class='footer'>
|
|
<p>Nærværskonsulenterne ApS<br/>
|
|
Brødemosevej 24A, 3300 Frederiksværk<br/>
|
|
kontakt@nvkn.dk</p>
|
|
|
|
<p><a href='mailto:kontakt@nvkn.dk?subject=Afmeld' style='color: #666;'>Klik her for at afmelde emails</a></p>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>";
|
|
}
|
|
|
|
|
|
|
|
[HttpGet]
|
|
public async Task<IActionResult> ViewResponse(int id) // Pass the response ID
|
|
{
|
|
var response = await _context.Responses
|
|
.Include(r => r.ResponseDetails)
|
|
.ThenInclude(rd => rd.ResponseAnswers)
|
|
.Include(r => r.ResponseDetails)
|
|
.ThenInclude(rd => rd.Question) // Include questions for detailed display
|
|
.ThenInclude(q => q.Answers) // Include all possible answers for each question
|
|
.FirstOrDefaultAsync(r => r.Id == id); // Find the response by ID
|
|
|
|
|
|
if (response == null)
|
|
{
|
|
return NotFound(); // If no response is found, return a NotFound result
|
|
}
|
|
|
|
return View(response); // Pass the response to the view
|
|
}
|
|
|
|
|
|
|
|
public string GenerateExpiryToken(DateTime expiryDate)
|
|
{
|
|
// Generate a unique token, for example, using a cryptographic library or a GUID
|
|
string token = Guid.NewGuid().ToString();
|
|
|
|
// Append the expiration date to the token (you might want to encrypt it for security)
|
|
string tokenWithExpiry = $"{token}|{expiryDate.ToString("yyyy-MM-ddTHH:mm:ssZ")}";
|
|
|
|
return tokenWithExpiry;
|
|
}
|
|
|
|
// Add these methods to your existing QuestionnaireController class
|
|
|
|
[HttpGet]
|
|
public IActionResult SetLogic(int id)
|
|
{
|
|
var questionnaire = _questionnaire.GetQuestionnaireWithQuestionAndAnswer(id);
|
|
|
|
if (questionnaire == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
var viewModel = new SetLogicViewModel
|
|
{
|
|
QuestionnaireId = questionnaire.Id,
|
|
QuestionnaireName = questionnaire.Title ?? "Untitled Survey",
|
|
Questions = questionnaire.Questions.Select((q, index) => new QuestionLogicViewModel
|
|
{
|
|
QuestionId = q.Id,
|
|
QuestionText = q.Text ?? "",
|
|
QuestionType = q.Type,
|
|
QuestionNumber = index + 1,
|
|
Answers = q.Answers.Select(a =>
|
|
{
|
|
var answerCondition = new AnswerConditionViewModel
|
|
{
|
|
AnswerId = a.Id,
|
|
AnswerText = a.Text ?? ""
|
|
};
|
|
|
|
// Parse existing condition if it exists
|
|
if (!string.IsNullOrEmpty(a.ConditionJson))
|
|
{
|
|
try
|
|
{
|
|
var condition = System.Text.Json.JsonSerializer.Deserialize<AnswerConditionViewModel>(a.ConditionJson);
|
|
if (condition != null)
|
|
{
|
|
answerCondition.ActionType = condition.ActionType;
|
|
answerCondition.TargetQuestionNumber = condition.TargetQuestionNumber;
|
|
answerCondition.SkipCount = condition.SkipCount;
|
|
answerCondition.EndMessage = condition.EndMessage;
|
|
}
|
|
}
|
|
catch (System.Text.Json.JsonException)
|
|
{
|
|
// If JSON is malformed, use default values
|
|
}
|
|
}
|
|
|
|
return answerCondition;
|
|
}).ToList()
|
|
}).ToList()
|
|
};
|
|
|
|
// Pass total question count for dropdown options
|
|
ViewBag.TotalQuestions = questionnaire.Questions.Count;
|
|
|
|
return View(viewModel);
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> SaveLogic(SaveConditionsViewModel model)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
TempData["Error"] = "Invalid data provided.";
|
|
return RedirectToAction(nameof(SetLogic), new { id = model.QuestionnaireId });
|
|
}
|
|
|
|
try
|
|
{
|
|
// Get questionnaire with answers
|
|
var questionnaire = _questionnaire.GetQuestionnaireWithQuestionAndAnswer(model.QuestionnaireId);
|
|
|
|
if (questionnaire == null)
|
|
{
|
|
TempData["Error"] = "Questionnaire not found.";
|
|
return RedirectToAction(nameof(Index));
|
|
}
|
|
|
|
// Update each answer's condition
|
|
foreach (var conditionUpdate in model.Conditions)
|
|
{
|
|
var answer = questionnaire.Questions
|
|
.SelectMany(q => q.Answers)
|
|
.FirstOrDefault(a => a.Id == conditionUpdate.AnswerId);
|
|
|
|
if (answer != null)
|
|
{
|
|
answer.ConditionJson = string.IsNullOrEmpty(conditionUpdate.ConditionJson)
|
|
? null
|
|
: conditionUpdate.ConditionJson;
|
|
}
|
|
}
|
|
|
|
// Save changes
|
|
await _questionnaire.Update(questionnaire);
|
|
await _questionnaire.commitAsync();
|
|
|
|
TempData["Success"] = "Conditional logic saved successfully!";
|
|
return RedirectToAction(nameof(SetLogic), new { id = model.QuestionnaireId });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
TempData["Error"] = "An error occurred while saving conditions: " + ex.Message;
|
|
return RedirectToAction(nameof(SetLogic), new { id = model.QuestionnaireId });
|
|
}
|
|
}
|
|
// Add these methods to your existing controller (the one with SetLogic and SaveLogic)
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> SaveAnswerCondition([FromBody] SaveAnswerConditionRequest request)
|
|
{
|
|
try
|
|
{
|
|
// Validate the request
|
|
if (request == null || request.AnswerId <= 0)
|
|
{
|
|
return Json(new { success = false, message = "Invalid answer ID provided." });
|
|
}
|
|
|
|
// Get the questionnaire with all related data
|
|
var questionnaire = _questionnaire.GetQuestionnaireWithQuestionAndAnswer(request.QuestionnaireId);
|
|
|
|
if (questionnaire == null)
|
|
{
|
|
return Json(new { success = false, message = "Questionnaire not found." });
|
|
}
|
|
|
|
// Find the specific answer
|
|
var answer = questionnaire.Questions
|
|
.SelectMany(q => q.Answers)
|
|
.FirstOrDefault(a => a.Id == request.AnswerId);
|
|
|
|
if (answer == null)
|
|
{
|
|
return Json(new { success = false, message = "Answer not found." });
|
|
}
|
|
|
|
// Validate and store the condition JSON
|
|
if (string.IsNullOrEmpty(request.ConditionJson))
|
|
{
|
|
// Clear the condition (set to continue)
|
|
answer.ConditionJson = null;
|
|
}
|
|
else
|
|
{
|
|
// Validate JSON format
|
|
try
|
|
{
|
|
var testParse = System.Text.Json.JsonSerializer.Deserialize<ConditionData>(request.ConditionJson);
|
|
answer.ConditionJson = request.ConditionJson;
|
|
}
|
|
catch (System.Text.Json.JsonException)
|
|
{
|
|
return Json(new { success = false, message = "Invalid condition data format." });
|
|
}
|
|
}
|
|
|
|
// Save changes using your repository pattern
|
|
await _questionnaire.Update(questionnaire);
|
|
await _questionnaire.commitAsync();
|
|
|
|
// Generate summary for response
|
|
string summary = GetConditionSummaryFromJson(answer.ConditionJson);
|
|
|
|
return Json(new
|
|
{
|
|
success = true,
|
|
message = "Answer condition saved successfully!",
|
|
summary = summary
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Log error if you have logging configured
|
|
// _logger?.LogError(ex, "Error saving answer condition for AnswerId: {AnswerId}", request.AnswerId);
|
|
return Json(new
|
|
{
|
|
success = false,
|
|
message = "An error occurred while saving the condition. Please try again."
|
|
});
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> ResetAnswerCondition([FromBody] ResetAnswerConditionRequest request)
|
|
{
|
|
try
|
|
{
|
|
if (request == null || request.AnswerId <= 0)
|
|
{
|
|
return Json(new { success = false, message = "Invalid answer ID provided." });
|
|
}
|
|
|
|
var questionnaire = _questionnaire.GetQuestionnaireWithQuestionAndAnswer(request.QuestionnaireId);
|
|
|
|
if (questionnaire == null)
|
|
{
|
|
return Json(new { success = false, message = "Questionnaire not found." });
|
|
}
|
|
|
|
var answer = questionnaire.Questions
|
|
.SelectMany(q => q.Answers)
|
|
.FirstOrDefault(a => a.Id == request.AnswerId);
|
|
|
|
if (answer == null)
|
|
{
|
|
return Json(new { success = false, message = "Answer not found." });
|
|
}
|
|
|
|
// Reset to default (Continue)
|
|
answer.ConditionJson = null;
|
|
|
|
await _questionnaire.Update(questionnaire);
|
|
await _questionnaire.commitAsync();
|
|
|
|
return Json(new
|
|
{
|
|
success = true,
|
|
message = "Answer condition reset successfully!"
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// _logger?.LogError(ex, "Error resetting answer condition for AnswerId: {AnswerId}", request.AnswerId);
|
|
return Json(new
|
|
{
|
|
success = false,
|
|
message = "An error occurred while resetting the condition. Please try again."
|
|
});
|
|
}
|
|
}
|
|
|
|
// Helper method to generate summary from JSON
|
|
private string GetConditionSummaryFromJson(string conditionJson)
|
|
{
|
|
if (string.IsNullOrEmpty(conditionJson))
|
|
{
|
|
return "Continue to the next question normally";
|
|
}
|
|
|
|
try
|
|
{
|
|
var condition = System.Text.Json.JsonSerializer.Deserialize<ConditionData>(conditionJson);
|
|
if (condition == null) return "Continue to the next question normally";
|
|
|
|
switch (condition.ActionType)
|
|
{
|
|
case 0: // Continue
|
|
return "Continue to the next question normally";
|
|
case 1: // SkipToQuestion
|
|
return condition.TargetQuestionNumber.HasValue
|
|
? $"Jump to Question {condition.TargetQuestionNumber}"
|
|
: "Jump to specific question";
|
|
case 2: // SkipCount
|
|
return $"Skip {condition.SkipCount ?? 1} question(s)";
|
|
case 3: // EndSurvey
|
|
return "End the survey immediately";
|
|
default:
|
|
return "Continue to the next question normally";
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
return "Continue to the next question normally";
|
|
}
|
|
}
|
|
|
|
// Request models - Add these classes to your project
|
|
public class SaveAnswerConditionRequest
|
|
{
|
|
public int AnswerId { get; set; }
|
|
public string ConditionJson { get; set; } = string.Empty;
|
|
public int QuestionnaireId { get; set; }
|
|
}
|
|
|
|
public class ResetAnswerConditionRequest
|
|
{
|
|
public int AnswerId { get; set; }
|
|
public int QuestionnaireId { get; set; }
|
|
}
|
|
|
|
public class ConditionData
|
|
{
|
|
public int ActionType { get; set; }
|
|
public int? TargetQuestionNumber { get; set; }
|
|
public int? SkipCount { get; set; }
|
|
public string? EndMessage { get; set; }
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|