/Data Handling/QuestionManager.cs

http://github.com/jamie124/SRS-Server · C# · 323 lines · 269 code · 35 blank · 19 comment · 32 complexity · 3a869bd9026714aa663af12c9edb4fc5 MD5 · raw file

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Runtime.Serialization;
  7. using System.Net.Sockets;
  8. namespace srs_server
  9. {
  10. [Serializable()]
  11. public class question
  12. {
  13. private int mQuestionID;
  14. private string mQuestion;
  15. private string mQuestionString;
  16. private string mQuestionType;
  17. private string[] mPossibleAnswers;
  18. private string mAnswer;
  19. public int QuestionID
  20. {
  21. get { return mQuestionID; }
  22. set { mQuestionID = value; }
  23. }
  24. public string Question
  25. {
  26. get { return mQuestion; }
  27. set { mQuestion = value; }
  28. }
  29. public string QuestionString
  30. {
  31. get { return mQuestionString; }
  32. set { mQuestionString = value; }
  33. }
  34. public string QuestionType
  35. {
  36. get { return mQuestionType; }
  37. set { mQuestionType = value; }
  38. }
  39. public string[] PossibleAnswers
  40. {
  41. get { return mPossibleAnswers; }
  42. set { mPossibleAnswers = value; }
  43. }
  44. public string Answer
  45. {
  46. get { return mAnswer; }
  47. set { mAnswer = value; }
  48. }
  49. }
  50. [Serializable()]
  51. public class QuestionManager
  52. {
  53. UserManager mUserManger;
  54. Dictionary<int, question> mQuestionList;
  55. internal Dictionary<int, question> QuestionList
  56. {
  57. get { return mQuestionList; }
  58. set { mQuestionList = value; }
  59. }
  60. private int mLastQuestionAddedToList;
  61. public QuestionManager(UserManager prUserManager)
  62. {
  63. mQuestionList = new Dictionary<int, question>();
  64. mLastQuestionAddedToList = 0;
  65. mUserManger = prUserManager;
  66. }
  67. // Send question data to tutors
  68. public void SendQuestionListToTutors()
  69. {
  70. int i = 0;
  71. if (mUserManger.UsersOnline.Count > 0)
  72. {
  73. while (i <= mUserManger.MaxUserKey)
  74. {
  75. // Only send the list to a tutor
  76. if (mUserManger.UsersOnline.ContainsKey(i))
  77. {
  78. if (mUserManger.UsersOnline[i].UserRole == "Tutor")
  79. {
  80. mUserManger.UsersOnline[i].QuestionListRequested = true;
  81. }
  82. }
  83. i++;
  84. }
  85. }
  86. }
  87. // Send question data to a specific tutor
  88. public void SendQuestionListToTutor(TcpClient prClient)
  89. {
  90. int i = 0;
  91. if (mUserManger.UsersOnline.Count > 0)
  92. {
  93. while (i < mUserManger.MaxUserKey)
  94. {
  95. // Only send the list to a tutor
  96. if (mUserManger.UsersOnline.ContainsKey(i))
  97. {
  98. if (mUserManger.UsersOnline[i].UserRole == "Tutor" && mUserManger.UsersOnline[i].Client == prClient)
  99. {
  100. mUserManger.UsersOnline[i].QuestionListRequested = true;
  101. }
  102. }
  103. i++;
  104. }
  105. }
  106. }
  107. // Insert the question number into the string
  108. public string InsertQuestionNumber(string prQuestionString, int prQuestionNum)
  109. {
  110. return prQuestionString.Insert(0, prQuestionNum.ToString() + "|");
  111. }
  112. public bool AddNewQuestion(question prQuestion)
  113. {
  114. if (!mQuestionList.ContainsValue(prQuestion))
  115. {
  116. if(!mQuestionList.ContainsKey(prQuestion.QuestionID))
  117. mQuestionList.Add(prQuestion.QuestionID, prQuestion);
  118. else
  119. mQuestionList.Add(mQuestionList.Keys.Last() + 1, prQuestion);
  120. SendQuestionListToTutors();
  121. return true;
  122. }
  123. else
  124. {
  125. return false;
  126. }
  127. }
  128. // Remove the requested question
  129. public bool RemoveQuestion(string prQuestionName)
  130. {
  131. // Get the ID of the question
  132. int iQuestionID = -1;
  133. foreach (KeyValuePair<int, question> iQuestion in mQuestionList)
  134. {
  135. if (iQuestion.Value.Question == prQuestionName)
  136. {
  137. iQuestionID = iQuestion.Key;
  138. break;
  139. }
  140. }
  141. // Remove the question
  142. if (iQuestionID > -1)
  143. {
  144. mQuestionList.Remove(iQuestionID);
  145. return true;
  146. }
  147. else
  148. {
  149. return false;
  150. }
  151. }
  152. // Delete a question from the dictionary
  153. public bool DeleteQuestion(question prQuestion)
  154. {
  155. int iQuestionIndex = 0;
  156. if (mQuestionList.ContainsValue(prQuestion))
  157. {
  158. foreach (KeyValuePair<int, question> iQuestion in mQuestionList)
  159. {
  160. if (iQuestion.Value == prQuestion)
  161. {
  162. iQuestionIndex = iQuestion.Key;
  163. break;
  164. }
  165. }
  166. mQuestionList.Remove(iQuestionIndex);
  167. return true;
  168. }
  169. else
  170. {
  171. return false;
  172. }
  173. }
  174. // Gets the largest key in question dictionary
  175. private int GetLargestKeyQuestionDict()
  176. {
  177. int iCurrentLargestKey = 0;
  178. foreach (KeyValuePair<int, question> iQuestion in mQuestionList)
  179. {
  180. if (iQuestion.Key > iCurrentLargestKey)
  181. {
  182. iCurrentLargestKey = iQuestion.Key;
  183. }
  184. }
  185. return iCurrentLargestKey;
  186. }
  187. public int GetNewQuestionID()
  188. {
  189. int iID = 0;
  190. iID = mQuestionList[mQuestionList.Last().Key].QuestionID + 1;
  191. return iID;
  192. }
  193. public bool IsListEmpty()
  194. {
  195. if (mQuestionList.Count == 0)
  196. return true;
  197. else
  198. return false;
  199. }
  200. public string GetLastQuestionAdded()
  201. {
  202. int i = mLastQuestionAddedToList;
  203. string iQuestionType = "";
  204. if (mQuestionList.ContainsKey(i))
  205. {
  206. switch (mQuestionList[i].QuestionType)
  207. {
  208. case "MC":
  209. iQuestionType = "Multi-Choice";
  210. break;
  211. case "SA":
  212. iQuestionType = "Short Answer";
  213. break;
  214. case "TF":
  215. iQuestionType = "True/False";
  216. break;
  217. case "MA":
  218. iQuestionType = "Matching";
  219. break;
  220. }
  221. mLastQuestionAddedToList++;
  222. return mQuestionList[i].Question + " - " + iQuestionType;
  223. }
  224. mLastQuestionAddedToList++;
  225. return "";
  226. }
  227. // Check if a new question is available
  228. public bool IsNewQuestionAvailable()
  229. {
  230. if (GetLargestKeyQuestionDict() >= mLastQuestionAddedToList)
  231. return true;
  232. else
  233. return false;
  234. }
  235. // Get the requested question object
  236. public question GetQuestionByID(int prQuestionID)
  237. {
  238. question iQuestion = mQuestionList[prQuestionID];
  239. return iQuestion;
  240. }
  241. // Returns the question id for provided questionname
  242. public int GetQuestionIDByString(string prQuestionName)
  243. {
  244. foreach (KeyValuePair<int, question> iQuestion in mQuestionList)
  245. {
  246. if (iQuestion.Value.Question == prQuestionName)
  247. {
  248. return iQuestion.Value.QuestionID;
  249. }
  250. }
  251. // Question not found
  252. return -1;
  253. }
  254. // Checks if the question is already in use
  255. public bool IsQuestionNameInUse(string prQuestionName)
  256. {
  257. foreach (KeyValuePair<int, question> iQuestion in mQuestionList)
  258. {
  259. if (iQuestion.Value.Question == prQuestionName)
  260. return true;
  261. }
  262. return false;
  263. }
  264. // Get question by name
  265. public question GetQuestionByName(string prQuestionName)
  266. {
  267. foreach (KeyValuePair<int, question> iQuestion in mQuestionList)
  268. {
  269. if (iQuestion.Value.Question == prQuestionName)
  270. {
  271. return iQuestion.Value;
  272. }
  273. }
  274. // Question not found
  275. return null;
  276. }
  277. // Get the the requested question
  278. public string GetQuestionStringByID(int prQuestionID)
  279. {
  280. return mQuestionList[prQuestionID].Question;
  281. }
  282. //Serialisation function.
  283. public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
  284. {
  285. info.AddValue("UserList", mQuestionList);
  286. }
  287. }
  288. }