ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/Qzhteln/trunk/TELTest/QuizContainer.cs
Revision: 2
Committed: Wed Feb 11 13:07:01 2026 UTC (8 weeks, 3 days ago) by figdor32
File size: 10836 byte(s)
Log Message:
Initial check-in

File Contents

# User Rev Content
1 figdor32 2 using System;
2     using System.Collections.Generic;
3     using System.IO;
4     using System.Linq;
5     using System.Text;
6     using System.Xml;
7    
8     namespace TELTest {
9     public class QuizContainer : IEquatable<QuizContainer> {
10     public QuizContainer() {
11     Created = DateTime.MinValue;
12     }
13     public string Title;
14     public string Author;
15     public string Password;
16     public DateTime Created;
17     public Guid Guid;
18     public QuizQuestion[] Questions;
19     public Visibility Visibility;
20     public string SerializeXML() {
21     XmlDocument xd = new XmlDocument();
22     var quiz = xd.CreateNode(XmlNodeType.Element, "Quiz", null);
23     var title = xd.CreateNode(XmlNodeType.Element, "Name", null);
24     title.InnerText = Title;
25     quiz.AppendChild(title);
26    
27     var author = xd.CreateNode(XmlNodeType.Element, "Author", null);
28     author.InnerText = Author;
29     quiz.AppendChild(author);
30    
31     var guid = xd.CreateNode(XmlNodeType.Element, "Guid", null);
32     guid.InnerText = Guid + "";
33     quiz.AppendChild(guid);
34    
35     var created = xd.CreateNode(XmlNodeType.Element, "Created", null);
36     created.InnerText = Created.ToString("O");
37     quiz.AppendChild(created);
38    
39     var pwd = xd.CreateNode(XmlNodeType.Element, "Password", null);
40     pwd.InnerText = Password;
41     quiz.AppendChild(pwd);
42    
43     var vis = xd.CreateNode(XmlNodeType.Element, "Visibility", null);
44     vis.InnerText = ((int)Visibility) + "";
45     quiz.AppendChild(vis);
46    
47     var questions = xd.CreateNode(XmlNodeType.Element, "Questions", null);
48     foreach (var item in Questions) {
49     var q = xd.CreateNode(XmlNodeType.Element, "Question", null);
50    
51     var cap = xd.CreateNode(XmlNodeType.Element, "Caption", null);
52     cap.InnerText = item.Content;
53     q.AppendChild(cap);
54    
55     foreach (var it2 in item.Answers) {
56     var ans = xd.CreateNode(XmlNodeType.Element, "Answer", null);
57     ans.InnerText = it2.Content;
58     if (it2.Correct) {
59     var attr = xd.CreateAttribute("Correct");
60     attr.InnerText = "True";
61     ans.Attributes.Append(attr);
62     }
63     q.AppendChild(ans);
64     }
65     questions.AppendChild(q);
66     }
67     quiz.AppendChild(questions);
68     return quiz.OuterXml;
69     }
70     public static QuizContainer FromXml(string content) {
71     XmlDocument xd = new XmlDocument();
72     xd.LoadXml(content);
73    
74     QuizContainer qc = new QuizContainer();
75     qc.Title = xd.GetElementsByTagName("Name")[0].InnerText;
76     qc.Author = xd.GetElementsByTagName("Author")[0].InnerText;
77     var pw = xd.GetElementsByTagName("Password");
78     if (pw.Count != 0) {
79     qc.Password = pw[0].InnerText;
80     }
81    
82     var vs = xd.GetElementsByTagName("Visibility");
83     if (vs.Count != 0) {
84     qc.Visibility = (Visibility)int.Parse(vs[0].InnerText);
85     }
86     qc.Guid = Guid.Parse(xd.GetElementsByTagName("Guid")[0].InnerText.Trim());
87     List<QuizQuestion> qst = new List<QuizQuestion>();
88     var q = xd.GetElementsByTagName("Questions")[0];
89     foreach (XmlNode item in q.ChildNodes) {
90     QuizQuestion qq = new QuizQuestion();
91     qq.Content = item["Caption"].InnerText;
92     List<QuizQuestionAnswer> ass = new List<QuizQuestionAnswer>();
93     var an = item.ChildNodes;
94     foreach (XmlNode aw in an) {
95    
96     if (aw.Name.ToUpper() != "ANSWER") continue;
97     ass.Add(new QuizQuestionAnswer() {
98     Content = aw.InnerText,
99     Correct = aw.Attributes["Correct"] != null && aw.Attributes["Correct"].InnerText.ToUpper() != "FALSE"
100     });
101     }
102     qq.Answers = ass.ToArray();
103     qst.Add(qq);
104     }
105     qc.Questions = qst.ToArray();
106     return qc;
107     }
108     public static QuizContainer FromQuizhouse_poor(string content) {
109     TextReader tr = new StringReader(content);
110     QuizContainer qc = new QuizContainer();
111     qc.Title = tr.ReadLine();
112     qc.Author = tr.ReadLine();
113     qc.Questions = new QuizQuestion[5];
114     for (int i = 0; i < 5; i++) {
115     qc.Questions[i] = new QuizQuestion() {
116     Content = tr.ReadLine(),
117     Answers = new[] {
118     new QuizQuestionAnswer(){ Content = tr.ReadLine() },
119     new QuizQuestionAnswer(){ Content = tr.ReadLine() },
120     new QuizQuestionAnswer(){ Content = tr.ReadLine() },
121     new QuizQuestionAnswer(){ Content = tr.ReadLine() },
122     }
123     };
124     string corr = tr.ReadLine();
125     switch (corr.ToUpper()[0]) {
126     case 'A':
127     qc.Questions[i].Answers[0].Correct = true;
128     break;
129     case 'B':
130     qc.Questions[i].Answers[1].Correct = true;
131     break;
132     case 'C':
133     qc.Questions[i].Answers[2].Correct = true;
134     break;
135     case 'D':
136     qc.Questions[i].Answers[3].Correct = true;
137     break;
138     }
139     }
140     return qc;
141     }
142    
143     public bool IsQuizhouse_poorCompliant() {
144     if (string.IsNullOrWhiteSpace(Title)) return false;
145     if (string.IsNullOrWhiteSpace(Author)) return false;
146     if (Questions.Length != 5) return false;
147     foreach (var item in Questions) {
148     if (item.Answers.Length != 4) return false;
149     int correctAnswers = 0;
150     foreach (var answer in item.Answers) {
151     if (answer.Correct) correctAnswers++;
152     }
153     if (correctAnswers != 1) return false;
154     }
155     return true;
156     }
157    
158     public string SerializeQuizhouse_poor() {
159     if (!IsQuizhouse_poorCompliant()) return null;
160     StringBuilder sb = new StringBuilder();
161     sb.AppendLineRN(Title);
162     sb.AppendLineRN(Author);
163     for (int i = 0; i < 5; i++) {
164     sb.AppendLineRN(Questions[i].Content);
165     char correct = ' ';
166     for (int j = 0; j < 4; j++) {
167     sb.AppendLineRN(Questions[i].Answers[j].Content);
168     if (Questions[i].Answers[j].Correct) correct = char.ToLower(Utilities.NumberToLetter(j)[0]);
169     }
170     sb.AppendLineRN(correct + "");
171     }
172     return sb.ToString();
173     }
174    
175     public override Boolean Equals(Object obj) => this.Equals(obj as QuizContainer);
176     public Boolean Equals(QuizContainer other) => !(other is null) && this.Title == other.Title && this.Author == other.Author && this.Password == other.Password && this.Created == other.Created && this.Guid.Equals(other.Guid) && Enumerable.SequenceEqual(this.Questions, other.Questions);
177     //EqualityComparer<QuizQuestion[]>.Default.Equals(this.Questions, other.Questions);
178    
179     public override Int32 GetHashCode() {
180     Int32 hashCode = -614606056;
181     hashCode = hashCode * -1521134295 + EqualityComparer<String>.Default.GetHashCode(this.Title);
182     hashCode = hashCode * -1521134295 + EqualityComparer<String>.Default.GetHashCode(this.Author);
183     hashCode = hashCode * -1521134295 + EqualityComparer<String>.Default.GetHashCode(this.Password);
184     hashCode = hashCode * -1521134295 + this.Created.GetHashCode();
185     hashCode = hashCode * -1521134295 + this.Guid.GetHashCode();
186     hashCode = hashCode * -1521134295 + EqualityComparer<QuizQuestion[]>.Default.GetHashCode(this.Questions);
187     return hashCode;
188     }
189    
190     public static Boolean operator ==(QuizContainer left, QuizContainer right) {
191     return EqualityComparer<QuizContainer>.Default.Equals(left, right);
192     }
193    
194     public static Boolean operator !=(QuizContainer left, QuizContainer right) {
195     return !(left == right);
196     }
197     }
198     public struct QuizQuestion : IEquatable<QuizQuestion> {
199     public string Content;
200     public QuizQuestionAnswer[] Answers;
201    
202     public override Boolean Equals(Object obj) => obj is QuizQuestion question && this.Equals(question);
203     public Boolean Equals(QuizQuestion other) => this.Content == other.Content && Enumerable.SequenceEqual(this.Answers, other.Answers);
204    
205     public override Int32 GetHashCode() {
206     Int32 hashCode = 2043989270;
207     hashCode = hashCode * -1521134295 + EqualityComparer<String>.Default.GetHashCode(this.Content);
208     hashCode = hashCode * -1521134295 + EqualityComparer<QuizQuestionAnswer[]>.Default.GetHashCode(this.Answers);
209     return hashCode;
210     }
211    
212     public static Boolean operator ==(QuizQuestion left, QuizQuestion right) {
213     return left.Equals(right);
214     }
215    
216     public static Boolean operator !=(QuizQuestion left, QuizQuestion right) {
217     return !(left == right);
218     }
219     }
220     public struct QuizQuestionAnswer : IEquatable<QuizQuestionAnswer> {
221     public string Content;
222     public bool Correct;
223    
224     public override Boolean Equals(Object obj) => obj is QuizQuestionAnswer answer && this.Equals(answer);
225     public Boolean Equals(QuizQuestionAnswer other) => this.Content == other.Content && this.Correct == other.Correct;
226    
227     public override Int32 GetHashCode() {
228     Int32 hashCode = -1538403773;
229     hashCode = hashCode * -1521134295 + EqualityComparer<String>.Default.GetHashCode(this.Content);
230     hashCode = hashCode * -1521134295 + this.Correct.GetHashCode();
231     return hashCode;
232     }
233    
234     public static Boolean operator ==(QuizQuestionAnswer left, QuizQuestionAnswer right) {
235     return left.Equals(right);
236     }
237    
238     public static Boolean operator !=(QuizQuestionAnswer left, QuizQuestionAnswer right) {
239     return !(left == right);
240     }
241     }
242     }