public async Task <ResponseModel> UnlockContent(int currentContentNo, int levelNo, Guid traineeId) { int serialNumber = currentContentNo + 1; /* * is the content is allowed to be unlocked by this user? * did the user finished the previous content/quiz? skip if the previous is 0 * insert new history entry */ Level currentLevel = await Db.Levels.FirstOrDefaultAsync(x => x.No == levelNo); Content nextContent = await Db.Contents.FirstOrDefaultAsync(x => x.No == serialNumber && x.LevelId == currentLevel.Id); ResponseModel response = await validationService.IsValidRequestAsync(nextContent.Id, traineeId); if (response.IsSuccess) { if (currentContentNo > 0) { Content currentContent = await Db.Contents.FirstOrDefaultAsync(x => x.No == currentContentNo && x.LevelId == currentLevel.Id); TraineeQuizHistory prevHistory = await Db.TraineeQuizHistories.FirstOrDefaultAsync( x => x.ContentId == currentContent.Id && x.TraineeId == traineeId); if (!prevHistory.IsCompleted) { response = new ResponseModel(null, false, MessageContainer.PreviousContentIsUncompleted); } } if (response.IsSuccess) { var history = new TraineeHistory() { ContentId = nextContent.Id, TraineeId = traineeId, LastAccessed = DateTime.Now, Unlocked = DateTime.Now, Point = 0, }; Db.TraineeHistories.Add(history); await Db.SaveChangesAsync(); response = new ResponseModel(new TraineeHistory() { Id = history.Id, ContentId = history.ContentId, TraineeId = history.TraineeId }); } } return(response); }
public QuizViewModel(TraineeQuizHistory history) { // this.Content = new ContentViewModel(history.Content); this.Id = history.Id; ContentId = history.ContentId; this.IsCompleted = history.IsCompleted; Quiz quiz = history.Quiz; this.Count = quiz.QuizQuestions.Count(x => x.IsActive); this.Name = quiz.Name; this.Questions = quiz.QuizQuestions.Where(x => x.IsActive).OrderBy(x => x.SerialNo).Select(x => new QuestionViewModel(x)).ToList(); }
public async Task <bool> SubmitAnswer(AnswerViewModel answer, Guid traineeId) { bool allCorrect = true; foreach (var model in answer.Answers) { Question dbQuestion = await Db.Questions.FindAsync(model.QuestionId); Option option = dbQuestion.Options.First(x => x.IsAnswer); if (option.Name != model.AnswerText) { allCorrect = false; break; } } TraineeQuizHistory history = await Db.TraineeQuizHistories.FindAsync(answer.QuizHistoryId); history.LastAccessed = DateTime.Now; if (allCorrect) { history.IsCompleted = true; history.Point = answer.Answers.Count; Trainee trainee = await Db.Trainees.FindAsync(traineeId); trainee.Point += history.Point; // save this answer history to the table for future reference foreach (Answer a in answer.Answers) { TraineeQuizAnswerHistory answerHistory = new TraineeQuizAnswerHistory() { TraineeId = traineeId, QuizHistoryId = answer.QuizHistoryId, QuestionId = a.QuestionId, Answer = a.AnswerText, IsCorrect = true, IsVerified = true, AnswerDate = DateTime.Now }; Db.TraineeQuizAnswerHistories.Add(answerHistory); } } await Db.SaveChangesAsync(); return(allCorrect); }
public QuizViewModel GetQuizDetailForTest(Guid contentId, Guid traineeId) { TraineeQuizHistory history = Db.TraineeQuizHistories.FirstOrDefault( x => x.ContentId == contentId && x.TraineeId == traineeId); if (history == null) { history = new TraineeQuizHistory() { ContentId = contentId, TraineeId = traineeId, IsCompleted = false, LastAccessed = DateTime.Now, Appeared = DateTime.Now, CorrectAnswer = 0, Point = 0, QuizId = Db.Contents.Find(contentId).Quizs.First().Id }; Db.TraineeQuizHistories.Add(history); Db.SaveChanges(); } return(new QuizViewModel(history)); }
public async Task <bool> SubmitAssignment(AnswerViewModel answer, Guid traineeId) { foreach (Answer a in answer.Answers) { TraineeQuizAnswerHistory answerHistory = new TraineeQuizAnswerHistory() { TraineeId = traineeId, QuizHistoryId = answer.QuizHistoryId, QuestionId = a.QuestionId, Answer = a.AnswerText, IsCorrect = false, IsVerified = false, AnswerDate = DateTime.Now }; Db.TraineeQuizAnswerHistories.Add(answerHistory); } TraineeQuizHistory history = await Db.TraineeQuizHistories.FindAsync(answer.QuizHistoryId); history.LastAccessed = DateTime.Now; history.IsCompleted = true; int i = await Db.SaveChangesAsync(); return(true); }