コード例 #1
0
        public async Task <ExamResult> DeleteExamResult(int id)
        {
            ExamResult er = _context.ExamResult.Find(id);

            if (er != null)
            {
                _context.ExamResult.Remove(er);
            }
            try
            {
                await _context.SaveChangesAsync();

                return(er);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
コード例 #2
0
        public string GenerateExamResult(ResultGenerate resultGenerate)
        {
            var             exam          = _context.Exam.Where(e => e.Id == resultGenerate.ExamId).FirstOrDefault();
            List <ExamMark> examMarksList = new List <ExamMark>();


            Section section = _context.Section.Where(s => s.Id == resultGenerate.SectionId).FirstOrDefault();

            if (section != null)
            {
                List <Student> studentListInSession = _context.Student.Where(st => st.SectionId == section.Id).ToList();
                if (studentListInSession.Count() > 0)
                {
                    string isUpdateOrInsert = "";
                    foreach (Student studentInSection in studentListInSession)
                    {
                        List <ExamMark> examMarkListForStudent = _context.ExamMark.Where(em => em.ExamId == resultGenerate.ExamId &&
                                                                                         em.StudentId == studentInSection.Id).ToList();

                        ExamResult examResult = new ExamResult();
                        int        totalMark  = 0;
                        float      obtainMark = 0;
                        bool       isPassed   = true;
                        double     greadPoint = 0.00;

                        foreach (ExamMark mark in examMarkListForStudent)
                        {
                            var rotine = _context.ExamRoutine.Where(r => r.Id == mark.ExamRoutineId).FirstOrDefault();
                            totalMark  += rotine.TotalNumber;
                            obtainMark += mark.ObtainMark;
                            if (isPassed)
                            {
                                isPassed = mark.ResultStatus;
                            }
                            greadPoint += mark.Point;
                        }
                        examResult.ResultPublishDate = DateTime.Now;
                        examResult.TotalMark         = totalMark;
                        examResult.TotalObtainMark   = obtainMark;
                        examResult.ResultStatus      = isPassed;
                        examResult.Point             = Math.Round(greadPoint / examMarkListForStudent.Count(), 2);

                        ExamResultPoint resultPoint = getGradePoint(exam.BranchId, examResult.Point);

                        if (resultPoint != null)
                        {
                            if (examResult.ResultStatus)
                            {
                                examResult.Grade = resultPoint.Grade;
                            }
                            else
                            {
                                examResult.Grade = "F";
                            }
                            examResult.Note = resultPoint.Note;
                        }
                        else
                        {
                            examResult.Grade = "##";
                            examResult.Note  = "Point Setting Invalid";
                        }


                        examResult.TotalPresent = examMarkListForStudent.Count();

                        examResult.ExamId    = resultGenerate.ExamId;
                        examResult.SectionId = resultGenerate.SectionId;
                        examResult.StudentId = examMarkListForStudent[0].StudentId;


                        ExamResult existExamResult = _context.ExamResult.Where(ee => ee.ExamId == resultGenerate.ExamId &&
                                                                               ee.SectionId == resultGenerate.SectionId &&
                                                                               ee.StudentId == examMarkListForStudent[0].StudentId).FirstOrDefault();

                        if (existExamResult == null)
                        {
                            _context.ExamResult.Add(examResult);
                            _context.SaveChanges();
                            isUpdateOrInsert = "Inserted ";
                        }
                        else
                        {
                            existExamResult.TotalMark       = examResult.TotalMark;
                            existExamResult.TotalObtainMark = examResult.TotalObtainMark;
                            existExamResult.ResultStatus    = examResult.ResultStatus;
                            existExamResult.Point           = examResult.Point;
                            existExamResult.Grade           = examResult.Grade;
                            existExamResult.Note            = examResult.Note;
                            existExamResult.TotalPresent    = examResult.TotalPresent;

                            _context.ExamResult.Update(existExamResult);
                            _context.SaveChanges();

                            isUpdateOrInsert = "Updated ";
                        }
                    }

                    var resultList = _context.ExamResult.Where(er => er.ExamId == resultGenerate.ExamId &&
                                                               er.SectionId == resultGenerate.SectionId).ToList();

                    if (resultList.Count() > 0)
                    {
                        resultList.Sort((a, b) => a.TotalObtainMark.CompareTo(b.TotalObtainMark));
                        resultList.Reverse();
                        var highestscore = resultList[0].TotalObtainMark;
                        var position     = 0;
                        foreach (ExamResult er in resultList)
                        {
                            er.HighestMark = highestscore;
                            er.Position    = position + 1;
                            position       = (int)er.Position;
                        }
                        _context.ExamResult.UpdateRange(resultList);
                        _context.SaveChanges();

                        return("Successfully " + isUpdateOrInsert + resultList.Count() + " Result for Section :" + section.SectionName);
                    }
                    return(" Total " + studentListInSession.Count() + " student result " + isUpdateOrInsert + " but failed to Set height and position for students.");
                }
            }
            return("This Session not exist in branch.");
        }