public bool AddSubject(NewSubject ns)
        {
            SubjectsData sd = this.repository.GetSubjectDataForAdding(ns.Name, ns.DepartamentId, ns.Ects, ns.FacultyId, ns.InstituteId, ns.IsExam, ns.PlanId, ns.SemesterId, ns.Specializations);
            if (sd != null)
                ns.AddError("Przedmiot o podanych danych już\nistnieje w planie");

            if (ns.IsElective && ns.IsGeneral)
                ns.AddError("Przedmiot nie może być\njednocześnie obowiązkowy i obieralny");

            if (!ns.IsElective && !ns.IsGeneral && (ns.Specializations == null || ns.Specializations.Count() == 0))
                ns.AddError("Przedmiot musi być obowiązkowy\nlub obieralny dla wszystkich lub\nprzynajmniej na jednej specjalności");

            if (ns.SubjectTypes == null || ns.SubjectTypes.Count() == 0)
                ns.AddError("Przedmiot musi być przypisany\nprzynajmniej do jednego typu");

            if(ns.Specializations != null)
            foreach(NewSpecializationData nsd in ns.Specializations)
                if (!nsd.IsElective && !nsd.IsGenereal)
                {
                    ns.AddError("Przedmiot na specjalizacji musi być\nobowiązkowy lub obieralny");
                    break;
                }

            if (ns != null && ns.IsValid)
            {
                this.repository.AddSubject(ns);
                return true;
            }
            return false;
        }
