public ActionResult AssignQuizToUsers(QuizAssignViewModel quizAssignViewModel)
        {
            try
            {
                var index = quizRepository.DeleteAssignedUser(quizAssignViewModel.quiz.QuizId);

                foreach (var userId in quizAssignViewModel.userIds)
                {
                    var result = quizRepository.AssignQuiz(quizAssignViewModel.quiz.QuizId, userId, quizAssignViewModel.DueDate);

                    var       emailBody    = quizAssignViewModel.quiz.QuizName + " - assigned to you. Please go through it. <br /> Your Due Date is - " + quizAssignViewModel.DueDate;
                    var       emailSubject = "Quiz Assigned - " + quizAssignViewModel.quiz.QuizName;
                    tblEmails objEmail     = new tblEmails();
                    var       objUser      = userRepository.GetUserById(userId);
                    objEmail.EmailTo      = objUser[0].EmailId;
                    objEmail.EmailSubject = emailSubject;
                    objEmail.EmailBody    = emailBody;
                    var emailResult = userRepository.InsertEmail(objEmail);
                }
            }
            catch (Exception ex)
            {
                newException.AddException(ex);
            }

            return(RedirectToAction("Index"));
        }
Exemplo n.º 2
0
        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"));
                }
            }
        }