protected void Page_Load(object sender, EventArgs e)
        {
            string htmlOutput = "";
            int moduleID = Convert.ToInt32(Request.QueryString["module"]);

            //get module name
            ModuleHandler moduleHandler = new ModuleHandler();
            Module module = new Module();
            module = moduleHandler.GetModuleDetails(Convert.ToInt32(moduleID));

            litHeader.Text = "<div class='alert alert-info'>" + module.ModuleCode + "</div>";

            StudentHandler studentHandler = new StudentHandler();
            List<Student> listStudents = studentHandler.GetStudentList(moduleID);

            //Check to make sure there is students in the system assigned to the module
            if (listStudents == null)
                litHeader.Text = "<h3>There are currently no students linked to this module</h3>";
            else
            {
                htmlOutput = "<tr><th>Student number</th><th>Firstname</th><th>Surname</th></tr>\n";
                //add modules to table as it is generated
                for (int i = 0; i < listStudents.Count; i++)
                {
                    htmlOutput += "<tr><td>" + listStudents[i].StudentNumber + "</td><td>" + listStudents[i].FirstName + "</td><td>" + listStudents[i].Surname + "</td></tr>\n";
                }
            }
            litStudentList.Text = htmlOutput;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            //test url: http://localhost:7820/SignIn.aspx?id=1&num=235346
            int rollCallID = Convert.ToInt32(Request.QueryString["id"]);
            string studentNumber = Request.QueryString["num"];

            //find roll call and check if it is active
            RollCallHandler rollCallHandler = new RollCallHandler();
            RollCall rollCall = new RollCall();

            rollCall = rollCallHandler.GetRollCallDetails(rollCallID);

            //check if auto disable exists
            if (rollCall.AutoDisable != "")
            {
                DateTime autoDisableDate = DateTime.Parse(rollCall.AutoDisable);

                if (DateTime.Compare(autoDisableDate, DateTime.Now) < 0)
                {
                    //auto disabl date reached, disable roll call
                    rollCallHandler.EndRollCall(rollCallID);
                    rollCall.Status = "disabled";
                }
            }

            if (rollCall.Status == "enabled")
            {

                //get student details
                StudentHandler studentHandler = new StudentHandler();
                Student student = new Student();
                student = studentHandler.GetStudentID(studentNumber);

                //sign in
                Student_RollCallHandler student_RollCallHandler = new Student_RollCallHandler();
                Student_RollCall student_RollCall = new Student_RollCall();

                student_RollCall.RollCallID = rollCallID;
                student_RollCall.StudentID = student.StudentID;
                student_RollCall.Status = "present";
                student_RollCall.TimeOfSignIn = DateTime.Now.ToString();

                student_RollCallHandler.AddNewRollCall(student_RollCall);

            }
            else
            {

            }
        }
        protected void btnGetReport_Click(object sender, EventArgs e)
        {
            string htmlOutput = "<thead><tr><th>Date <i class='fa fa-sort'></i></th><th>Status <i class='fa fa-sort'></i></th></tr></thead>";
            litReport.Text = "";
            //for each roll call held show status of selected student

            RollCallHandler rollCallHandler = new RollCallHandler();
            //get list of roll calls held for the module
            List<RollCall> listRollCalls = rollCallHandler.GetRollCallList(Convert.ToInt32(dlModules.SelectedValue));

            //get a students attendance for each of the found roll calls
            Student_RollCall student_RollCall = new Student_RollCall();
            Student_RollCallHandler student_RollCallHandler = new Student_RollCallHandler();
            try
            {

                foreach (RollCall r in listRollCalls)
                {
                    student_RollCall = student_RollCallHandler.GetStudentAttendance(r.RollCallID, Convert.ToInt32(dlStudents.SelectedValue));

                    try
                    {

                        litReport.Text += student_RollCall.Status + "<br/>";
                        DateTime date = Convert.ToDateTime(r.TimeOfRollCall);
                        htmlOutput += "<tr><td>" + date.ToString("MM/dd/yyyy HH:mm tt") + "</td><td>" + student_RollCall.Status + "</td></tr>\n";
                    }
                    catch (NullReferenceException)
                    {
                        litReport.Text += "Absent<br/>";
                        DateTime date = Convert.ToDateTime(r.TimeOfRollCall);
                        htmlOutput += "<tr><td>" + date.ToString("MM/dd/yyyy HH:mm tt") + "</td><td>absent</td></tr>\n";
                    }
                }

                Student student = new Student();
                StudentHandler studentHandler = new StudentHandler();
            }
            catch
            {
                htmlOutput = "<div class='alert alert-danger'>No records found</div>";
            }
            litReport.Text = htmlOutput;
        }
        protected void dlModules_SelectedIndexChanged(object sender, EventArgs e)
        {
            //get students from selected module
            StudentHandler studentHandler = new StudentHandler();

            List<Student> listStudents = studentHandler.GetStudentList(Convert.ToInt32(dlModules.SelectedValue));

            if (listStudents == null)
            {
                dlStudents.Items.Clear();
                dlStudents.Items.Add("No students");
            }
            else
            {
                dlStudents.DataSource = listStudents;
                dlStudents.DataTextField = "StudentNumber";
                dlStudents.DataValueField = "StudentID";
                dlStudents.DataBind();
            }
        }
        protected void btnAddStudents_Click(object sender, EventArgs e)
        {
            string filePath = @"\temp\";

            //check if file is selected
            if (fileUploadControl.HasFile)
            {
                try
                {
                    //only accept .csv files
                    if (fileUploadControl.PostedFile.ContentType == "application/vnd.ms-excel")//for .csv use text/csv
                    {
                        //check file is within maximum size limit
                        if (fileUploadControl.PostedFile.ContentLength < 3072000)
                        {
                            //get file name from the upload control
                            string filename = Path.GetFileName(fileUploadControl.FileName);

                            //get the extension name of the file
                            string extension = filename.Substring(filename.LastIndexOf("."));
                            //remove the extension from the file name
                            filename = filename.Substring(0, filename.LastIndexOf("."));
                            //combine path, file name and extension
                            filePath += filename + extension;

                            //all checks successfull, save file
                            fileUploadControl.SaveAs(Server.MapPath(@"~" + filePath));

                            //now use file contents from local temp folder
                            Student student = new Student();
                            student.ModuleID = Convert.ToInt32(Request.QueryString["module"]);
                            StudentHandler addStudent = new StudentHandler();

                            lblProgress.Text = "<br/>";

                            int moduleID = Convert.ToInt32(Request.QueryString["module"]);
            /////////////////////
                            var reader = new StreamReader(File.OpenRead(@"C:\Users\micks\Documents\BitBucket\UAttendWeb\WebApp" + filePath));

                            while (!reader.EndOfStream)
                            {
                                var line = reader.ReadLine();
                                var values = line.Split(',');

                                student.StudentNumber = values[0];
                                student.FirstName = values[1];
                                student.Surname = values[2];
                                student.ModuleID = moduleID;

                                addStudent.AddNewStudent(student);
                            }
                            reader.Close();
                            Response.Redirect("Modules.aspx");
                        }
                        else
                        {
                            lblProgress.Text = "The file has to be less than 3 megabytes!";
                        }
                    }
                    else
                    {
                        lblProgress.Text = "Only .csv files are accepted!";
                    }
                }
                catch (Exception ex)
                {
                    lblProgress.Text = "The file failed to upload <br/>" + ex;
                }
            }
            else
            {
                lblProgress.Text = "Select a file to add students";
            }
        }
        protected void btnGetReport_Click(object sender, EventArgs e)
        {
            //when a module us selected display all students linked to that module, and their attendance for each roll call
            //data needed: student name, status

            //get studentID, firstname, surname using ModuleID
            //for each student use studentID to find roll call status

            StudentHandler studentHandler = new StudentHandler();
            RollCallHandler rollCallHandler = new RollCallHandler();
            Student_RollCallHandler student_RollCallHandler = new Student_RollCallHandler();
            Student_RollCall student_RollCall = new Student_RollCall();

            List<Student> listStudents = studentHandler.GetStudentList(Convert.ToInt32(dlModules.SelectedValue));

            litReport.Text = "";
            string htmlOutput = "<thead><tr><th>Student <i class='fa fa-sort'></i></th><th>Percent <i class='fa fa-sort'></i></th></tr></thead>";

            try
            {
                //get list of roll calls IDs held for a module
                List<RollCall> listRollCalls = rollCallHandler.GetRollCallList(Convert.ToInt32(dlModules.SelectedValue));

                int countRollCall = 0;
                int countStudent = 0;
                string[,] studentData = new string[listStudents.Count, 2];

                string[] student = new string[listStudents.Count] ;
                int[] attending = new int[listRollCalls.Count];
                int temp = 0;

                for (int i = 0; i < listStudents.Count; i++)
                {
                    studentData[i, 1] = "0";
                }

                foreach (Student s in listStudents)
                {
                    student[countStudent] = s.FirstName + " " + s.Surname;
                    studentData[countStudent, 0] = s.FirstName + " " + s.Surname + " " + s.StudentNumber;

                    //now get this students attendance records, one for each roll call, if no record exists assume absent
                    foreach (RollCall r in listRollCalls)
                    {
                        student_RollCall = student_RollCallHandler.GetStudentAttendance(r.RollCallID, s.StudentID);
                        try
                        {
                            litReport.Text += s.FirstName + "&nbsp" + student_RollCall.Status;

                            attending[countRollCall] += 1;

                            temp = Convert.ToInt32(studentData[countStudent, 1]);
                            temp += 1;
                            studentData[countStudent, 1] = temp.ToString();
                        }
                        catch (NullReferenceException)
                        {
                            litReport.Text += s.FirstName + "&nbsp" + "absent";

                            attending[countRollCall] += 0;

                            temp = Convert.ToInt32(studentData[countStudent, 1]);
                            temp += 0;
                            studentData[countStudent, 1] = temp.ToString();
                        }
                        countRollCall++;
                    }

                    countStudent++;
                    countRollCall = 0;

                    litReport.Text += "</br></br>";

                }

                litReport.Text = "";

                for (int i = 0; i < listStudents.Count; i++)
                {
                    studentData[i, 1] = (Convert.ToDouble(studentData[i, 1]) / listRollCalls.Count * 100).ToString();
                    studentData[i, 1] = Math.Round(Convert.ToDouble(studentData[i, 1]), 0).ToString();

                    htmlOutput += "<tr><td>" + studentData[i, 0] + "</td><td>" + studentData[i, 1] + "</td></tr>\n";
                    litReport.Text += studentData[i, 0] + " " + studentData[i, 1] + "</br>";
                }
            }
            catch
            {
                htmlOutput = "<div class='alert alert-danger'>No records found</div>";
            }

            litReport.Text = htmlOutput;
        }