public void Answer(GameClient client, int answerIndex) { var player = PlayerManager.GetPlayers().SingleOrDefault(plyr => plyr.GetIdentifier() == client.Character.Token); if (player.Answered == false) // if user haven't answer, then we allow the user to give his/her answer { player.Answered = true; var question = Questions.Peek(); Reward reward = RewardManager.CreateReward(player.GetIdentifier(), answerIndex == question.Answers.SingleOrDefault(answer => answer.IsCorrect == true)?.Id, player.Answered == false); player.Receive(reward); var questionAttempted = new QuestionAttempted() { AnswerId = answerIndex, QuestionId = question.Id, AttemptedOn = DateTime.Now, AttemptedBy = player.Client.Character.CharacterDb.AccountId, AttemptedFor = this.GetIdentifier() }; Attempts.Add(questionAttempted); } }
//Update UserQuizDetail table from the UserQuizResponse to use it in UserResult table. This is to make the code less coupled //so that even if the parameters of the UserQuizDetail has changed, we need not have to change everything. public UserQuizDetail UpdateUserQuizDetail(UserQuizResponse userQuizResponse) { UserQuizDetail userQuizDetail = new UserQuizDetail(); List <QuestionAttempted> questionsAttempted = new List <QuestionAttempted>(); string quizId = userQuizResponse.QuizId; int userId = userQuizResponse.UserId; string domainName = userQuizResponse.DomainName; DateTime time = DateTime.Now; List <Object> questionsList = userQuizResponse.QuestionsAttempted; Console.WriteLine(questionsList.Count + "COUNT"); int questionCount = 1; foreach (var item in questionsList) { QuestionAttempted question = new QuestionAttempted(); try { // Question item = x as Question; Object x = item; JObject Parseddetail = JObject.Parse(JsonConvert.SerializeObject(x)); string questionType = Parseddetail.GetValue("questionType").ToString(); Console.WriteLine("THIS IS THE TYP" + questionType); System.Reflection.Assembly b = System.Reflection.Assembly.Load("Potentiometer.Core"); Type QuestionType = b.GetType("Potentiometer.Core.QuestionTypes." + questionType); object instanceObjectOfQuestion = Activator.CreateInstance(QuestionType); JsonConvert.PopulateObject(JsonConvert.SerializeObject(x), instanceObjectOfQuestion); Console.WriteLine("THIS IS THE POPILATED QUESTION " + JsonConvert.SerializeObject(x)); string questionId = Parseddetail.GetValue("questionId").ToString(); Console.WriteLine("THIS IS THE QUESTION ID " + questionId); string questionText = Parseddetail.GetValue("questionText").ToString(); List <Result.Models.Options> options = JsonConvert.DeserializeObject <List <Result.Models.Options> >(Parseddetail.GetValue("options").ToString()); // Parseddetail.GetValue("options").ToString() ; string conceptTagsString = Parseddetail.GetValue("conceptTags").ToString(); int difficultyLevel = Convert.ToInt32(Parseddetail.GetValue("difficultyLevel").ToString()); //string userResponse = Parseddetail.GetValue("userResponse").ToString(); string raw = Parseddetail.GetValue("raw").ToString(); // string correctOption = Parseddetail.GetValue("correctOption").ToString(); string taxonomy = Parseddetail.GetValue("taxonomy").ToString(); taxonomy = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(taxonomy); Console.WriteLine(conceptTagsString.Substring(1, conceptTagsString.Length - 2).Split(',')); string[] arr = conceptTagsString.Substring(1, conceptTagsString.Length - 2).Split(','); string[] conceptTags = new string[arr.Length]; int index = 0; foreach (string s in arr) { string replacement = Regex.Replace(s, @"\t|\n|\r", ""); replacement = replacement.Replace('"', ' ').Trim(); conceptTags[index] = replacement; index++; } Console.WriteLine("---"); question.QuestionId = questionId; question.QuestionText = questionText; question.QuestionNumber = questionCount++; question.Raw = raw; question.DifficultyLevel = difficultyLevel; question.Taxonomy = taxonomy; question.ConceptTags = conceptTags; question.QuestionType = questionType; List <string> optionList = new List <string>(); foreach (var option in options) { optionList.Add(option.Raw); } question.Options = optionList; if (questionType == "MCQ") { string resp = JsonConvert.DeserializeObject <Result.Models.Options>(Parseddetail.GetValue("response").ToString()).Raw; string ans = JsonConvert.DeserializeObject <Result.Models.Options>(Parseddetail.GetValue("correctAnswer").ToString()).Raw; resp = resp.Trim(); ans = ans.Trim(); Console.WriteLine(resp + " " + resp.Length); Console.WriteLine(ans + " " + ans.Length); question.IsCorrect = (resp == ans); Console.WriteLine(question.IsCorrect); question.Response = resp; question.CorrectAns = ans; } else { List <Result.Models.Options> resp = JsonConvert.DeserializeObject <List <Result.Models.Options> >(Parseddetail.GetValue("response").ToString()); List <Result.Models.Options> ans = JsonConvert.DeserializeObject <List <Result.Models.Options> >(Parseddetail.GetValue("correctAnswer").ToString()); HashSet <string> h = new HashSet <string>(); HashSet <string> h1 = new HashSet <string>(); foreach (var itemm in resp) { h.Add(itemm.Raw); } foreach (var item1 in ans) { h1.Add(item1.Raw); } question.IsCorrect = h.SetEquals(h1) && h.Count == h1.Count; question.ResponseList = resp; question.CorrectAnsList = ans; } questionsAttempted.Add(question); } catch (Exception e) { Console.WriteLine("exception occured -------- " + e); } //string questionId = item.QuestionId; //string questionText = item.QuestionText; //List<Result.Models.Options> options = item.Options; //string questionType = item.QuestionType; //string domain = item.Domain; //string[] conceptTags = item.ConceptTags; //int difficultyLevel = item.DifficultyLevel; //string userResponse = item.Response.Raw; //string raw = item.Raw; //string correctOption = item.CorrectAnswer.Raw; //Boolean isCorrect = (userResponse==correctOption); //string taxonomy = item.Taxonomy; } userQuizDetail.QuizId = quizId; userQuizDetail.UserId = userId; userQuizDetail.Domain = domainName; userQuizDetail.Time = time; userQuizDetail.QuestionsAttempted = questionsAttempted; _context.UserQuizDetail.InsertOne(userQuizDetail); return(userQuizDetail); }
/// <summary> /// This is for Endpoint 3 as discussed on Discord /// </summary> /// <returns></returns> public QuizStat[] GetQuizStats() { Mutex.WaitOne(); List <Controller.Database.Tables.Quiz> quizzes = GetDbSet <Controller.Database.Tables.Quiz>() .Include(quiz => quiz.Questions) .ThenInclude(questionQuiz => questionQuiz.Question) .ThenInclude(question => question.Questions) .ThenInclude(questionAttempt => questionAttempt.Account) .Include(quiz => quiz.Questions) .ThenInclude(questionQuiz => questionQuiz.Question) .ThenInclude(question => question.Answers) .Include(quiz => quiz.Sessions) .ThenInclude(session => session.Account) .Where(quiz => quiz.IsCustom == 1 && quiz.Sessions.Count > 0).ToList(); // Process the data now QuizStat[] quizStats = new QuizStat[quizzes.Count]; for (int i = 0; i < quizStats.Length; i++) { Controller.Database.Tables.Quiz quiz = quizzes.ElementAt(i); Dictionary <string, Student> StudentStat = new Dictionary <string, Student>(); List <int> questionList = quiz.Questions.Select(quizQuestion => quizQuestion.QuestionId).ToList(); foreach (Session session in quiz.Sessions) { if (!StudentStat.ContainsKey(session.AccountId)) { StudentStat.Add(session.AccountId, new Student(questionList, session.Account)); } } List <QuestionStat> questionStats = new List <QuestionStat>(); List <string> sessions = quiz.Sessions.Select(x => x.Id).ToList(); var quizStat = new QuizStat(); quizStat.QuizName = quiz.Name; int questionCount = 0; foreach (QuestionQuiz questionQuiz in quiz.Questions) { QuestionStat questionStat = new QuestionStat(); List <QuestionAttempted> allAttempt = new List <QuestionAttempted>(); Controller.Database.Tables.Question questionEntity = questionQuiz.Question; questionStat.QuestionId = ++questionCount; questionStat.QuestionText = questionEntity.Text; var questionAttemptedForQuiz = questionEntity.Questions.Where(x => sessions.Contains(x.AttemptedFor)).ToList(); var individualAttempts = questionAttemptedForQuiz.GroupBy(x => x.AttemptedBy); foreach (var studentAttempt in individualAttempts) { QuestionAttempted qa = studentAttempt.Last(); allAttempt.Add(qa); StudentStat[qa.Account.Id].QuestionsAnswers[qa.QuestionId] = qa.AnswerId; if (qa.Answer.IsCorrect == 1) { StudentStat[qa.Account.Id].NumOfCorrect += 1; } } questionStat.CorrectAnswerCount = allAttempt.Where(attempt => attempt.Answer.IsCorrect == 1).Count(); questionStat.CorrectAnswer = questionEntity.Answers.Where(answer => answer.IsCorrect == 1).First().Text; List <Controller.Database.Tables.Answer> wrongAnswers = questionEntity.Answers.Where(answer => answer.IsCorrect == 0).OrderBy(answer => answer.Id).ToList(); questionStat.WrongAnswerOneCount = allAttempt.Where(attempt => attempt.AnswerId == wrongAnswers.ElementAt(0).Id).Count(); questionStat.WrongAnswerOne = wrongAnswers.ElementAt(0).Text; questionStat.WrongAnswerTwoCount = allAttempt.Where(attempt => attempt.AnswerId == wrongAnswers.ElementAt(1).Id).Count(); questionStat.WrongAnswerTwo = wrongAnswers.ElementAt(1).Text; questionStat.WrongAnswerThreeCount = allAttempt.Where(attempt => attempt.AnswerId == wrongAnswers.ElementAt(2).Id).Count(); questionStat.WrongAnswerThree = wrongAnswers.ElementAt(2).Text; questionStats.Add(questionStat); } quizStat.QuestionStats = questionStats.ToArray(); quizStat.StudentAttempted = StudentStat.Values.ToArray(); quizStats[i] = quizStat; } Mutex.ReleaseMutex(); return(quizStats); }