public static int GenerateListeningTestPaper(
            this TestCategoryManager _TestCategoryManager,
            ListeningMediaManager _ListeningMediaManager,
            ListeningBaseQuestionManager _ListeningBaseQuestionManager,
            PieceOfTestManager _PieceOfTestManager,
            int UserId,
            int?InstructorId)
        {
            // Kiến tạo danh sách câu hỏi và câu trả lời, đồng thời xáo trộn câu trả lời
            var paper = new ListeningTestPaper
            {
                ListeningPartOnes = ListeningTestPaper.Generate(1, _TestCategoryManager, _ListeningMediaManager, _ListeningBaseQuestionManager),
                ListeningPartTwos = ListeningTestPaper.Generate(2, _TestCategoryManager, _ListeningMediaManager, _ListeningBaseQuestionManager),
            };
            // Khởi tạo đối tượng lưu trữ bài kiểm tra này và lưu paper mặc định có đáp án đúng vào
            var piceOfTest = new PieceOfTest
            {
                UserId           = UserId,
                InstructorId     = InstructorId,
                TypeCode         = TestCategory.LISTENING,
                PartId           = -1,
                ResultOfTestJson = JsonConvert.SerializeObject(paper),
            };

            // Lưu trữ bài thi vào database trước khi bắt đầu
            _PieceOfTestManager.Add(piceOfTest);
            // Xóa đáp án đúng trong paper
            return(piceOfTest.Id);
        }
        public IActionResult CheckFullListening(ListeningTestPaper paper)
        {
            string message = string.Empty;

            bool status = paper.IsPaperFullSelection();

            return(Json(new { status, message }));
        }
        public IActionResult ListeningReview(int id)
        {
            if (id <= 0)
            {
                return(NotFoundTest());
            }

            ViewBag.Title = "LISTENING TESTING";

            ViewBag.IsReviewMode = true;

            // Sau khi hoàn tất lọc các lỗi, tiến hành lấy
            PieceOfTest piece = _PieceOfTestManager.Get(id);

            if (piece == null)
            {
                return(NotFoundTest());
            }

            if (piece.InstructorId != User.Id() && piece.UserId != User.Id())
            {
                this.NotifyError("You are not authorized to view or manipulate this test");
                return(RedirectToAction(nameof(HomeController.Index), NameUtils.ControllerName <HomeController>()));
            }

            // Lấy chủ sở hữu của bài kiểm tra
            User owner = _UserManager.Get(piece.UserId);

            ViewData["Owner"] = new User
            {
                Avatar    = owner.Avatar,
                FirstName = owner.FirstName,
                LastName  = owner.LastName
            };

            // Thời gian làm bài
            ViewBag.Timer = piece.TimeToFinished;

            // Điểm của bài thi
            ViewBag.Scores = piece.Scores;

            // Bài thi của học viên
            ListeningTestPaper userPaper = JsonConvert.DeserializeObject <ListeningTestPaper>(piece.ResultOfUserJson ?? "");
            // Bài thi mẫu (Được tạo khi thi, của học viên)
            ListeningTestPaper resultPaper = JsonConvert.DeserializeObject <ListeningTestPaper>(piece.ResultOfTestJson ?? "");

            bool isReviewOfFailTest = piece.ResultOfUserJson == null || piece.ResultOfUserJson.Length <= 0 || piece.UpdatedTime == null;

            ViewBag.IsReviewOfFailTest = isReviewOfFailTest;
            ViewBag.ResultPaper        = resultPaper;

            if (isReviewOfFailTest)
            {
                userPaper = resultPaper;
            }

            return(View(nameof(Listening), userPaper));
        }
        public IActionResult Listening(int id)
        {
            ViewBag.Title = "LISTENING TESTING";
            if (id <= 0)
            {
                return(NotFoundTest());
            }

            // Sau khi hoàn tất lọc các lỗi, tiến hành xử lý, đếm số câu đúng
            PieceOfTest piece = _PieceOfTestManager.Get(id);

            // Nếu tìm không thấy bài Test
            if (piece == null)
            {
                return(NotFoundTest());
            }

            if (piece.InstructorId != User.Id() && piece.UserId != User.Id())
            {
                this.NotifyError("You are not authorized to view or manipulate this test");
                return(RedirectToAction(nameof(HomeController.Index), NameUtils.ControllerName <HomeController>()));
            }

            // Lấy chủ sở hữu của bài kiểm tra
            User owner = _UserManager.Get(piece.UserId);

            ViewData["Owner"] = new User
            {
                Avatar    = owner.Avatar,
                FirstName = owner.FirstName,
                LastName  = owner.LastName
            };

            // Nếu bài thi đã hoàn thành, thì chuyển sang màn hình review
            if (piece.ResultOfUserJson != null && piece.ResultOfUserJson.Length > 0 && piece.UpdatedTime != null)
            {
                return(RedirectToAction(nameof(ListeningReview), new { id }));
            }

            // Tránh timer bị reset
            if (piece.CreatedTime != null)
            {
                ViewBag.Timer = DateTime.UtcNow.Subtract((DateTime)piece.CreatedTime).TotalSeconds;
            }

            ListeningTestPaper paper = JsonConvert.DeserializeObject <ListeningTestPaper>(piece.ResultOfTestJson)
                                       .RemoveCorrectAnswers();

            paper.PiceOfTestId = piece.Id;

            this.NotifySuccess("Try your best, Good Luck!");

            return(View(paper));
        }
        public IActionResult Listening(ListeningTestPaper paper)
        {
            if (paper == null)
            {
                this.NotifyError("Not found yor test");
                return(Json(new { status = false, message = string.Empty, location = "/" }));
            }

            if (paper.PiceOfTestId <= 0)
            {
                this.NotifyError("Not found yor test");
                return(Json(new { status = false, message = string.Empty, location = "/" }));
            }

            // Sau khi hoàn tất lọc các lỗi, tiến hành xử lý, đếm số câu đúng
            PieceOfTest piece = _PieceOfTestManager.Get(paper.PiceOfTestId);

            if (piece == null)
            {
                this.NotifyError("Not found yor test");
                return(Json(new { status = false, message = string.Empty, location = "/" }));
            }

            if (piece.InstructorId != User.Id() && piece.UserId != User.Id())
            {
                this.NotifyError("You are not authorized to view or manipulate this test");
                return(Json(new { status = false, message = string.Empty, location = "/" }));
            }

            int total = paper.TotalQuestions(); // Tổng số câu hỏi

            if (total <= 0)
            {
                return(Json(new { status = false, message = "The test does not have any questions", location = string.Empty }));
            }

            // Tính toán số điểm
            float scores = paper.ScoreCalculate(piece.ResultOfTestJson);

            // Thời gian kết thúc bài thi
            float timeToFinished = DateTime.UtcNow.Subtract((DateTime)piece.CreatedTime).TotalSeconds.ToFloat();

            // Cập nhật dữ liệu
            piece.ResultOfUserJson = JsonConvert.SerializeObject(paper);
            piece.Scores           = scores;
            piece.TimeToFinished   = timeToFinished;
            _PieceOfTestManager.Update(piece);

            // Chuyển đến trang kết quả
            return(Json(new { status = true, message = "Successful submission of exams", location = $"{Url.Action(nameof(Result), NameUtils.ControllerName<TestPaperController>())}/{piece.Id}" }));
        }
        public IActionResult LoadTranscript([FromQuery] int id, [FromQuery] int mediaId)
        {
            if (id <= 0 || mediaId <= 0)
            {
                return(Content(string.Empty));
            }

            // Lấy bài thi theo mã
            PieceOfTest piece = _PieceOfTestManager.Get(id);

            // Nếu không có, trả về
            if (piece == null)
            {
                return(Content(string.Empty));
            }

            if (piece.TypeCode == TestCategory.TEST_ALL)
            {
                // Lấy trang giấy thi
                GeneralTestPaper paper = JsonConvert.DeserializeObject <GeneralTestPaper>(piece.ResultOfTestJson);

                // Nếu trang giấy rỗng
                if (paper == null)
                {
                    return(Content(string.Empty));
                }

                // Cố gắng tìm kiếm transcript ở part 1
                if (paper.ListeningTestPaper.ListeningPartOnes != null && paper.ListeningTestPaper.ListeningPartOnes.Any(x => x.ListeningMedia.Id == mediaId))
                {
                    return(Content(paper.ListeningTestPaper.ListeningPartOnes.Where(x => x.ListeningMedia.Id == mediaId).FirstOrDefault()?.ListeningMedia?.Transcript ?? ""));
                }

                // Cố gắng tìm kiếm transcript ở part 2
                if (paper.ListeningTestPaper.ListeningPartTwos != null && paper.ListeningTestPaper.ListeningPartTwos.Any(x => x.ListeningMedia.Id == mediaId))
                {
                    return(Content(paper.ListeningTestPaper.ListeningPartTwos.Where(x => x.ListeningMedia.Id == mediaId).FirstOrDefault()?.ListeningMedia?.Transcript ?? ""));
                }
            }
            else
            {
                // Lấy trang giấy thi
                ListeningTestPaper paper = JsonConvert.DeserializeObject <ListeningTestPaper>(piece.ResultOfTestJson);

                // Nếu trang giấy rỗng
                if (paper == null)
                {
                    return(Content(string.Empty));
                }

                // Cố gắng tìm kiếm transcript ở part 1
                if (paper.ListeningPartOnes != null && paper.ListeningPartOnes.Any(x => x.ListeningMedia.Id == mediaId))
                {
                    return(Content(paper.ListeningPartOnes.Where(x => x.ListeningMedia.Id == mediaId).FirstOrDefault()?.ListeningMedia?.Transcript ?? ""));
                }

                // Cố gắng tìm kiếm transcript ở part 2
                if (paper.ListeningPartTwos != null && paper.ListeningPartTwos.Any(x => x.ListeningMedia.Id == mediaId))
                {
                    return(Content(paper.ListeningPartTwos.Where(x => x.ListeningMedia.Id == mediaId).FirstOrDefault()?.ListeningMedia?.Transcript ?? ""));
                }
            }

            // Nếu cũng không có thì trả về rỗng
            return(Content(string.Empty));
        }