private static IudicoNodeInfo ParseNode(Node node, ICourseStorage storage) { IudicoNodeInfo ni = new IudicoNodeInfo(); ni.Id = node.Id; ni.CourseId = node.CourseId; var path = storage.GetNodePath(node.Id) + ".html"; TextReader reader = File.OpenText(path); XmlDocument xmlDoc = FromHtml(reader); // find iudico object nodes <object iudico-question="..." ... > foreach (XmlElement el in xmlDoc.GetElementsByTagName("object")) { bool iudicoQuestion = false; IudicoQuestionInfo qi = null; // check if it is iudico object if (el.Attributes["iudico-question"] != null && el.Attributes["iudico-question"].Value == "true") { iudicoQuestion = true; switch (el.Attributes["iudico-type"].Value) { case "iudico-simple": qi = new IudicoSimpleQuestion { Type = IudicoQuestionType.IudicoSimple }; break; case "iudico-choice": qi = new IudicoChoiceQuestion { Type = IudicoQuestionType.IudicoChoice }; break; case "iudico-compile": qi = new IudicoCompiledTest { Type = IudicoQuestionType.IudicoCompile }; break; } } if (iudicoQuestion == true) { // <param name="..." value="..." /> foreach (XmlNode xmlNode in el.ChildNodes) { if (xmlNode.Attributes["name"] != null && xmlNode.Attributes["name"].Value == "rank") { double score = 0; double.TryParse(xmlNode.Attributes["value"].Value, out score); qi.MaxScore += score; } if (xmlNode.Attributes["name"] != null && xmlNode.Attributes["name"].Value == "question") { qi.QuestionText = xmlNode.Attributes["value"].Value; } switch (qi.Type) { case IudicoQuestionType.IudicoSimple: if (xmlNode.Attributes["name"] != null && xmlNode.Attributes["name"].Value == "correctAnswer") { (qi as IudicoSimpleQuestion).CorrectAnswer = xmlNode.Attributes["value"].Value; } break; case IudicoQuestionType.IudicoChoice: if (xmlNode.Attributes["name"] != null && xmlNode.Attributes["name"].Value == "correct") { (qi as IudicoChoiceQuestion).CorrectChoice = xmlNode.Attributes["value"].Value; } if (xmlNode.Attributes["name"] != null && xmlNode.Attributes["name"].Value.StartsWith("option")) { (qi as IudicoChoiceQuestion).Options.Add(new Tuple <string, string>(xmlNode.Attributes["name"].Value, xmlNode.Attributes["value"].Value)); } break; case IudicoQuestionType.IudicoCompile: if (xmlNode.Attributes["name"] != null && xmlNode.Attributes["name"].Value.StartsWith("testInput")) { (qi as IudicoCompiledTest).TestInputs.Add(new Tuple <string, string>(xmlNode.Attributes["name"].Value, xmlNode.Attributes["value"].Value)); } if (xmlNode.Attributes["name"] != null && xmlNode.Attributes["name"].Value.StartsWith("testOutput")) { (qi as IudicoCompiledTest).TestOutputs.Add(new Tuple <string, string>(xmlNode.Attributes["name"].Value, xmlNode.Attributes["value"].Value)); } break; } } ni.QuestionsInfo.Add(qi); } } return(ni); }
public IudicoCourseInfo GetCourseInfo(int id) { var db = this.GetDbContext(); var courseInfo = new IudicoCourseInfo { Id = id }; var databaseNodesInfo = db.NodesInfo.Where(n => n.CourseId == id); foreach (var ni in databaseNodesInfo) { var nodeInfo = new IudicoNodeInfo { CourseId = id, Id = ni.NodeId }; var databaseQuestionsInfo = db.QuestionsInfo.Where(q => q.NodeId == ni.NodeId); foreach (var qi in databaseQuestionsInfo) { IudicoQuestionInfo questionInfo = null; switch (qi.Type) { case 1: var simpleQuestion = db.SimpleQuestions.Single(q => q.QuestionId == qi.Id); questionInfo = new IudicoSimpleQuestion { QuestionText = qi.Text, MaxScore = qi.MaxScore, Type = IudicoQuestionType.IudicoSimple, CorrectAnswer = simpleQuestion.CorrectAnswer }; break; case 2: var options = db.ChoiceQuestionsOptions.Where(q => q.QuestionId == qi.Id); var correctChoice = db.ChoiceQuestionsCorrectChoices.Single(q => q.QuestionId == qi.Id); questionInfo = new IudicoChoiceQuestion { QuestionText = qi.Text, MaxScore = qi.MaxScore, Type = IudicoQuestionType.IudicoChoice, CorrectChoice = correctChoice.CorrectChoice, Options = (from o in options select new Tuple <string, string>(o.Option, o.Description)).ToList() }; break; case 3: var compiledTests = db.CompiledTestQuestions.Where(q => q.QuestionId == qi.Id); questionInfo = new IudicoCompiledTest { QuestionText = qi.Text, MaxScore = qi.MaxScore, Type = IudicoQuestionType.IudicoCompile, TestInputs = (from ct in compiledTests select new Tuple <string, string>("testInput" + ct.TestNumber, ct.TestInput)).ToList(), TestOutputs = (from ct in compiledTests select new Tuple <string, string>("testOutput" + ct.TestNumber, ct.TestOutput)).ToList(), }; break; } nodeInfo.QuestionsInfo.Add(questionInfo); } courseInfo.NodesInfo.Add(nodeInfo); } return(courseInfo); }