public MemoryStream GenerateCSV()
        {
            MemoryStream outStream = new MemoryStream();
            StreamWriter writer    = new StreamWriter(outStream);

            // Headings
            writer.Write("School_id" + delimiter);
            writer.Write("Teacher_id" + delimiter);
            writer.Write("Teacher_number" + delimiter);
            writer.Write("State_teacher_id" + delimiter);
            writer.Write("Teacher_email" + delimiter);
            writer.Write("First_name" + delimiter);
            writer.Write("Middle_name" + delimiter);
            writer.Write("Last_name" + delimiter);
            writer.Write("Title" + delimiter);
            writer.Write("Username" + delimiter);
            writer.Write("Password" + delimiter);

            writer.Write(Environment.NewLine);

            StaffRepository    _staffRepo             = new StaffRepository(_dbConnectionString);
            List <StaffMember> staff                  = _staffRepo.GetAll();
            List <string>      seenTeacherCertNumbers = new List <string>();

            // Get all sections
            SchoolClassRepository screpo   = new SchoolClassRepository(_dbConnectionString);
            List <SchoolClass>    sections = screpo.GetAll();

            // Get all teacher assignments
            TeacherAssignmentRepository teacherAssignmentRepo  = new TeacherAssignmentRepository(_dbConnectionString);
            List <TeacherAssignment>    allTeachingAssignments = teacherAssignmentRepo.GetAll();

            // Sort teacher assignments into a dictionary for easier consumption
            // Only send up classes that have teachers assigned
            Dictionary <int, List <TeacherAssignment> > assignmentsByClassID = new Dictionary <int, List <TeacherAssignment> >();

            foreach (TeacherAssignment ta in allTeachingAssignments)
            {
                if (ta.Class != null)
                {
                    if (!assignmentsByClassID.ContainsKey(ta.Class.iClassID))
                    {
                        assignmentsByClassID.Add(ta.Class.iClassID, new List <TeacherAssignment>());
                    }
                    if (ta.Teacher.TeachingCertificateNumber.Length > 0)
                    {
                        assignmentsByClassID[ta.Class.iClassID].Add(ta);
                    }
                }
            }

            // Now make a list of teachers we need
            Dictionary <int, StaffMember> activeTeachers = new Dictionary <int, StaffMember>();

            foreach (int classID in assignmentsByClassID.Keys)
            {
                foreach (TeacherAssignment ta in assignmentsByClassID[classID])
                {
                    if (ta.Teacher != null)
                    {
                        if (!activeTeachers.ContainsKey(ta.Teacher.iStaffId))
                        {
                            activeTeachers.Add(ta.Teacher.iStaffId, ta.Teacher);
                        }
                    }
                }
            }

            // Now make the CSV
            foreach (StaffMember teacher in activeTeachers.Values)
            {
                if ((teacher.School.DAN != "0") && (teacher.School.DAN.Length > 1) && (!string.IsNullOrEmpty(teacher.TeachingCertificateNumber) && (!seenTeacherCertNumbers.Contains(teacher.TeachingCertificateNumber))))
                {
                    seenTeacherCertNumbers.Add(teacher.TeachingCertificateNumber);
                    writer.Write(stringContainer + teacher.School.DAN + stringContainer + delimiter);                // School id
                    writer.Write(stringContainer + teacher.TeachingCertificateNumber + stringContainer + delimiter); // Teacher id
                    writer.Write(stringContainer + teacher.TeachingCertificateNumber + stringContainer + delimiter); // Teacher number
                    writer.Write(stringContainer + "" + stringContainer + delimiter);                                // State teacher id
                    writer.Write(stringContainer + teacher.EmailAddress + stringContainer + delimiter);              // teacher email
                    writer.Write(stringContainer + teacher.FirstName + stringContainer + delimiter);                 // firstname
                    writer.Write(stringContainer + "" + stringContainer + delimiter);                                // middlename
                    writer.Write(stringContainer + teacher.LastName + stringContainer + delimiter);                  // lastname
                    writer.Write(stringContainer + "" + stringContainer + delimiter);                                // title
                    writer.Write(stringContainer + teacher.EmailAddress + stringContainer + delimiter);              // username
                    writer.Write(stringContainer + "" + stringContainer + delimiter);                                // password
                    writer.Write(Environment.NewLine);
                }
            }

            writer.Flush();
            outStream.Flush();
            return(outStream);
        }
        public MemoryStream GenerateCSV()
        {
            MemoryStream outStream = new MemoryStream();
            StreamWriter writer    = new StreamWriter(outStream);

            // Headings
            writer.Write("School_id" + delimiter);
            writer.Write("Staff_id" + delimiter);
            writer.Write("Staff_email" + delimiter);
            writer.Write("First_name" + delimiter);
            writer.Write("Last_name" + delimiter);
            writer.Write("Department" + delimiter);
            writer.Write("Title" + delimiter);
            writer.Write("Username" + delimiter);
            writer.Write("Password" + delimiter);

            writer.Write(Environment.NewLine);

            StaffRepository    _staffRepo             = new StaffRepository(_dbConnectionString);
            List <StaffMember> allStaff               = _staffRepo.GetAll();
            List <string>      seenTeacherCertNumbers = new List <string>();

            // Get all sections
            SchoolClassRepository screpo   = new SchoolClassRepository(_dbConnectionString);
            List <SchoolClass>    sections = screpo.GetAll();

            // Get all teacher assignments
            TeacherAssignmentRepository teacherAssignmentRepo  = new TeacherAssignmentRepository(_dbConnectionString);
            List <TeacherAssignment>    allTeachingAssignments = teacherAssignmentRepo.GetAll();

            // Sort teacher assignments into a dictionary for easier consumption
            // Only send up classes that have teachers assigned
            Dictionary <int, List <TeacherAssignment> > assignmentsByClassID = new Dictionary <int, List <TeacherAssignment> >();

            foreach (TeacherAssignment ta in allTeachingAssignments)
            {
                if (ta.Class != null)
                {
                    if (!assignmentsByClassID.ContainsKey(ta.Class.iClassID))
                    {
                        assignmentsByClassID.Add(ta.Class.iClassID, new List <TeacherAssignment>());
                    }
                    if (ta.Teacher.TeachingCertificateNumber.Length > 0)
                    {
                        assignmentsByClassID[ta.Class.iClassID].Add(ta);
                    }
                }
            }

            // Now make a list of teachers we need
            Dictionary <int, StaffMember> activeTeachers = new Dictionary <int, StaffMember>();

            foreach (int classID in assignmentsByClassID.Keys)
            {
                foreach (TeacherAssignment ta in assignmentsByClassID[classID])
                {
                    if (ta.Teacher != null)
                    {
                        if (!activeTeachers.ContainsKey(ta.Teacher.iStaffId))
                        {
                            activeTeachers.Add(ta.Teacher.iStaffId, ta.Teacher);
                        }
                    }
                }
            }

            // Now find all the teachers who aren't on the above list
            List <StaffMember> activeStaffWithoutClasses = new List <StaffMember>();

            foreach (StaffMember teacher in allStaff)
            {
                if ((!activeTeachers.ContainsKey(teacher.iStaffId)) && teacher.IsEnabled)
                {
                    activeStaffWithoutClasses.Add(teacher);
                }
            }

            // Now make the CSV
            foreach (StaffMember staff in activeStaffWithoutClasses)
            {
                if (!string.IsNullOrEmpty(staff.EmailAddress))
                {
                    string schoolID = staff.School.DAN;
                    if (staff.School.isFake)
                    {
                        schoolID = "DEFAULT_DISTRICT_OFFICE";
                    }

                    writer.Write(stringContainer + schoolID + stringContainer + delimiter);           // School id
                    writer.Write(stringContainer + staff.EmailAddress + stringContainer + delimiter); // Staff id
                    writer.Write(stringContainer + staff.EmailAddress + stringContainer + delimiter); // staff email
                    writer.Write(stringContainer + staff.FirstName + stringContainer + delimiter);    // firstname
                    writer.Write(stringContainer + staff.LastName + stringContainer + delimiter);     // lastname
                    writer.Write(stringContainer + "" + stringContainer + delimiter);                 // department
                    writer.Write(stringContainer + "" + stringContainer + delimiter);                 // title
                    writer.Write(stringContainer + staff.EmailAddress + stringContainer + delimiter); // username
                    writer.Write(stringContainer + "" + stringContainer + delimiter);                 // password
                    writer.Write(Environment.NewLine);
                }
            }

            writer.Flush();
            outStream.Flush();
            return(outStream);
        }
