Ejemplo n.º 1
0
        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);
                }
            }
        }
Ejemplo n.º 2
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            dgv.EndEdit();

            if (!IsValid())
            {
                return;
            }

            try
            {
                List <Data.JHSCETakeRecord> sceUpdateList = new List <JHSchool.Data.JHSCETakeRecord>();
                List <Data.JHSCETakeRecord> sceInsertList = new List <JHSchool.Data.JHSCETakeRecord>();
                List <Data.JHSCETakeRecord> sceDeleteList = new List <JHSchool.Data.JHSCETakeRecord>();

                bool scattendNeedSave = false;

                foreach (DataGridViewRow row in dgv.Rows)
                {
                    if (row.IsNewRow)
                    {
                        continue;
                    }

                    if ("" + row.Tag == "課程總成績")
                    {
                        #region 課程總成績
                        if ("" + row.Cells[chScore.Index].Value != "" + _scattend.Score)
                        {
                            scattendNeedSave = true;
                            decimal d;
                            if (decimal.TryParse("" + row.Cells[chScore.Index].Value, out d))
                            {
                                _scattend.Score = d;
                            }
                            else
                            {
                                _scattend.Score = null;
                            }
                        }
                        if ("" + row.Cells[chEffort.Index].Value != "" + _scattend.Effort)
                        {
                            scattendNeedSave = true;
                            int i;
                            if (int.TryParse("" + row.Cells[chEffort.Index].Value, out i))
                            {
                                _scattend.Effort = i;
                            }
                            else
                            {
                                _scattend.Effort = null;
                            }
                        }
                        if ("" + row.Cells[chText.Index].Value != _scattend.Text)
                        {
                            scattendNeedSave = true;
                            _scattend.Text   = "" + row.Cells[chText.Index].Value;
                        }
                        #endregion
                    }
                    else if (row.Tag != null)
                    {
                        #region  評量成績記錄的情況
                        Data.JHSCETakeRecord sce = row.Tag as Data.JHSCETakeRecord;

                        if (!string.IsNullOrEmpty("" + row.Cells[chScore.Index].Value))
                        {
                            sce.Score = decimal.Parse("" + row.Cells[chScore.Index].Value);
                        }
                        else
                        {
                            sce.Score = null;
                        }

                        if (!string.IsNullOrEmpty("" + row.Cells[chEffort.Index].Value))
                        {
                            sce.Effort = int.Parse("" + row.Cells[chEffort.Index].Value);
                        }
                        else
                        {
                            sce.Effort = null;
                        }

                        sce.Text = "" + row.Cells[chText.Index].Value;

                        if (!sce.Score.HasValue && !sce.Effort.HasValue && string.IsNullOrEmpty(sce.Text))
                        {
                            sceDeleteList.Add(sce);
                        }
                        else
                        {
                            sceUpdateList.Add(sce);
                        }
                        #endregion
                    }
                    else
                    {
                        #region 無評量成績記錄的情況
                        bool needsave = false;
                        if (!string.IsNullOrEmpty("" + row.Cells[chScore.Index].Value))
                        {
                            needsave = true;
                        }
                        if (!string.IsNullOrEmpty("" + row.Cells[chEffort.Index].Value))
                        {
                            needsave = true;
                        }
                        if (!string.IsNullOrEmpty("" + row.Cells[chText.Index].Value))
                        {
                            needsave = true;
                        }
                        if (needsave)
                        {
                            Data.JHSCETakeRecord sce = new Data.JHSCETakeRecord();
                            sce.RefCourseID   = _course.ID;
                            sce.RefExamID     = "" + row.Cells[chExamName.Index].Tag;
                            sce.RefSCAttendID = _scattend != null ? _scattend.ID : "";
                            sce.RefStudentID  = _student.ID;
                            if (!string.IsNullOrEmpty("" + row.Cells[chScore.Index].Value))
                            {
                                sce.Score = decimal.Parse("" + row.Cells[chScore.Index].Value);
                            }
                            else
                            {
                                sce.Score = null;
                            }

                            if (!string.IsNullOrEmpty("" + row.Cells[chEffort.Index].Value))
                            {
                                sce.Effort = int.Parse("" + row.Cells[chEffort.Index].Value);
                            }
                            else
                            {
                                sce.Effort = null;
                            }

                            sce.Text = "" + row.Cells[chText.Index].Value;
                            sceInsertList.Add(sce);
                        }
                        #endregion
                    }
                }

                if (sceUpdateList.Count > 0)
                {
                    Data.JHSCETake.Update(sceUpdateList);
                }
                if (sceInsertList.Count > 0)
                {
                    Data.JHSCETake.Insert(sceInsertList);
                }
                if (sceDeleteList.Count > 0)
                {
                    Data.JHSCETake.Delete(sceDeleteList);
                }

                if (scattendNeedSave)
                {
                    Data.JHSCAttend.Update(_scattend);
                }

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                MsgBox.Show("儲存失敗。" + ex.Message);
            }
        }