예제 #1
0
 public void SaveResponse(object answerId)
 {
     Question question = Quiz.CurrentQuestion;
     if (!Quiz.CurrentQuestion.Answers.Any(a => (int)_AssignmentQuizRepository.IdOfAnswer(a) == (int)answerId))
     {
         return;
     }
     Answer answer = Quiz.CurrentQuestion.Answers.Where(a => (int)_AssignmentQuizRepository.IdOfAnswer(a) == (int)answerId).Single();
     QuestionResponse response = new QuestionResponse(DateTime.Now, answer);
     Quiz.CurrentQuestion.Responses.Add(response);
     if (IsFinished)
     {
         Quiz.Assignment.Completed = true;
     }
     _AssignmentQuizRepository[AssignmentId] = Quiz;
     ShowIncorrectMessage = !response.Answer.IsCorrect;
     if (Quiz.NextQuestion != null)
     {
         SaveCurrentTestTakingStep(Quiz.NextQuestion.Order);
     }
 }
예제 #2
0
        new public AssignmentQuiz this[object assignmentId]
        {
            get
            {
                Assignment assignment = base[assignmentId];

                string quizQuery = @"
                    SELECT	Quiz.[QuizTopic], Quiz.[NumCorrectRequired], QuestionID, Text, PresentationOrder
                    FROM	[dbo].[Question]
                    JOIN	[dbo].[QuizDefinition] AS Quiz
	                    ON	Quiz.[QuizDefinitionID] = Question.[QuizDefinitionID]
                    JOIN	[dbo].[EmployeeQuizAssignments] AS Assignment
	                    ON	Assignment.[QuizDefinitionID] = Question.[QuizDefinitionID]
                    WHERE	Assignment.[Id] = @Id
                    ";

                KeyValuePair<IList<object>, IList<Question>> questions =
                    DBHelper.ExecuteReader<KeyValuePair<IList<object>, IList<Question>>>(new SqlConnection(DBHelper.ConnectionString), new SqlCommand(quizQuery), LoadQuestions,
                        new SqlParameter() { ParameterName = "@Id", DbType = DbType.Int64, Value = assignmentId });

                string responseQuery = @"
                    SELECT	[ResponseID], [AnswerID], [QuestionID], [ResponseDateTime]
                    FROM	[dbo].[Response]
                    WHERE	[EmployeeQuizAssignmentId] = @Id
                    ";

                List<QuestionResponse> questionResponses = new List<QuestionResponse>();
                questionResponses.AddRange(
                    DBHelper.ExecuteReader<IList<QuestionResponse>>(new SqlConnection(DBHelper.ConnectionString), new SqlCommand(responseQuery),
                     delegate(IDataReader reader)
                     {
                         List<QuestionResponse> responses = new List<QuestionResponse>();
                         while (reader.Read())
                         {
                             Answer answer = _AnswersById[reader.GetInt32(reader.GetOrdinal("AnswerID"))];
                             QuestionResponse response = new QuestionResponse(
                                 reader.GetDateTime(reader.GetOrdinal("ResponseDateTime")),
                                 answer);
                             responses.Add(response);
                             _IdsByHashCode.Add(response.GetHashCode(), reader.GetInt32(reader.GetOrdinal("QuestionID")));
                         }
                         return responses;
                     },
                     new SqlParameter() { ParameterName = "@Id", DbType = DbType.Int64, Value = assignmentId }));

                foreach (Question question in questions.Value)
                {
                    object idOfQuestion = _IdsByHashCode[question.GetHashCode()];
                    //Get responses that match it.
                    List<QuestionResponse> matchingResponses = questionResponses.Where(q => (int)_IdsByHashCode[q.GetHashCode()] == (int)idOfQuestion).ToList();
                    foreach (QuestionResponse response in matchingResponses)
                    {
                        question.Responses.Add(response);
                    }
                }

                AssignmentQuiz quiz = new AssignmentQuiz(questions.Key[0].ToString(), (int)questions.Key[1], assignment, questions.Value);

                return quiz;
            }
            set
            {
                base[assignmentId] = value.Assignment;

                string insertResponse = @"
                    INSERT INTO [dbo].[Response] ([EmployeeQuizAssignmentId], [QuestionID], [AnswerID], [Correct], [ResponseDateTime])
                    VALUES (@Id, @QuestionId, @AnswerId, @Correct, @ResponseDateTime)
                    ";

                if (value.Questions != null)
                {
                    foreach (Question question in value.Questions)
                    {
                        if (question.Responses != null)
                        {
                            foreach (QuestionResponse response in question.Responses)
                            {
                                if (!_IdsByHashCode.ContainsKey(response.GetHashCode()))
                                {
                                    object responseId =
                                        DBHelper.ExecuteNonQuery<object>(
                                            new SqlConnection(DBHelper.ConnectionString), new SqlCommand(insertResponse),
                                                delegate(int rowsAffected, IDbConnection connection)
                                                {
                                                    if (rowsAffected == 1)
                                                    {
                                                        string identityQuery = @"SELECT SCOPE_IDENTITY()";
                                                        return
                                                            DBHelper.ExecuteReader<object>(
                                                                connection, new SqlCommand(identityQuery),
                                                                delegate(IDataReader reader)
                                                                {
                                                                    reader.Read();
                                                                    return reader[0];
                                                                });
                                                    }
                                                    return null;
                                                },
                                                new SqlParameter() { ParameterName = "@Id", DbType = DbType.Int64, Value = _IdsByHashCode[value.Assignment.GetHashCode()] },
                                                new SqlParameter() { ParameterName = "@QuestionId", DbType = DbType.Int32, Value = _IdsByHashCode[question.GetHashCode()] },
                                                new SqlParameter() { ParameterName = "@AnswerId", DbType = DbType.Int32, Value = _IdsByHashCode[response.Answer.GetHashCode()] },
                                                new SqlParameter() { ParameterName = "@Correct", DbType = DbType.Boolean, Value = response.Answer.IsCorrect },
                                                new SqlParameter() { ParameterName = "@ResponseDateTime", DbType = DbType.DateTime, Value = response.Responded });

                                    if (responseId != null)
                                    {
                                        _IdsByHashCode.Add(response.GetHashCode(), responseId);
                                    }
                                } //if
                            } //foreach
                        } //if
                    } //foreach
                } //if
            }
        }