CommonController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. using NEducation.Models;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Configuration;
  6. using System.Linq;
  7. using System.Net.Http;
  8. using System.ServiceModel;
  9. using System.Text;
  10. using System.Web;
  11. using System.Web.Mvc;
  12. using NEducation.Code;
  13. // FOR COMMON VIEW -> USABLE VIEWS
  14. namespace NEducation.Controllers
  15. {
  16. public class CommonController : Controller
  17. {
  18. // GET: Common
  19. public ActionResult Index()
  20. {
  21. String courseType = Session["courseType"] as String;
  22. switch (courseType)
  23. {
  24. case "1": // voca
  25. return Redirect("/Voca");
  26. case "2": // grammar
  27. return Redirect("/Grammar");
  28. case "3": // Listening
  29. return Redirect("/Listening");
  30. case "4": // Music
  31. return Redirect("/Music");
  32. }
  33. Session["courseType"] = "1";
  34. return Redirect("/Home");
  35. }
  36. public ActionResult Error()
  37. {
  38. return View();
  39. }
  40. //public JsonResult CheckAnswer(string questionId, string customerAnswer, string correctAnswer)
  41. public JsonResult CheckAnswer(string questionId, string customerAnswer)
  42. {
  43. //int count = int.Parse(Session["correctCount"].ToString());
  44. // get correct answer from server
  45. var correctAnswer = "";
  46. var testingType = Session["modelTestingType"] as string;
  47. if (testingType == "grammar")
  48. {
  49. GrammarModel model = Session["model"] as GrammarModel;
  50. for (int i = 0; i < model.questions.Count; i++)
  51. {
  52. if (questionId == model.questions[i].id)
  53. {
  54. correctAnswer = model.questions[i].answerTrue;
  55. }
  56. }
  57. }
  58. else if (testingType == "vocab")
  59. {
  60. VocabularyModel model = Session["model"] as VocabularyModel;
  61. for (int i = 0; i < model.questions.Count; i++)
  62. {
  63. if (questionId == model.questions[i].id)
  64. {
  65. correctAnswer = model.questions[i].answerTrue;
  66. }
  67. }
  68. }
  69. else if (testingType == "listen")
  70. {
  71. ListeningModel model = Session["model"] as ListeningModel;
  72. for (int i = 0; i < model.questions.Count; i++)
  73. {
  74. if (questionId == model.questions[i].id)
  75. {
  76. correctAnswer = model.questions[i].answerTrue;
  77. }
  78. }
  79. }
  80. var listAnswers = customerAnswer.Split(',');
  81. var listCorrect = correctAnswer.Split(',');
  82. // compares 2 lists
  83. var check = 1;
  84. if (listAnswers.Length == listCorrect.Length)
  85. {
  86. for (int i = 0; i < listAnswers.Length; i++)
  87. {
  88. if (listCorrect.Contains(listAnswers[i]))
  89. {
  90. check *= 1;
  91. }
  92. else
  93. {
  94. check *= 0;
  95. }
  96. }
  97. }
  98. if (check == 1)
  99. {
  100. //count = count + 1;
  101. //Session["correctCount"] = count;
  102. return Json(new
  103. {
  104. correct = 1,
  105. result = correctAnswer
  106. });
  107. }
  108. else
  109. {
  110. return Json(new
  111. {
  112. correct = 0,
  113. result = correctAnswer
  114. });
  115. }
  116. }
  117. public ActionResult EditTestResult(string dataAnswer)
  118. {
  119. var result = JsonConvert.DeserializeObject<DataResult>(dataAnswer);
  120. int countWrong = 0;
  121. int countCorrect = 0;
  122. int time = 0;
  123. foreach (AnswerGuess answer in result.answerGuess)
  124. {
  125. time += answer.time;
  126. if (answer.isRight == 1)
  127. {
  128. countCorrect++;
  129. }
  130. else
  131. {
  132. countWrong++;
  133. }
  134. }
  135. //ViewBag.courseName = Session["courseName"] as String;
  136. //ViewBag.lessonName = Session["lessonName"] as String;
  137. //ViewBag.time = time;
  138. //ViewBag.correct = countCorrect;
  139. //ViewBag.wrong = countWrong;
  140. //ViewBag.percent = Math.Round((decimal)countCorrect * 100 / result.total);
  141. System.Diagnostics.Debug.WriteLine("dataAnswer: " + dataAnswer);
  142. TestingResult testingResult = new TestingResult();
  143. testingResult.courseName = Session["courseName"] as String;
  144. testingResult.lessonName = Session["lessonName"] as String;
  145. testingResult.timeElapsed = time;
  146. testingResult.countCorrect = countCorrect;
  147. testingResult.countWrong = countWrong;
  148. testingResult.countTotal = result.total;
  149. testingResult.percent = (int)Math.Round((decimal)countCorrect * 100 / result.total);
  150. Session["testingResult"] = testingResult;
  151. System.Diagnostics.Debug.WriteLine("result: " + result);
  152. Code.QuestionResult questionResult = new Code.QuestionResult();
  153. questionResult.LESSON_ID = Session["lessonId"] as String;
  154. questionResult.users = Session["msisdn"] as String;
  155. questionResult.msisdn = Session["msisdn"] as String;
  156. questionResult.ANSWER_LIST = new List<Code.AnswerResult>();
  157. for (int i = 0; i < result.answerGuess.Length; i++)
  158. {
  159. Code.AnswerResult answer = new Code.AnswerResult();
  160. answer.QUESTION_ID = result.answerGuess[i].questionId;
  161. answer.RESULT = result.answerGuess[i].isRight.ToString();
  162. answer.TIME = time.ToString();
  163. answer.ANSWER = result.answerGuess[i].pos == "1" ? "A" :
  164. result.answerGuess[i].pos == "2" ? "B" :
  165. result.answerGuess[i].pos == "3" ? "C" : "D";
  166. questionResult.ANSWER_LIST.Add(answer);
  167. System.Diagnostics.Debug.WriteLine("answer: " + answer);
  168. }
  169. // update test result for ranking
  170. //String wsUser = ConfigurationManager.AppSettings["wsUser"];
  171. //String wsPassword = ConfigurationManager.AppSettings["wsPassword"];
  172. //String msisdn = Session["msisdn"] as String;
  173. //NEduService.WsNEduClient wsClient = new NEduService.WsNEduClient();
  174. //UtilsController.SetWsClient(ref wsClient, Session.SessionID);
  175. //wsClient.wsUpdateTestResult(wsUser, wsPassword, msisdn, testingResult.percent + "", lessonId);
  176. //UserRequest userRequest = new UserRequest();
  177. //if (Session["msisdn"] == null) userRequest.users = null;
  178. //else userRequest.users = Session["msisdn"] as string;
  179. //userRequest.lessonId = lessonId;
  180. String rs = UtilsController.SendPost(questionResult, Session.SessionID, UtilsController.WsType.GetAnsweerOfQuestion);
  181. System.Diagnostics.Debug.WriteLine("res: " + rs);
  182. //ListLessonData lessons = new ListLessonData(rs);
  183. //Session["questions"] = lessons;
  184. //model.questions = lessons.questions;
  185. //System.Diagnostics.Debug.WriteLine(model.questions);
  186. return Content("1");
  187. }
  188. //public JsonResult
  189. // get all courses of this catogory type
  190. public ActionResult Course(String courseType, String searchname)
  191. {
  192. if (CheckAuthToken())
  193. {
  194. CourseModel model = new CourseModel();
  195. UserRequest userRequest = new UserRequest();
  196. if (Session["msisdn"] == null) userRequest.users = null;
  197. else userRequest.users = Session["msisdn"] as string;
  198. userRequest.categoryCode = UtilsController.GetCategoryCode(courseType);
  199. System.Diagnostics.Debug.WriteLine("Choose: " + UtilsController.GetCategoryCode(courseType));
  200. Session["courseType"] = courseType;
  201. // get all lessions => course => category of each tenor
  202. // tenor is a list of vocabulary + listening + writing
  203. String rs = UtilsController.SendPost(userRequest, Session.SessionID, UtilsController.WsType.GetLessonOfCategory);
  204. UserActionResult res = new UserActionResult(rs);
  205. if (res.status == "0")
  206. {
  207. Tenor tenor = new Tenor(rs);
  208. Session["tenor"] = tenor;
  209. model.courseType = courseType;
  210. // a list category always has one category
  211. // pass all category info and all courses to CourseView
  212. model.category = tenor.listCategory[0];
  213. //System.Diagnostics.Debug.WriteLine(tenor.listCategory[0].listCourse[0].listLesson);
  214. return View("CourseView", model);
  215. }
  216. else
  217. {
  218. return View("Error");
  219. }
  220. }
  221. else
  222. {
  223. Session.Clear();
  224. return Redirect("/Home/Index");
  225. }
  226. }
  227. // get all lession of this courses
  228. public ActionResult Lesson(String courseId, String courseType)
  229. {
  230. if (CheckAuthToken())
  231. {
  232. System.Diagnostics.Debug.WriteLine("common courseId " + courseId + " courseType: " + courseType
  233. + " category " + UtilsController.GetCategoryCode(courseType));
  234. // save courseId
  235. if (courseId != null)
  236. Session["courseId"] = courseId;
  237. // get all lessions from course
  238. LessonModel model = new LessonModel();
  239. model.courseType = courseType != null ? courseType : Session["courseType"] as String;
  240. // convert to tenor that contains all categories
  241. Tenor tenor;
  242. // courseType != null => request from /Individual
  243. // courseType == null => request from /Common/Course
  244. if (courseType != null)
  245. {
  246. UserRequest userRequest = new UserRequest();
  247. if (Session["msisdn"] == null) userRequest.users = null;
  248. else userRequest.users = Session["msisdn"] as string;
  249. userRequest.categoryCode = UtilsController.GetCategoryCode(courseType);
  250. // get all lessions => course => category of each tenor
  251. // tenor is a list of vocabulary + listening + writing
  252. String rs = UtilsController.SendPost(userRequest, Session.SessionID, UtilsController.WsType.GetLessonOfCategory);
  253. UserActionResult res = new UserActionResult(rs);
  254. if (res.status == "0")
  255. {
  256. tenor = new Tenor(rs);
  257. Session["tenor"] = tenor;
  258. }
  259. else
  260. {
  261. return Redirect("/Common/Error");
  262. }
  263. }
  264. else
  265. {
  266. tenor = Session["tenor"] as Tenor;
  267. }
  268. // get category in a list
  269. Category category = tenor.listCategory[0];
  270. // get a course by course id from category
  271. for (int i = 0; i < category.listCourse.Count; i++)
  272. {
  273. //System.Diagnostics.Debug.WriteLine(i + " " + category.listCourse[i]);
  274. if (category.listCourse[i].id == Session["courseId"] as string)
  275. {
  276. model.course = category.listCourse[i];
  277. System.Diagnostics.Debug.WriteLine("common course: " + model.course);
  278. }
  279. }
  280. if (model.course != null)
  281. return View("LessonView", model);
  282. else
  283. return Redirect("/Common/Error");
  284. }
  285. else
  286. {
  287. Session.Clear();
  288. return Redirect("/Home/Index");
  289. }
  290. }
  291. private bool CheckAuthToken()
  292. {
  293. if (Session["AuthToken"] != null && Request.Cookies["AuthToken"] != null)
  294. {
  295. if (!Session["AuthToken"].ToString().Equals(Request.Cookies["AuthToken"].Value))
  296. {
  297. return false;
  298. }
  299. else
  300. {
  301. return true;
  302. }
  303. }
  304. else
  305. {
  306. return false;
  307. }
  308. }
  309. }
  310. }