| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- using NEducation.Models;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Linq;
- using System.Net.Http;
- using System.ServiceModel;
- using System.Text;
- using System.Web;
- using System.Web.Mvc;
- using NEducation.Code;
- // FOR COMMON VIEW -> USABLE VIEWS
- namespace NEducation.Controllers
- {
- public class CommonController : Controller
- {
- // GET: Common
- public ActionResult Index()
- {
- String courseType = Session["courseType"] as String;
- switch (courseType)
- {
- case "1": // voca
- return Redirect("/Voca");
- case "2": // grammar
- return Redirect("/Grammar");
- case "3": // Listening
- return Redirect("/Listening");
- case "4": // Music
- return Redirect("/Music");
- }
- Session["courseType"] = "1";
- return Redirect("/Home");
- }
- public ActionResult Error()
- {
- return View();
- }
- //public JsonResult CheckAnswer(string questionId, string customerAnswer, string correctAnswer)
- public JsonResult CheckAnswer(string questionId, string customerAnswer)
- {
- //int count = int.Parse(Session["correctCount"].ToString());
- // get correct answer from server
- var correctAnswer = "";
- var testingType = Session["modelTestingType"] as string;
- if (testingType == "grammar")
- {
- GrammarModel model = Session["model"] as GrammarModel;
- for (int i = 0; i < model.questions.Count; i++)
- {
- if (questionId == model.questions[i].id)
- {
- correctAnswer = model.questions[i].answerTrue;
- }
- }
- }
- else if (testingType == "vocab")
- {
- VocabularyModel model = Session["model"] as VocabularyModel;
- for (int i = 0; i < model.questions.Count; i++)
- {
- if (questionId == model.questions[i].id)
- {
- correctAnswer = model.questions[i].answerTrue;
- }
- }
- }
- else if (testingType == "listen")
- {
- ListeningModel model = Session["model"] as ListeningModel;
- for (int i = 0; i < model.questions.Count; i++)
- {
- if (questionId == model.questions[i].id)
- {
- correctAnswer = model.questions[i].answerTrue;
- }
- }
- }
- var listAnswers = customerAnswer.Split(',');
- var listCorrect = correctAnswer.Split(',');
- // compares 2 lists
- var check = 1;
- if (listAnswers.Length == listCorrect.Length)
- {
- for (int i = 0; i < listAnswers.Length; i++)
- {
- if (listCorrect.Contains(listAnswers[i]))
- {
- check *= 1;
- }
- else
- {
- check *= 0;
- }
- }
- }
- if (check == 1)
- {
- //count = count + 1;
- //Session["correctCount"] = count;
- return Json(new
- {
- correct = 1,
- result = correctAnswer
- });
- }
- else
- {
- return Json(new
- {
- correct = 0,
- result = correctAnswer
- });
- }
- }
- public ActionResult EditTestResult(string dataAnswer)
- {
- var result = JsonConvert.DeserializeObject<DataResult>(dataAnswer);
- int countWrong = 0;
- int countCorrect = 0;
- int time = 0;
- foreach (AnswerGuess answer in result.answerGuess)
- {
- time += answer.time;
- if (answer.isRight == 1)
- {
- countCorrect++;
- }
- else
- {
- countWrong++;
- }
- }
- //ViewBag.courseName = Session["courseName"] as String;
- //ViewBag.lessonName = Session["lessonName"] as String;
- //ViewBag.time = time;
- //ViewBag.correct = countCorrect;
- //ViewBag.wrong = countWrong;
- //ViewBag.percent = Math.Round((decimal)countCorrect * 100 / result.total);
- System.Diagnostics.Debug.WriteLine("dataAnswer: " + dataAnswer);
- TestingResult testingResult = new TestingResult();
- testingResult.courseName = Session["courseName"] as String;
- testingResult.lessonName = Session["lessonName"] as String;
- testingResult.timeElapsed = time;
- testingResult.countCorrect = countCorrect;
- testingResult.countWrong = countWrong;
- testingResult.countTotal = result.total;
- testingResult.percent = (int)Math.Round((decimal)countCorrect * 100 / result.total);
- Session["testingResult"] = testingResult;
- System.Diagnostics.Debug.WriteLine("result: " + result);
- Code.QuestionResult questionResult = new Code.QuestionResult();
- questionResult.LESSON_ID = Session["lessonId"] as String;
- questionResult.users = Session["msisdn"] as String;
- questionResult.msisdn = Session["msisdn"] as String;
- questionResult.ANSWER_LIST = new List<Code.AnswerResult>();
- for (int i = 0; i < result.answerGuess.Length; i++)
- {
- Code.AnswerResult answer = new Code.AnswerResult();
- answer.QUESTION_ID = result.answerGuess[i].questionId;
- answer.RESULT = result.answerGuess[i].isRight.ToString();
- answer.TIME = time.ToString();
- answer.ANSWER = result.answerGuess[i].pos == "1" ? "A" :
- result.answerGuess[i].pos == "2" ? "B" :
- result.answerGuess[i].pos == "3" ? "C" : "D";
- questionResult.ANSWER_LIST.Add(answer);
- System.Diagnostics.Debug.WriteLine("answer: " + answer);
- }
- // update test result for ranking
- //String wsUser = ConfigurationManager.AppSettings["wsUser"];
- //String wsPassword = ConfigurationManager.AppSettings["wsPassword"];
- //String msisdn = Session["msisdn"] as String;
- //NEduService.WsNEduClient wsClient = new NEduService.WsNEduClient();
- //UtilsController.SetWsClient(ref wsClient, Session.SessionID);
- //wsClient.wsUpdateTestResult(wsUser, wsPassword, msisdn, testingResult.percent + "", lessonId);
- //UserRequest userRequest = new UserRequest();
- //if (Session["msisdn"] == null) userRequest.users = null;
- //else userRequest.users = Session["msisdn"] as string;
- //userRequest.lessonId = lessonId;
- String rs = UtilsController.SendPost(questionResult, Session.SessionID, UtilsController.WsType.GetAnsweerOfQuestion);
- System.Diagnostics.Debug.WriteLine("res: " + rs);
- //ListLessonData lessons = new ListLessonData(rs);
- //Session["questions"] = lessons;
- //model.questions = lessons.questions;
- //System.Diagnostics.Debug.WriteLine(model.questions);
- return Content("1");
- }
- //public JsonResult
- // get all courses of this catogory type
- public ActionResult Course(String courseType, String searchname)
- {
- if (CheckAuthToken())
- {
- CourseModel model = new CourseModel();
- UserRequest userRequest = new UserRequest();
- if (Session["msisdn"] == null) userRequest.users = null;
- else userRequest.users = Session["msisdn"] as string;
- userRequest.categoryCode = UtilsController.GetCategoryCode(courseType);
- System.Diagnostics.Debug.WriteLine("Choose: " + UtilsController.GetCategoryCode(courseType));
- Session["courseType"] = courseType;
- // get all lessions => course => category of each tenor
- // tenor is a list of vocabulary + listening + writing
- String rs = UtilsController.SendPost(userRequest, Session.SessionID, UtilsController.WsType.GetLessonOfCategory);
- UserActionResult res = new UserActionResult(rs);
- if (res.status == "0")
- {
- Tenor tenor = new Tenor(rs);
- Session["tenor"] = tenor;
- model.courseType = courseType;
- // a list category always has one category
- // pass all category info and all courses to CourseView
- model.category = tenor.listCategory[0];
- //System.Diagnostics.Debug.WriteLine(tenor.listCategory[0].listCourse[0].listLesson);
- return View("CourseView", model);
- }
- else
- {
- return View("Error");
- }
- }
- else
- {
- Session.Clear();
- return Redirect("/Home/Index");
- }
- }
- // get all lession of this courses
- public ActionResult Lesson(String courseId, String courseType)
- {
- if (CheckAuthToken())
- {
- System.Diagnostics.Debug.WriteLine("common courseId " + courseId + " courseType: " + courseType
- + " category " + UtilsController.GetCategoryCode(courseType));
- // save courseId
- if (courseId != null)
- Session["courseId"] = courseId;
- // get all lessions from course
- LessonModel model = new LessonModel();
- model.courseType = courseType != null ? courseType : Session["courseType"] as String;
- // convert to tenor that contains all categories
- Tenor tenor;
- // courseType != null => request from /Individual
- // courseType == null => request from /Common/Course
- if (courseType != null)
- {
- UserRequest userRequest = new UserRequest();
- if (Session["msisdn"] == null) userRequest.users = null;
- else userRequest.users = Session["msisdn"] as string;
- userRequest.categoryCode = UtilsController.GetCategoryCode(courseType);
- // get all lessions => course => category of each tenor
- // tenor is a list of vocabulary + listening + writing
- String rs = UtilsController.SendPost(userRequest, Session.SessionID, UtilsController.WsType.GetLessonOfCategory);
- UserActionResult res = new UserActionResult(rs);
- if (res.status == "0")
- {
- tenor = new Tenor(rs);
- Session["tenor"] = tenor;
- }
- else
- {
- return Redirect("/Common/Error");
- }
- }
- else
- {
- tenor = Session["tenor"] as Tenor;
- }
- // get category in a list
- Category category = tenor.listCategory[0];
- // get a course by course id from category
- for (int i = 0; i < category.listCourse.Count; i++)
- {
- //System.Diagnostics.Debug.WriteLine(i + " " + category.listCourse[i]);
- if (category.listCourse[i].id == Session["courseId"] as string)
- {
- model.course = category.listCourse[i];
- System.Diagnostics.Debug.WriteLine("common course: " + model.course);
- }
- }
- if (model.course != null)
- return View("LessonView", model);
- else
- return Redirect("/Common/Error");
- }
- else
- {
- Session.Clear();
- return Redirect("/Home/Index");
- }
- }
- private bool CheckAuthToken()
- {
- if (Session["AuthToken"] != null && Request.Cookies["AuthToken"] != null)
- {
- if (!Session["AuthToken"].ToString().Equals(Request.Cookies["AuthToken"].Value))
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- else
- {
- return false;
- }
- }
- }
- }
|