Esempio n. 1
0
        // to add questions to a new exam
        public async Task <bool> AddExamQuestions(Exam exam, List <int> questions)
        {
            //create exam
            var examEntity = _mapper.Map <ExamEntity>(exam);

            var examCreated = await _context.Exams.AddAsync(examEntity);

            await Save();

            //add questions to the exam
            var examQuestionsEntities = new List <ExamsQuestions>();

            foreach (var question in questions)
            {
                var examQuestion = new ExamsQuestions
                {
                    ExamId     = examCreated.Entity.Id,
                    QuestionId = question
                };

                examQuestionsEntities.Add(examQuestion);
            }

            await _context.AddRangeAsync(examQuestionsEntities);

            return(await Save());
        }
Esempio n. 2
0
        // to add an exisiting question to an existing exam
        public async Task <ExamQuestion> AddExamQuestion(int examId, int questionId)
        {
            var examQuestionEntity = new ExamsQuestions
            {
                ExamId     = examId,
                QuestionId = questionId
            };

            var examQuestionCreated =
                await _context.AddAsync(examQuestionEntity);

            await Save();

            return(_mapper.Map <ExamQuestion>(examQuestionCreated.Entity));
        }
        public async Task <IActionResult> AddQuestion([FromForm] ExamDto model)
        {
            using (var txscope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                try
                {
                    if (!ModelState.IsValid)
                    {
                        txscope.Dispose();
                        return(RedirectToAction("AddQuestion", model.Id));
                    }
                    #region LessonFile
                    string newExamFile = string.Empty, newLicenseFile = string.Empty;
                    if (model.ExamFile != null)
                    {
                        newExamFile = $@"Exam-{CommonMethod.GetFileName(model.ExamFile.FileName)}";
                        await CommonMethod.UploadFileAsync(HostingEnvironment.WebRootPath, FilePathListConstant.ExamFiles, newExamFile, model.ExamFile);
                    }
                    #endregion

                    if (model.QuestionId != 0)
                    {
                        var examQuestionObj = _examQuestionService.GetSingle(x => x.Id == model.QuestionId);
                        examQuestionObj.Question = model.Question;
                        examQuestionObj.Answer   = model.Answer;
                        examQuestionObj.FileName = newExamFile;
                        var questionUpdateResult = await _examQuestionService.UpdateAsync(examQuestionObj, Accessor, User.GetUserId());

                        if (questionUpdateResult != null)
                        {
                            var ExamLessonList = _examLessonService.GetAll(x => x.ExamQuestionId == questionUpdateResult.Id);
                            _examLessonService.DeleteRange(ExamLessonList);
                            _examLessonService.Save();
                            List <long> lessonIdList = JsonConvert.DeserializeObject <List <long> >(model.LessonIdList);
                            foreach (var lessonid in lessonIdList)
                            {
                                var ExamLesson = new ExamLessons();
                                ExamLesson.ExamQuestionId = questionUpdateResult.Id;
                                ExamLesson.LessonId       = lessonid;
                                var examLessonResult = await _examLessonService.InsertAsync(ExamLesson, Accessor, User.GetUserId());
                            }
                        }
                        //StaffLog
                        if (User.IsInRole(UserRoles.Staff))
                        {
                            await _staffLog.InsertAsync(new Log { CreatedDate = DateTime.Now, StaffId = User.GetUserId(), Description = ResponseConstants.UpdateExamQuestion }, Accessor, User.GetUserId());
                        }
                        txscope.Complete();
                        return(JsonResponse.GenerateJsonResult(1, ResponseConstants.UpdateExamQuestion));
                    }
                    else
                    {
                        model.CreatedDate = DateTime.UtcNow;
                        ExamsQuestions eq = new ExamsQuestions();
                        eq.ExamId   = model.ExamId;
                        eq.IsActive = true;
                        eq.Question = model.Question;
                        eq.Answer   = model.Answer;
                        eq.FileName = newExamFile;
                        var result = await _examQuestionService.InsertAsync(eq, Accessor, User.GetUserId());

                        if (result != null)
                        {
                            //Adding Lessons to the Exams Questions
                            List <long> lessonList = JsonConvert.DeserializeObject <List <long> >(model.LessonIdList);

                            foreach (var lessonid in lessonList)
                            {
                                var ExamLesson = new ExamLessons();
                                ExamLesson.ExamQuestionId = result.Id;
                                ExamLesson.LessonId       = lessonid;
                                var examLessonResult = await _examLessonService.InsertAsync(ExamLesson, Accessor, User.GetUserId());
                            }

                            //StaffLog
                            if (User.IsInRole(UserRoles.Staff))
                            {
                                await _staffLog.InsertAsync(new Log { CreatedDate = DateTime.UtcNow, StaffId = User.GetUserId(), Description = ResponseConstants.CreateNewExamQuestion }, Accessor, User.GetUserId());
                            }
                            txscope.Complete();
                            return(JsonResponse.GenerateJsonResult(1, ResponseConstants.CreateNewExamQuestion));
                        }



                        txscope.Dispose();
                        return(JsonResponse.GenerateJsonResult(0, ResponseConstants.SomethingWrong));
                    }
                }
                catch (Exception ex)
                {
                    txscope.Dispose();
                    ErrorLog.AddErrorLog(ex, "AddQuestion");
                    return(JsonResponse.GenerateJsonResult(0, ResponseConstants.SomethingWrong));
                }
            }
        }