Example #1
0
        public LastAnswerSheetResult GetLastAnswerSheet(string sessionToken)
        {
            var result = new LastAnswerSheetResult();
            var userId = _checkSessionToken(sessionToken, result);

            if (userId.HasValue)
            {
                var answerSheets = mContext.AnswerSheet
                                   .Where(a => a.IsClosed != true && a.UserId == userId)
                                   .OrderByDescending(a => a.Id)
                                   .ToArray();

                Models.AnswerSheet lastAnswerSheet = null;
                Models.Exam        lastExam        = null;

                foreach (var item in answerSheets)
                {
                    var exam = mContext.Exam.FirstOrDefault(e => e.Id == item.ExamId);
                    if (exam != null)
                    {
                        var deadline = item.CreatedAt.AddMinutes(exam.Time);
                        if (deadline < DateTime.Now && lastAnswerSheet == null)
                        {
                            lastAnswerSheet = item;
                            lastExam        = exam;
                        }
                        else
                        {
                            item.IsClosed = true;
                        }
                    }
                }

                mContext.SaveChanges();

                result.AnswerSheet = lastAnswerSheet;
                result.Exam        = lastExam;
            }

            return(result);
        }
Example #2
0
        public BasicResult MakeExam(string sessionToken, Models.Exam exam, int numberOf)
        {
            var result = new BasicResult();
            var userId = _checkSessionToken(sessionToken, result);

            if (userId.HasValue)
            {
                var quizzesId = mContext.Quizzes
                                .Where(q => q.Subject.Equals(exam.Subject))
                                .Select(q => q.Id)
                                .ToArray();
                var count = quizzesId.Length;
                if (count > numberOf)
                {
                    var idies  = new long[numberOf];
                    var random = new Random();
                    while (numberOf > 0)
                    {
                        var idx = quizzesId[random.Next(0, count)];
                        if (idies.Contains(idx) == false)
                        {
                            idies[--numberOf] = idx;
                        }
                    }

                    exam.Quizzes = string.Join(",", idies.Select(x => x.ToString()).ToArray());

                    mContext.Exam.Add(exam);
                    mContext.SaveChanges();
                }
                else
                {
                    result.MessageError = string.Format("Số lượng câu hỏi lớn hơn số lượng câu hỏi trong CSDL.");
                }
            }

            return(result);
        }