Esempio n. 1
0
        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);
        }
Esempio n. 2
0
 /// <summary>Checks whether the current user is the owner of the item with the specified Id.</summary>
 /// <param name="itemId">The Id of the item in question.</param>
 /// <returns>True if the user owns the item, false if not.</returns>
 public static bool IsOwnerOf(int itemId)
 {
     List<Item> items = new List<Item>(GetOwnedFileInfosByEmail(_sessionUser.Email));
     items.AddRange(GetOwnedPackagesByEmail(_sessionUser.Email));
     return items.Any<Item>(item => item.Id == itemId);
 }