Ejemplo n.º 1
0
        public JsonResult GetInformationGroup(int examId, int questionId)
        {
            string functionName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            try
            {
                //Check id exam exist in the database
                if (!_examRepository.ExamExist(examId))
                {
                    Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(Constants.groupNotFound));
                    return(Json(MessageResult.GetMessage(MessageType.GROUP_NOT_FOUND)));
                }

                if (!ModelState.IsValid)
                {
                    Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(Constants.notFound));
                    return(Json(MessageResult.GetMessage(MessageType.NOT_FOUND)));
                }
                QuestionEntity question = null;
                //This is get all information of the exam by examId
                List <ExamQuestionEntity> examQuestionEntity = _examQuestionRepository.getListQuestions(examId);

                QuestionListResult questionListResult = new QuestionListResult();
                foreach (var examQuestion in examQuestionEntity)
                {
                    if (examQuestion.QuestionId == questionId)
                    {
                        question = _questionRepository.getQuestionInformation(questionId);
                        break;
                    }
                }

                questionListResult.questionId     = question.QuestionId;
                questionListResult.questionNumber = question.QuestionNumber;
                questionListResult.part           = question.Part;
                questionListResult.image          = question.Image;
                questionListResult.fileMp3        = question.FileMp3;
                questionListResult.questionName   = question.QuestionName;
                questionListResult.A             = question.A;
                questionListResult.B             = question.B;
                questionListResult.C             = question.C;
                questionListResult.D             = question.D;
                questionListResult.correctAnswer = question.CorrectAnswer;
                questionListResult.team          = question.Team;

                return(Json(questionListResult));
            }
            catch (Exception ex)
            {
                Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(ex.Message));
                return(Json(MessageResult.ShowServerError(ex.Message)));
            }
        }
Ejemplo n.º 2
0
        public JsonResult GetListQuestion(int accountId, int examId, int page)
        {
            string functionName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            try
            {
                if (!_accountRepository.AccountExists(accountId))
                {
                    Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(Constants.accountNotFound));
                    return(Json(MessageResult.GetMessage(MessageType.ACCOUNT_NOT_FOUND)));
                }

                //Check id exam exist in the database
                if (!_examRepository.ExamExist(examId))
                {
                    Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(Constants.groupNotFound));
                    return(Json(MessageResult.GetMessage(MessageType.GROUP_NOT_FOUND)));
                }

                if (!ModelState.IsValid)
                {
                    Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(Constants.notFound));
                    return(Json(MessageResult.GetMessage(MessageType.NOT_FOUND)));
                }

                //This is get all questions of the exam by id exam
                List <ExamQuestionEntity> examQuestionEntity = _examQuestionRepository.getListQuestions(examId);
                List <AnswerUserEntity>   answerUsers        = _answerUserRepository.GetAnswerUserEntities(accountId);

                List <QuestionEntity> listQuestionEntities = new List <QuestionEntity>();
                foreach (var examQuestion in examQuestionEntity)
                {
                    // Get all informations of the question by questionId and save it in the list
                    QuestionEntity questionEntity = _questionRepository.getQuestionInformation(examQuestion.QuestionId);
                    listQuestionEntities.Add(questionEntity);
                }

                List <QuestionListResult> questionLists = new List <QuestionListResult>();
                foreach (var item in listQuestionEntities)
                {
                    QuestionListResult q = new QuestionListResult();
                    q.questionId    = item.QuestionId;
                    q.part          = item.Part;
                    q.image         = item.Image;
                    q.fileMp3       = item.FileMp3;
                    q.questionName  = item.QuestionName;
                    q.A             = item.A;
                    q.B             = item.B;
                    q.C             = item.C;
                    q.D             = item.D;
                    q.correctAnswer = item.CorrectAnswer;
                    q.team          = item.Team;
                    foreach (var answer in answerUsers)
                    {
                        if (q.questionId == answer.QuestionId)
                        {
                            q.answerUser = answer.AnswerKey;
                        }
                    }
                    questionLists.Add(q);
                }

                List <QuestionListResult> pagging = Pagging.GetQuestions(page, questionLists);

                return(Json(pagging));
            }
            catch (Exception ex)
            {
                Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(ex.Message));
                return(Json(MessageResult.ShowServerError(ex.Message)));
            }
        }
