public CMSResult SendAppNotification(TestPaperDeleteViewModel viewmodel, List <ListOfPlayerId> listOfParentPlayerId, List <ListOfPlayerId> listOfStudentPlayerId,
                                             int arrangeTestId)
        {
            var notificationList = new List <SendNotificationByPlayerId>();
            var cmsResult        = new CMSResult();

            listOfParentPlayerId  = listOfParentPlayerId.Where(s => !string.IsNullOrWhiteSpace(s.ParentPlayerId)).ToList();
            listOfStudentPlayerId = listOfStudentPlayerId.Where(s => !string.IsNullOrWhiteSpace(s.ParentPlayerId)).ToList();
            foreach (var playerid in listOfParentPlayerId)
            {
                var sendAppNotification = new SendNotificationByPlayerId
                {
                    Message    = "Test-" + viewmodel.Title + "$^$Date:" + viewmodel.Date.ToString("dd-MM-yyyy").Split(' ')[0] + "$^$Start Time:" + viewmodel.StartTime + "$^$Duration:" + viewmodel.TimeDuration + "$^$TestPaperId:" + viewmodel.TestPaperId + "$^$" + 1 + "," + arrangeTestId,
                    PlayerIds  = playerid.ParentPlayerId,
                    AppIds     = ConfigurationManager.AppSettings[Common.Constants.ParentAppId],
                    RestApiKey = ConfigurationManager.AppSettings[Common.Constants.ParentRestAppId]
                };
                notificationList.Add(sendAppNotification);
            }

            foreach (var playerid in listOfStudentPlayerId)
            {
                var sendAppNotification = new SendNotificationByPlayerId
                {
                    Message    = "Test-" + viewmodel.Title + "$^$Date:" + viewmodel.Date.ToString("dd-MM-yyyy").Split(' ')[0] + "$^$Start Time:" + viewmodel.StartTime + "$^$Duration:" + viewmodel.TimeDuration + "$^$TestPaperId:" + viewmodel.TestPaperId + "$^$" + 1 + "," + arrangeTestId,
                    PlayerIds  = playerid.ParentPlayerId,
                    AppIds     = ConfigurationManager.AppSettings[Common.Constants.StudentAppId],
                    RestApiKey = ConfigurationManager.AppSettings[Common.Constants.StudentRestAppId]
                };
                notificationList.Add(sendAppNotification);
            }

            if (notificationList.Count > 0)
            {
                var notification = notificationList.ToArray();
                HostingEnvironment.QueueBackgroundWorkItem(cancellationToken => _sendNotificationService.StartProcessingByPlayerId(notification, cancellationToken));
                cmsResult.Results.Add(new Result {
                    Message = "App Notification sent successfully.", IsSuccessful = true
                });
                return(cmsResult);
            }
            else
            {
                cmsResult.Results.Add(new Result {
                    Message = "No one is registered in parent app.", IsSuccessful = false
                });
                return(cmsResult);
            }
        }
 public ActionResult Delete(TestPaperDeleteViewModel viewModel)
 {
     if (ModelState.IsValid)
     {
         var result = _testPaperService.Delete(viewModel.TestPaperId);
         if (result.Success)
         {
             Success(result.Results.FirstOrDefault().Message);
             ModelState.Clear();
         }
         else
         {
             _logger.Warn(result.Results.FirstOrDefault().Message);
             Warning(result.Results.FirstOrDefault().Message, true);
         }
     }
     return(RedirectToAction("Index"));
 }
        public bool SendEmail(List <string> listOfEmail, List <string> listOfName, TestPaperDeleteViewModel viewModel, List <string> listOfStudentBatch)
        {
            string userRole = User.Identity.GetUserName() + "(" + "Master Admin" + ")";
            var    roles    = _aspNetRolesService.GetCurrentUserRole(User.Identity.GetUserId());

            if (roles == "BranchAdmin")
            {
                var branchAdmin = _branchAdminService.GetBranchAdminById(User.Identity.GetUserId());
                userRole = branchAdmin.BranchName;
            }

            var i = 0;

            MailModel[] emailAddress = new MailModel[listOfName.Count];
            foreach (var Name in listOfName)
            {
                string body = string.Empty;
                using (StreamReader reader = new StreamReader(Server.MapPath("~/MailDesign/NotificationMailDesign.html")))
                {
                    body = reader.ReadToEnd();
                }
                body = body.Replace("{BranchName}", userRole);
                body = body.Replace("{NotificationMessage}", "Test Type : " + viewModel.TestType + "<br/>Test title :" + viewModel.Title +
                                    "<br/>Class Name :" + viewModel.ClassName + "<br/>Batch Name :" + listOfStudentBatch[i] + "<br/>Subject Name :" + viewModel.SubjectName);
                body = body.Replace("{UserName}", Name);

                var emailMessage = new MailModel
                {
                    Body    = body,
                    Subject = "Online test arrange",
                    To      = listOfEmail[i]
                };

                emailAddress[i] = emailMessage;
                i++;
            }
            HostingEnvironment.QueueBackgroundWorkItem(cancellationToken => _emailService.StartProcessing(emailAddress, cancellationToken));
            return(true);
        }
        public JsonResult SendTestPaper(TestPaperDeleteViewModel viewModel)
        {
            var                   cmsResult             = new CMSResult();
            int                   studentCount          = 0;
            List <string>         listOfEmail           = new List <string>();
            List <string>         listOfName            = new List <string>();
            List <string>         listOfNumber          = new List <string>();
            List <ListOfPlayerId> listOfParentPlayerId  = new List <ListOfPlayerId>();
            List <ListOfPlayerId> listOfStudentPlayerId = new List <ListOfPlayerId>();
            List <string>         listOfStudentBatch    = new List <string>();

            if (ModelState.IsValid)
            {
                var studentList = _studentService.GetStudentByBranchClassBatchForTestPaper(viewModel.SelectedBranches, viewModel.ClassId.ToString(), viewModel.SelectedBatches);
                var batchIds    = viewModel.SelectedBatches.Split(',').Where(x => !string.IsNullOrEmpty(x)).Select(int.Parse).ToList();
                var batch       = _repository.LoadList <Batch>(x => batchIds.Contains(x.BatchId));

                foreach (var student in studentList)
                {
                    if (batchIds.Contains(student.BatchId))
                    {
                        listOfEmail.Add(student.Email);
                        listOfName.Add(student.Name);
                        listOfNumber.Add(student.StudentContact);
                        listOfStudentPlayerId.Add(new ListOfPlayerId
                        {
                            SId            = student.SId,
                            ParentPlayerId = student.studentAppPlayerId
                        });
                        //listOfParentPlayerId.Add(new ListOfPlayerId
                        //{
                        //    SId = student.SId,
                        //    ParentPlayerId = student.parentAppPlayerId
                        //});
                        listOfStudentBatch.Add(student.BatchName);
                    }
                }

                listOfParentPlayerId  = listOfParentPlayerId.Where(s => !string.IsNullOrWhiteSpace(s.ParentPlayerId)).ToList();
                listOfStudentPlayerId = listOfStudentPlayerId.Where(s => !string.IsNullOrWhiteSpace(s.ParentPlayerId)).ToList();

                var numberOfStudent = (viewModel.Email ? listOfEmail.Distinct().ToList().Count : 0) + (viewModel.SMS ? listOfNumber.Distinct().ToList().Count : 0) +
                                      listOfParentPlayerId.Count + listOfStudentPlayerId.Count;

                int arrangeTestId = 0;
                studentCount = listOfEmail.Distinct().ToList().Count;

                if (numberOfStudent > 0)
                {
                    try
                    {
                        var arrangeTest = new ArrengeTest
                        {
                            TestPaperId      = viewModel.TestPaperId,
                            SelectedBranches = viewModel.SelectedBranches != null ? viewModel.SelectedBranches : "",
                            SelectedBatches  = viewModel.SelectedBatches != null ? viewModel.SelectedBatches : "",
                            StudentCount     = studentCount,
                            Media            = viewModel.Media,
                            Date             = viewModel.Date,
                            StartTime        = Convert.ToDateTime(viewModel.StartTime),
                            TimeDuration     = viewModel.TimeDuration
                        };

                        var result = _testPaperService.SaveArrengeTest(arrangeTest);

                        if (result.Success)
                        {
                            arrangeTestId = arrangeTest.ArrengeTestId;
                            var resultTest = _testPaperService.UpdateTestStatus(new TestPaper
                            {
                                TestPaperId = viewModel.TestPaperId
                            });
                        }
                        else
                        {
                            cmsResult.Results.Add(new Result {
                                Message = result.Results[0].Message, IsSuccessful = false
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.Error(ex.Message + "catch SendTestPaper");
                    }
                }

                if (listOfName.Count > 0)
                {
                    if (viewModel.Email)
                    {
                        if (listOfEmail.Count > 0)
                        {
                            var result = SendEmail(listOfEmail.Distinct().ToList(), listOfName.Distinct().ToList(), viewModel, listOfStudentBatch);
                            studentCount = listOfEmail.Distinct().Count();
                            if (result == true)
                            {
                                cmsResult.Results.Add(new Result {
                                    Message = "Email sent successfully.", IsSuccessful = true
                                });
                            }
                            else
                            {
                                cmsResult.Results.Add(new Result {
                                    Message = "Something went wrong to send email.", IsSuccessful = false
                                });
                            }
                        }
                    }
                    if (viewModel.SMS)
                    {
                        if (listOfNumber.Count > 0)
                        {
                            var result = SendSMS(viewModel.Title, listOfNumber.Distinct().ToList());
                            studentCount = listOfNumber.Distinct().Count();
                            if (result.Success)
                            {
                                cmsResult.Results.Add(new Result {
                                    Message = result.Results[0].Message, IsSuccessful = true
                                });
                            }
                            else
                            {
                                cmsResult.Results.Add(new Result {
                                    Message = result.Results[0].Message, IsSuccessful = false
                                });
                            }
                        }
                    }
                    if (viewModel.AppNotification)
                    {
                        var response = SendAppNotification(viewModel, listOfParentPlayerId, listOfStudentPlayerId, arrangeTestId);
                        studentCount = listOfParentPlayerId.Distinct().Count();
                        studentCount = listOfStudentPlayerId.Distinct().Count();
                        if (response.Success)
                        {
                            cmsResult.Results.Add(new Result {
                                Message = response.Results[0].Message, IsSuccessful = true
                            });
                        }
                        else
                        {
                            cmsResult.Results.Add(new Result {
                                Message = response.Results[0].Message, IsSuccessful = false
                            });
                        }
                    }
                }
                else
                {
                    cmsResult.Results.Add(new Result {
                        Message = "No student available please select another batch.", IsSuccessful = false
                    });
                }
                int resultCount = cmsResult.Results.Where(x => x.IsSuccessful == true).Count();
            }
            else
            {
                var errors = ModelState.Values.SelectMany(x => x.Errors).ToArray();
                foreach (var item in errors)
                {
                    cmsResult.Results.Add(new Result {
                        Message = item.ErrorMessage, IsSuccessful = false
                    });
                }
            }

            return(Json(cmsResult, JsonRequestBehavior.AllowGet));
        }