private async Task <ExamForRenderDTO> RenderExamCategoryFromId(Guid id, RenderExamTypes type)
        {
            // Get skill part by Id
            var res = await _ExamCategoryRepository.GetAsync(id);

            if (res == null)
            {
                return(null);
            }
            return(await ConvertExamForRender(res, type));
        }
        public async Task <IActionResult> RenderExam(
            RenderExamTypes renderType,
            Guid destId,
            [FromQuery(Name = "instructor")] Guid?instructor)
        {
            // Check if user have other test
            var previousLogId = _ExamLogService.GetLastExamNotFinished();

            if (previousLogId != null && previousLogId != Guid.Empty)
            {
                // Ngắn chặn việc lặp lại tạo bài test
                return(Redirect($"/exams/re-work/{previousLogId}"));
            }

            var res = await _RenderExamService.GetRenderExam(renderType, destId);

            if (!res.Success || res.Data == null)
            {
                this.ToastError(res.Message);
                return(Redirect("/exams/exam-categories"));
            }

            var model = new ExamRenderViewModel();

            model.ExamContent = res.Data;

            // Lưu nhật ký bài thi lại
            model.ExamLogId = await SaveExamLogs(model.ExamContent, destId, instructor);

            if (model.ExamLogId == null || model.ExamLogId == Guid.Empty)
            {
                this.ToastError(L["Can not save your exam, please try again"]);
                return(Redirect("/exams/exam-categories"));
            }

            model = await ProcessParams(model);

            ViewBag.RenderType = res.Data.RenderExamType;

            return(View(AppTheme.ExamContainer, model));
        }
        public async Task <ResponseWrapper <ExamForRenderDTO> > GetRenderExam(RenderExamTypes type, Guid?destID)
        {
            var res = new ResponseWrapper <ExamForRenderDTO>()
                      .ErrorReponseWrapper(null, "Error", 304);
            var examForRender = new ExamForRenderDTO();

            if (type == RenderExamTypes.SkillPart)
            {
                // Process skill part
                var skillPart = await _ExamSkillPartRepository.GetAsync(destID.Value);

                if (skillPart == null)
                {
                    return(res);
                }
                var micSkillPart = new List <MicroSkillPartDTO> {
                    await ConvertRenderSkillPart(skillPart)
                };

                // Process skill cat
                var skillCat = await _ExamSkillCategoryRepository.GetAsync(skillPart.ExamSkillCategoryId);

                var micSkillCat = new List <MicroSkillCategoryDTO>
                {
                    new MicroSkillCategoryDTO
                    {
                        Id    = skillCat.Id,
                        Order = skillCat.Order,
                        Name  = skillCat.Name,
                        LimitTimeInMinutes = skillCat.LimitTimeInMinutes,
                        MaxScores          = skillCat.MaxScores,
                        SkillParts         = micSkillPart
                    }
                };

                // Process exam
                var examCat = await _ExamCategoryRepository.GetAsync(skillCat.ExamCategoryId);

                examForRender = new ExamForRenderDTO
                {
                    Id              = examCat.Id,
                    Name            = examCat.Name,
                    Description     = examCat.Description,
                    RenderExamType  = type,
                    SkillCategories = micSkillCat
                };
            }
            else if (type == RenderExamTypes.SkillCategory)
            {
                // Process kill cat
                var skillCat = await _ExamSkillCategoryRepository.GetAsync(destID.Value);

                if (skillCat == null)
                {
                    return(res);
                }
                var micSkillCat = new List <MicroSkillCategoryDTO> {
                    await ConvertRenderSkillCategory(skillCat)
                };
                // Process exam
                var examCat = await _ExamCategoryRepository.GetAsync(skillCat.ExamCategoryId);

                examForRender = new ExamForRenderDTO
                {
                    Id              = examCat.Id,
                    Name            = examCat.Name,
                    Description     = examCat.Description,
                    RenderExamType  = type,
                    SkillCategories = micSkillCat
                };
            }
            else if (type == RenderExamTypes.Synthetic)
            {
                examForRender = await RenderExamCategoryFromId(destID.Value, type);
            }
            else
            {
                return(res);
            }
            return(new ResponseWrapper <ExamForRenderDTO>()
                   .SuccessReponseWrapper(examForRender, "Success"));
        }
        private async Task <ExamForRenderDTO> ConvertExamForRender(ExamCategory examCat, RenderExamTypes type)
        {
            var query = await _ExamSkillCategoryRepository.GetQueryableAsync();

            var skillCats = query
                            .Where(x => x.ExamCategoryId == examCat.Id)
                            .ToList();
            var microSkillCats = new List <MicroSkillCategoryDTO>();

            foreach (var item in skillCats)
            {
                var converted = await ConvertRenderSkillCategory(item);

                microSkillCats.Add(converted);
            }
            // Sắp xếp chuẩn
            microSkillCats = microSkillCats.OrderBy(x => x.Order).ToList();
            return(new ExamForRenderDTO
            {
                Id = examCat.Id,
                Name = examCat.Name,
                Description = examCat.Description,
                RenderExamType = type,
                SkillCategories = microSkillCats
            });
        }