public ActionResult StartTest(int id) { using (var db = new UsersContext()) { var test = db.Tests.FirstOrDefault(t => t.TestId == id); if (test == null) {return RedirectToAction("RolePermissions", "Home");} var userId = WebSecurity.GetUserId(User.Identity.Name); var actionInProgress = db.Actions.FirstOrDefault(a => a.User.UserId == userId && a.Status == 1); if (actionInProgress != null || test.Questions.Count == 0) { return RedirectToAction("Tests", "Test"); } var action=new Action { User = db.UserProfiles.FirstOrDefault(u => u.UserId == userId), Test = test, Status = 1, TestsInProgres = new List<TestInProgres>() }; var listQuestion = test.Questions.OrderBy(q => q.Number).ToList(); foreach (var question in listQuestion) { action.TestsInProgres.Add(new TestInProgres { Action = action, Question = question, Submitted = false }); } action.DateStart = DateTime.Now; db.Actions.Add(action); db.SaveChanges(); } return RedirectToAction("TestInAction", "Test", new {number = 1}); }
public ActionResult FileUpload() { var userId = WebSecurity.GetUserId(User.Identity.Name); var directory = Server.MapPath("~/TestFiles/User="******"text/xml") { var fileName = Path.GetFileName(file.FileName); var path = Path.Combine(directory, fileName); file.SaveAs(path); using (var db = new UsersContext()) { try { var test = XmlToTestParser.ParseFromXml(path); test.User = db.UserProfiles.FirstOrDefault(u => u.UserId == userId); test.PathFile = path; db.Tests.Add(test); db.SaveChanges(); } catch (Exception) { return RedirectToAction("TeacherSpace", "Teacher"); } } } } return RedirectToAction("TeacherSpace", "Teacher"); }
private void SaveResultOfRadioQuestion(QuestionInProgresWrapper result, TestInProgres currentQuestion, UsersContext db) { var userAnswer = ""; for (var index = 0; index < ((QuestionWithVariants)currentQuestion.Question).VariantAnswers.Count; index++) { int number; if (Int32.TryParse(result.SelectedAnswer, out number)) { userAnswer += (number == index) ? "1" : "0"; } else { userAnswer += "0"; } } currentQuestion.UserAnswer = userAnswer; currentQuestion.Submitted = true; db.SaveChanges(); }
private void SaveResultOfCheckedQuestion(QuestionInProgresWrapper result, TestInProgres currentQuestion, UsersContext db) { var userAnswer = result.AnswerVariantList.Aggregate("", (current, res) => current + (res.Selected ? "1" : "0")); currentQuestion.UserAnswer = userAnswer; currentQuestion.Submitted = true; db.SaveChanges(); }
private void RemoveFromProgres(Action action, UsersContext db) { db.Actions.Attach(action); var testsList= action.TestsInProgres.ToList(); foreach (var testInProgres in testsList) { action.TestsInProgres.Remove(testInProgres); db.TestsInProgres.Remove(testInProgres); } db.SaveChanges(); }
private void CopyToHistory(Action action, UsersContext db) { foreach (var test in action.TestsInProgres) { var testToHistory = new TestHistory { Action = action, Number = test.Question.Number, Question = test.Question, UserAnswer = test.UserAnswer }; db.TestHistory.Add(testToHistory); db.SaveChanges(); } }
private void CalculateResult(int userId, Action action, UsersContext db) { var testResult = new TestResults { User = db.UserProfiles.FirstOrDefault(u => u.UserId == userId), Action = action, Test = action.Test, DatePassing = action.DatePassing }; double pointSum = 0; foreach (var test in action.TestsInProgres) { if (test.UserAnswer != null) { switch (test.Question.TestType) { case DataConst.CheckedType: { pointSum += (CalculateChecked(test)); break; } case DataConst.RadioType: { pointSum += (CalculateRadio(test)); break; } } } } testResult.PercentTaken = Convert.ToInt32((pointSum/action.TestsInProgres.Count)*100); testResult.Passed = testResult.PercentTaken >= DataConst.PercentLimit; db.TestsResults.Add(testResult); db.SaveChanges(); }
private void SubbmitTest(int userId, TestInProgres currentQuestion, Action action, UsersContext db) { currentQuestion.Submitted = true; action.DatePassing = DateTime.Now; action.Status = 2; db.SaveChanges(); CalculateResult(userId, action, db); CopyToHistory(action, db); RemoveFromProgres(action, db); }
public ActionResult DeleteTest(int id) { try { using (var db = new UsersContext()) { var test = db.Tests.FirstOrDefault(t => t.TestId == id); db.Tests.Attach(test); var listQuestion = test.Questions.ToList(); foreach (var question in listQuestion) { if (question is QuestionWithVariants) { var listVariant = ((QuestionWithVariants) question).VariantAnswers.ToList(); foreach (var variantAnswer in listVariant) { db.VariantAnswers.Remove(variantAnswer); } db.QuestionWithVariants.Remove((QuestionWithVariants) question); } else { db.Questions.Remove(question); } } db.Tests.Remove(test); db.SaveChanges(); } return RedirectToAction("TeacherSpace", "Teacher"); } catch (Exception) { return RedirectToAction("TeacherSpace", "Teacher"); } }
public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl) { string provider = null; string providerUserId = null; if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId)) { return RedirectToAction("Manage"); } if (ModelState.IsValid) { // Insert a new user into the database using (UsersContext db = new UsersContext()) { UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower()); // Check if user already exists if (user == null) { // Insert name into the profile table db.UserProfiles.Add(new UserProfile { UserName = model.UserName }); db.SaveChanges(); OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName); OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false); return RedirectToLocal(returnUrl); } else { ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name."); } } } ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName; ViewBag.ReturnUrl = returnUrl; return View(model); }