/// <summary> /// Method to read questions from an xml file /// </summary> /// <param name="xmlFilePath">Fully qualified path to xml file</param> /// <returns>A specially formatted dictionary containing the questions in the xml file</returns> public static Dictionary<string, List<Question>> ReadExamToFormat(string xmlFilePath) { XPathDocument doc = new XPathDocument(xmlFilePath); XPathNavigator nav = doc.CreateNavigator(); XmlNamespaceManager nm = new XmlNamespaceManager(nav.NameTable); Dictionary<string, List<Question>> formattedQuestionList = new Dictionary<string, List<Question>>(); XPathExpression expression = nav.Compile("//Section"); XPathNodeIterator rootIterator = nav.Select(expression); while (rootIterator.MoveNext()) { string sectionTitle = rootIterator.Current.GetAttribute("Title", nm.DefaultNamespace); List<Question> sectionQuestions = new List<Question>(); XPathNodeIterator subIterator = rootIterator.Current.SelectChildren(XPathNodeType.Element); while (subIterator.MoveNext()) { Question ques = new Question(); ques.SectionTitle = sectionTitle; ques.QuestionNumber = Convert.ToInt32(subIterator.Current.GetAttribute("No", nm.DefaultNamespace)); Dictionary<char, string> options = new Dictionary<char, string>(); XPathNodeIterator iter = subIterator.Current.SelectChildren(XPathNodeType.Element); while (iter.MoveNext()) { if (iter.Current.LocalName == "Text") { ques.QuestionText = iter.Current.Value; } else if (iter.Current.LocalName == "Image") { if (string.IsNullOrWhiteSpace(iter.Current.Value)) { ques.QuestionImagePath = null; } else { ques.QuestionImagePath = Path.Combine(Path.GetDirectoryName(xmlFilePath), iter.Current.Value); } } else if (iter.Current.LocalName == "Answer") { ques.QuestionAnswer = Convert.ToChar(iter.Current.Value); } if (iter.Current.LocalName == "Options") { XPathNodeIterator ite = iter.Current.SelectChildren(XPathNodeType.Element); while (ite.MoveNext()) { char option; string optionText; option = Convert.ToChar(ite.Current.GetAttribute("Title", nm.DefaultNamespace)); optionText = ite.Current.Value; options.Add(option, optionText); } ques.QuestionOptions = options; } } sectionQuestions.Add(ques); } formattedQuestionList.Add(sectionTitle, sectionQuestions); } return formattedQuestionList; }
private void PushPreviousQuestion(TreeNode questionNode) { Question queryResult = rawQuestionList.FirstOrDefault(s => s.SectionTitle == questionNode.Parent.Text && s.QuestionNumber == Convert.ToInt32(questionNode.Text.Replace("Question ", ""))); if (queryResult != null) { int questionIndex = rawQuestionList.IndexOf(queryResult); rawQuestionList[questionIndex].AnswerExplanation = txt_answer_explanation.Text; if (pan_options.Controls.Count > 0) { if ((pan_options.Controls.OfType<OptionControl>().First<OptionControl>(p => p.IsChecked == true)) != null) { rawQuestionList[questionIndex].QuestionAnswer = (pan_options.Controls.OfType<OptionControl>().First<OptionControl>(p => p.IsChecked == true)).OptionLetter; } else { rawQuestionList[questionIndex].QuestionAnswer = 'A'; } } rawQuestionList[questionIndex].QuestionImagePath = pct_question_picture.ImageLocation; rawQuestionList[questionIndex].QuestionNumber = Int32.Parse(questionNode.Text.Replace("Question ", "")); Dictionary<char, string> questionOptionList = new Dictionary<char, string>(); foreach (OptionControl option in (pan_options.Controls.OfType<OptionControl>())) { questionOptionList.Add(option.OptionLetter, option.OptionText); } rawQuestionList[questionIndex].QuestionOptions = questionOptionList; rawQuestionList[questionIndex].QuestionText = txt_question_text.Text; rawQuestionList[questionIndex].SectionTitle = questionNode.Parent.Text; } else { Question question = new Question(); question.AnswerExplanation = txt_answer_explanation.Text; if (pan_options.Controls.Count > 0) { if ((pan_options.Controls.OfType<OptionControl>().First<OptionControl>(p => p.IsChecked == true)) != null) { question.QuestionAnswer = (pan_options.Controls.OfType<OptionControl>().First<OptionControl>(p => p.IsChecked == true)).OptionLetter; } else { question.QuestionAnswer = 'A'; } } question.QuestionImagePath = pct_question_picture.ImageLocation; question.QuestionNumber = Int32.Parse(questionNode.Text.Replace("Question ", "")); Dictionary<char, string> questionOptionList = new Dictionary<char, string>(); foreach (OptionControl option in (pan_options.Controls.OfType<OptionControl>())) { questionOptionList.Add(option.OptionLetter, option.OptionText); } question.QuestionOptions = questionOptionList; question.QuestionText = txt_question_text.Text; question.SectionTitle = questionNode.Parent.Text; rawQuestionList.Add(question); } }
private void Open(string examFilePath) { CloseExam(null, null); New_Exam(null, null); CurrentFileNameWithExtension = examFilePath; string foldername = Path.GetFileNameWithoutExtension(examFilePath); string extractionFolderPath = Path.Combine(GlobalPathVariables.creatorFolderPath, foldername); if (Directory.Exists(extractionFolderPath)) { Directory.Delete(extractionFolderPath, true); Directory.CreateDirectory(extractionFolderPath); } else { Directory.CreateDirectory(extractionFolderPath); } using (ZipFile zip = new ZipFile(examFilePath)) { zip.ExtractAll(extractionFolderPath, ExtractExistingFileAction.OverwriteSilently); } string[] temp = Directory.GetFiles(extractionFolderPath, "*.xml", SearchOption.TopDirectoryOnly); string xmlFilePath = null; if (temp.Length > 0) xmlFilePath = temp[0]; if (string.IsNullOrWhiteSpace(xmlFilePath)) { MessageBox.Show("Sorry, the selected exam file is invalid!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { XPathDocument doc = new XPathDocument(xmlFilePath); XPathNavigator navigator = doc.CreateNavigator(); string version = ""; XPathExpression expression = navigator.Compile("//FileVersion"); XPathNodeIterator iterator = navigator.Select(expression); while (iterator.MoveNext()) { version = iterator.Current.Value; } expression = navigator.Compile("//ExamTitle"); iterator = navigator.Select(expression); while (iterator.MoveNext()) { txt_exam_title.Text = iterator.Current.Value; } expression = navigator.Compile("//TimeAllowed"); iterator = navigator.Select(expression); while (iterator.MoveNext()) { num_time_limit.Value = Convert.ToDecimal(iterator.Current.Value); } expression = navigator.Compile("//PassingScore"); iterator = navigator.Select(expression); while (iterator.MoveNext()) { num_passing_score.Value = Convert.ToDecimal(iterator.Current.Value); } expression = navigator.Compile("//ExamCode"); iterator = navigator.Select(expression); while (iterator.MoveNext()) { txt_exam_code.Text = iterator.Current.Value; } expression = navigator.Compile("//ExamInstructions"); iterator = navigator.Select(expression); while (iterator.MoveNext()) { txt_exam_instructions.Text = iterator.Current.Value; } expression = navigator.Compile("//Section"); iterator = navigator.Select(expression); while (iterator.MoveNext()) { TreeNode secNode = new TreeNode(); secNode.Name = "sectionNode" + (trv_explorer.Nodes[0].Nodes.Count); secNode.Text = iterator.Current.GetAttribute("Title", ""); secNode.ImageIndex = 1; secNode.SelectedImageIndex = 1; secNode.ContextMenuStrip = cms_explorer; XPathNodeIterator subIterator = iterator.Current.SelectChildren(XPathNodeType.Element); while (subIterator.MoveNext()) { Question question = new Question(); question.SectionTitle = iterator.Current.GetAttribute("Title", ""); question.QuestionNumber = Convert.ToInt32(subIterator.Current.GetAttribute("No", "")); Dictionary<char, string> options = new Dictionary<char, string>(); XPathNodeIterator subIter = subIterator.Current.SelectChildren(XPathNodeType.Element); while (subIter.MoveNext()) { if (subIter.Current.LocalName == "Text") { question.QuestionText = subIter.Current.Value; } else if (subIter.Current.LocalName == "Image") { question.QuestionImagePath = subIter.Current.Value; } else if (subIter.Current.LocalName == "Answer") { question.QuestionAnswer = Convert.ToChar(subIter.Current.Value); } if (subIter.Current.LocalName == "Options") { XPathNodeIterator ite = subIter.Current.SelectChildren(XPathNodeType.Element); while (ite.MoveNext()) { char option; string optionText; string tempp = ite.Current.GetAttribute("Title", ""); option = Convert.ToChar(tempp); optionText = ite.Current.Value; options.Add(option, optionText); } question.QuestionOptions = options; } if (version == "1.0") { question.AnswerExplanation = "Version 1.0 files do not support explanations"; } else { if (iterator.Current.LocalName == "AnswerExplanation") question.AnswerExplanation = iterator.Current.Value; } } rawQuestionList.Add(question); TreeNode quesNode = new TreeNode(); quesNode.ImageIndex = 2; quesNode.SelectedImageIndex = 2; quesNode.ContextMenuStrip = cms_explorer; quesNode.Text = "Question " + (secNode.Nodes.Count + 1); quesNode.Name = "questionNode" + secNode.Nodes.Count; secNode.Nodes.Add(quesNode); } trv_explorer.Nodes[0].Nodes.Add(secNode); } trv_explorer.ExpandAll(); closeExamToolStripMenuItem.Enabled = true; printPreviewToolStripMenuItem.Enabled = true; printToolStripButton.Enabled = true; printToolStripMenuItem.Enabled = true; savedAs = true; AllChangesSaved = true; } }
/// <summary> /// This method makes sure when the save method is called the current question is committed /// </summary> private void AddCurrentQuestion() { try { if (trv_explorer.SelectedNode.Name.Contains("ques")) { bool exists = false; Question present = new Question(); try { var temp = tempExamStore.FindAll(s => s.SectionTitle == (trv_explorer.SelectedNode.Parent.Text)); present = temp.SingleOrDefault(c => c.QuestionNumber == Convert.ToInt32(trv_explorer.SelectedNode.Text.Replace("Question ", ""))); exists = tempExamStore.Contains(present); } catch (InvalidOperationException ex) { Debug.Print(ex.Message + Environment.NewLine + ex.InnerException); } finally { if (tempExamStore.Contains(present)) { int index = tempExamStore.IndexOf(present); tempExamStore[index].QuestionText = txt_question_text.Text; tempExamStore[index].QuestionImagePath = pct_question_picture.ImageLocation; try { tempExamStore[index].QuestionAnswer = Convert.ToChar(((OptionControl)pan_options.Controls.OfType<OptionControl>().First<OptionControl>(p => p.IsChecked == true)).OptionLetter); } catch (InvalidOperationException) { tempExamStore[index].QuestionAnswer = 'A'; } tempExamStore[index].QuestionNumber = Convert.ToInt32(trv_explorer.SelectedNode.Text.Replace("Question ", "")); Dictionary<char, string> tempDic = new Dictionary<char, string>(); foreach (var ctrl in pan_options.Controls.OfType<OptionControl>()) { tempDic.Add(Convert.ToChar(ctrl.OptionLetter), ctrl.OptionText); } tempExamStore[index].QuestionOptions = tempDic; tempExamStore[index].SectionTitle = trv_explorer.SelectedNode.Parent.Text; } else { //save the previous question Question ques = new Question(); try { ques.QuestionAnswer = Convert.ToChar(((OptionControl)pan_options.Controls.OfType<OptionControl>().First<OptionControl>(p => p.IsChecked == true)).OptionLetter); } catch (InvalidOperationException) { ques.QuestionAnswer = 'A'; } ques.QuestionImagePath = pct_question_picture.ImageLocation; ques.QuestionNumber = Convert.ToInt32(trv_explorer.SelectedNode.Text.Replace("Question ", "")); Dictionary<char, string> tempDic = new Dictionary<char, string>(); foreach (var ctrl in pan_options.Controls.OfType<OptionControl>()) { tempDic.Add(Convert.ToChar(ctrl.OptionLetter), ctrl.OptionText); } ques.QuestionOptions = tempDic; ques.QuestionText = txt_question_text.Text; ques.SectionTitle = trv_explorer.SelectedNode.Parent.Text; //add question to store this.tempExamStore.Add(ques); } } } } catch (NullReferenceException ex) { Debug.Print("Error: " + ex.Message + ", Inner Exception: " + ex.InnerException); } catch (ArgumentNullException ex) { Debug.Print("Error: " + ex.Message + ", Inner Exception: " + ex.InnerException); } }
private void trv_explorer_BeforeSelect(object sender, TreeViewCancelEventArgs e) { try { if (((TreeView)sender).SelectedNode.Name.Contains("ques")) { bool exists = false; Question present = new Question(); try { var temp = tempExamStore.FindAll(s => s.SectionTitle == ((TreeView)sender).SelectedNode.Parent.Text); present = temp.SingleOrDefault(c => c.QuestionNumber == Convert.ToInt32(((TreeView)sender).SelectedNode.Text.Replace("Question ", ""))); exists = tempExamStore.Contains(present); } catch (NullReferenceException ex) { Debug.Print(ex.Message + Environment.NewLine + ex.InnerException); } finally { if (tempExamStore.Contains(present)) { int index = tempExamStore.IndexOf(present); tempExamStore[index].QuestionText = txt_question_text.Text; tempExamStore[index].QuestionImagePath = pct_question_picture.ImageLocation; try { tempExamStore[index].QuestionAnswer = Convert.ToChar(((OptionControl)pan_options.Controls.OfType<OptionControl>().First<OptionControl>(p => p.IsChecked == true)).OptionLetter); } catch (InvalidOperationException) { MessageBox.Show("An answer to this question has not been selected, the first option ('A') has been selected by default. Return to the question to change it","Option not selected"); tempExamStore[index].QuestionAnswer = 'A'; } tempExamStore[index].QuestionNumber = Convert.ToInt32(((TreeView)sender).SelectedNode.Text.Replace("Question ", "")); Dictionary<char, string> tempDic = new Dictionary<char, string>(); foreach (var ctrl in pan_options.Controls.OfType<OptionControl>()) { tempDic.Add(Convert.ToChar(ctrl.OptionLetter), ctrl.OptionText); } tempExamStore[index].QuestionOptions = tempDic; tempExamStore[index].SectionTitle = ((TreeView)sender).SelectedNode.Parent.Text; //clear the boxes txt_question_text.Clear(); pct_question_picture.Image = null; pct_question_picture.ImageLocation = null; pan_options.Controls.Clear(); } else { //save the previous question Question ques = new Question(); try { ques.QuestionAnswer = Convert.ToChar(((OptionControl)pan_options.Controls.OfType<OptionControl>().First<OptionControl>(p => p.IsChecked == true)).OptionLetter); } catch (InvalidOperationException) { ques.QuestionAnswer = 'A'; } ques.QuestionImagePath = pct_question_picture.ImageLocation; ques.QuestionNumber = Convert.ToInt32(((TreeView)sender).SelectedNode.Text.Replace("Question ", "")); Dictionary<char, string> tempDic = new Dictionary<char, string>(); foreach (var ctrl in pan_options.Controls.OfType<OptionControl>()) { tempDic.Add(Convert.ToChar(ctrl.OptionLetter), ctrl.OptionText); } ques.QuestionOptions = tempDic; ques.QuestionText = txt_question_text.Text; ques.SectionTitle = ((TreeView)sender).SelectedNode.Parent.Text; //clear the boxes txt_question_text.Clear(); pct_question_picture.Image = null; pan_options.Controls.Clear(); RefreshInputControls(); //add question to store this.tempExamStore.Add(ques); } } } } catch (NullReferenceException) { // A necessary Evil } }
private void trv_explorer_AfterSelect(object sender, TreeViewEventArgs e) { if (((TreeView)sender).SelectedNode.Name.Contains("ques")) { //enable relevant options addOptionToolStripMenuItem.Enabled = true; newQuestionToolStripMenuItem.Enabled = false; btn_new_question.Enabled = false; newSectionToolStripMenuItem.Enabled = false; btn_new_section.Enabled = false; splcn_main_view.Panel2.Enabled = true; editToolStripMenuItem1.Enabled = false; previousItemToolStripMenuItem.Enabled = true; nextItemToolStripMenuItem.Enabled = true; //enable printing btn_print_exam.Enabled = true; printToolStripMenuItem.Enabled = true; btn_print_preview.Enabled = true; //display question lbl_question_and_section.Text = "Section: " + ((TreeView)sender).SelectedNode.Parent.Text + ", " + ((TreeView)sender).SelectedNode.Text; //RefreshInputControls(); try { bool exists = false; Question present = new Question(); try { var temp = tempExamStore.FindAll(s => s.SectionTitle == ((TreeView)sender).SelectedNode.Parent.Text); present = temp.SingleOrDefault(c => c.QuestionNumber == Convert.ToInt32(((TreeView)sender).SelectedNode.Text.Replace("Question ", ""))); exists = tempExamStore.Contains(present); } catch (NullReferenceException ex) { Debug.Print("Error: " + ex.Message + ", Inner Exception: " + ex.InnerException); } finally { if (exists) { txt_question_text.Text = present.QuestionText; if (string.IsNullOrWhiteSpace(present.QuestionImagePath)) { pct_question_picture.Image = null; pct_question_picture.ImageLocation = null; btn_clear_picture.Visible = false; btn_select_picture.Visible = false; pct_question_picture.Visible = false; } else { pct_question_picture.Image = new Bitmap(present.QuestionImagePath); pct_question_picture.ImageLocation = present.QuestionImagePath; btn_clear_picture.Visible = true; btn_select_picture.Visible = true; pct_question_picture.Visible = true; } foreach (var item in present.QuestionOptions) { OptionControl ctrl = new OptionControl(); ctrl.OptionLetter = item.Key; ctrl.OptionText = item.Value; if (item.Key == present.QuestionAnswer) { ctrl.IsChecked = true; } if (pan_options.Controls.Count != 0) { OptionControl info = (OptionControl)pan_options.Controls[pan_options.Controls.OfType<OptionControl>().Count() - 1]; ctrl.Location = new Point(info.Location.X, info.Location.Y + 35); int num = Convert.ToInt32(info.Name.Replace("optionControl", "")); ctrl.Name = "optionControl" + (num + 1); } else { ctrl.Location = new Point(0, 0); ctrl.Name = "optionControl1"; } pan_options_ControlChanged(btn_add_option, null); pan_options.Controls.Add(ctrl); } } else { btn_select_picture.Visible = false; btn_clear_picture.Visible = false; pct_question_picture.Visible = false; } } } catch (NullReferenceException ex) { //MessageBox.Show(ex.Message + " " + ex.InnerException + Environment.NewLine + ex.Source); Debug.Print("Error: " + ex.Message + ", Inner Exception: " + ex.InnerException); } catch (ArgumentNullException ex) { //MessageBox.Show(ex.Message + " " + ex.InnerException + Environment.NewLine + ex.Source); Debug.Print("Error: " + ex.Message + ", Inner Exception: " + ex.InnerException); } } //enable add questions else if (((TreeView)sender).SelectedNode.Name.Contains("secNode")) { splcn_main_view.Panel2.Enabled = false; addOptionToolStripMenuItem.Enabled = false; newQuestionToolStripMenuItem.Enabled = true; btn_new_question.Enabled = true; newSectionToolStripMenuItem.Enabled = false; btn_new_section.Enabled = false; previousItemToolStripMenuItem.Enabled = true; nextItemToolStripMenuItem.Enabled = true; //clear status text lbl_question_and_section.Text = ""; //dislplay edit option editToolStripMenuItem1.Enabled = true; //disable printing btn_print_exam.Enabled = false; printToolStripMenuItem.Enabled = false; btn_print_preview.Enabled = false; } //enable add sections else if (((TreeView)sender).SelectedNode.Name.Contains("examNode")) { splcn_main_view.Panel2.Enabled = false; newSectionToolStripMenuItem.Enabled = true; addOptionToolStripMenuItem.Enabled = false; btn_new_section.Enabled = true; newQuestionToolStripMenuItem.Enabled = false; btn_new_question.Enabled = false; previousItemToolStripMenuItem.Enabled = false; nextItemToolStripMenuItem.Enabled = false; //display edit option editToolStripMenuItem1.Enabled = false; //disable printing btn_print_exam.Enabled = false; printToolStripMenuItem.Enabled = false; btn_print_preview.Enabled = false; } else { splcn_main_view.Panel2.Enabled = false; addOptionToolStripMenuItem.Enabled = false; newQuestionToolStripMenuItem.Enabled = false; addOptionToolStripMenuItem.Enabled = false; btn_new_question.Enabled = false; newSectionToolStripMenuItem.Enabled = false; btn_new_section.Enabled = false; //disable printing btn_print_exam.Enabled = false; printToolStripMenuItem.Enabled = false; btn_print_preview.Enabled = false; } }