Пример #3
0
        public MemoryStream GenerateCSV()
        {
            MemoryStream outStream = new MemoryStream();
            StreamWriter writer    = new StreamWriter(outStream);

            // Headings
            writer.Write("School_id" + delimiter);
            writer.Write("Section_id" + delimiter);
            writer.Write("Teacher_id" + delimiter);
            writer.Write("Teacher_2_id" + delimiter);
            writer.Write("Teacher_3_id" + delimiter);
            writer.Write("Teacher_4_id" + delimiter);
            writer.Write("Teacher_5_id" + delimiter);
            writer.Write("Teacher_6_id" + delimiter);
            writer.Write("Teacher_7_id" + delimiter);
            writer.Write("Teacher_8_id" + delimiter);
            writer.Write("Teacher_9_id" + delimiter);
            writer.Write("Teacher_10_id" + delimiter);
            writer.Write("Name" + delimiter);
            writer.Write("Section_number" + delimiter);
            writer.Write("Grade" + delimiter);
            writer.Write("Course_name" + delimiter);
            writer.Write("Course_number" + delimiter);
            writer.Write("Course_description" + delimiter);
            writer.Write("Period" + delimiter);
            writer.Write("Subject" + delimiter);
            writer.Write("Term_name" + delimiter);
            writer.Write("Term_start" + delimiter);
            writer.Write("Term_end" + delimiter);

            writer.Write(Environment.NewLine);

            SchoolClassRepository screpo   = new SchoolClassRepository(_dbConnectionString);
            List <SchoolClass>    sections = screpo.GetAll();

            TeacherAssignmentRepository teacherAssignmentRepo  = new TeacherAssignmentRepository(_dbConnectionString);
            List <TeacherAssignment>    allTeachingAssignments = teacherAssignmentRepo.GetAll();

            // Sort teacher assignments into a dictionary for easier consumption
            // Only send up classes that have teachers assigned
            Dictionary <int, List <TeacherAssignment> > assignmentsByClassID = new Dictionary <int, List <TeacherAssignment> >();

            // Don't send empty classes, so get enrolment counts for each class
            StudentClassEnrolmentRepository enrolmentRepo            = new StudentClassEnrolmentRepository(_dbConnectionString);
            Dictionary <int, int>           enrolmentCountsByClassID = enrolmentRepo.GetEnrolmentCountsByClassID();

            foreach (TeacherAssignment ta in allTeachingAssignments)
            {
                if (ta.Class != null)
                {
                    if (!assignmentsByClassID.ContainsKey(ta.Class.iClassID))
                    {
                        assignmentsByClassID.Add(ta.Class.iClassID, new List <TeacherAssignment>());
                    }
                    if (ta.Teacher.TeachingCertificateNumber.Length > 0)
                    {
                        assignmentsByClassID[ta.Class.iClassID].Add(ta);
                    }
                }
            }

            foreach (SchoolClass sc in sections)
            {
                if (enrolmentCountsByClassID.ContainsKey(sc.iClassID))
                {
                    if (enrolmentCountsByClassID[sc.iClassID] > 0)
                    {
                        if (assignmentsByClassID.ContainsKey(sc.iClassID))
                        {
                            List <TeacherAssignment> thisClassAssignments = assignmentsByClassID[sc.iClassID];
                            if (thisClassAssignments.Count > 0)
                            {
                                writer.Write(stringContainer + sc.School.DAN + stringContainer + delimiter);                                                                                                            // School id
                                writer.Write(stringContainer + sc.iClassID + stringContainer + delimiter);                                                                                                              // Section id
                                writer.Write(stringContainer + (thisClassAssignments.Count >= 1 ? thisClassAssignments[0].Teacher.TeachingCertificateNumber.ToString() : string.Empty) + stringContainer + delimiter);  // Teacher 1
                                writer.Write(stringContainer + (thisClassAssignments.Count >= 2 ? thisClassAssignments[1].Teacher.TeachingCertificateNumber.ToString() : string.Empty) + stringContainer + delimiter);  // Teacher 2
                                writer.Write(stringContainer + (thisClassAssignments.Count >= 3 ? thisClassAssignments[2].Teacher.TeachingCertificateNumber.ToString() : string.Empty) + stringContainer + delimiter);  // Teacher 3
                                writer.Write(stringContainer + (thisClassAssignments.Count >= 4 ? thisClassAssignments[3].Teacher.TeachingCertificateNumber.ToString() : string.Empty) + stringContainer + delimiter);  // Teacher 4
                                writer.Write(stringContainer + (thisClassAssignments.Count >= 5 ? thisClassAssignments[4].Teacher.TeachingCertificateNumber.ToString() : string.Empty) + stringContainer + delimiter);  // Teacher 5
                                writer.Write(stringContainer + (thisClassAssignments.Count >= 6 ? thisClassAssignments[5].Teacher.TeachingCertificateNumber.ToString() : string.Empty) + stringContainer + delimiter);  // Teacher 6
                                writer.Write(stringContainer + (thisClassAssignments.Count >= 7 ? thisClassAssignments[6].Teacher.TeachingCertificateNumber.ToString() : string.Empty) + stringContainer + delimiter);  // Teacher 7
                                writer.Write(stringContainer + (thisClassAssignments.Count >= 8 ? thisClassAssignments[7].Teacher.TeachingCertificateNumber.ToString() : string.Empty) + stringContainer + delimiter);  // Teacher 8
                                writer.Write(stringContainer + (thisClassAssignments.Count >= 9 ? thisClassAssignments[8].Teacher.TeachingCertificateNumber.ToString() : string.Empty) + stringContainer + delimiter);  // Teacher 9
                                writer.Write(stringContainer + (thisClassAssignments.Count >= 10 ? thisClassAssignments[9].Teacher.TeachingCertificateNumber.ToString() : string.Empty) + stringContainer + delimiter); // Teacher 10
                                writer.Write(stringContainer + sc.Name + stringContainer + delimiter);                                                                                                                  // Name
                                writer.Write(stringContainer + sc.Section + stringContainer + delimiter);                                                                                                               // Section number
                                writer.Write(stringContainer + "" + stringContainer + delimiter);                                                                                                                       // Grade
                                writer.Write(stringContainer + sc.Course.Name + stringContainer + delimiter);                                                                                                           // Course Name
                                writer.Write(stringContainer + sc.Course.CourseCode + stringContainer + delimiter);                                                                                                     // Course Number
                                writer.Write(stringContainer + "" + stringContainer + delimiter);                                                                                                                       // Course Description
                                writer.Write(stringContainer + "" + stringContainer + delimiter);                                                                                                                       // Period
                                writer.Write(stringContainer + "" + stringContainer + delimiter);                                                                                                                       // Subject
                                writer.Write(stringContainer + "" + stringContainer + delimiter);                                                                                                                       // Term name
                                writer.Write(stringContainer + "" + stringContainer + delimiter);                                                                                                                       // Term start
                                writer.Write(stringContainer + "" + stringContainer + delimiter);                                                                                                                       // Term end
                                writer.Write(Environment.NewLine);
                            }
                        }
                    }
                }
            }

            writer.Flush();
            outStream.Flush();
            return(outStream);
        }