Exemplo n.º 1
0
        public ActionResult Create(Models.Question question, HttpPostedFileBase Image, List <Answer> listAnswers)
        {
            question.CreatedBy  = int.Parse(Session["Name"].ToString());
            question.ModifiedBy = int.Parse(Session["Name"].ToString());
            if (Image != null && Image.ContentLength > 0)
            {
                string filePath = Path.Combine(Server.MapPath("~/Content/QuestionUpload/Images/"),
                                               Path.GetFileName(Image.FileName));
                Image.SaveAs(filePath);
                question.Image = Image.FileName;
            }
            else
            {
                question.Image = null;
            }

            var addQuestion = questionService.AddQuestion(question);

            TranferID.ID = addQuestion;
            // Create Answer
            foreach (var i in listAnswers)
            {
                i.QuestionID = TranferID.ID;
                if (i.QuestionID <= 0)
                {
                    return(RedirectToAction("Create", "Question"));
                }
                else
                {
                    answerService.AddAnswer(i);
                }
            }

            return(RedirectToAction("Index"));
        }
 public IHttpActionResult Post([FromBody] AnswerVM answerVM)
 {
     if (answerVM == null || !answerVM.isWalid())
     {
         return(BadRequest());
     }
     _answerService.AddAnswer(Mapper.Map <AnswerDTO>(answerVM));
     return(Ok());
 }
Exemplo n.º 3
0
 public ActionResult Create(AnswerViewModel answer, int?questionId)   // [Bind(Include = "AnswerId,Name,Complexity,Rate,Duration,IsApproved")]
 {
     if (ModelState.IsValid)
     {
         AnswerDto answerDto = _mapper.Map <AnswerViewModel, AnswerDto>(answer);
         _answerService.AddAnswer(answerDto);
         return(RedirectToAction("Details", "Questions", new { id = answer.QuestionId }));
     }
     return(View(answer));
 }
Exemplo n.º 4
0
        public IActionResult AddAnswer([FromBody] AnswerDto answerDto)
        {
            if (ModelState.IsValid)
            {
                _answerService.AddAnswer(answerDto);
                return(StatusCode(StatusCodes.Status201Created));
            }

            return(BadRequest());
        }
Exemplo n.º 5
0
        public IActionResult AddAnswer(AddAnswerRequest model)
        {
            string tokenString   = Request.Headers["Authorization"];
            int    currentUserId = JwtAthentication.GetCurrentUserId(tokenString);

            model.UserId = currentUserId;

            var answers = _answerService.AddAnswer(model);

            answers.ForEach(x => x.AvatarUrl = Url.Content(string.Format("~/Images/UserAvatars/{0}", x.Avatar)));

            return(Ok(new { answers = answers }));
        }
Exemplo n.º 6
0
 public void Post([FromBody] Answer value)
 {
     _ias.AddAnswer(value);
 }
