示例#1
0
        static async Task <bool> CreateQuestionAsync(ResultQuestion question)
        {
            var client = new HttpClient
            {
                BaseAddress = new Uri(AppSettings.BackendUrl)
            };

            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            var response = await client.PostAsJsonAsync("api/Questions", question);

            response.EnsureSuccessStatusCode();
            if (response.IsSuccessStatusCode)
            {
                var returnQuestion = await response.Content.ReadAsAsync <ResultQuestion>();

                foreach (var answersForAQuestion in question.Answers)
                {
                    answersForAQuestion.QuestionId = returnQuestion.id;
                    await client.PostAsJsonAsync($"api/Questions/{returnQuestion.id}/answersForAQuestions",
                                                 answersForAQuestion);
                }
            }
            return(response.IsSuccessStatusCode);
        }
示例#2
0
        static async Task RunAsync(string importFolder)
        {
            var files = Directory.EnumerateFiles(importFolder, "*.*", SearchOption.TopDirectoryOnly)
                        .Where(s => s.EndsWith(".docx") || s.EndsWith(".doc")).ToList();
            var successFolder = Path.Combine(importFolder, "success");
            var failFolder    = Path.Combine(importFolder, "fail");

            //var resultFolder = Path.Combine(importFolder, "result");

            try
            {
                // tạo thư mục nếu thư mục không tồn tại
                if (!Directory.Exists(importFolder))
                {
                    Directory.CreateDirectory(importFolder);
                }
                if (!Directory.Exists(successFolder))
                {
                    Directory.CreateDirectory(successFolder);
                }
                if (!Directory.Exists(failFolder))
                {
                    Directory.CreateDirectory(failFolder);
                }


                var textExtractor = new TextExtractor();
                Console.WriteLine($"There are {files.Count} file(s) found");
                var successCount = 0;
                foreach (var file in files)
                {
                    var fileName = Path.GetFileName(file);
                    try
                    {
                        Console.WriteLine($"Extracting file: {file}");
                        var text = textExtractor.Extract(file).Text;
                        text = Regex.Replace(text, @"^\s+$[\r\n]*", "", RegexOptions.Multiline);
                        var resultQuestion = new ResultQuestion(fileName, text);
                        var result         = await CreateQuestionAsync(resultQuestion);

                        MoveOverwrite(file, Path.Combine(successFolder, fileName));
                        //var jsonResult = JsonConvert.SerializeObject(resultQuestion);
                        //var jsonPath = Path.Combine(resultFolder, $"{fileName}.json");
                        //File.WriteAllText(jsonPath, jsonResult, Encoding.UTF8);
                        Console.WriteLine($"Extracted file: {file}");
                        successCount++;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Extract file ${file} fail with error: {ex.GetBaseException().Message}");
                        MoveOverwrite(file, Path.Combine(failFolder, fileName));
                    }
                }
                Console.WriteLine($"{successCount} file(s) were successfully converted");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.GetBaseException().Message);
            }
        }
示例#3
0
        public ActionResult PassResult(PassTestModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Json(new { isValid = false }));
            }


            var test = testsRepo.FindOne(new ByIdSpecify <Test>(model.Id));

            var result = new TestResult
            {
                UserId   = this.usersRepo.FindOne(new UserByLoginSpecify(User.Identity.Name)).Id,
                TestId   = test.Id,
                PassDate = DateTime.Now
            };

            testsResRepo.SaveOrUpdate(result);

            var correctAnswers = 0;

            foreach (var question in model.Questions)
            {
                var resultQuestion = new ResultQuestion
                {
                    QuestionText = question.QuestionValue,
                    TestResultId = result.Id
                };

                qResRepo.SaveOrUpdate(resultQuestion);

                foreach (var answer in question.Answers)
                {
                    if (answer.IsCorrect && answer.IsChoisen)
                    {
                        correctAnswers++;
                    }
                    var resultAnswer = new ResultAnswer
                    {
                        AnswerText       = answer.AnswerValue,
                        ResultQuestionId = resultQuestion.Id,
                        IsCorrect        = answer.IsCorrect,
                        IsChoisen        = answer.IsChoisen
                    };

                    aResRepo.SaveOrUpdate(resultAnswer);

                    //resultQuestion.ResultAnswers.Add(resultAnswer);
                }

                //result.ResultQuestions.Add(resultQuestion);
            }

            ViewBag.correctAnswers = correctAnswers;
            ViewBag.totalQuestios  = model.Questions.Count;
            return(View());
        }
 public IActionResult addAnswer([FromBody] ResultQuestion model)
 {
     try
     {
         return(new ObjectResult(_resultQuestion.Add(model)));
     }
     catch
     {
         return(BadRequest());
     }
 }
 public IActionResult editAnswer([FromBody] ResultQuestion model)
 {
     try
     {
         return(new ObjectResult(this._resultQuestion.Edit(model)));
     }
     catch
     {
         return(BadRequest());
     }
 }
