public static List <InternalExamScoreRecord> Select(IEnumerable <string> StudentIDs, UserOptions Options)
        {
            //key:CourseID
            Dictionary <string, JHCourseRecord> dictCourses = Utilities.GetCourseDict(Options.SchoolYear, Options.Semester);
            //key:AssessmentSetupID
            Dictionary <string, JHAEIncludeRecord> dictAEIncludes = Utilities.GetAEIncludeDict(dictCourses.Values.ToAssessmentSetupIDs(), Options.Exam);

            //類似學期成績的結構…
            Dictionary <string, InternalExamScoreRecord> dictStudentScores = new Dictionary <string, InternalExamScoreRecord>();

            // 取得評量比例
            Utilities.ScorePercentageHSDict = Utilities.GetScorePercentageHS();

            #region 取得及轉換評量科目成績
            int size   = 200;
            int thread = 5;
            FunctionSpliter <string, JHSCETakeRecord> spliter = new FunctionSpliter <string, JHSCETakeRecord>(size, thread);
            spliter.Function = delegate(List <string> studentKeysPart)
            {
                return(JHSCETake.Select(null, studentKeysPart, new string[] { Options.Exam.ID }, null, null));
            };

            foreach (JHSCETakeRecord sce in spliter.Execute(StudentIDs.ToList()))
            {
                if (!dictCourses.ContainsKey(sce.RefCourseID))
                {
                    continue;                                            //評量成績所屬課程非本學期,跳過
                }
                if (Options.Exam.ID != sce.RefExamID)
                {
                    continue;                                   //評量成績的試別不符,跳過
                }
                JHCourseRecord course = dictCourses[sce.RefCourseID];
                if (!dictAEIncludes.ContainsKey(course.RefAssessmentSetupID))
                {
                    continue;                                                           //如果課程沒有評量設定,跳過
                }
                JHAEIncludeRecord ae = dictAEIncludes[course.RefAssessmentSetupID];

                //每個學生一個 InternalExamScoreRecord
                if (!dictStudentScores.ContainsKey(sce.RefStudentID))
                {
                    dictStudentScores.Add(sce.RefStudentID, new InternalExamScoreRecord(sce.RefStudentID));
                }

                if (!dictStudentScores[sce.RefStudentID].Subjects.ContainsKey(course.Subject))
                {
                    SubjectScore subjectScore = new SubjectScore();
                    subjectScore.Domain  = course.Domain;
                    subjectScore.Subject = course.Subject;
                    subjectScore.Period  = course.Period;
                    subjectScore.Credit  = course.Credit;
                    subjectScore.Score   = Utilities.GetScore(new HC.JHSCETakeRecord(sce), new HC.JHAEIncludeRecord(ae), Options.ScoreSource);

                    if (subjectScore.Score.HasValue)
                    {
                        dictStudentScores[sce.RefStudentID].Subjects.Add(course.Subject, subjectScore);
                    }
                }
            }
            #endregion

            #region 計算評量領域成績
            JHSchool.Evaluation.Calculation.ScoreCalculator defaultCalculator = new JHSchool.Evaluation.Calculation.ScoreCalculator(null);

            StudentScore.SetClassMapping();
            List <StudentScore> Students = ToStudentScore(StudentIDs);
            Students.ReadCalculationRule(null);
            foreach (StudentScore student in Students)
            {
                student.SemestersScore.Add(SemesterData.Empty, new global::JHEvaluation.ScoreCalculation.ScoreStruct.SemesterScore(Options.SchoolYear, Options.Semester));

                if (!dictStudentScores.ContainsKey(student.Id))
                {
                    continue;
                }

                global::JHEvaluation.ScoreCalculation.ScoreStruct.SemesterScore semesterScore = student.SemestersScore[SemesterData.Empty];
                foreach (SubjectScore score in dictStudentScores[student.Id].Subjects.Values)
                {
                    //科目成績偷偷進位
                    if (score.Score.HasValue)
                    {
                        if (student.CalculationRule != null)
                        {
                            score.Score = student.CalculationRule.ParseSubjectScore(score.Score.Value);
                        }
                        else
                        {
                            score.Score = defaultCalculator.ParseSubjectScore(score.Score.Value);
                        }
                    }

                    if (!semesterScore.Subject.Contains(score.Subject))
                    {
                        semesterScore.Subject.Add(score.Subject, new SemesterSubjectScore(score));
                    }
                }
            }

            Students.CalcuateDomainSemesterScore(new string[] { });
            #endregion

            foreach (StudentScore student in Students)
            {
                if (!dictStudentScores.ContainsKey(student.Id))
                {
                    dictStudentScores.Add(student.Id, new InternalExamScoreRecord(student.Id));
                }
                InternalExamScoreRecord examScore = dictStudentScores[student.Id];

                examScore.Subjects = ToSubjects(student.SemestersScore[SemesterData.Empty].Subject);
                examScore.Domains  = ToDomains(student.SemestersScore[SemesterData.Empty].Domain);
            }

            return(new List <InternalExamScoreRecord>(dictStudentScores.Values));
        }
