public void CreateQuizJSON(TblQuiz objQuiz, int rows) { try { JavaScriptSerializer json_serializer = new JavaScriptSerializer(); json_serializer.MaxJsonLength = int.MaxValue; object[] objTblQue = (object[])json_serializer.DeserializeObject(objQuiz.hdnData); var QuizData = quizRepository.GetQuizByID(rows); var ques = QuizData[0].TblQuestions; List <int> queIds = new List <int>(); foreach (var que in ques) { queIds.Add(que.QuestionId); } int index = 0; foreach (Dictionary <string, object> item in objQuiz.questionObject) { item["QuestionId"] = Convert.ToString(queIds[index]); index++; } SCORMJSON jsonData = new SCORMJSON(); ConfigData config = new ConfigData(); config.scormType = 1.2; jsonData.config = config; QuizData quiz = new QuizData(); quiz.title = "Quiz"; quiz.nameLabel = "Name"; quiz.name = objQuiz.QuizName; quiz.descLabel = "Description"; quiz.description = objQuiz.QuizDescription; quiz.timeLabel = "Time remaining: "; quiz.duration = objQuiz.Duration == 0 ? null : objQuiz.Duration; quiz.passingScore = 80;//This need to be change quiz.minScore = 0; quiz.maxScore = 100; quiz.multipleAttempts = false; quiz.timeoutMessage = "Exam time ended."; jsonData.quiz = quiz; ReviewQuizData reviewQuiz = new ReviewQuizData(); reviewQuiz.title = "Review Quiz"; reviewQuiz.nameLabel = "Name:"; reviewQuiz.name = objQuiz.QuizName; reviewQuiz.descLabel = "Description:"; reviewQuiz.description = objQuiz.QuizDescription; reviewQuiz.scoreLabel = "Score:"; jsonData.reviewQuiz = reviewQuiz; List <QuestionsData> lstQuestions = new List <QuestionsData>(); foreach (Dictionary <string, object> item in objQuiz.questionObject) { QuestionsData que = new QuestionsData(); que.questionText = Convert.ToString(item["QuestionText"]); if (Convert.ToInt32(item["QuestionTypeId"]) == 1) { que.type = "mcq"; que.instructionText = "Options"; que.randomOptions = Convert.ToBoolean(item["isRandomOption"]); List <OptionsData> lstOptions = new List <OptionsData>(); var optionsCount = (object[])item["Options"]; int[] arrAnswers = new int[optionsCount.Length]; int counter = 0; foreach (Dictionary <string, object> itemNew1 in (object[])item["Options"]) { OptionsData ops = new OptionsData(); ops.text = Convert.ToString(itemNew1["OptionText"]); ops.feedback = Convert.ToString(itemNew1["OptionFeedback"]); arrAnswers[counter] = Convert.ToInt32(itemNew1["CorrectOption"]); lstOptions.Add(ops); counter++; } que.answer = arrAnswers; que.points = 1; que.options = lstOptions; } if (Convert.ToInt32(item["QuestionTypeId"]) == 2) { que.type = "mrq"; que.instructionText = "Options"; que.randomOptions = Convert.ToBoolean(item["isRandomOption"]); List <OptionsData> lstOptions = new List <OptionsData>(); var optionsCount = (object[])item["Options"]; int[] arrAnswers = new int[optionsCount.Length]; int counter = 0; foreach (Dictionary <string, object> itemNew1 in (object[])item["Options"]) { OptionsData ops = new OptionsData(); ops.text = Convert.ToString(itemNew1["OptionText"]); arrAnswers[counter] = Convert.ToInt32(itemNew1["CorrectOption"]); lstOptions.Add(ops); counter++; } que.answer = arrAnswers; que.points = 1; que.options = lstOptions; CorrectFeedbackData correctFeedback = new CorrectFeedbackData(); correctFeedback.text = Convert.ToString(item["CorrectFeedback"]); que.correctFeedback = correctFeedback; IncorrectFeedbackData inCorrectFeedback = new IncorrectFeedbackData(); inCorrectFeedback.text = Convert.ToString(item["InCorrectFeedback"]); que.incorrectFeedback = inCorrectFeedback; } if (Convert.ToInt32(item["QuestionTypeId"]) == 3) { que.type = "para"; que.placeholderText = "Write your answer here"; que.answer = null; que.points = 1; CorrectFeedbackData correctFeedback = new CorrectFeedbackData(); correctFeedback.text = Convert.ToString(item["CorrectFeedback"]); que.correctFeedback = correctFeedback; IncorrectFeedbackData inCorrectFeedback = new IncorrectFeedbackData(); inCorrectFeedback.text = Convert.ToString(item["InCorrectFeedback"]); que.incorrectFeedback = inCorrectFeedback; } if (Convert.ToInt32(item["QuestionTypeId"]) == 4) { que.type = "video"; string base64String = string.Empty; if (!string.IsNullOrEmpty(Convert.ToString(item["mediaFile"]))) { base64String = Convert.ToString(item["mediaFile"]); byte[] newBytes = Convert.FromBase64String(base64String); MemoryStream ms = new MemoryStream(newBytes, 0, newBytes.Length); ms.Write(newBytes, 0, newBytes.Length); string fileName = Convert.ToString(item["qTypeId"]); string DestinationPath = System.Configuration.ConfigurationManager.AppSettings["ScormDestinationPath"]; DestinationPath = DestinationPath + "\\" + objQuiz.QuizName + "\\data\\media"; FileStream file = new FileStream(DestinationPath + "\\" + fileName, FileMode.Create, FileAccess.Write); ms.WriteTo(file); file.Close(); ms.Close(); que.path = "data//media//" + fileName; } else { string fileName = Convert.ToString(item["qTypeId"]); var extn = fileName.Split('.'); int count = extn.Length; fileName = Convert.ToString(item["QuestionId"]) + "." + extn[count - 1]; string DestinationPath = System.Configuration.ConfigurationManager.AppSettings["ScormDestinationPath"]; DestinationPath = DestinationPath + "\\" + objQuiz.QuizName + "\\data\\media"; string path = System.Configuration.ConfigurationManager.AppSettings["QuizMediaPath"]; System.IO.File.Move(path + "\\" + fileName, DestinationPath + "\\" + fileName); que.path = "data//media//" + fileName; } } if (Convert.ToInt32(item["QuestionTypeId"]) == 5) { que.type = "audio"; string base64String = string.Empty; if (!string.IsNullOrEmpty(Convert.ToString(item["mediaFile"]))) { base64String = Convert.ToString(item["mediaFile"]); byte[] newBytes = Convert.FromBase64String(base64String); MemoryStream ms = new MemoryStream(newBytes, 0, newBytes.Length); ms.Write(newBytes, 0, newBytes.Length); string fileName = Convert.ToString(item["qTypeId"]); string DestinationPath = System.Configuration.ConfigurationManager.AppSettings["ScormDestinationPath"]; DestinationPath = DestinationPath + "\\" + objQuiz.QuizName + "\\data\\media"; FileStream file = new FileStream(DestinationPath + "\\" + fileName, FileMode.Create, FileAccess.Write); ms.WriteTo(file); file.Close(); ms.Close(); que.path = "data//media//" + fileName; } else { string fileName = Convert.ToString(item["qTypeId"]); var extn = fileName.Split('.'); int count = extn.Length; fileName = Convert.ToString(item["QuestionId"]) + "." + extn[count - 1]; string DestinationPath = System.Configuration.ConfigurationManager.AppSettings["ScormDestinationPath"]; DestinationPath = DestinationPath + "\\" + objQuiz.QuizName + "\\data\\media"; string path = System.Configuration.ConfigurationManager.AppSettings["QuizMediaPath"]; System.IO.File.Move(path + "\\" + fileName, DestinationPath + "\\" + fileName); que.path = "data//media//" + fileName; } } lstQuestions.Add(que); } jsonData.questions = lstQuestions; string jsonFilePath = System.Configuration.ConfigurationManager.AppSettings["ScormDestinationPath"]; jsonFilePath = jsonFilePath + "\\" + objQuiz.QuizName + "\\data\\json\\"; var json = new JavaScriptSerializer().Serialize(jsonData); System.IO.File.WriteAllText(jsonFilePath + "quizdata.json", json); } catch (Exception ex) { newException.AddException(ex); } }
public async Task <ActionResult> LaunchQuiz(int QuizId, string code) //public ActionResult LaunchQuiz(int QuizId, string code) { //newException.AddDummyException("Quiz ID First - " + Convert.ToString(QuizId)); bool isPlek = false; try { TblUser sessionUser = (TblUser)Session["UserSession"]; if (string.IsNullOrEmpty(code)) { QuizSession quizForSession = new QuizSession(); quizForSession.QuizId = Convert.ToInt32(QuizId); quizForSession.isUsed = false; lstQuiz.Add(quizForSession); Session["QuizId"] = lstQuiz; if (string.IsNullOrEmpty(Convert.ToString(Session["QuizId"]))) { newException.AddDummyException("Quiz Id in LaunchQuiz is Null"); } } List <TblQuiz> lstAllQuiz = new List <TblQuiz>(); if (sessionUser == null) { TenantRepository tr = new TenantRepository(); List <TblTenant> tenantDetails = new List <TblTenant>(); string host = Request.Url.Host; var tenantList1 = tr.VerifyTenantDomain(host); if (tenantList1.Count > 0) { if (tenantList1[0].TenantId == 6) { if (string.IsNullOrEmpty(code)) { return(Redirect("https://pumplace.plek.co/oidc/auth?client_id=1663f8ca-7b8a-42eb-ae3b-845e32be1baf&scope=openid&response_type=code&redirect_uri=https://quiz.rockettech.co.nz/")); } else { using (var client = new HttpClient()) { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Add("Access-Control-Allow-Origin", "*"); string tokenApi = "https://pumplace.plek.co/oidc/token"; var values = new Dictionary <string, string>(); values.Add("client_id", "1663f8ca-7b8a-42eb-ae3b-845e32be1baf"); values.Add("client_secret", "TZD4Jr837e6uymsJdyxH2jk8qxGQiK7gXDCmYqtC"); values.Add("grant_type", "authorization_code"); values.Add("code", code); values.Add("redirect_uri", "https://quiz.rockettech.co.nz/"); var content = new FormUrlEncodedContent(values); var responseTask = await client.PostAsync(tokenApi, content); var result = await responseTask.Content.ReadAsStringAsync(); var data = (JObject)JsonConvert.DeserializeObject(result); string token = data["access_token"].Value <string>();//Access Token Received //Now get User Details string userApi = "https://pumplace.plek.co/oidc/me"; client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token); HttpResponseMessage response = await client.GetAsync(userApi); if (response.IsSuccessStatusCode) { string jsondata = await response.Content.ReadAsStringAsync(); var userData = (JObject)JsonConvert.DeserializeObject(jsondata); string email = userData["email"].Value <string>(); string name = userData["username"].Value <string>(); var userId = ur.IsUserExist(email, Request.Url.Host); if (userId == 0) { try { //Create user TblUser newUser = new TblUser(); newUser.FirstName = name; newUser.EmailId = email; newUser.RoleId = 3; newUser.IsActive = true; newUser.CreatedBy = 1; CommonFunctions common = new CommonFunctions(); newUser.Password = common.GetEncodePassword("123456"); var tenantList = tr.VerifyTenantDomain(Request.Url.Host); newUser.TenantId = tenantList[0].TenantId; var newUserId = ur.AddUser(newUser); if (newUserId > 0) { //Assign Quiz to Newly Created User //newException.AddDummyException("111 - " + Convert.ToString(QuizId)); var objQuiz = quizRepository.GetQuizByID(Convert.ToInt32(QuizId)); if (objQuiz != null) { if (objQuiz[0] != null) { if (objQuiz[0].TenantId == 6) { var assign = quizRepository.AssignQuiz(Convert.ToInt32(QuizId), newUserId, null); } } } userId = newUserId; } } catch (Exception ex) { newException.AddException(ex); } } else { //check Quiz assigned or not var isAssigned = quizRepository.CheckQuizAssignedUser(Convert.ToInt32(QuizId), userId); if (!isAssigned) { newException.AddDummyException("222 - " + QuizId); var objQuiz = quizRepository.GetQuizByID(Convert.ToInt32(QuizId)); if (objQuiz != null) { if (objQuiz[0] != null) { if (objQuiz[0].TenantId == 6) { var assign = quizRepository.AssignQuiz(Convert.ToInt32(QuizId), userId, null); } } } } } List <TblUser> tblUser = ur.GetUserById(userId); Session["UserSession"] = tblUser[0]; lstAllQuiz = quizRepository.GetQuizForLaunch(Convert.ToInt32(QuizId), userId); JavaScriptSerializer json_serializer = new JavaScriptSerializer(); json_serializer.MaxJsonLength = int.MaxValue; lstAllQuiz[0].hdnLaunchData = json_serializer.Serialize(lstAllQuiz[0]); isPlek = true; } } } } if (tenantList1[0].TenantId == 9) { //newException.AddDummyException("In - 111 "); CommonFunctions common = new CommonFunctions(); var password = common.GetEncodePassword("123456"); TblUser tblUser = ur.IsValidUser("*****@*****.**", password, Request.Url.Host); lstAllQuiz = quizRepository.GetQuizForLaunch(Convert.ToInt32(QuizId), tblUser.UserId); JavaScriptSerializer json_serializer = new JavaScriptSerializer(); json_serializer.MaxJsonLength = int.MaxValue; lstAllQuiz[0].hdnLaunchData = json_serializer.Serialize(lstAllQuiz[0]); //newException.AddDummyException("In - 222 "); isPlek = true; } } } else { if (sessionUser.TenantId == 6) { isPlek = true; //check Quiz assigned or not var isAssigned = quizRepository.CheckQuizAssignedUser(Convert.ToInt32(QuizId), sessionUser.UserId); //newException.AddDummyException(Convert.ToString(isAssigned)); if (!isAssigned) { //newException.AddDummyException("333 - " + QuizId); var objQuiz = quizRepository.GetQuizByID(Convert.ToInt32(QuizId)); if (objQuiz != null) { if (objQuiz[0] != null) { if (objQuiz[0].TenantId == 6) { var assign = quizRepository.AssignQuiz(Convert.ToInt32(QuizId), sessionUser.UserId, null); lstAllQuiz = quizRepository.GetQuizForLaunch(Convert.ToInt32(QuizId), sessionUser.UserId); JavaScriptSerializer json_serializer = new JavaScriptSerializer(); json_serializer.MaxJsonLength = int.MaxValue; lstAllQuiz[0].hdnLaunchData = json_serializer.Serialize(lstAllQuiz[0]); } } } } else { lstAllQuiz = quizRepository.GetQuizForLaunch(Convert.ToInt32(QuizId), sessionUser.UserId); JavaScriptSerializer json_serializer = new JavaScriptSerializer(); json_serializer.MaxJsonLength = int.MaxValue; lstAllQuiz[0].hdnLaunchData = json_serializer.Serialize(lstAllQuiz[0]); } } if (sessionUser.TenantId == 9) { //newException.AddDummyException("In - 111 "); CommonFunctions common = new CommonFunctions(); var password = common.GetEncodePassword("123456"); TblUser tblUser = ur.IsValidUser("*****@*****.**", password, Request.Url.Host); lstAllQuiz = quizRepository.GetQuizForLaunch(Convert.ToInt32(QuizId), tblUser.UserId); JavaScriptSerializer json_serializer = new JavaScriptSerializer(); json_serializer.MaxJsonLength = int.MaxValue; lstAllQuiz[0].hdnLaunchData = json_serializer.Serialize(lstAllQuiz[0]); //newException.AddDummyException("In - 222 "); isPlek = true; } else { lstAllQuiz = quizRepository.GetQuizForLaunch(Convert.ToInt32(QuizId), sessionUser.UserId); JavaScriptSerializer json_serializer = new JavaScriptSerializer(); json_serializer.MaxJsonLength = int.MaxValue; lstAllQuiz[0].hdnLaunchData = json_serializer.Serialize(lstAllQuiz[0]); } } if (isPlek) { return(View("LaunchQuizForPlek", lstAllQuiz[0])); } else { return(View("LaunchQuizNew", lstAllQuiz[0])); } } catch (Exception ex) { newException.AddException(ex); if (isPlek) { return(View("LaunchQuizForPlek")); } else { return(View("LaunchQuizNew")); } } }