public IEnumerable <Question> SearchByTestId(int testId, string keyword, int currentPage = 0, int take = 50, bool mappedQuestionOnly = false, bool withAnswerOption = false, bool withCorrectAnswerOption = false) { int skip = Convert.ToInt32(currentPage * take); const string query = @"SELECT DISTINCT [Q].*,[QC].QuestionCategoryName, [TWQ].TestId, ISNULL(COUNT(*) OVER(),0) AS TotalRecordCount FROM [dbo].[Question] [Q] LEFT JOIN [dbo].[QuestionCategory] [QC] ON [Q].QuestionCategoryId = [QC].QuestionCategoryId LEFT JOIN (SELECT TWQ.QuestionId,TWQ.TestId FROM [dbo].[TestWiseQuestion] [TWQ] WHERE [TWQ].[TestId] =@TestId )TWQ ON TWQ.QuestionId = Q.QuestionId WHERE 1 = 1 AND (@TestId = 0 OR @MappedOnly = 0 OR [TWQ].TestId = (CASE WHEN @MappedOnly = 1 THEN @TestId ELSE [TWQ].TestId END)) AND (LOWER([Q].[QuestionText]) LIKE '%' + LOWER(@Keyword) + '%' OR LOWER([Q].[Tags]) LIKE '%' + LOWER(@Keyword) + '%' OR LOWER([Q].[Marks]) LIKE '%' + LOWER(@Keyword) + '%' OR LOWER([Q].[DifficultyLevel]) LIKE '%' + LOWER(@Keyword) + '%' OR LOWER([QC].[QuestionCategoryName]) LIKE '%' + LOWER(@Keyword) + '%' OR LOWER([Q].[AnswerExplanation]) LIKE '%' + LOWER(@Keyword) + '%') ORDER BY [Q].[QuestionId] DESC OFFSET @Skip ROWS FETCH NEXT @Take ROWS ONLY"; var questionList = _dbContext.SqlConnection.Query <Question>(query, new { TestId = testId, Keyword = keyword, Skip = skip, Take = take, MappedOnly = mappedQuestionOnly?1 : 0 }).ToList(); if (withAnswerOption) { if (questionList.Any()) { for (int i = 0; i < questionList.Count(); i++) { var answerOption = _iQuestionAnswerOptionRepository.GetByQuestionId(questionList.ElementAt(i).QuestionId, withCorrectAnswerOption); questionList.ElementAt(i).QuestionAnswerOptionList = answerOption; } } } return(questionList); }
public Message FinishTest(TestTaken testTaken) { Message message; try { using (var scope = new TransactionScope()) { _dbContext.SqlConnection.Open(); if (testTaken.TakenId == 0) { return(SetMessage.SetErrorMessage("Taken Information not found. System unable to process your test result without taken id.")); } var isExist = _iTestTakenRepository.Get(testTaken); var affectedRow = 0; isExist.EndTime = DateTime.UtcNow; var testInformation = _iTestRepository.Get(new Test { TestId = testTaken.TestId }); var correctAnswer = 0; foreach (var details in testTaken.TestTakenDetails) { details.TakenId = isExist.TakenId; details.TestId = isExist.TestId; var correctAnswerOption = String.Join(",", _iQuestionAnswerOptionRepository.GetByQuestionId(details.QuestionId, true).Where(c => c.IsCorrectAnswer).Select(a => a.AnswerOptionId)); var givenAnswerOption = String.Join(",", testTaken.TestTakenDetails.Where(c => c.QuestionId == details.QuestionId).Select(a => a.AnswerOptionId)); if (correctAnswerOption == givenAnswerOption) { details.IsCorrectAnswer = true; correctAnswer++; } _iTestTakenDetailsRepository.InsertWithoutIdentity(details); } var totalQuestion = testInformation.NoOfQuestion; var score = Convert.ToDecimal((correctAnswer * 100.0 / totalQuestion)); isExist.Score = score; affectedRow = _iTestTakenRepository.Update(isExist); var user = _iUserRepository.Get(new User { UserId = isExist.UserId }); //if (user != null && user.RaasForceUserId != null && user.RaasForceUserId > 0) //{ // var restClient = new RestClient(ConfigurationManager.AppSettings["ApiUrl"]); // var request = new RestRequest("api/iTestApp/InsertOrUpdateTestResult?param1=" + user.RaasForceUserId + "¶m2=" + testTaken.TestId + "¶m3=" + testTaken.TakenId + "¶m4=" + score.ToString("#"), Method.GET) { RequestFormat = DataFormat.Json }; // var result = restClient.Execute<HttpResponseMessage>(request); //} message = affectedRow > 0 ? SetMessage.SetSuccessMessage("Information has been saved successfully.") : SetMessage.SetInformationMessage("No data has been saved."); scope.Complete(); } } catch (Exception exception) { return(SetMessage.SetErrorMessage(exception.Message)); } finally { _dbContext.SqlConnection.Close(); } return(message); }