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_OnlineResault : System.Web.UI.Page
{
    //*************************************
    SqlConnection con;
    SqlCommand cmd;
    //*************************************
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                int corrects = 0, mistakes = 0, noneAnswers = 0, testAnswered = 0, testCorrectAnswer = 0;
                string answerType = "", onlineName = "";
                double barom = 0.0, manfi = 0.0, mark = 0.0, manfiTotal = 0.0;
                C_myClass.MenueItemsCreate(User, panelMenues);
                Label lblTitle = (Label)this.Master.FindControl("lblTitle");
                lblTitle.Text = "نتیجه آزمون";
                int onlineId = Int32.Parse(Request.QueryString["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);
                onlineName = _UseFull.C_Usefull.FindStringWithIntegerShart(con, cmd, "t_OnlineExams", "onlineName", "onlineId", onlineId);
                try
                {
                    con.Open();
                    cmd.CommandText = "select t_OnlineQuestions.barom, t_OnlineQuestions.manfi, t_OnlineQuestions.answerType, t_OnlineQuestions.testAnswer, t_OnlineAnswers.testAnswer 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)
                    {
                        while (rdr.Read())
                        {
                            barom = (rdr.GetValue(0) != DBNull.Value ? Math.Round(rdr.GetDouble(0), 2) : 0.0);
                            manfi = (rdr.GetValue(1) != DBNull.Value ? Math.Round(rdr.GetDouble(1), 2) : 0.0);
                            answerType = (rdr.GetValue(2) != DBNull.Value ? rdr.GetString(2) : "");
                            testCorrectAnswer = (rdr.GetValue(3) != DBNull.Value ? rdr.GetInt32(3) : 0);
                            testAnswered = (rdr.GetValue(4) != DBNull.Value ? rdr.GetInt32(4) : 0);
                            if (testAnswered == testCorrectAnswer)
                            {
                                corrects++;
                                mark += barom;
                            }
                            else if ((testAnswered != testCorrectAnswer) && (testAnswered != -1))
                            {
                                mistakes++;
                                manfiTotal += manfi;
                            }
                            else if ((testAnswered != testCorrectAnswer) && (testAnswered == -1))
                            {
                                noneAnswers++;
                            }
                        }
                    }
                    rdr.Close();
                    lblOnlineName.Text = onlineName;
                    lblCorrectCount.Text = corrects.ToString();
                    lblMistakeCount.Text = mistakes.ToString();
                    lblNoneAnswerCount.Text = noneAnswers.ToString();
                    lblResaultNoManfi.Text = mark.ToString();
                    lblResaultWithManfi.Text = ( mark - manfiTotal >= 0 ?(mark - manfiTotal).ToString() : "0");
                }
                catch
                {
                }
                finally
                {
                    con.Close();
                }
            }
        }
        catch
        {
        }
        finally
        {
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        try
        {
            string returnUrl = Request.QueryString["returnUrl"].ToString();
            Response.Redirect(returnUrl);
        }
        catch
        {
        }
    }
}
