Beispiel #1
0
        /// <summary>
        /// Gets the channel.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="identifier">The identifier.</param>
        /// <returns></returns>
        private InteractionChannelCache GetChannel(RockContext rockContext, string identifier)
        {
            if (identifier.IsNotNullOrWhitespace())
            {
                // Find by Id
                int?id = identifier.AsIntegerOrNull();
                if (id.HasValue)
                {
                    var channel = InteractionChannelCache.Read(id.Value);
                    if (channel != null)
                    {
                        return(channel);
                    }
                }

                // Find by Guid
                Guid?guid = identifier.AsGuidOrNull();
                if (guid.HasValue)
                {
                    var channel = InteractionChannelCache.Read(guid.Value);
                    if (channel != null)
                    {
                        return(channel);
                    }
                }

                if (!id.HasValue && !guid.HasValue)
                {
                    // Find by Name
                    InteractionChannel interactionChannel = new InteractionChannelService(rockContext)
                                                            .Queryable().AsNoTracking()
                                                            .FirstOrDefault(c => c.Name == identifier);
                    if (interactionChannel != null)
                    {
                        return(InteractionChannelCache.Read(interactionChannel));
                    }

                    // If still no match, and we have a name, create a new channel
                    using (var newRockContext = new RockContext())
                    {
                        interactionChannel      = new InteractionChannel();
                        interactionChannel.Name = identifier;
                        new InteractionChannelService(newRockContext).Add(interactionChannel);
                        newRockContext.SaveChanges();
                        return(InteractionChannelCache.Read(interactionChannel.Id));
                    }
                }
            }

            return(null);
        }
        public void Execute(IJobExecutionContext context)
        {
            JobDataMap dataMap     = context.JobDetail.JobDataMap;
            var        rockContext = new RockContext();

            InteractionChannelService   channelService     = new InteractionChannelService(rockContext);
            InteractionComponentService componentService   = new InteractionComponentService(rockContext);
            InteractionService          interactionService = new InteractionService(rockContext);
            ScheduleService             scheduleService    = new ScheduleService(rockContext);
            LocationService             locationService    = new LocationService(rockContext);
            AttendanceService           attendanceService  = new AttendanceService(rockContext);
            GroupService groupService = new GroupService(rockContext);

            // Load the channel
            InteractionChannelCache channel = InteractionChannelCache.Read(dataMap.GetString("InteractionChannel").AsGuid());

            // Setup
            int    campusLocationTypeId = DefinedValueCache.Read(Rock.SystemGuid.DefinedValue.LOCATION_TYPE_CAMPUS).Id;
            var    groupType            = GroupTypeCache.Read(dataMap.GetString("GroupType").AsGuid());
            var    groups    = groupService.GetByGroupTypeId(groupType.Id);
            string operation = !string.IsNullOrWhiteSpace(dataMap.GetString("Operation")) ? dataMap.GetString("Operation") : null;

            // Fetch the job so we can get the last run date/time
            int jobId      = Convert.ToInt16(context.JobDetail.Description);
            var jobService = new ServiceJobService(rockContext);
            var job        = jobService.Get(jobId);

            DateTime lastRun = job?.LastSuccessfulRunDateTime ?? DateTime.MinValue;

            var componentCampusMapping = dataMap.GetString("ComponentCampusMapping").AsDictionaryOrNull();

            foreach (var componentName in componentCampusMapping.Keys)
            {
                var         component = componentService.Queryable().Where(cs => cs.Name.ToLower() == componentName.ToLower() && cs.ChannelId == channel.Id).FirstOrDefault();
                CampusCache campus    = CampusCache.All().Where(c => c.Name == componentCampusMapping[componentName]).FirstOrDefault();

                if (campus != null && component != null)
                {
                    Group group = groups.Where(g => g.IsActive == true && g.CampusId == campus.Id).FirstOrDefault();
                    if (group?.GroupLocations != null)
                    {
                        foreach (var gl in group?.GroupLocations)
                        {
                            Location location = gl.Location;
                            foreach (Schedule schedule in gl.Schedules)
                            {
                                var occurrences = schedule.GetOccurrences(DateTime.MinValue, DateTime.Now);
                                foreach (var occurrence in occurrences)
                                {
                                    DateTime startDate = occurrence.Period.StartTime.Value;
                                    DateTime endDate   = occurrence.Period.EndTime.Value;

                                    var peopleAttended = interactionService.Queryable().Where(
                                        i => i.InteractionComponentId == component.Id &&
                                        i.InteractionDateTime <= endDate &&
                                        i.InteractionEndDateTime >= startDate &&
                                        i.PersonAliasId != null &&
                                        (i.CreatedDateTime > lastRun || i.PersonalDevice.ModifiedDateTime > lastRun || i.PersonalDevice.CreatedDateTime > lastRun) &&
                                        (operation == null || i.Operation == operation)
                                        ).Select(i => i.PersonAliasId).Distinct();
                                    int newAttendance = 0;
                                    foreach (int personAliasId in peopleAttended)
                                    {
                                        // Make sure we don't already have an attendance Record
                                        if (!attendanceService.Queryable().Any(a => DbFunctions.TruncateTime(a.StartDateTime) == occurrence.Period.StartTime.Value.Date && a.ScheduleId == schedule.Id && a.PersonAliasId == personAliasId && a.GroupId == group.Id && a.LocationId == location.Id && a.DidAttend == true))
                                        {
                                            Attendance attendance = new Attendance()
                                            {
                                                PersonAliasId = personAliasId,
                                                CampusId      = campus.Id,
                                                GroupId       = group.Id,
                                                LocationId    = location.Id,
                                                ScheduleId    = schedule.Id,
                                                StartDateTime = occurrence.Period.StartTime.Value,
                                                EndDateTime   = occurrence.Period?.EndTime?.Value,
                                                DidAttend     = true
                                            };
                                            attendanceService.Add(attendance);
                                            newAttendance++;
                                        }
                                    }
                                    if (newAttendance > 0)
                                    {
                                        rockContext.SaveChanges();
                                        context.Result += string.Format("{0} people attended {1} on {2} (Component {3}).\n", newAttendance, campus.Name, occurrence.Period.StartTime.Value.ToString("MM/dd/yyyy h:mm tt"), component.Name);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Renders the specified writer.
        /// </summary>
        /// <param name="badge">The badge.</param>
        /// <param name="writer">The writer.</param>
        public override void Render(PersonBadgeCache badge, System.Web.UI.HtmlTextWriter writer)
        {
            Guid?  interactionChannelGuid = GetAttributeValue(badge, "InteractionChannel").AsGuid();
            string badgeColor             = GetAttributeValue(badge, "BadgeColor");

            if (interactionChannelGuid.HasValue && !String.IsNullOrEmpty(badgeColor))
            {
                string dateRange = GetAttributeValue(badge, "DateRange");
                string badgeIcon = GetAttributeValue(badge, "BadgeIconCss");

                var interactionChannel = InteractionChannelCache.Read(interactionChannelGuid.Value);

                string detailPageUrl = string.Empty;

                if (!String.IsNullOrEmpty(GetAttributeValue(badge, "DetailPage")))
                {
                    int pageId = Rock.Web.Cache.PageCache.Read(Guid.Parse(GetAttributeValue(badge, "DetailPage"))).Id;

                    detailPageUrl = System.Web.VirtualPathUtility.ToAbsolute($"~/page/{pageId}?ChannelId={interactionChannel.Id}");
                }


                writer.Write($"<div class='badge badge-interactioninrange badge-id-{badge.Id} fa-3x' data-toggle='tooltip' data-original-title=''>");

                writer.Write("</div>");

                writer.Write($@"
                <script>
                    Sys.Application.add_load(function () {{
                                                
                        $.ajax({{
                                type: 'GET',
                                url: Rock.settings.get('baseUrl') + 'api/PersonBadges/InteractionsInRange/{Person.Id}/{interactionChannel.Id}/{HttpUtility.UrlEncode(dateRange)}' ,
                                statusCode: {{
                                    200: function (data, status, xhr) {{
                                    
                                var interactionCount = data;
                                var opacity = 0;
                                if(data===0){{
                                    opacity = 0.4;
                                }} else {{
                                    opacity = 1;    
                                }}
                                var linkUrl = '{detailPageUrl}';

                                 if (linkUrl != '') {{
                                                badgeContent = '<a href=\'' + linkUrl + '\'><span class=\'badge-content fa-layers fa-fw\' style=\'opacity:'+ opacity +'\'><i class=\'fas {badgeIcon} badge-icon\' style=\'color: {badgeColor}\'></i><span class=\'fa-layers-counter\'>'+ interactionCount +'</span></span></a>';
                                            }} else {{
                                                badgeContent = '<div class=\'badge-content \' style=\'opacity:'+ opacity +'\'><i class=\'fas {badgeIcon} badge-icon\' style=\'color: {badgeColor}\'></i><span class=\'fa-layers-counter\'>'+ interactionCount +'</span></div>';
                                            }}

                                            $('.badge-interactioninrange.badge-id-{badge.Id}').html(badgeContent);
                                        }}
                                }},
                        }});
                    }});
                </script>
                
            ");
            }
        }