protected void loadForm(bool knowledgetest) { string xmlFile = Server.MapPath("Licensetest.xml"); if (knowledgetest == true) { xmlFile = Server.MapPath("knowledgetest.xml"); } XmlDocument doc = new XmlDocument(); StringBuilder str = new StringBuilder(); doc.Load(xmlFile); XmlNodeList nodeList = doc.SelectNodes("/formular/fraga"); foreach (XmlNode node in nodeList) { Fraga fraga = new Fraga(); fraga.titel = node["titel"].InnerText; fraga.id = Convert.ToInt16(node.Attributes[0].Value); fraga.del = node["del"].InnerText; fraga.bild = node["bild"].InnerText; foreach (XmlNode svarNode in node["svar"].ChildNodes) { Svar svar = new Svar(); svar.id = Convert.ToInt16(svarNode.Attributes[0].Value); svar.titel = svarNode.InnerText; svar.ratt = Convert.ToBoolean(Convert.ToInt16(svarNode.Attributes[1].Value)); fraga.svarsLista.Add(svar); } frageLista.Add(fraga); } if (knowledgetest == true) { str.Append("<div class='what_test' style='display: none;'>1</div>"); } else { str.Append("<div class='what_test' style='display: none;'>0</div>"); } str.Append("<div id='fragor' class='rearrange' runat='server' ID='rattning'>"); foreach (Fraga fraga in frageLista) { int antalRattSvar = 0; str.Append("<div class='fraga' data=" + fraga.id + ">"); str.Append("<div class='del'><span>Del:</span> " + fraga.del + "</div>"); str.Append("<h2>" + fraga.titel + "</h2>"); if (fraga.bild != "null") { str.Append("<div class='img_form'><img src='" + fraga.bild + "'></div>"); } foreach (Svar svar in fraga.svarsLista) { if (svar.ratt == true) { antalRattSvar++; } str.Append("<div class='cbLabel'>"); str.Append("<input type='checkbox' name='" + fraga.id + "-" + svar.id + "' class='checkBox'> "); str.Append("<label>" + svar.titel + "</label><br />"); str.Append("</div>"); } str.Append("<p class='antalRattSvar' data='" + antalRattSvar + "'>Det finns <span>" + antalRattSvar + "</span> rätt svar på den här frågan.</p>"); str.Append("</div>"); } str.Append("</div>"); lblForm.Text = str.ToString(); }
protected void Grade() { string whatTest = Request.Form["what"]; string connectionString = WebConfigurationManager.ConnectionStrings["JE-Banken"].ConnectionString; NpgsqlConnection conn = new NpgsqlConnection(connectionString); User u = (User)Session["user"]; int antalFragor = 0; int antalRatt = 0; XmlDocument doc = new XmlDocument(); List<Svar> svarList = new List<Svar>(); List<testdel> tdList = new List<testdel>(); doc.Load(MapPath("/tests/" + u.employeenumber.ToString() + ".xml")); XmlNodeList nodeList = doc.SelectNodes("/formular/fraga"); foreach (XmlNode fraga in nodeList) { antalFragor++; Svar s = new Svar(); s.del = fraga["del"].InnerText; if (!tdList.Any(x => x.del == fraga["del"].InnerText)) { testdel td = new testdel(); td.del = fraga["del"].InnerText; tdList.Add(td); } int svarAntalRatt = 0; int antalSvaradeRatt = 0; foreach (XmlNode svar in fraga["svar"].ChildNodes) { if (Convert.ToInt32(svar.Attributes["ratt"].Value) == 1) { svarAntalRatt++; } if (Convert.ToInt32(svar.Attributes["ratt"].Value) == 1 && Convert.ToInt32(svar.Attributes["gissat"].Value) == 1) { antalSvaradeRatt++; } } if (svarAntalRatt == antalSvaradeRatt) { antalRatt++; s.ratt = true; } svarList.Add(s); } Response.Write("<h2 class='antalRatt'>Antal poäng: " + antalRatt + "/" + antalFragor + "</h2>"); Response.Write("<div class='responseHidden'></div>"); foreach (testdel td in tdList) { foreach (Svar s in svarList) { if (s.del == td.del) { td.antalFragor++; if (s.ratt) { td.antalRatt++; } } } } string score = antalRatt.ToString() + "/" + antalFragor.ToString(); bool passed = false; bool totalpassed = false; bool testpartpassed = true; if ((Convert.ToDouble(antalRatt) / Convert.ToDouble(antalFragor)) >= 0.7) { totalpassed = true; } foreach (testdel td in tdList) { if ((Convert.ToDouble(td.antalRatt) / Convert.ToDouble(td.antalFragor)) < 0.6) { testpartpassed = false; // 1 = knowledgetest // 0 = licensetest if (whatTest != "1") { string sql2 = "UPDATE employees SET licensed = @licensed WHERE employeenumber=" + u.employeenumber; NpgsqlCommand cmd2 = new NpgsqlCommand(sql2, conn); cmd2.Parameters.AddWithValue("licensed", false); conn.Open(); cmd2.ExecuteScalar(); conn.Close(); } godkand = "Tyvärr, du blev underkänd!"; break; } } if (totalpassed == true && testpartpassed == true) { passed = true; if (whatTest != "1") { string sql2 = "UPDATE employees SET licensed = @licensed WHERE employeenumber=" + u.employeenumber; NpgsqlCommand cmd2 = new NpgsqlCommand(sql2, conn); cmd2.Parameters.AddWithValue("licensed", true); conn.Open(); cmd2.ExecuteScalar(); conn.Close(); } godkand = "Grattis, du blev godkänd!"; } string sql = ""; if (u.licensed == null) { sql = "INSERT INTO licensetest(score, date, passed, employeenumber) VALUES(@score, @date, @passed, @employeenumber)"; NpgsqlCommand cmd = new NpgsqlCommand(sql, conn); cmd.Parameters.AddWithValue("score", score); cmd.Parameters.AddWithValue("date", DateTime.Today); cmd.Parameters.AddWithValue("passed", passed); cmd.Parameters.AddWithValue("employeenumber", u.employeenumber); conn.Open(); cmd.ExecuteScalar(); conn.Close(); } if (u.licensed == false) { sql = "UPDATE licensetest SET score = @score, date = @date, passed = @passed WHERE employeenumber=" + u.employeenumber.ToString(); NpgsqlCommand cmd = new NpgsqlCommand(sql, conn); cmd.Parameters.AddWithValue("score", score); cmd.Parameters.AddWithValue("date", DateTime.Today); cmd.Parameters.AddWithValue("passed", passed); conn.Open(); cmd.ExecuteScalar(); conn.Close(); } if (u.licensed == true) { sql = "SELECT EXISTS(SELECT 1 FROM knowledgetest WHERE employeenumber="+u.employeenumber+")"; NpgsqlCommand cmd = new NpgsqlCommand(sql, conn); conn.Open(); bool exists = Convert.ToBoolean(cmd.ExecuteScalar()); conn.Close(); if (exists) { sql = "UPDATE knowledgetest SET score = @score, date = @date, passed = @passed WHERE employeenumber=" + u.employeenumber.ToString(); NpgsqlCommand cmd2 = new NpgsqlCommand(sql, conn); cmd2.Parameters.AddWithValue("score", score); cmd2.Parameters.AddWithValue("date", DateTime.Today); cmd2.Parameters.AddWithValue("passed", passed); conn.Open(); cmd2.ExecuteScalar(); conn.Close(); } if (!exists) { sql = "INSERT INTO knowledgetest(score, date, passed, employeenumber) VALUES(@score, @date, @passed ,@employeenumber)"; NpgsqlCommand cmd3 = new NpgsqlCommand(sql, conn); cmd3.Parameters.AddWithValue("score", score); cmd3.Parameters.AddWithValue("date", DateTime.Today); cmd3.Parameters.AddWithValue("passed", passed); cmd3.Parameters.AddWithValue("employeenumber", u.employeenumber); conn.Open(); cmd3.ExecuteScalar(); conn.Close(); } } Response.Write(godkand); }