Exemplo n.º 1
0
 static void ExcuseJuniors()
 {
     using (WebhostEntities db = new WebhostEntities())
     {
         Term           term    = db.Terms.Find(DateRange.GetCurrentOrLastTerm());
         List <Student> Juniors = db.Students.Where(s => s.isActive && s.GraduationYear == 2017).ToList();
         foreach (Student student in Juniors)
         {
             List <int> classes = student.Sections.Where(sec => sec.Terms.Contains(term)).Select(sec => sec.id).ToList();
             AttendanceControl.ExcuseStudent(student.ID, classes, "On the Junior Trip", ADUser.AttendanceBot, DateTime.Today);
             Console.WriteLine("Excused {0} {1} from {2} classes.", student.FirstName, student.LastName, classes.Count);
         }
     }
     Console.WriteLine("Done!");
     Console.ReadKey();
 }
        protected void SubmitBtn_Click(object sender, EventArgs e)
        {
            LogInformation("Attempting To Submit an excused absence.");
            int studentId;

            try
            {
                studentId = Convert.ToInt32(StudentNameCBX.SelectedValue);
            }
            catch
            {
                ShowError("You must select a student to excuse.");
                return;
            }

            DateTime date = DateTime.Today;

            if (!TodayCB.Checked)
            {
                try
                {
                    date = DateRange.GetDateTimeFromString(DateInput.Text);
                }
                catch
                {
                    ShowError("Please select a date using the calendar.");
                    return;
                }
            }

            if (NotesInput.Text.Equals(""))
            {
                ShowError("You must enter a reason for the excused absence.");
                return;
            }

            List <int> selectedBlocks = new List <int>();

            foreach (ListItem item in BlocksCBL.Items)
            {
                if (item.Selected)
                {
                    selectedBlocks.Add(Convert.ToInt32(item.Value));
                }
            }

            using (WebhostEntities db = new WebhostEntities())
            {
                List <int> sectionIds = new List <int>();
                Term       theTerm    = null;

                foreach (Term term in db.Terms)
                {
                    DateRange termrange = new DateRange(term.StartDate, term.EndDate);
                    if (termrange.Contains(date))
                    {
                        theTerm = term;
                        break;
                    }
                }

                if (theTerm == null)
                {
                    ShowError("The selected Date is not in any Term.");
                    return;
                }

                foreach (int blkid in selectedBlocks)
                {
                    Block block = db.Blocks.Where(b => b.id == blkid).Single();
                    foreach (Section section in block.Sections.Where(sec => sec.Terms.Contains(theTerm)).ToList())
                    {
                        if (section.Students.Where(stu => stu.ID == studentId).Count() > 0)
                        {
                            sectionIds.Add(section.id);
                        }
                    }
                }

                if (MultiDayCB.Checked)
                {
                    DateTime end = new DateTime();
                    try
                    {
                        end = DateRange.GetDateTimeFromString(EndDateInput.Text);
                    }
                    catch
                    {
                        ShowError("Please select the End Date from the calendar.");
                        return;
                    }

                    String report = AttendanceControl.ExcuseStudent(studentId, sectionIds, NotesInput.Text, ((BasePage)Page).user, new DateRange(date, end));
                    State.log.WriteLine(report);
                }
                else
                {
                    String report = AttendanceControl.ExcuseStudent(studentId, sectionIds, NotesInput.Text, ((BasePage)Page).user, date);
                    State.log.WriteLine(report);
                }

                LogInformation("Excused attendance submitted for student id {0}.  See Webhost MySQL Connection log for details.", studentId);
            }
        }