Exemplo n.º 7
0
        public ActionResult ImportExamPaper(HttpPostedFileBase excelfile)
        {
            if (excelfile == null)
            {
                Failure = "Please choose excel file to import exam paper";
                return(RedirectToAction("ImportExamPaper"));
            }
            else
            {
                if (excelfile.FileName.EndsWith("xls") || excelfile.FileName.EndsWith("xlsx"))
                {
                    try
                    {
                        string path = Path.Combine(Server.MapPath("~/FileExcel/"), Guid.NewGuid().ToString() + Path.GetExtension(excelfile.FileName));
                        excelfile.SaveAs(path);
                        Excel.Application application = new Excel.Application
                        {
                            Visible = true
                        };
                        Excel.Workbook  workbook  = application.Workbooks.Open(path);
                        Excel.Worksheet worksheet = workbook.Sheets[@"ExamPaper"];
                        Excel.Range     range     = worksheet.UsedRange;

                        Models.ExamPaper examPaper = new Models.ExamPaper();
                        examPaper.Title = ((Excel.Range)range.Cells[3, 2]).Text;
                        examPaper.Time  = int.Parse(((Excel.Range)range.Cells[4, 2]).Text);
                        if (((Excel.Range)range.Cells[5, 2]).Text == "Public")
                        {
                            examPaper.Status = true;
                        }
                        else if (((Excel.Range)range.Cells[5, 2]).Text == "Draff")
                        {
                            examPaper.Status = false;
                        }
                        else
                        {
                            Failure = "Exam paper status must be select from dropdown list";
                            return(RedirectToAction("ImportExamPaper"));
                        }
                        examPaper.IsActive     = Boolean.Parse(((Excel.Range)range.Cells[6, 2]).Text);
                        examPaper.CreatedBy    = int.Parse(Session["Name"].ToString());
                        examPaper.CreatedDate  = DateTime.Now;
                        examPaper.ModifiedBy   = int.Parse(Session["Name"].ToString());
                        examPaper.ModifiedDate = DateTime.Now;
                        int examPaperId          = examPaperService.Create(examPaper);
                        var listQuestionCategory = questionCategorySevice.GetAll();
                        for (int row = 11; row <= range.Rows.Count; row++)
                        {
                            int level = 0;
                            if (((Excel.Range)range.Cells[row, 2]).Text == "Hard")
                            {
                                level = 3;
                            }
                            else if (((Excel.Range)range.Cells[row, 2]).Text == "Normal")
                            {
                                level = 2;
                            }
                            else if (((Excel.Range)range.Cells[row, 2]).Text == "Easy")
                            {
                                level = 1;
                            }
                            else
                            {
                                Failure = "Question level must be select from dropdown list";
                                return(RedirectToAction("ImportExamPaper"));
                            }
                            int categoryId = 0;
                            int k          = 0;
                            foreach (var item in listQuestionCategory)
                            {
                                if (((Excel.Range)range.Cells[row, 3]).Text == item.Name)
                                {
                                    categoryId = item.CategoryID;
                                    k++;
                                }
                            }
                            if (k == 0)
                            {
                                Failure = "Question category must be select from dropdown list";
                                return(RedirectToAction("ImportExamPaper"));
                            }
                            Models.Question question = new Models.Question
                            {
                                Content      = ((Excel.Range)range.Cells[row, 1]).Text,
                                CategoryID   = categoryId,
                                Level        = level,
                                IsActive     = true,
                                CreatedBy    = int.Parse(Session["Name"].ToString()),
                                CreatedDate  = DateTime.Now,
                                ModifiedBy   = int.Parse(Session["Name"].ToString()),
                                ModifiedDate = DateTime.Now
                            };
                            int questionId = questionService.AddQuestion(question);

                            Answer answer = new Answer();
                            int    j      = 5;
                            for (int i = 4; i <= 13; i += 2)
                            {
                                string content = ((Excel.Range)range.Cells[row, i]).Text;
                                if (content != "")
                                {
                                    answer.AnswerContent = content;
                                    answer.IsCorrect     = Boolean.Parse(((Excel.Range)range.Cells[row, j]).Text);
                                    answer.QuestionID    = questionId;
                                    answerService.AddAnswer(answer);
                                }
                                else
                                {
                                    continue;
                                }
                                j += 2;
                            }
                            examPaperQuestionService.InsertExamPaperQuestion(examPaperId, questionId);
                        }
                    }
                    catch (Exception ex)
                    {
                        Failure = ex.Message;
                        return(RedirectToAction("ImportExamPaper"));
                    }
                    Success = "Import exam paper successfully!";
                    return(RedirectToAction("ExamPapers"));
                }
                else
                {
                    Failure = "Please choose excel file to import exam paper";
                    return(RedirectToAction("ImportExamPaper"));
                }
            }
        }
Exemplo n.º 8
0
        public async Task <AddedAnswerDto> AddAnswer(AnswerDto answer)
        {
            var result = new AddedAnswerDto()
            {
                Answer = answer
            };

            // Basic checks first
            if (result.Answer == null)
            {
                return(SetErrorMessage(result, "Incoming data is null."));
            }

            // This might throw exception if there was a header but invalid. But if someone is just messing with us we will return nothing.
            if (!ParseAntiForgeryHeader(_antiforgery, result, HttpContext))
            {
                return(result);
            }

            // Let's first check for profanities.
            bool gotProfanityIssues = CheckProfanity(_profanityService, result);

            if (gotProfanityIssues)
            {
                return(result);
            }

            // Add left word and right word to suggestions
            var addedSuggestion = _suggestionService.AddSuggestion(new SuggestionDto()
            {
                Phrase = answer.LeftWord
            });

            // Add the right word if different from left
            if (answer.LeftWord != answer.RightWord)
            {
                addedSuggestion = _suggestionService.AddSuggestion(new SuggestionDto()
                {
                    Phrase = answer.RightWord
                });
            }

            // Save the user in case we need statistics update
            ApplicationUserDto user = null;

            // Load user if he is logged in
            if (User.Identity.IsAuthenticated && User.Identity.Name != null)
            {
                user = _userService.FindByUserName(User.Identity.Name);
            }
            // Set the user id in the answer if user is found
            if (user != null)
            {
                answer.UserId = user.UserId;
                // Check if user statistics is loaded
                _statisticsService.LoadUserStatictics(user);
            }

            // Add answer. This will not touch user's cache or user service.
            result = await _answerService.AddAnswer(answer);

            // Need to update user stats if new answer was added
            if (result.IsNew && user != null)
            {
                user.NumberOfAnswers++;
                result.UserLevelingResult = _userService.LevelUser(user, EventType.AnswerAdded);
            }

            return(result);
        }
Exemplo n.º 9
0
 public ActionResult AddAnswer([FromForm] AddAnswerRequest request)
 {
     answerService.AddAnswer(request);
     return(Ok());
 }