示例#6
0
        public ResultQuestion CountQuestion(string id)
        {
            ResultQuestion result    = new ResultQuestion();
            var            questions = _question.Find(q => q.GroupId == id).ToList();

            result.Count = questions.Count;
            var group = _group.Find(g => g.Id == id).FirstOrDefault();

            result.Name = group.Name;
            return(result);
        }
 public bool Edit(ResultQuestion o)
 {
     try
     {
         _log.LogInformation("BEGIN => Edit");
         _context.Update(o);
         _context.SaveChanges();
         _log.LogInformation("END <= Edit");
         return(true);
     }
     catch (Exception e)
     {
         _log.LogError("ERROR => Edit : [%s]", e);
         return(false);
     }
 }
 public bool Add(ResultQuestion o)
 {
     try
     {
         _log.LogInformation("BEGIN => Add");
         _context.ResultQuestions.Add(o);
         _context.SaveChanges();
         _log.LogInformation("END <= Add");
         return(true);
     }
     catch (Exception e)
     {
         _log.LogError("ERROR <= Add : [%s]", e);
         return(false);
     }
 }
示例#9
0
        private ResultsViewModel validate(Quiz quiz, QuizViewModel viewModel)
        {
            ResultsViewModel results = new ResultsViewModel();

            results.copyInfo(quiz);
            for (int i = 0; i < quiz.Questions.Count; i++)
            {
                if (quiz.Questions[i].Picture != null)
                {
                    results.Images.Add(i, FileToByteArray(quiz.Questions[i].Picture));
                }
                ResultQuestion resultQuestion = new ResultQuestion(quiz.Questions[i], viewModel.Questions[i]);
                results.Questions.Add(resultQuestion);
            }
            results.countRightAnswers();
            return(results);
        }
        public ActionResult PassResult(PassTestModel model)
        {
            if (!ModelState.IsValid)
            {
                return Json(new { isValid = false });
            }

            var test = testsRepo.FindOne(new ByIdSpecify<Test>(model.Id));

            var result = new TestResult
                {
                    UserId = this.usersRepo.FindOne(new UserByLoginSpecify(User.Identity.Name)).Id,
                    TestId = test.Id,
                    PassDate = DateTime.Now
                };

            testsResRepo.SaveOrUpdate(result);

            var correctAnswers = 0;

            foreach (var question in model.Questions)
            {
                var resultQuestion = new ResultQuestion
                    {
                        QuestionText = question.QuestionValue,
                        TestResultId = result.Id
                    };

                qResRepo.SaveOrUpdate(resultQuestion);

                foreach (var answer in question.Answers)
                {
                    if (answer.IsCorrect && answer.IsChoisen)
                        correctAnswers++;
                    var resultAnswer = new ResultAnswer
                        {
                            AnswerText = answer.AnswerValue,
                            ResultQuestionId = resultQuestion.Id,
                            IsCorrect = answer.IsCorrect,
                            IsChoisen = answer.IsChoisen
                        };

                    aResRepo.SaveOrUpdate(resultAnswer);

                    //resultQuestion.ResultAnswers.Add(resultAnswer);
                }

                //result.ResultQuestions.Add(resultQuestion);
            }

            ViewBag.correctAnswers = correctAnswers;
            ViewBag.totalQuestios = model.Questions.Count;
            return View();
        }