public GradingRules(List <Question> questions, AllAttempts allAttempts) { answerMap = new Dictionary <int, SortedSet <Answer> >(); foreach (Question question in questions) { answerMap[question.Id] = new SortedSet <Answer>(); } foreach (Attempt attempt in allAttempts.GetAttempts()) { Count++; foreach (Question question in questions) { Answer answer = attempt.GetAnswer(question.Id); if (answer.Text.Length > 0) { answerMap[question.Id].Add(answer); } } } }
public static void SetScores() { // Get information about all of the short answer questions that appear on the quiz List <Question> questions = GetQuestions(); Console.WriteLine(questions.Count + " short answer questions found"); // Get information about all submissions to the quiz List <Submission> submissions = GetSubmissions(); Console.WriteLine(submissions.Count + " submissions found"); // Get information about attempts AllAttempts attempts = GetAllAttempts(questions); Console.WriteLine(attempts.Count + " attempts found"); // Read the grading rules for this quiz GradingRules rules; using (var input = new CsvReader(new StreamReader(@"..\..\" + GetQuizFilename()))) { rules = new GradingRules(input); } Console.WriteLine(GetQuizFilename() + " is based on " + rules.Count + " attempts"); // Warn the user int delta = attempts.Count - rules.Count; if (delta != 0) { Console.WriteLine("There have " + delta + " attempts since " + GetQuizFilename() + " was created"); Console.WriteLine("You should recreate that file"); } // Make sure we proceed Console.Write("Update grades in Canvas? [y/n] "); if (Console.ReadLine().Trim().ToLower() != "y") { return; } // Change the grade of attempt if necessary foreach (Attempt attempt in attempts.GetAttempts()) { Submission submission = GetSubmission(submissions, attempt.UserID, attempt.Number); dynamic correction = GetCorrection(attempt, questions, rules, out string name); if (correction == null) { Console.WriteLine("unchanged (v" + attempt.Number + ") " + name); } else if (submission == null) { Console.WriteLine("*MISSING* (v" + attempt.Number + ") " + name); } else { using (HttpClient client = CanvasHttp.MakeClient()) { String url = String.Format("/api/v1/courses/{0}/quizzes/{1}/submissions/{2} ", Auth.COURSE_ID, quizID, submission.Id); var content = new StringContent(correction.ToString(), Encoding.UTF8, "application/json"); HttpResponseMessage response = client.PutAsync(url, content).Result; //HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); if (response.IsSuccessStatusCode) { Console.WriteLine("updated (v" + attempt.Number + ") " + name); } else { Console.WriteLine("*FAILED* (v" + attempt.Number + ") " + name + " " + response.StatusCode); return; } } } } }