Ejemplo n.º 1
0
        public ActionResult _ajax_GetPieStatistics(int course, int company, int department, string start, string end, int user)
        {
            bool superadmin = SessionManager.inst.IsSuperadmin();
            bool admin      = SessionManager.inst.IsAdmin();
            bool manager    = SessionManager.inst.IsManager();

            if (!superadmin)
            {
                company = company != 0 ? company : SessionManager.inst.Company().Id;
            }
            if (manager && !admin)
            {
                department = department != 0 ? department : SessionManager.inst.User().DepartmentId;
            }
            if (!superadmin && !admin && !manager)
            {
                var username = System.Security.Principal.WindowsPrincipal.Current.Identity.Name;
                var profile  = AdminUserRepository.GetUserProfileByUsername(username);
                if (UserRepository.GetListOfRoles(username).Count() == 1 && UserRepository.GetListOfRoles(username).FirstOrDefault().Name == "User")
                {
                    user = profile.UserProfileId;
                }
            }

            var      customCourseIds = StatisticsRepository.MasterId2customId(course, company, department, user).Distinct().ToList();
            DateTime?startDateTime   = null;
            DateTime?endDateTime     = null;

            if (start != null)
            {
                startDateTime = DateTime.ParseExact(start, dateFormat, CultureInfo.InvariantCulture);
            }
            if (end != null)
            {
                endDateTime = DateTime.ParseExact(end, dateFormat, CultureInfo.InvariantCulture);
            }


            var allAssignedCourses = StatisticsRepository.CoursesAssignedToUsers(company, department, user)
                                     .Where(c => course == 0 ? true : c.CustomCours.MasterCourseId == course).Select(i => i.CustomCourseId).ToList();

            var startedCourses = StatisticsRepository.StartedCourses(customCourseIds, company, department, user);

            var allCompletedCourses = StatisticsRepository.TotalCompletedCourses(company, department, startDateTime, endDateTime, user)
                                      .Where(i => (course == 0 || i.CustomCours.MasterCourseId == course)).ToList();

            int     coursesSartedNotCompleted = startedCourses - allCompletedCourses.Count();
            int     coursesNotStarted         = allAssignedCourses.Count() - startedCourses;
            decimal onePercent = 0;

            if (allAssignedCourses.Count() > 0)
            {
                onePercent = ((decimal)100 / allAssignedCourses.Count());
            }
            var obj = new GetPieStatisticsJson
            {
                usersCompleted    = onePercent * allCompletedCourses.Count(),
                usersNotCompleted = onePercent * coursesSartedNotCompleted,
                usersNotStarted   = onePercent * coursesNotStarted
            };
            //var result = _statisticsRepository.UsersCompletedCoursesCount(companyId, start, end);
            var json = new JavaScriptSerializer().Serialize(obj);

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