using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class rl_students_pg_OnlineExam : System.Web.UI.Page
{
    //***********************************
    SqlConnection con;
    SqlCommand cmd;
    //***********************************
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                C_myClass.MenueItemsCreate(User, panelMenues);
                Label lblTitle = (Label)this.Master.FindControl("lblTitle");
                lblTitle.Text = "آزمون آنلاین";
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                int studentId = _UseFull.C_Usefull.FindIntegerWithStringShart(con, cmd, "t_Students", "studentId", "userName", User.Identity.Name);
                SqlDataSource1.SelectCommand = "SELECT Distinct t_OnlineExams.onlineId, t_OnlineExams.onlineName, t_OnlineExams.onlineType, t_OnlineExams.testNum, t_OnlineExams.explainNum, t_OnlineExams.testScore, t_OnlineExams.explainScore, t_OnlineExams.designer FROM " +
                    "t_OnlineExams INNER JOIN t_OnlineAccess ON t_OnlineExams.onlineId = t_OnlineAccess.onlineId " +
                    "INNER JOIN t_ClassStudents ON t_OnlineAccess.classId = t_ClassStudents.classId " +
                    "WHERE t_ClassStudents.studentId = " + studentId;
                GridView1.DataBind();
            }
        }
        catch(Exception ex)
        {
        }
        finally
        {
        }
    }

    protected string FindButtonText(object OnlineId)
    {
        bool participated = false;
        string res = "";
        try
        {
            int onlineId = Int32.Parse(OnlineId.ToString());
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            cmd = con.CreateCommand();
            int studentId = _UseFull.C_Usefull.FindIntegerWithStringShart(con, cmd, "t_Students", "studentId", "userName", User.Identity.Name);
            try
            {
                con.Open();
                cmd.CommandText = "select * from t_OnlineQuestions inner join t_OnlineAnswers ON t_OnlineQuestions.questionId = t_OnlineAnswers.questionId WHERE t_OnlineQuestions.onlineId = " + onlineId + " AND t_OnlineAnswers.studentId = " + studentId;
                SqlDataReader rdr = cmd.ExecuteReader();
                if (rdr.HasRows)
                {
                    participated = true;
                }
                rdr.Close();
                if (participated)
                {
                    res = "نتیجه آزمون";
                }
                else
                {
                    res = "شرکت در آزمون";
                }
            }
            catch
            {
            }
            finally
            {
                con.Close();
            }
        }
        catch
        {
        }
        return res;
    }

    protected string FindCssClass(object OnlineId)
    {
        bool participated = false;
        string res = "";
        try
        {
            int onlineId = Int32.Parse(OnlineId.ToString());
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            cmd = con.CreateCommand();
            int studentId = _UseFull.C_Usefull.FindIntegerWithStringShart(con, cmd, "t_Students", "studentId", "userName", User.Identity.Name);
            try
            {
                con.Open();
                cmd.CommandText = "select * from t_OnlineQuestions inner join t_OnlineAnswers ON t_OnlineQuestions.questionId = t_OnlineAnswers.questionId WHERE t_OnlineQuestions.onlineId = " + onlineId + " AND t_OnlineAnswers.studentId = " + studentId;
                SqlDataReader rdr = cmd.ExecuteReader();
                if (rdr.HasRows)
                {
                    participated = true;
                }
                rdr.Close();
                if (participated)
                {
                    res = "gridsmallbuttonstrue";
                }
                else
                {
                    res = "gridsmallbuttons";
                }
            }
            catch
            {
            }
            finally
            {
                con.Close();
            }
        }
        catch
        {
        }
        return res;
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Participate")
            {
                ///////////////////////////////////
                bool participated = false;
                int onlineId = Int32.Parse(e.CommandArgument.ToString());
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                int studentId = _UseFull.C_Usefull.FindIntegerWithStringShart(con, cmd, "t_Students", "studentId", "userName", User.Identity.Name);
                try
                {
                    con.Open();
                    cmd.CommandText = "select * from t_OnlineQuestions inner join t_OnlineAnswers ON t_OnlineQuestions.questionId = t_OnlineAnswers.questionId WHERE t_OnlineQuestions.onlineId = " + onlineId + " AND t_OnlineAnswers.studentId = " + studentId;
                    SqlDataReader rdr = cmd.ExecuteReader();
                    if (rdr.HasRows)
                    {
                        participated = true;
                    }
                    rdr.Close();
                    if (participated)
                    {
                        Response.Redirect("pg_OnlineResault.aspx?onlineId=" + onlineId + "&returnUrl=pg_OnlineExam.aspx");
                    }
                    else
                    {
                        Response.Redirect("pg_OnlineParticipate.aspx?onlineId=" + onlineId + "&returnUrl=pg_OnlineExam.aspx");
                    }
                }
                catch
                {
                }
                finally
                {
                    con.Close();
                }
            }
        }
        catch(Exception ex)
        {
        }
    }
}
