public static void storeResponses(MarkingScript markingScript, PupilDetail pupil) { using (var sqlConnection = new SqlConnection(dbConnection)) { sqlConnection.Open(); try { var testDate = DateTime.Now; foreach (Mark mark in markingScript.marks) { var response = new ResponseRecord() { PID = pupil.ID, UPN = pupil.UPN, schoolCode = pupil.SCH, subject = markingScript.test.subject, testYear = markingScript.test.testYear, testDate = testDate, question = mark.question.ToString(), answer = mark.score, response = (!String.IsNullOrEmpty(mark.response)) ? (mark.response.Length <= 50) ? mark.response : mark.response.Substring(0, 49) + "«" : null }; sqlConnection.Execute( @"insert rp_Responses([PID],[UPN],[schoolCode],[Subject],[TestYear],[TestDate],[Question],[Answer],[Response]) values (@PID, @UPN, @schoolCode, @subject, @testYear, @testDate, @question, @answer, @response) ", response); } } catch (Exception e) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound) { ReasonPhrase = e.Message }); } finally { sqlConnection.Close(); } } }
public IHttpActionResult MarkResponses() { // decode the querystring string json = HttpUtility.UrlDecode(Request.RequestUri.Query).Substring(1); // replace reduce data size character replacements // --single quotes json = string.Join("'", json.Split('^')); // --double quotes json = string.Join("\"", json.Split('~')); // --spaces json = string.Join(" ", json.Split('_')); // create responses json var pupilResponses = JsonConvert.DeserializeObject<PupilResponses>(json); // marking script path var patToScripts = System.Configuration.ConfigurationManager.AppSettings["MarkingScriptsPath"]; MarkingScript markingScript = new MarkingScript(); try { var testPaper = pupilResponses.id; var totalScore = 0; // load marking script markingScript = JsonConvert.DeserializeObject<MarkingScript>(File.ReadAllText(patToScripts + "\\" + testPaper + ".json")); // mark each question foreach (Response r in pupilResponses.responses) { // find marking schema for question Mark markingSchema = markingScript.marks.Find(x => x.question == r.q); // mark question based on type var score = 0; switch (markingSchema.type) { case "match": score = Scorer.markQuestionTypeMatch(markingSchema, r.r); break; case "matchabs": score = Scorer.matchQuestionTypeNumericalAbsolute(markingSchema, r.r); break; case "matchcontains": score = Scorer.matchQuestionTypeContainsResponses(markingSchema, r.r); break; case "matchcombo": score = Scorer.markQuestionTypeMatchMultipleResponse(markingSchema, r.r); break; case "matchwildcard": score = Scorer.markQuestionTypeMatchWildCard(markingSchema, r.r); break; case "count": score = Scorer.markQuestionTypeCount(markingSchema, r.r); break; case "combo": score = Scorer.matchQuestionMultipleResponses(markingSchema, r.r); break; case "combogroup": score = Scorer.matchQuestionMultipleResponsesGrouping(markingSchema, r.r); break; case "range": score = Scorer.markQuestionTypeRange(markingSchema, r.r); break; } // set score markingSchema.score = score; totalScore += score; // set response markingSchema.response = r.r; } // set total score markingScript.totalScore = totalScore; // store responses and scores if we are not returning the score if (!pupilResponses.returnScores) DataAccess.storeResponses(markingScript, pupilResponses.login); } catch (Exception e) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound) { ReasonPhrase = e.Message }); } if (pupilResponses.returnScores) return Ok("{ \"system\": \"" + System.Configuration.ConfigurationManager.AppSettings["CompileSettings"] + "\", \"totalScore\": " + markingScript.totalScore + " }"); else return Ok("success"); }