void SetRankData(ResRankDataPacket packet) { if (packet == null) { return; } if (RankData.ContainsKey(packet.RankData.SType)) { RankData[packet.RankData.SType] = packet.RankData; } else { RankData.Add(packet.RankData.SType, packet.RankData); } }
/// <summary> /// 按科目組織排名資料。 /// </summary> /// <param name="selectedsubjs">科目名稱->RankData</param> /// <returns></returns> private Dictionary <string, RankData> ArrangeBySubject(Dictionary <string, decimal> selectedsubjs) { Dictionary <string, RankData> ranks = new Dictionary <string, RankData>(); foreach (RSemesterScore semsscore in SemesterScores.ForBy(SelectedSchoolYear, SelectedSemester)) { foreach (RSubjectScore subjscore in semsscore.Subjects) { //不在科目清單中就跳過。 if (!selectedsubjs.ContainsKey(subjscore.Name)) { continue; } //「排名」不存在就新增。 if (!ranks.ContainsKey(subjscore.Name)) { ranks.Add(subjscore.Name, new RankData()); ranks[subjscore.Name].Name = subjscore.Name; } RankData rank = ranks[subjscore.Name]; //將學生成績加入到排名資料中。 if (!rank.ContainsKey(semsscore.RefStudentID)) { rank.Add(semsscore.RefStudentID, new RankScore(subjscore.GetScoreOrDefault(0), null)); } else { throw new ArgumentException("學生有重覆的學期成績(資料異常!)。"); } } } return(ranks); }
private void btnRank_Click(object sender, EventArgs e) { if (!IsValid()) { MsgBox.Show("輸入資料不正確,請先修正。"); return; } _collector.Clear(); if (Type.Equals("Exam")) { try { #region 評量成績排名 if (Category.Equals("Subject")) { #region 科目 //將使用者選擇的「科目名稱、權重」集中於 subjects 集合。 Dictionary <string, decimal> subjects = new Dictionary <string, decimal>(); foreach (DataGridViewRow row in dgv.Rows) { string value = "" + row.Cells[chCheck.Index].Value; bool b; if (bool.TryParse(value, out b) && b == true) { string subject = "" + row.Cells[chCategory.Index].Value; decimal period = decimal.Parse("" + row.Cells[chPeriod.Index].Value); subjects.Add(subject, period); } } //_courses 轉換成 ID 清單,如果該課程的科目使用者並沒有選擇就過慮掉。 List <string> courseIDs = new List <string>(); foreach (CourseRecord record in _courses) { if (subjects.ContainsKey(record.Subject)) { courseIDs.Add(record.ID); } } //取得課程的特定評量成績。 List <Data.JHSCETakeRecord> sce_records = Data.JHSCETake.SelectByCourseAndExam(courseIDs, (cboExam.SelectedItem as Data.JHExamRecord).ID); Dictionary <string, Data.JHSCETakeRecord> dic_sce = AsDictionary(sce_records); if (rbSeparate.Checked) { #region 分科排名 List <string> studentIDs = _students.AsKeyList(); Dictionary <string, RankData> data = new Dictionary <string, RankData>(); foreach (string each in subjects.Keys) { data.Add(each, new RankData()); data[each].Name = each; } foreach (Data.JHSCAttendRecord record in _scattends) { CourseRecord course = Course.Instance.Items[record.RefCourseID]; if ("" + course.SchoolYear != cboSchoolYear.Text) { continue; } if ("" + course.Semester != cboSemester.Text) { continue; } if (!subjects.ContainsKey(course.Subject)) { continue; } RankData rankData = data[course.Subject]; decimal? score = null; if (dic_sce.ContainsKey(record.ID)) { Data.JHSCETakeRecord jr = dic_sce[record.ID]; score = jr.Score; } if (rankData.ContainsKey(record.RefStudentID)) { throw new ArgumentException(string.Format("學生「{0}」在同一學期修習「{1}」科目一次以上。", Student.Instance[record.RefStudentID].Name, course.Subject)); } if (score.HasValue) { rankData.Add(record.RefStudentID, new RankScore(score.Value, null)); } else if (UseZero) { rankData.Add(record.RefStudentID, new RankScore(0, null)); } } foreach (RankData rankData in data.Values) { _collector.AddRankData(rankData); } #endregion } else { #region 運算排名 List <string> studentIDs = _students.AsKeyList(); Dictionary <string, RankData> data = new Dictionary <string, RankData>(); foreach (string each in subjects.Keys) { data.Add(each, new RankData()); data[each].Name = each; } foreach (Data.JHSCAttendRecord record in _scattends) { CourseRecord course = Course.Instance.Items[record.RefCourseID]; if ("" + course.SchoolYear != cboSchoolYear.Text) { continue; } if ("" + course.Semester != cboSemester.Text) { continue; } if (!subjects.ContainsKey(course.Subject)) { continue; } RankData rankData = data[course.Subject]; decimal? score = null; if (dic_sce.ContainsKey(record.ID)) { Data.JHSCETakeRecord jr = dic_sce[record.ID]; score = jr.Score; } if (rankData.ContainsKey(record.RefStudentID)) { throw new ArgumentException(string.Format("學生「{0}」在同一學期修習「{1}」科目一次以上。", Student.Instance[record.RefStudentID].Name, course.Subject)); } if (score.HasValue) { rankData.Add(record.RefStudentID, new RankScore(score.Value, null)); } else if (UseZero) { rankData.Add(record.RefStudentID, new RankScore(0, null)); } } RankData combineData = new RankData(); combineData.Name = CombineRankType.ToString(); //<學生編號,<科目名稱,成績>> Dictionary <string, Dictionary <string, decimal> > combineScore = new Dictionary <string, Dictionary <string, decimal> >(); decimal periodCount = 0; foreach (RankData rankData in data.Values) //每一個 rankData 是特定科目的所有成績。 { foreach (string stuId in rankData.Keys) //id 是學生編號。 { if (!combineScore.ContainsKey(stuId)) { combineScore.Add(stuId, new Dictionary <string, decimal>()); } if (!combineScore[stuId].ContainsKey(rankData.Name)) //rankData.Name 是科目名稱。 { if (CombineRankType == CombineRankType.加權平均 || CombineRankType == CombineRankType.加權總分) { combineScore[stuId].Add(rankData.Name, rankData[stuId].Score * subjects[rankData.Name]); } else { combineScore[stuId].Add(rankData.Name, rankData[stuId].Score); } } } //加總科目權重。 if (CombineRankType == CombineRankType.加權平均) { periodCount += subjects[rankData.Name]; } } foreach (string id in combineScore.Keys) { decimal total = 0; foreach (decimal score in combineScore[id].Values) { total += score; } if (CombineRankType == CombineRankType.加權平均) { total = total / periodCount; } else if (CombineRankType == CombineRankType.算數平均) { total = total / combineScore[id].Count; } if (!combineData.ContainsKey(id)) { total = decimal.Round(total, Carry, MidpointRounding.AwayFromZero); combineData.Add(id, new RankScore(total, null)); } } combineData.Tag = data; _collector.AddRankData(combineData); #endregion } _collector.Perform(); _collector.Export(rbCombine.Checked, rbAllRank.Checked, txtTopRank.Text, (chkPrintByClass.Enabled) ? chkPrintByClass.Checked : false, _reportName); #endregion } #endregion } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
private void btnRank_Click(object sender, EventArgs e) { if (!IsValid()) { MsgBox.Show("輸入資料不正確,請先修正。"); return; } _collector.Clear(); if (Type.Equals("Semester")) { #region 學期成績排名 if (Category.Equals("Subject")) { #region 科目 Dictionary <string, decimal> selectedItems = GetSelectedItems(); List <string> studentIDs = Students.AsKeyList(); Dictionary <string, RankData> ranks = ArrangeBySubject(selectedItems); if (rbSeparate.Checked) { #region 分科排名 foreach (RankData rankData in ranks.Values) { _collector.AddRankData(rankData); } #endregion } else { #region 運算排名 RankData combinRank = new RankData(); combinRank.Name = CombineRankType.ToString(); //StudentID->Subject->Score Dictionary <string, Dictionary <string, decimal> > combineScore = new Dictionary <string, Dictionary <string, decimal> >(); decimal periodCount = 0; foreach (RankData eachRank in ranks.Values) { foreach (string studId in eachRank.Keys) //學生編號 { if (!combineScore.ContainsKey(studId)) { combineScore.Add(studId, new Dictionary <string, decimal>()); } if (!combineScore[studId].ContainsKey(eachRank.Name)) { if (CombineRankType == CombineRankType.加權平均 || CombineRankType == CombineRankType.加權總分) { combineScore[studId].Add(eachRank.Name, eachRank[studId].Score * selectedItems[eachRank.Name]); } else { combineScore[studId].Add(eachRank.Name, eachRank[studId].Score); } } } if (CombineRankType == CombineRankType.加權平均) { periodCount += selectedItems[eachRank.Name]; } } foreach (string id in combineScore.Keys) { decimal total = 0; foreach (decimal score in combineScore[id].Values) { total += score; } if (CombineRankType == CombineRankType.加權平均) { total = total / periodCount; } else if (CombineRankType == CombineRankType.算數平均) { total = total / combineScore[id].Count; } if (!combinRank.ContainsKey(id)) { total = decimal.Round(total, Carry, MidpointRounding.AwayFromZero); combinRank.Add(id, new RankScore(total, null)); } } combinRank.Tag = ranks; _collector.AddRankData(combinRank); #endregion } _collector.Perform(); _collector.Export(rbCombine.Checked, rbAllRank.Checked, txtTopRank.Text, (chkPrintByClass.Enabled) ? chkPrintByClass.Checked : false, _reportName); #endregion } else if (Category.Equals("Domain")) { #region 領域 //Dictionary<string, decimal> domains = new Dictionary<string, decimal>(); //foreach (DataGridViewRow row in dgv.Rows) //{ // string value = "" + row.Cells[chCheck.Index].Value; // bool b; // if (bool.TryParse(value, out b) && b == true) // { // string domain = "" + row.Cells[chCategory.Index].Value; // decimal period = decimal.Parse("" + row.Cells[chPeriod.Index].Value); // domains.Add(domain, period); // } //} //if (rbSeparate.Checked) //{ // #region 分科排名 // List<string> studentIDs = Students.AsKeyList(); // Dictionary<string, RankData> data = new Dictionary<string, RankData>(); // foreach (Data.JHSemesterScoreRecord record in _semsScores) // { // if (!studentIDs.Contains(record.RefStudentID)) continue; // foreach (K12.Data.DomainScore domain in record.Domains.Values) // { // if (!domains.ContainsKey(domain.Domain)) continue; // if (!data.ContainsKey(domain.Domain)) // data.Add(domain.Domain, new RankData()); // RankData rankData = data[domain.Domain]; // rankData.Name = domain.Domain; // if (!rankData.ContainsKey(record.RefStudentID)) // { // if (domain.Score.HasValue) // rankData.Add(record.RefStudentID, new RankScore(domain.Score.Value, null)); // //else if (UseZero) // // rankData.Add(record.RefStudentID, new RankScore(0, null)); // } // } // string learnDomain = "學習領域"; // if (domains.ContainsKey(learnDomain)) // { // if (!data.ContainsKey(learnDomain)) // data.Add(learnDomain, new RankData()); // RankData rankData = data[learnDomain]; // rankData.Name = learnDomain; // if (!rankData.ContainsKey(record.RefStudentID)) // { // if (record.LearnDomainScore.HasValue) // rankData.Add(record.RefStudentID, new RankScore(record.LearnDomainScore.Value, null)); // //else if (UseZero) // // rankData.Add(record.RefStudentID, new RankScore(0, null)); // } // } // string courseLearn = "課程學習"; // if (domains.ContainsKey(courseLearn)) // { // if (!data.ContainsKey(courseLearn)) // data.Add(courseLearn, new RankData()); // RankData rankData = data[courseLearn]; // rankData.Name = courseLearn; // if (!rankData.ContainsKey(record.RefStudentID)) // { // if (record.CourseLearnScore.HasValue) // rankData.Add(record.RefStudentID, new RankScore(record.CourseLearnScore.Value, null)); // //else if (UseZero) // // rankData.Add(record.RefStudentID, new RankScore(0, null)); // } // } // } // foreach (RankData rankData in data.Values) // _collector.AddRankData(rankData); // #endregion //} //else //{ // #region 運算排名 // List<string> studentIDs = Students.AsKeyList(); // Dictionary<string, RankData> data = new Dictionary<string, RankData>(); // foreach (Data.JHSemesterScoreRecord record in _semsScores) // { // if (!studentIDs.Contains(record.RefStudentID)) continue; // foreach (K12.Data.DomainScore domain in record.Domains.Values) // { // if (!domains.ContainsKey(domain.Domain)) continue; // if (!data.ContainsKey(domain.Domain)) // data.Add(domain.Domain, new RankData()); // RankData rankData = data[domain.Domain]; // rankData.Name = domain.Domain; // if (!rankData.ContainsKey(record.RefStudentID)) // { // if (domain.Score.HasValue) // rankData.Add(record.RefStudentID, new RankScore(domain.Score.Value, null)); // //else if (UseZero) // // rankData.Add(record.RefStudentID, new RankScore(0, null)); // } // } // } // RankData combineData = new RankData(); // combineData.Name = CombineRankType.ToString(); // Dictionary<string, Dictionary<string, decimal>> combineScore = new Dictionary<string, Dictionary<string, decimal>>(); // decimal periodCount = 0; // foreach (RankData rankData in data.Values) // { // foreach (string id in rankData.Keys) // { // if (!combineScore.ContainsKey(id)) // combineScore.Add(id, new Dictionary<string, decimal>()); // if (!combineScore[id].ContainsKey(rankData.Name)) // { // if (CombineRankType == CombineRankType.加權平均 || CombineRankType == CombineRankType.加權總分) // combineScore[id].Add(rankData.Name, rankData[id].Score * domains[rankData.Name]); // else // combineScore[id].Add(rankData.Name, rankData[id].Score); // } // } // if (CombineRankType == CombineRankType.加權平均) // periodCount += domains[rankData.Name]; // } // foreach (string id in combineScore.Keys) // { // decimal total = 0; // foreach (decimal score in combineScore[id].Values) // total += score; // if (CombineRankType == CombineRankType.加權平均) // total = total / periodCount; // else if (CombineRankType == CombineRankType.算數平均) // total = total / combineScore[id].Count; // if (!combineData.ContainsKey(id)) // { // total = decimal.Round(total, Carry, MidpointRounding.AwayFromZero); // combineData.Add(id, new RankScore(total, null)); // } // } // combineData.Tag = data; // _collector.AddRankData(combineData); // #endregion //} //_collector.Perform(); //_collector.Export(rbCombine.Checked, rbAllRank.Checked, txtTopRank.Text, (chkPrintByClass.Enabled) ? chkPrintByClass.Checked : false, _reportName); #endregion } #endregion } }