public List <Service> GetPublicServices([FromUri] int?UserWorkId)
        {
            //Hours to public Serive
            String   _hoursToPublicService = ParameterRepository.findByCode("hours_To_Public_Service");
            int      hoursToPublicService  = (int)Int32.Parse(_hoursToPublicService);
            DateTime dateToCompare         = DateTime.Now.AddHours(-1 * hoursToPublicService);

            //Service Provider Speed
            String _PublicServiceUserSpeed = ParameterRepository.findByCode("Public_Service_User_Speed");
            double PublicServiceUserSpeed  = Double.Parse(_PublicServiceUserSpeed);
            //Avg Service In a day
            String _PublicServiceUserAvg = ParameterRepository.findByCode("Public_Service_User_Avg_Services");
            double PublicServiceUserAvg  = Double.Parse(_PublicServiceUserAvg);

            List <Service> services = db.Services.Where(a => a.Status.Equals("Active") &&
                                                        a.ServiceProviderNewDate.CompareTo(dateToCompare) <= 0)
                                      .Include("Comments")
                                      .Include("UserWork")
                                      .Include("ServiceProvider")
                                      .Include("Creator")
                                      .OrderByDescending(a => a.CreationDate)
                                      .ToList();

            if (UserWorkId != null)
            {
                services = services.Where(a => a.UserWorkId.Equals(UserWorkId)).ToList();
            }
            return(services);
        }
        public ActionResult Register(int?TestId, string employee_code)
        {
            // register the user for the Test
            Employees employee = db
                                 .Employees
                                 .Include("Registrations")
                                 .FirstOrDefault(emp => emp.EId.Equals(employee_code, StringComparison.InvariantCultureIgnoreCase));

            Test test = db.Tests.FirstOrDefault(t => t.Id == TestId);

            List <Registration> registrations = db
                                                .Registrations
                                                .Where(reg => reg.TestId == TestId && reg.EmployeeId == employee.Id).ToList();

            // If the employee has already registered for the Test --->
            var _maxNumberOfAllowedExams = ParameterRepository.findByCode("max_number_of_allowed_exams");

            int maxNumberOfAllowedExams = Int32.Parse(_maxNumberOfAllowedExams);

            if (registrations.Count() >= maxNumberOfAllowedExams)
            {
                Session["TOKEN"]       = registrations[0].Token;
                TempData["errMessage"] = "You can only take the test twice, If you believe that there is something wrong please contact your direct manager";
                return(RedirectToAction("InstructionPage", new { TestId, employee_code }));
                //return RedirectToAction("FinalResult", new { TestId, token = registrations[0].Token });
            }

            // Create New Registration
            else
            {
                Registration newRegistration = new Registration()
                {
                    EmployeeId       = employee.Id,
                    TestId           = TestId.GetValueOrDefault(),
                    RegistrationDate = DateTime.Now,
                    ExpiresDate      = DateTime.Now.AddMinutes(test.DurationInMinutes),
                    Token            = Guid.NewGuid()
                };

                employee.Registrations.Add(newRegistration);
                db.Registrations.Add(newRegistration);

                db.SaveChanges();

                Session["TOKEN"]      = newRegistration.Token;
                Session["ExpireDate"] = newRegistration.ExpiresDate;
                Session.Timeout       = test.DurationInMinutes;
            }

            return(RedirectToAction("EvalPage", new { @token = Session["TOKEN"] }));
        }
        public List <ServiceViewModel> GetPublicServicesWithoutFilter()
        {
            String   _hoursToPublicService = ParameterRepository.findByCode("hours_To_Public_Service");
            int      hoursToPublicService  = _hoursToPublicService == null? 24: (int)Int32.Parse(_hoursToPublicService);
            DateTime dateToCompare         = DateTime.Now.AddHours(-1 * hoursToPublicService);
            //Service Provider Speed
            String _PublicServiceUserSpeed = ParameterRepository.findByCode("Public_Service_User_Speed");
            double PublicServiceUserSpeed  = _PublicServiceUserSpeed == null?1.0:Double.Parse(_PublicServiceUserSpeed);
            //Avg Service In a day
            String _PublicServiceUserAvg = ParameterRepository.findByCode("Public_Service_User_Avg_Services");
            double PublicServiceUserAvg  = _PublicServiceUserAvg == null?1.0: Double.Parse(_PublicServiceUserAvg);

            String          userId      = core.getCurrentUser().Id;
            ApplicationUser user        = db.Users.Where(a => a.Id.Equals(userId)).Include("userWorkBinding").FirstOrDefault();
            List <int>      userWorkIds = new List <int>();
            List <Service>  services    = db.Services.Where(a => a.Status.Equals("Active") &&
                                                            a.ServiceProviderNewDate.CompareTo(dateToCompare) <= 0)
                                          .Include("Comments")
                                          .Include("UserWork")
                                          .Include("ServiceProvider")
                                          .Include("Creator")
                                          .OrderByDescending(a => a.CreationDate)
                                          .ToList();

            if (user.userWorkBinding.Count > 0)
            {
                foreach (var item in user.userWorkBinding)
                {
                    userWorkIds.Add(item.UserWorkId);
                }
                services = services.Where(a => userWorkIds.Contains(a.UserWorkId)).ToList();
            }
            else
            {
                services = new List <Service>();
            }

            List <ServiceViewModel> result = new List <ServiceViewModel>();

            foreach (var item in services)
            {
                result.Add(getMapping(item));
            }


            return(result.Where(a => a.ServiceProviderAvgServices < PublicServiceUserAvg && a.ServiceProviderSpeed < PublicServiceUserSpeed).ToList());
        }
        public Service ReceiveService([FromBody] Service temp)
        {
            if (temp.id.Equals(null))
            {
                core.throwExcetpion("Id is null");
            }

            if (temp.ServiceProviderId.Equals(null))
            {
                core.throwExcetpion("Service Provider id can't be null");
            }


            Service service = db.Services.Where(a => a.id.Equals(temp.id))
                              .FirstOrDefault();

            String   _hoursToPublicService = ParameterRepository.findByCode("hours_To_Public_Service");
            int      hoursToPublicService  = (int)Int32.Parse(_hoursToPublicService);
            DateTime dateToCompare         = DateTime.Now.AddHours(-1 * hoursToPublicService);

            if (service.ServiceProviderNewDate.CompareTo(dateToCompare) > 0)
            {
                core.throwExcetpion("لقد تم استلام الخدمة من قبل مستخدم آخر");
            }

            DreamHistory history = new DreamHistory();

            history.NewInterpreterId     = temp.ServiceProviderId;
            history.OldInterpreterId     = service.ServiceProviderId;
            history.ServiceId            = service.id;
            history.CreatorId            = core.getCurrentUser().Id;
            history.CreationDate         = DateTime.Now;
            history.LastModificationDate = DateTime.Now;
            db.DreamHistories.Add(history);
            service.ServiceProviderId      = temp.ServiceProviderId;
            service.ServiceProviderNewDate = DateTime.Now;



            SaveService(service);
            return(service);
        }
        public ActionResult FinalResult(int TestId, Guid token)
        {
            fillUserData();
            Registration registration = db.Registrations.Include("Test").Where(r => r.Token.Equals(token)).FirstOrDefault();

            Test test = db.Tests.Where(r => r.Id.Equals(TestId)).FirstOrDefault();

            if (registration == null)
            {
                TempData["errMessage"] = "Invalid Token. Please Register again for the Test";
                return(RedirectToAction("Index"));
            }
            double totalMarks = db
                                .TestXQuestions
                                .Include("Question")
                                .Where(txq => txq.TestId == registration.TestId)
                                .Sum(x => x.Question.points);

            // Total Mark for the Test
            ViewBag.TotalMark = db
                                .TestXQuestions
                                .Include("Question")
                                .Where(txq => txq.TestId == registration.TestId).Take(test.QuestionLimit)
                                .Sum(x => x.Question.points);

            var textXPapers = db
                              .TestXPapers
                              .Include("Choice")
                              .Include("TestXQuestion")
                              .Where(x => x.RegistrationId == registration.Id)
                              .ToList();

            double markScored = db.TestXPapers
                                .Include("Choice")
                                .Include("TestXQuestion")
                                .Where(x => x.RegistrationId == registration.Id)
                                .Sum(x => x.MarkScored);

            Session["TOKEN"] = null;
            //var isOnProcuctionParameter =db.SystemParameters.Where(x => x.Code.Equals("exam_result_email")).Select(y => y.Value).FirstOrDefault();
            //var emailParameter = db.SystemParameters.Where(x => x.Code.Equals("exam_result_email")).Select(y => y.Value).FirstOrDefault();

            var   _isOnProcuctionParameter = ParameterRepository.findByCode("is_on_production");
            Int32 isOnProcuctionParameter  = Int32.Parse(_isOnProcuctionParameter);

            if (isOnProcuctionParameter == 1)
            {
                var emailParameter = ParameterRepository.findByCode("exam_result_email");

                List <string> receivers    = EmailHelper.getReceivers(emailParameter);
                String        employeeName = db.Employees.Find(registration.EmployeeId).Name;
                String        testName     = db.Tests.Find(registration.TestId).Name;
                String        subject      = testName
                                             + " Test Result For The Employee " + employeeName;
                String body = "The employee " + employeeName + "did " + testName + " test and get the result " + markScored;
                EmailHelper.sendEmail(receivers, subject, body);
            }


            ViewBag.EmployeeId = registration.EmployeeId;

            return(View(textXPapers));
        }