private void GradeTest() { List <Student> studentsThatAnswered = db.GetAllStudentsThatAnsweredToATest(currentTest, currentClass); List <Question> allQuestions = db.GetAllQuestionsOfATest(currentTest.IdTest); dgwTestResults.Rows.Clear(); // !!!! erase when fully debugged // dgwTestResults.Columns.Clear(); // !!!! erase when fully debugged // int gridRow = 0, gridColumn = 0; GridAddData(gridRow, gridColumn, "Domande e risposte"); gridRow++; gridColumn = 1; List <Answer> correctQuestionAnswers; // showing the correct answers and weights of the questions foreach (Question q in allQuestions) { correctQuestionAnswers = db.GetAllCorrectAnswersToThisQuestionOfThisTest( q.IdQuestion, currentTest.IdTest); GridAddData(gridRow, gridColumn, q.Text); GridAddData(gridRow, gridColumn + 1, "Risposte esatte"); GridAddData(gridRow, gridColumn + 2, "Costo errore"); // scan all the correct answers to this question for (int iAnswer = 0; iAnswer < correctQuestionAnswers.Count; iAnswer++) { GridAddData(gridRow + iAnswer + 1, gridColumn, ((iAnswer + 1).ToString() + "- " + correctQuestionAnswers[iAnswer].Text)); GridAddData(gridRow + iAnswer + 1, gridColumn + 1, correctQuestionAnswers[iAnswer].IsCorrect.ToString()); GridAddData(gridRow + iAnswer + 1, gridColumn + 2, correctQuestionAnswers[iAnswer].ErrorCost.ToString()); } // show the weight of the question GridAddData(gridRow + correctQuestionAnswers.Count + 1, gridColumn, "Peso della domanda"); GridAddData(gridRow + correctQuestionAnswers.Count + 1, gridColumn + 1, q.Weight.ToString()); gridColumn += 3; } gridRow += 7; gridColumn = 0; GridAddData(gridRow, gridColumn, "Correzione delle risposte degli allievi"); gridRow++; // showing and grading the answers of the students foreach (Student s in studentsThatAnswered) { GridAddData(gridRow, gridColumn, s.LastName + " " + s.FirstName); GridAddData(gridRow, gridColumn + 3, "Risposta allievo"); GridAddData(gridRow, gridColumn + 3, "Costo errori"); double weightedSum = 0; double sumOfWeights = 0; gridColumn = 0; // grading of students' answers foreach (Question q in allQuestions) { correctQuestionAnswers = db.GetAllCorrectAnswersToThisQuestionOfThisTest( q.IdQuestion, currentTest.IdTest); List <StudentsAnswer> studentsQuestionAnswers = db.GetAllAnswersOfAStudentToAQuestionOfThisTest( s.IdStudent, q.IdQuestion, currentTest.IdTest); int budgetOfQuestion = 100; gridColumn++; GridAddData(gridRow, gridColumn, q.Text); // scan all the correct answers to this question for (int iCorrectAnswer = 0; iCorrectAnswer < correctQuestionAnswers.Count; iCorrectAnswer++) { GridAddData(gridRow + iCorrectAnswer + 1, gridColumn, (correctQuestionAnswers[iCorrectAnswer].Text)); bool found = false; int budgetDecreaseForThisAnswer; int iStudentAnswer; // scan all the answers that the current student has given to this question for (iStudentAnswer = 0; iStudentAnswer < studentsQuestionAnswers.Count; iStudentAnswer++) { if (correctQuestionAnswers[iCorrectAnswer].IdAnswer == studentsQuestionAnswers[iStudentAnswer].IdAnswer) { found = true; break; } } if (found) { if (correctQuestionAnswers[iCorrectAnswer].IsCorrect == studentsQuestionAnswers[iStudentAnswer].StudentsBoolAnswer) { // if answer is correct it doesn't decrease the budget budgetDecreaseForThisAnswer = 0; } else { // if not correct it decreases the budget of ErrorCost budgetDecreaseForThisAnswer = (int)correctQuestionAnswers[iCorrectAnswer].ErrorCost; budgetOfQuestion -= budgetDecreaseForThisAnswer; } GridAddData(gridRow + iCorrectAnswer + 1, gridColumn + 1, studentsQuestionAnswers[iStudentAnswer].StudentsBoolAnswer.ToString()); } else { // no answer: question is spoiled budgetDecreaseForThisAnswer = 100; budgetOfQuestion -= budgetDecreaseForThisAnswer; GridAddData(gridRow + iCorrectAnswer + 1, gridColumn + 1, "No ans"); } GridAddData(gridRow + iCorrectAnswer + 1, gridColumn + 2, budgetDecreaseForThisAnswer.ToString()); GridAddData(gridRow + iCorrectAnswer + 1, gridColumn, correctQuestionAnswers[iCorrectAnswer].Text); if (budgetOfQuestion < 0) { budgetOfQuestion = 0; } } GridAddData(gridRow + 5, gridColumn + 1, "Punti per domanda"); GridAddData(gridRow + 5, gridColumn + 2, budgetOfQuestion.ToString()); weightedSum += (double)q.Weight * budgetOfQuestion; // currently mean is NOT Weigthed !!!! sumOfWeights += (double)q.Weight; gridColumn += 2; } // weighted mean double weightedMean = weightedSum / sumOfWeights; GridAddData(gridRow, gridColumn + 1, "Media pesata"); GridAddData(gridRow + 1, gridColumn + 1, weightedMean.ToString()); gridRow += 7; gridColumn = 0; } }