Пример #2
0
        private void btnAddSubject_Click(object sender, EventArgs e)
        {
            lblValidation.Text = string.Empty;
            Institute institute = null;
            if (!cbInstitute.SelectedItem.ToString().Equals("Brak"))
                institute = InstituteController.Instance.GetInstitute(cbInstitute.SelectedItem.ToString(), plan.DepartamentID);

            if (institute == null && !cbInstitute.SelectedItem.ToString().Equals("Brak"))
            {
                MessageBox.Show("Wybrany instytut ju¿ nie istnieje");
                FillWithInstitutes();
                return;
            }

            Semester semester = SemesterController.Instance.GetSemester(cbSemester.SelectedItem.ToString());
            
            if(semester == null)
            {
                MessageBox.Show("Wybrany semestr ju¿ nie istnieje");
                FillWithSemesters();
                return;
            }

            List<NewSubjectTypeData> nstdlist = new List<NewSubjectTypeData>();
            for (int i = 0; i < dgSubjectTypes.Rows.Count; i++)
            { 
                if(Convert.ToInt32(dgSubjectTypes.Rows[i].Cells["hours"].Value) > 0)
                {
                    SubjectType st = SubjectTypeController.Instance.GetSubjectType(dgSubjectTypes.Rows[i].Cells["subjectType"].Value.ToString());
                    if (st != null)
                    {
                        NewSubjectTypeData nstd = new NewSubjectTypeData()
                        {
                            Hours = Convert.ToInt32(dgSubjectTypes.Rows[i].Cells["hours"].Value),
                            SubjectTypeId = st.SubjectTypeID
                        };

                        nstdlist.Add(nstd);
                    }
                }
            }

            List<NewSpecializationData> nspdlist = new List<NewSpecializationData>();
            if (dgSpecializations.Enabled == true)
                for (int i = 0; i < dgSpecializations.Rows.Count; i++)
                {
                    if (dgSpecializations.Rows[i].Cells["specialization"].Value != null)
                    {
                        SpecializationEdit s = SpecializationController.Instance.GetSpecializationEdit(dgSpecializations.Rows[i].Cells["specialization"].Value.ToString());
                        if (s != null)
                        {
                            NewSpecializationData nspd = new NewSpecializationData()
                            {
                                IsElective = Convert.ToBoolean(dgSpecializations.Rows[i].Cells["elective"].Value),
                                IsGenereal = Convert.ToBoolean(dgSpecializations.Rows[i].Cells["general"].Value),
                                SpecializationId = s.SpecializationID
                            };

                            nspdlist.Add(nspd);
                        }
                    }
                }

            NewSubject subject = new NewSubject()
            {
                DepartamentId = plan.DepartamentID,
                Ects = Convert.ToDouble(seEcts.Value),
                FacultyId = plan.FacultyID,
                InstituteId = institute == null ? 0 : institute.InstituteID,
                IsExam = ckbxExam.Checked, 
                Name = tbSubjectName.Text,
                SemesterId = semester.SemesterID,
                SubjectTypes = nstdlist,
                PlanId = plan.PlanID,
                Specializations = nspdlist.Count > 0 ? nspdlist : null,
                IsElective = cbElective.Checked,
                IsGeneral = cbGeneral.Checked
            };

            if ((plan.SemesterStart.HasValue && plan.SemesterStart.Value > semester.Semester1)
                || (plan.SemesterEnd.HasValue && plan.SemesterEnd.Value < semester.Semester1))
                subject.AddError("W planie nie mo¿e\nbyæ przedmiotu na takim semestrze");

            if (SubjectController.Instance.AddSubject(subject))
                RadMessageBox.Show("Przedmiot zosta³ dodany", "Wiadomoœæ");
            else
            {
                string errors = string.Empty;
                foreach (string error in subject.Errors)
                    errors = errors + error + "\n";

                lblValidation.Text = errors;
            }
        }
        public void AddSubject(NewSubject subject)
        {
            Subject s = this.GetSubject(subject.Name);
            if (s == null)
            {
                Subject newSubject = new Subject()
                {
                    Name = subject.Name
                };
                this.AddSubjectName(newSubject);
            }

            if (subject.Specializations != null && subject.Specializations.Count() > 0 && subject.IsGeneral == false)
            {
                s = this.GetSubject(subject.Name);

                for (int i = 0; i < subject.Specializations.Count(); i++)
                {
                    SpecializationsData specDat = new SpecializationsData()
                    {
                        IsElective = subject.Specializations.ElementAt(i).IsElective,
                        IsGeneral = subject.Specializations.ElementAt(i).IsGenereal,
                        SpecializationID = subject.Specializations.ElementAt(i).SpecializationId
                    };

                    SPDatabase.DB.SpecializationsDatas.AddObject(specDat);

                    SubjectsData sd = new SubjectsData()
                    {
                        DepartamentID = subject.DepartamentId,
                        Ects = subject.Ects,
                        FacultyID = subject.FacultyId,
                        InstituteID = subject.InstituteId,
                        IsExam = subject.IsExam,
                        SemesterID = subject.SemesterId,
                        SpecializationDataID = specDat.SpecializationDataID,
                        SubjectID = s.SubjectID,
                        IsElective = false,
                        IsGeneral = false
                    };

                    if (subject.InstituteId > 0)
                        sd.InstituteID = subject.InstituteId;
                    else
                        sd.InstituteID = null;

                    Plan p = GetPlan(subject.PlanId);
                    if (p != null)
                        sd.Plans.Add(p);

                    foreach (NewSubjectTypeData d in subject.SubjectTypes)
                    {
                        SubjectTypesData std = new SubjectTypesData()
                        {
                            Hours = d.Hours,
                            SubjectTypeID = d.SubjectTypeId
                        };

                        sd.SubjectTypesDatas.Add(std);
                    }

                    SPDatabase.DB.SubjectsDatas.AddObject(sd);
                    SPDatabase.DB.SaveChanges();
                }
            }

            if(subject.IsGeneral || subject.IsElective)
            {
                s = this.GetSubject(subject.Name);

                SubjectsData sdd = new SubjectsData()
                {
                    DepartamentID = subject.DepartamentId,
                    Ects = subject.Ects,
                    FacultyID = subject.FacultyId,
                    IsExam = subject.IsExam,
                    SemesterID = subject.SemesterId,
                    SpecializationDataID = null,
                    SubjectID = s.SubjectID,
                    IsElective = subject.IsElective,
                    IsGeneral = subject.IsGeneral
                };

                if (subject.InstituteId > 0)
                    sdd.InstituteID = subject.InstituteId;
                else
                    sdd.InstituteID = null;

                Plan pp = GetPlan(subject.PlanId);
                if (pp != null)
                    sdd.Plans.Add(pp);

                foreach (NewSubjectTypeData d in subject.SubjectTypes)
                {
                    SubjectTypesData std = new SubjectTypesData()
                    {
                        Hours = d.Hours,
                        SubjectTypeID = d.SubjectTypeId
                    };

                    sdd.SubjectTypesDatas.Add(std);
                }

                SPDatabase.DB.SubjectsDatas.AddObject(sdd);
                SPDatabase.DB.SaveChanges();
            }
        }
        public void CopyArchivePlan(int sourcePlanId, int targetPlanId)
        {
            Plan source = this.repository.GetPlan(sourcePlanId);
            Plan target = this.repository.GetPlan(targetPlanId);
            
            if (source != null && target != null)
            {
                foreach (SubjectsData sd in source.SubjectsDatas)
                {
                    if (target.SemesterStart <= sd.Semester.Semester1 && target.SemesterEnd >= sd.Semester.Semester1)
                    {
                        NewSubject ns = new NewSubject()
                                            {
                                                PlanId = targetPlanId,
                                                DepartamentId = sd.DepartamentID,
                                                Ects = sd.Ects,
                                                FacultyId = sd.FacultyID,
                                                IsElective = sd.IsElective,
                                                IsExam = sd.IsExam,
                                                IsGeneral = sd.IsGeneral,
                                                Name = sd.Subject.Name,
                                                SemesterId = sd.SemesterID,
                                            };

                        if (sd.SpecializationsData != null)
                        {
                            List<NewSpecializationData> specList = new List<NewSpecializationData>();
                            NewSpecializationData nsd = new NewSpecializationData()
                                                            {
                                                                IsElective = sd.SpecializationsData.IsElective,
                                                                IsGenereal = sd.SpecializationsData.IsGeneral,
                                                                SpecializationId =
                                                                    sd.SpecializationsData.SpecializationID
                                                            };

                            specList.Add(nsd);
                            ns.Specializations = specList.AsEnumerable();
                        }

                        List<NewSubjectTypeData> nstdList = new List<NewSubjectTypeData>();

                        foreach (SubjectTypesData std in sd.SubjectTypesDatas)
                        {
                            NewSubjectTypeData nstd = new NewSubjectTypeData()
                                                          {
                                                              Hours = (float) std.Hours,
                                                              SubjectTypeId = std.SubjectTypeID
                                                          };

                            nstdList.Add(nstd);
                        }

                        ns.SubjectTypes = nstdList.AsEnumerable();

                        if (sd.InstituteID > 0)
                            ns.InstituteId = (int) sd.InstituteID;
                        else
                            sd.InstituteID = null;

                        SubjectController.Instance.AddSubject(ns);
                    }
                }
            }
        }