示例#2
0
        private void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            // 取得班級學生人數
            Dictionary <string, int> ClassStudCount = new Dictionary <string, int>();

            foreach (ClassExamScoreData cee in _data)
            {
                if (!ClassStudCount.ContainsKey(cee.Class.ID))
                {
                    ClassStudCount.Add(cee.Class.ID, cee.Students.Count);
                }
            }

            Workbook  book = new Workbook();
            Worksheet ws;
            int       rowIndex = 0;
            Workbook  template = new Workbook();

            if (_data.Count > 30 || _UserSelectCount > 14)
            {
                book.Open(new MemoryStream(Resource1.班級評量成績平均比較表60));
                ws = book.Worksheets[0];
                template.Open(new MemoryStream(Resource1.班級評量成績平均比較表60));
            }
            else
            {
                book.Open(new MemoryStream(Resource1.班級評量成績平均比較表));
                ws = book.Worksheets[0];
                template.Open(new MemoryStream(Resource1.班級評量成績平均比較表));
            }
            Range all = template.Worksheets.GetRangeByName("All");

            //Range printDate = template.Worksheets.GetRangeByName("PrintDate");
            //printDate[0, 0].PutValue(DateTime.Today.ToString("yyyy/MM/dd"));

            Range title = template.Worksheets.GetRangeByName("Title");
            //Range feedback = template.Worksheets.GetRangeByName("Feedback");
            Range rowHeaders    = template.Worksheets.GetRangeByName("RowHeaders");
            Range columnHeaders = template.Worksheets.GetRangeByName("ColumnHeaders");
            //Range rankColumnHeader = template.Worksheets.GetRangeByName("RankColumnHeader");

            int RowNumber     = all.RowCount;
            int DataRowNumber = rowHeaders.RowCount;

            //_data.Sort(delegate(ClassExamScoreData x, ClassExamScoreData y)
            //{
            //    return x.Class.Name.CompareTo(y.Class.Name);
            //});

            int dataRowIndex = rowHeaders.FirstRow;

            List <string>            headers    = new List <string>();
            Dictionary <string, int> colMapping = new Dictionary <string, int>();

            foreach (ClassExamScoreData ced in _data)
            {
                //ws.Cells.CreateRange(rowIndex, RowNumber, false).Copy(all);

                //int classColIndex = 0;

                foreach (string courseID in ced.ValidCourseIDs)
                {
                    JHCourseRecord course = _courseDict[courseID];
                    if (!headers.Contains(course.Subject))
                    {
                        headers.Add(course.Subject);
                    }

                    #region comment
                    //if (!colMapping.ContainsKey(GetTaggedDomain(course.Domain)) && _domains.Contains(course.Domain))
                    //{
                    //    colMapping.Add(GetTaggedDomain(course.Domain), classColIndex);
                    //    ws.Cells[columnHeaders.FirstRow + rowIndex, columnHeaders.FirstColumn + classColIndex].PutValue(course.Domain);
                    //    ws.Cells[columnHeaders.FirstRow + rowIndex, columnHeaders.FirstColumn + classColIndex].Style.Font.IsBold = true;

                    //    classColIndex++;
                    //    ws.Cells[columnHeaders.FirstRow + rowIndex, columnHeaders.FirstColumn + classColIndex].PutValue("排序");
                    //    classColIndex++;
                    //}
                    //if (!colMapping.ContainsKey(course.Subject))
                    //{
                    //    colMapping.Add(course.Subject, classColIndex);
                    //    ws.Cells[columnHeaders.FirstRow + rowIndex, columnHeaders.FirstColumn + classColIndex].PutValue(course.Subject);

                    //    classColIndex++;
                    //    ws.Cells[columnHeaders.FirstRow + rowIndex, columnHeaders.FirstColumn + classColIndex].PutValue("排序");
                    //    classColIndex++;
                    //}
                    #endregion
                }

                foreach (StudentRow row in ced.Rows.Values)
                {
                    foreach (var sce in row.RawScoreList)
                    {
                        if (sce.RefExamID != _exam.ID)
                        {
                            continue;
                        }
                        JHCourseRecord course = _courseDict[sce.RefCourseID];

                        if (!headers.Contains(GetTaggedDomain(course.Domain)))
                        {
                            headers.Add(GetTaggedDomain(course.Domain));
                        }
                    }
                }
            }

            #region 產生 Headers
            headers.Sort(Sort);
            int classColIndex = 0;
            foreach (string each in headers)
            {
                if (IsDomain(each))
                {
                    if (_domains.Contains(GetOriginalDomain(each)))
                    {
                        colMapping.Add(each, classColIndex);
                        ws.Cells[columnHeaders.FirstRow + rowIndex, columnHeaders.FirstColumn + classColIndex].PutValue(GetOriginalDomain(each));
                        ws.Cells[columnHeaders.FirstRow + rowIndex, columnHeaders.FirstColumn + classColIndex].Style.Font.IsBold = true;
                        classColIndex++;
                        ws.Cells[columnHeaders.FirstRow + rowIndex, columnHeaders.FirstColumn + classColIndex].PutValue("排序");
                        classColIndex++;
                    }
                }
                else
                {
                    colMapping.Add(each, classColIndex);
                    ws.Cells[columnHeaders.FirstRow + rowIndex, columnHeaders.FirstColumn + classColIndex].PutValue(each);
                    classColIndex++;
                    ws.Cells[columnHeaders.FirstRow + rowIndex, columnHeaders.FirstColumn + classColIndex].PutValue("排序");
                    classColIndex++;
                }
            }
            #endregion

            foreach (ClassExamScoreData ced in _data)
            {
                Dictionary <string, decimal?> subjectScores = new Dictionary <string, decimal?>();
                Dictionary <string, int>      subjectCount  = new Dictionary <string, int>();

                foreach (StudentRow row in ced.Rows.Values)
                {
                    foreach (var sce in row.RawScoreList)
                    {
                        if (sce.RefExamID != _exam.ID)
                        {
                            continue;
                        }
                        JHCourseRecord course = _courseDict[sce.RefCourseID];

                        //if (!headers.Contains(GetTaggedDomain(course.Domain)))
                        //{
                        //    headers.Add(GetTaggedDomain(course.Domain));
                        //}
                        //if (!colMapping.ContainsKey(GetTaggedDomain(course.Domain)) && _domains.Contains(course.Domain))
                        //{
                        //    colMapping.Add(GetTaggedDomain(course.Domain), classColIndex);
                        //    //ws.Cells[columnHeaders.FirstRow + rowIndex, columnHeaders.FirstColumn + classColIndex].PutValue(course.Domain);
                        //    //ws.Cells[columnHeaders.FirstRow + rowIndex, columnHeaders.FirstColumn + classColIndex].Style.Font.IsBold = true;

                        //    classColIndex += 2;
                        //    //ws.Cells[columnHeaders.FirstRow + rowIndex, columnHeaders.FirstColumn + classColIndex].PutValue("排序");
                        //    //classColIndex++;
                        //}

                        string ds = GetDomainSubjectKey(course.Domain, course.Subject);
                        if (!subjectScores.ContainsKey(ds))
                        {
                            subjectScores.Add(ds, 0);
                            subjectCount.Add(ds, 0);
                        }
                        if (sce.Score.HasValue)
                        {
                            subjectCount[ds]++;
                            decimal value = decimal.Zero;
                            if (row.StudentScore.CalculationRule != null)
                            {
                                value = row.StudentScore.CalculationRule.ParseSubjectScore(sce.Score.Value);
                            }
                            else
                            {
                                value = _calc.ParseSubjectScore(sce.Score.Value);
                            }
                            subjectScores[ds] += value;
                        }
                    }
                }

                foreach (string ds in new List <string>(subjectScores.Keys))
                {
                    if (subjectScores[ds].HasValue && subjectCount[ds] > 0)
                    {
                        subjectScores[ds] = subjectScores[ds].Value / (decimal)subjectCount[ds];
                    }
                }

                Dictionary <string, decimal?> domainScores = new Dictionary <string, decimal?>();
                Dictionary <string, int>      domainCount  = new Dictionary <string, int>();

                foreach (string ds in subjectScores.Keys)
                {
                    string domain = GetOnlyDomain(ds);
                    if (!domainScores.ContainsKey(domain))
                    {
                        domainScores.Add(domain, 0);
                        domainCount.Add(domain, 0);
                    }
                    domainCount[domain]++;
                    domainScores[domain] += subjectScores[ds];
                }

                foreach (string domain in new List <string>(domainScores.Keys))
                {
                    if (domainScores[domain].HasValue && domainCount[domain] > 0)
                    {
                        domainScores[domain] = domainScores[domain].Value / (decimal)domainCount[domain];
                    }
                }

                #region 填入班級平均
                //int rankIndex = rankColumnHeader.FirstColumn;

                // 班級平均
                ClassCourseAvg cca = new ClassCourseAvg();

                foreach (var stud in ced.Students)
                {
                    if (ced.Rows.ContainsKey(stud.ID))
                    {
                        //阿寶...這也許是地雷....

                        // 當學生不屬於任何班級跳過
                        if (stud.Class == null)
                        {
                            continue;
                        }

                        cca.ClassID   = stud.Class.ID;
                        cca.ClassName = stud.Class.Name;
                        StudentRow srow = ced.Rows[stud.ID];
                        foreach (CourseScore cs in srow.CourseScoreList)
                        {
                            if (cs.Score.HasValue)
                            {
                                cca.ClassID   = stud.Class.ID;
                                cca.ClassName = stud.Class.Name;
                                decimal value = decimal.Zero;
                                if (srow.StudentScore.CalculationRule != null)
                                {
                                    value = srow.StudentScore.CalculationRule.ParseSubjectScore(cs.Score.Value);
                                }
                                else
                                {
                                    value = _calc.ParseSubjectScore(cs.Score.Value);
                                }
                                cca.AddSubjectScore(_courseDict[cs.CourseID].Subject, value);
                            }
                            //decimal? s = GetRoundScore(cs.Score);
                            //if (s.HasValue)
                            //{
                            //    cca.ClassID = stud.Class.ID;
                            //    cca.ClassName = stud.Class.Name;
                            //    cca.AddCourseScore(cs.CourseID, s.Value);
                            //}
                        }
                    }
                }

                // 班級名稱
                ws.Cells[dataRowIndex, 0].PutValue(cca.ClassName);
                // 放班級人數
                int peoColIdx = 32;
                // 找到人數放位置
                for (int i = 32; i <= 60; i++)
                {
                    if (ws.Cells[2, i].StringValue.Trim() == "人數")
                    {
                        peoColIdx = i;
                        break;
                    }
                }
                if (cca.ClassID != null && cca.ClassName != "")
                {
                    if (ClassStudCount.ContainsKey(cca.ClassID))
                    {
                        ws.Cells[dataRowIndex, peoColIdx].PutValue(ClassStudCount[cca.ClassID].ToString());
                    }
                }


                foreach (KeyValuePair <string, decimal?> val in cca.GetSubjectStudScoreAvg())
                {
                    if (val.Value.HasValue)
                    {
                        decimal value = Math.Round((decimal)val.Value, 2, MidpointRounding.AwayFromZero);
                        ws.Cells[dataRowIndex, colMapping[val.Key] + 1].PutValue(value);
                    }
                }
                //填入領域
                bool domainHasData = false;
                foreach (string domain in _domains)
                {
                    if (!domainScores.ContainsKey(domain))
                    {
                        continue;
                    }
                    if (!domainScores[domain].HasValue)
                    {
                        continue;
                    }

                    string taggedDomain = GetTaggedDomain(domain);
                    if (colMapping.ContainsKey(taggedDomain))
                    {
                        decimal value = Math.Round((decimal)domainScores[domain].Value, 2, MidpointRounding.AwayFromZero);
                        ws.Cells[dataRowIndex, colMapping[taggedDomain] + 1].PutValue(value);
                        domainHasData = true;
                    }
                }
                if (cca.GetSubjectStudScoreAvg().Count > 0 || domainHasData)
                {
                    dataRowIndex++;
                }

                #endregion

                #region 填入標題及回條
                //Ex. 新竹市立光華國民中學 97 學年度第 1 學期    第1次平時評量成績單
                ws.Cells[title.FirstRow + rowIndex, title.FirstColumn].PutValue(string.Format("{0}  {1}  {2} 各班各科平均成績比較表", SchoolName, Semester, _exam.Name));
                //Ex. 101 第1次平時評量回條 (家長意見欄)
                //ws.Cells[feedback.FirstRow + rowIndex, feedback.FirstColumn].PutValue(string.Format("{0}  {1}回條  (家長意見欄)", ced.Class.Name, _exam.Name));
                #endregion

                //rowIndex += RowNumber;
                //ws.HPageBreaks.Add(rowIndex, 0);
            }

            e.Result = book;
        }