protected void btnBeginRollCall_Click(object sender, EventArgs e)
        {
            RollCall rollCall = new RollCall();
            RollCallHandler rollCallHandler = new RollCallHandler();

            //add roll call entry to DB
            rollCall.TimeOfRollCall = DateTime.Now.ToString();
            rollCall.ModuleID = Convert.ToInt32(dlModules.SelectedValue);
            //rollCall.Status = "Enabled"; not needed

            rollCallID = rollCallHandler.AddNewRollCall(rollCall);

            //generate and display QR Code and pin
            string encodeQR = rollCallID.ToString();

            QRCodeEncoder encoder = new QRCodeEncoder();
            Bitmap hi = encoder.Encode(encodeQR);
            hi.Save(Server.MapPath("~/temp/" + encodeQR + ".jpg"), ImageFormat.Jpeg);
            QRcode.ImageUrl = "~/temp/" + encodeQR + ".jpg";

            litPin.Text = "<div class='alert alert-info'>Pin: " + rollCallID + "</div>";
            //change interface to suite active roll call session
            btnBeginRollCall.Visible = false;
            dlModules.Visible = false;

            //litAutoDisable.Visible = true;
            txtTime.Visible = true;
            txtDate.Visible = true;
            btnAutoDisable.Visible = true;

            btnPauseRollCall.Visible = true;
            btnEndRollCall.Visible = true;

            Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "makeVisible()", true);
        }
        // GET api/<controller>
        public IEnumerable<string> Get()
        {
            RollCallHandler rollCallHandler = new RollCallHandler();
            List<RollCall> listRollCalls = rollCallHandler.GetRollCallList(1);

            return new string[] { "value1", listRollCalls[0].Status };
        }
        // GET api/nfc/5
        public List<RollCall> Get(string module)
        {
            RollCallHandler rollCallHandler = new RollCallHandler();
            List<RollCall> listRollCalls = rollCallHandler.GetRollCallList(Convert.ToInt32(module));

            //generate a table to list all modules
            //string output = "";

            //Check to make sure there is modules in the system assigned to the lecturer
            if (listRollCalls == null)
            {

            }
            else
            {
                //output = "<tr><th>Date and time</th><th>Status</th><th>Toggle Status</th></tr>";
                //add modules to table as it is generated
                for (int i = 0; i < listRollCalls.Count; i++)
                {
                    //moduleID = listRollCalls[i].ModuleID;
                    if (listRollCalls[i].Status != "enabled")
                    {
                        listRollCalls.RemoveAt(i);
                        i--;
                        //output += "<tr><td>" + listRollCalls[i].TimeOfRollCall + "</td><td>" + listRollCalls[i].Status + " </td><td><a href=\"ToggleRollCall.aspx?id=" + listRollCalls[i].RollCallID + "\">Make Disabled</a></td></tr>\n";
                    }
                }
            }
            //litRollCallList.Text = output;

            return listRollCalls;
        }
        protected void btnAutoDisable_Click(object sender, EventArgs e)
        {
            string time = txtTime.Value + ":00";
            string date = txtDate.Value;
            DateTime dateTime = DateTime.ParseExact(date + " " + time, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);

            //litPin.Text += " " + dateTime.ToString();

            //create database entry with dateTime
            RollCallHandler rollCallHandler = new RollCallHandler();
            rollCallHandler.SetAutoDisable(rollCallID, dateTime.ToString());
        }
        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 btnConfirm_Click(object sender, EventArgs e)
        {
            if (status == "enabled")
            {
                RollCallHandler rollCallHandler = new RollCallHandler();
                rollCallHandler.EndRollCall(rollCallID);
            }
            else
            {
                RollCallHandler rollCallHandler = new RollCallHandler();
                rollCallHandler.ResumeRollCall(rollCallID);
            }

            Response.Redirect("RollCallHistory.aspx");
        }
        protected void btnGetHistory_Click(object sender, EventArgs e)
        {
            RollCallHandler rollCallHandler = new RollCallHandler();
            int moduleID = Convert.ToInt32(dlModules.SelectedValue);
            List<RollCall> listRollCalls = rollCallHandler.GetRollCallList(moduleID);

            litAlert.Text = "";

            //generate a table to list all modules
            string htmlOutput = "";

            //Check to make sure there is modules in the system assigned to the lecturer
            if (listRollCalls == null)
                litAlert.Text = "<div class='alert alert-warning'>There are no previous roll calls for this module</div>";
            else
            {
                htmlOutput = "<thead><tr><th>Date and time <i class='fa fa-sort'></i></th><th>Pin Code <i class='fa fa-sort'></i></th><th>Status <i class='fa fa-sort'></i></th><th>Toggle Status</th></tr></thead>";
                //add modules to table as it is generated
                for (int i = 0; i < listRollCalls.Count; i++)
                {
                    if (listRollCalls[i].AutoDisable != "")
                    {
                        DateTime autoDisableDate = DateTime.Parse(listRollCalls[i].AutoDisable);

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

                    moduleID = listRollCalls[i].ModuleID;
                    if (listRollCalls[i].Status == "enabled")
                    {
                        htmlOutput += "<tr><td>" + listRollCalls[i].TimeOfRollCall + "</td><td>" + listRollCalls[i].RollCallID + "</td><td>" + listRollCalls[i].Status + " </td><td><a href=\"ToggleRollCall.aspx?id=" + listRollCalls[i].RollCallID + "\">Make Disabled</a></td></tr>\n";
                    }
                    else
                    {
                        htmlOutput += "<tr><td>" + listRollCalls[i].TimeOfRollCall + "</td><td>" + listRollCalls[i].RollCallID + "</td><td>" + listRollCalls[i].Status + " </td><td><a href=\"ToggleRollCall.aspx?id=" + listRollCalls[i].RollCallID + "\">Make Enabled</a></td></tr>\n";
                    }
                }
            }
            litRollCallList.Text = htmlOutput;
        }
        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 Page_Load(object sender, EventArgs e)
        {
            rollCallID = Convert.ToInt32(Request.QueryString["id"]);

            //get module name
            RollCallHandler rollCallHandler = new RollCallHandler();
            RollCall rollCall = new RollCall();
            rollCall = rollCallHandler.GetRollCallDetails(Convert.ToInt32(rollCallID));
            status = rollCall.Status;

            if (status == "enabled")
            {
                litHeader.Text = "<div class='alert alert-info'>Disable roll call of " + rollCall.TimeOfRollCall + "</div>";
            }
            else
            {
                litHeader.Text = "<div class='alert alert-info'>Enable roll call of " + rollCall.TimeOfRollCall + "</div>";
            }
        }
        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;
        }
        protected void btnPauseRollCall_Click(object sender, EventArgs e)
        {
            RollCallHandler rollCallHandler = new RollCallHandler();

            if (btnPauseRollCall.Text == "Pause Roll Call")
            {
                rollCallHandler.PauseRollCall(rollCallID);
                QRcode.Visible = false;
                litPin.Visible = false;

                btnPauseRollCall.Text = "Resume Roll Call";
            }
            else if (btnPauseRollCall.Text == "Resume Roll Call")
            {
                rollCallHandler.ResumeRollCall(rollCallID);
                QRcode.Visible = true;
                litPin.Visible = true;

                btnPauseRollCall.Text = "Pause Roll Call";
            }
        }
 protected void btnEndRollCall_Click(object sender, EventArgs e)
 {
     RollCallHandler rollCallHandler = new RollCallHandler();
     rollCallHandler.EndRollCall(rollCallID);
     Response.Redirect("rollcallSession.aspx");
 }