Ejemplo n.º 3
0
        public JsonResult GetListQuestionByPart(int examId, string part, int accountId)
        {
            string functionName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            try
            {
                if (!_examRepository.ExamExist(examId))
                {
                    Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(Constants.groupNotFound));
                    return(Json(MessageResult.GetMessage(MessageType.GROUP_NOT_FOUND)));
                }

                if (!ModelState.IsValid)
                {
                    Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(Constants.notFound));
                    return(Json(MessageResult.GetMessage(MessageType.NOT_FOUND)));
                }

                //This is get all questions of the exam by id exam
                List <ExamQuestionEntity> examQuestionEntity = _examQuestionRepository.getListQuestions(examId);

                List <QuestionEntity> listQuestionEntities = new List <QuestionEntity>();
                foreach (var examQuestion in examQuestionEntity)
                {
                    // Get all informations of the question by questionId and save it in the list
                    QuestionEntity questionEntity = _questionRepository.getQuestionInformation(examQuestion.QuestionId);
                    listQuestionEntities.Add(questionEntity);
                }

                List <QuestionListResult> questionLists      = new List <QuestionListResult>();
                List <AnswerUserEntity>   answerUserEntities = _answerUserRepository.GetAnswerUserEntities(accountId);

                List <AnswerUserResult> answerUserResults = new List <AnswerUserResult>();
                foreach (var examQuestion in listQuestionEntities)
                {
                    QuestionListResult q = new QuestionListResult();
                    foreach (var item in answerUserEntities)
                    {
                        if (item.QuestionId == examQuestion.QuestionId)
                        {
                            q.answerUser = item.AnswerKey;
                            break;
                        }
                    }
                    if (examQuestion.Part.Equals(part))
                    {
                        q.questionId     = examQuestion.QuestionId;
                        q.questionNumber = examQuestion.QuestionNumber;
                        q.part           = examQuestion.Part;
                        q.image          = examQuestion.Image;
                        q.fileMp3        = examQuestion.FileMp3;
                        q.questionName   = examQuestion.QuestionName;
                        q.A             = examQuestion.A;
                        q.B             = examQuestion.B;
                        q.C             = examQuestion.C;
                        q.D             = examQuestion.D;
                        q.correctAnswer = examQuestion.CorrectAnswer;
                        q.team          = examQuestion.Team;

                        questionLists.Add(q);
                    }
                }

                return(Json(questionLists.OrderBy(q => q.questionNumber)));
            }
            catch (Exception ex)
            {
                Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(ex.Message));
                return(Json(MessageResult.ShowServerError(ex.Message)));
            }
        }
        /// <summary>
        /// GET: /participants/questionlistresult/[participantId]/[questionlistId]
        ///  Shows all data for a specific participant and questionlist
        /// </summary>
        /// <param name="participantId">id of the participant to use in retrieving data</param>
        /// <param name="questionlistId">id of the questionlist to get data from</param>
        /// <returns>Returns a view with viewbags containing all data needed to display data on page</returns>
        public ActionResult QuestionlistResult(int participantId, int questionlistId)
        {
            //Get Participant model by id
            Participant participant = db.Participants.Find(participantId);

            //Get QuestionList model by id
            QuestionList qlist = db.QuestionLists.Find(questionlistId);

            ViewBag.questionListName = qlist.questionListName;

            //Query for finding the amount of questions in questionlist
            string getNumOfQuestions = "SELECT COUNT(Question_idQuestion) as numQuestions FROM QuestionList_Question WHERE QuestionList_idQuestionList =" + qlist.idQuestionList;
            int    numOfQuestions    = db.Database.SqlQuery <int>(getNumOfQuestions).Single();

            //Set the number of questions in a viewbag to use in the view
            ViewBag.numOfQuestions = numOfQuestions;

            //Query for finding questions from specific QuestionList
            string                     getQuestions  = "SELECT * FROM Question JOIN QuestionList_Question Q2 on Question.idQuestion = Q2.Question_idQuestion WHERE Q2.QuestionList_idQuestionList = " + qlist.idQuestionList;
            List <Question>            questionslist = db.Database.SqlQuery <Question>(getQuestions).ToList();
            Dictionary <int, Question> list          = new Dictionary <int, Question>();

            ViewBag.questions = questionslist;


            List <QuestionListResult> results        = new List <QuestionListResult>();
            StringBuilder             sqlQueryString = new StringBuilder();

            sqlQueryString.Append("select q.idQuestion, q.questionText,ao.answerText,ao.correctAnswer, qr.attempt, datediff(ms, qr.startTime, qr.endTime) as totalTime from QuestionResult as qr" +
                                  " join AnswerOption as ao on qr.AnswerOption_idAnswer = ao.idAnswer" +
                                  " join Question as q on q.idQuestion = ao.Question_idQuestion" +
                                  " where qr.QuestionList_idQuestionList = " + questionlistId +
                                  " and qr.Participant_idParticipant = " + participantId);
            results = db.Database.SqlQuery <QuestionListResult>(sqlQueryString.ToString()).OrderBy(qr => qr.idQuestion).OrderBy(qr => qr.attempt).ToList();

            var questions = results.Select(r => r.idQuestion).Distinct().ToList();
            List <Dictionary <int, QuestionListResult> > sortedList = new List <Dictionary <int, QuestionListResult> >();

            //Create a list with all unique attempts
            List <int> attempts = results.Select(r => r.attempt).Distinct().ToList();

            List <int> correctAnswers = new List <int>();

            //Get all correct answer for each attempt made by a participant
            foreach (int attempt in attempts)
            {
                int correctPerAttempt = results.Where(r => r.attempt == attempt && r.correctAnswer == 1).Count();
                correctAnswers.Add(correctPerAttempt);
            }
            ViewBag.correctAnswers = correctAnswers;


            ViewBag.attempts = attempts;
            if (!attempts.Count().Equals(0))
            {
                ViewBag.attemptCount = attempts.Last();
            }
            else
            {
                ViewBag.attemptCount = 0;
            }

            foreach (var question in questions)
            {
                List <QuestionListResult>            questionListResults = results.Where(qr => qr.idQuestion.Equals(question)).ToList();
                Dictionary <int, QuestionListResult> dict = new Dictionary <int, QuestionListResult>();

                // Check if the question has a result for each attempt, if so add it to an dictionary
                foreach (int attempt in attempts)
                {
                    if (!questionListResults.Where(qr => qr.attempt == attempt).Count().Equals(0))
                    {
                        QuestionListResult attemptQuestion = questionListResults.Where(qr => qr.attempt == attempt).Single();
                        if (attemptQuestion != null)
                        {
                            dict.Add(attempt, attemptQuestion);
                        }
                    }
                }

                sortedList.Add(dict);
            }
            ViewBag.results = sortedList;


            return(View(participant));
        }