Example #1
0
        public static string Grade(String MatrixMapJSON, String AnswerJSON, String question, String assignment)
        {
            //initialize floats for total point value and their grade so far on assignment
            float questionValue = 0;
            float currentGrade  = 0;

            //Get session variables
            float[,] sessionMatrix = (float[, ])HttpContext.Current.Session["matrix"];
            float[] sessionActualAnswer = (float[])HttpContext.Current.Session["actualAnswer"];
            //int sessionMinNumOfRowsOps = (int)HttpContext.Current.Session["minNumOfRowOps"];
            Boolean sessionInconsistent  = (Boolean)HttpContext.Current.Session["inconsistent"];
            int     sessionNumOfFreeVars = (int)HttpContext.Current.Session["numOfFreeVars"];
            String  assignId             = (String)HttpContext.Current.Session["assignment"];
            string  feedback             = "";

            //first we need to check if the homework assignment is in the database
            AssignComponent.Assigner check = new AssignComponent.Assigner();
            int theresanassignment         = check.checkAssignmentExists(assignId);

            if (theresanassignment == 1)
            {
                //query to fetch question's point value and their current grade on the assignment
                string          connStr = ConfigurationManager.ConnectionStrings["linearhmwkdb"].ConnectionString;
                MySqlConnection msqcon  = new MySqlConnection(connStr);
                try
                {
                    msqcon.Open();
                    String          query  = "SELECT q.pointValue, ha.grade FROM question AS q JOIN hmwkassignment AS ha WHERE ha.homeworkId=q.homeworkId AND ha.assignmentId = " + assignment + " AND q.number = " + question;
                    MySqlCommand    msqcmd = new MySqlCommand(query, msqcon);
                    MySqlDataReader points = msqcmd.ExecuteReader();
                    points.Read();
                    questionValue = points.GetFloat(0);
                    currentGrade  = points.GetFloat(1);
                    points.Close();
                }
                catch (Exception)
                {
                    throw;
                }


                Dictionary <int, float[, ]>    MatrixMap = JsonConvert.DeserializeObject <Dictionary <int, float[, ]> >(MatrixMapJSON);
                MatrixBuilder.MatrixOperations mb        = new MatrixBuilder.MatrixOperations();

                //get the amount to deduct by dividing the total points by our approximate number of steps to solve (+1 for first matrix) (+1 for copying the answer correctly)
                //shifted then rounded to keep it to 2 decimal points, then shifted back
                float deductValue = (float)Math.Round((questionValue / ((float)mb.countOperationsNeeded(sessionMatrix) + 2F)) * 100F) / 100F;
                //if above gives a value of 0 (a rediculously large number of steps), correct to .01 so points are actually deducted
                if (deductValue == 0)
                {
                    deductValue = .01F;
                }

                //should probably check if the first matrix is the actual first matrix
                float[,] augMatrix = null;
                MatrixMap.TryGetValue(0, out augMatrix);
                //Not sure if this if works
                if (!mb.checkMatrixEquality(sessionMatrix, augMatrix))
                {
                    feedback += "<div>The first matrix does not match the given matrix.<div>";
                }

                if (AnswerJSON.Contains("I"))
                {
                    feedback += mb.checkSingleRowOperation(MatrixMap);
                    if (!sessionInconsistent)
                    {
                        feedback += "<div>The matrix is actually consistent.<div>";
                    }
                }
                else if (AnswerJSON.Contains("F"))
                {
                    feedback += mb.checkSingleRowOperation(MatrixMap);
                    if (sessionNumOfFreeVars > 0)
                    {
                        Dictionary <int, String> AnswersConverted = JsonConvert.DeserializeObject <Dictionary <int, String> >(AnswerJSON);
                        String[] Answers = new String[AnswersConverted.Count()];
                        AnswersConverted.Values.CopyTo(Answers, 0);
                        feedback += mb.checkFreeVariableAnswers(sessionMatrix, Answers);
                    }
                    else
                    {
                        feedback += "<div>The solution actually contains no free variables.<div>";
                    }
                }
                else
                {
                    //Dictionary<int, float> AnswersConverted = JsonConvert.DeserializeObject<Dictionary<int, float>>(AnswerJSON);
                    //float[] Answers = new float[AnswersConverted.Count()];
                    //AnswersConverted.Values.CopyTo(Answers, 0);
                    float[,] studentident = null;
                    feedback += mb.checkSingleRowOperation(MatrixMap);
                    MatrixMap.TryGetValue(MatrixMap.Count() - 1, out studentident);
                    //Will need to also check answers here
                    feedback += mb.checkForIdentity(studentident, sessionMatrix);
                }

                float grade = questionValue;
                //ensure there was feedback, if not, will retain value of the question's total points
                if (!feedback.Equals(""))
                {
                    //Run through feedback, create array of statements based on . to count number of point deductions
                    String[] deductionStrings = feedback.Split('.');
                    //set grade to the total minus the deduct amount times number of mistakes
                    grade = questionValue - deductValue * (deductionStrings.Length - 1);
                    //correct any odd values from results close to 0 by re-rounding to 2 decimal points
                    grade = (float)Math.Round(grade * 100F) / 100F;
                }
                //make their grade go no lower than 0 (no negative points)
                if (grade < 0)
                {
                    grade = 0;
                }
                //create grade to write back to database
                float updatedGrade = grade + currentGrade;

                //Need to display points somehow
                if (grade == 1)
                {
                    feedback += "<!-- -->";
                }
                feedback += "<div><strong>Points Earned: </strong>" + grade + " / " + questionValue + "<div>";

                //Update grade in database
                msqcon = new MySqlConnection(connStr);
                try
                {
                    msqcon.Open();
                    String       query  = "UPDATE hmwkassignment AS ha SET ha.grade = " + updatedGrade + " WHERE assignmentId = " + assignment;
                    MySqlCommand msqcmd = new MySqlCommand(query, msqcon);
                    msqcmd.ExecuteNonQuery();
                }
                catch (Exception)
                {
                    throw;
                }
            }

            return(String.IsNullOrEmpty(feedback) ? null : feedback);
        }
        public static string Grade(String MatrixMapJSON, String question, String assignment)
        {
            //initialize floats for total point value and their grade so far on assignment
            float questionValue = 0;
            float currentGrade = 0;

            //Get session variables
            float[,] sessionMatrix = (float[,])HttpContext.Current.Session["matrix"];
            float[] sessionActualAnswer = (float[])HttpContext.Current.Session["actualAnswer"];
            //int sessionMinNumOfRowsOps = (int)HttpContext.Current.Session["minNumOfRowOps"];
            Boolean sessionInconsistent = (Boolean)HttpContext.Current.Session["inconsistent"];
            int sessionNumOfFreeVars = (int)HttpContext.Current.Session["numOfFreeVars"];
            String assignId = (String) HttpContext.Current.Session["assignment"];
            string feedback = "";
            //first we need to check if the homework assignment is in the database
            AssignComponent.Assigner check = new AssignComponent.Assigner();
            int theresanassignment = check.checkAssignmentExists(assignId);
            if (theresanassignment == 1)
            {
                //query to fetch question's point value and their current grade on the assignment
                string connStr = ConfigurationManager.ConnectionStrings["linearhmwkdb"].ConnectionString;
                MySqlConnection msqcon = new MySqlConnection(connStr);
                try
                {
                    msqcon.Open();
                    String query = "SELECT q.pointValue, ha.grade FROM question AS q JOIN hmwkassignment AS ha WHERE ha.homeworkId=q.homeworkId AND ha.assignmentId = " + assignment + " AND q.number = " + question;
                    MySqlCommand msqcmd = new MySqlCommand(query, msqcon);
                    MySqlDataReader points = msqcmd.ExecuteReader();
                    points.Read();
                    questionValue = points.GetFloat(0);
                    currentGrade = points.GetFloat(1);
                    points.Close();
                }
                catch (Exception)
                {
                    throw;
                }

                Dictionary<int, float[,]> MatrixMap = JsonConvert.DeserializeObject<Dictionary<int, float[,]>>(MatrixMapJSON);
                MatrixBuilder.MatrixOperations mb = new MatrixBuilder.MatrixOperations();

                //get the amount to deduct by dividing the total points by our approximate number of steps to solve (+1 for first matrix) (+1 for copying the answer correctly)
                //shifted then rounded to keep it to 2 decimal points, then shifted back
                float deductValue = (float)Math.Round((questionValue / ((float)mb.countOperationsNeeded(sessionMatrix) + 2F)) * 100F) / 100F;
                //if above gives a value of 0 (a rediculously large number of steps), correct to .01 so points are actually deducted
                if (deductValue == 0) deductValue = .01F;

                //should probably check if the first matrix is the actual first matrix
                float[,] augMatrix = null;
                MatrixMap.TryGetValue(0, out augMatrix);

                float[,] sessionMatrixWithIdentity = new float[sessionMatrix.GetLength(0), sessionMatrix.GetLength(1) * 2];
                for (int i = 0; i < sessionMatrix.GetLength(0); i++)
                {
                    for (int j = 0; j < sessionMatrix.GetLength(1); j++)
                    {
                        sessionMatrixWithIdentity[i, j] = sessionMatrix[i, j];
                    }
                }
                int row = 0;
                for (int j = sessionMatrix.GetLength(1); j < sessionMatrixWithIdentity.GetLength(1); j++)
                {
                    sessionMatrixWithIdentity[row, j] = 1;
                    row++;
                }

                if (!mb.checkMatrixEquality(sessionMatrixWithIdentity, augMatrix))
                {
                    feedback += "<div>The first matrix does not match the given matrix.<div>";
                }
                List<int> keysToInclude = new List<int>();
                for (int i =0; i < MatrixMap.Count - 1; i++)
                {
                    keysToInclude.Add(i);
                }
                Dictionary<int, float[,]> MatrixMapWithoutFirstOrLast = MatrixMap.Where(kvp => keysToInclude.Contains(kvp.Key)).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
                feedback += mb.checkSingleRowOperationInverseQuestion(MatrixMapWithoutFirstOrLast);

                float[,] studentInverse = null;
                MatrixMap.TryGetValue(MatrixMap.Count - 1, out studentInverse);
                if (!mb.checkInverse(sessionMatrix, studentInverse))
                {
                    feedback += "<div>The final matrix does not match the inverse for this matrix.<div>";
                }

                float grade = questionValue;
                //ensure there was feedback, if not, will retain value of the question's total points
                if (!feedback.Equals(""))
                {
                    //Run through feedback, create array of statements based on . to count number of point deductions
                    String[] deductionStrings = feedback.Split('.');
                    //set grade to the total minus the deduct amount times number of mistakes
                    grade = questionValue - deductValue * (deductionStrings.Length - 1);
                    //correct any odd values from results close to 0 by re-rounding to 2 decimal points
                    grade = (float)Math.Round(grade * 100F) / 100F;
                }
                //make their grade go no lower than 0 (no negative points)
                if (grade < 0) grade = 0;
                //create grade to write back to database
                float updatedGrade = grade + currentGrade;

                //Need to display points somehow
                if (grade == 1)
                {
                    feedback += "<!-- -->";
                }
                feedback += "<div><strong>Points Earned: </strong>" + grade + " / " + questionValue + "<div>";

                //Update grade in database
                msqcon = new MySqlConnection(connStr);
                try
                {
                    msqcon.Open();
                    String query = "UPDATE hmwkassignment AS ha SET ha.grade = " + updatedGrade + " WHERE assignmentId = " + assignment;
                    MySqlCommand msqcmd = new MySqlCommand(query, msqcon);
                    msqcmd.ExecuteNonQuery();
                }
                catch (Exception)
                {
                    throw;
                }
            }
            else if (theresanassignment == 0)
            {
                feedback += "<-!- -->";
            }
            return String.IsNullOrEmpty(feedback) ? null : feedback;
        }