/// <summary> /// Shows the active users. /// </summary> private void ShowActiveUsers() { int? siteId = GetAttributeValue( "Site" ).AsIntegerOrNull(); if (!siteId.HasValue) { lMessages.Text = "<div class='alert alert-warning'>No site is currently configured.</div>"; return; } else { int? pageViewCount = GetAttributeValue( "PageViewCount" ).AsIntegerOrNull(); if ( !pageViewCount.HasValue || pageViewCount.Value == 0 ) { pageViewCount = 1; } StringBuilder sbUsers = new StringBuilder(); var site = SiteCache.Read( siteId.Value ); lSiteName.Text = "<h4>" + site.Name + "</h4>"; lSiteName.Visible = GetAttributeValue( "ShowSiteNameAsTitle" ).AsBoolean(); lMessages.Text = string.Empty; using ( var rockContext = new RockContext() ) { IQueryable<PageView> pageViewQry = new PageViewService( rockContext ).Queryable( "Page" ); // Query to get who is logged in and last visit was to selected site var activeLogins = new UserLoginService( rockContext ).Queryable( "Person" ) .Where( l => l.PersonId.HasValue && l.IsOnLine == true ) .OrderByDescending( l => l.LastActivityDateTime ) .Select( l => new { login = l, pageViews = pageViewQry .Where( v => v.PersonAlias.PersonId == l.PersonId ) .OrderByDescending( v => v.DateTimeViewed ) .Take( pageViewCount.Value ) } ) .Where( a => a.pageViews.Any() && a.pageViews.FirstOrDefault().SiteId == site.Id ); if ( CurrentUser != null ) { activeLogins = activeLogins.Where( m => m.login.UserName != CurrentUser.UserName ); } foreach ( var activeLogin in activeLogins ) { var login = activeLogin.login; var pageViews = activeLogin.pageViews.ToList(); Guid? latestSession = pageViews.FirstOrDefault().SessionId; string pageViewsHtml = activeLogin.pageViews.ToList() .Where( v => v.SessionId == latestSession ) .Select( v => HttpUtility.HtmlEncode( v.Page.PageTitle ) ).ToList().AsDelimited( "<br> " ); TimeSpan tsLastActivity = login.LastActivityDateTime.HasValue ? RockDateTime.Now.Subtract( login.LastActivityDateTime.Value ) : TimeSpan.MaxValue; string className = tsLastActivity.Minutes <= 5 ? "recent" : "not-recent"; // create link to the person string personLink = login.Person.FullName; if ( GetAttributeValue( "PersonProfilePage" ) != null ) { string personProfilePage = GetAttributeValue( "PersonProfilePage" ); var pageParams = new Dictionary<string, string>(); pageParams.Add( "PersonId", login.Person.Id.ToString() ); var pageReference = new Rock.Web.PageReference( personProfilePage, pageParams ); personLink = string.Format( @"<a href='{0}'>{1}</a>", pageReference.BuildUrl(), login.Person.FullName ); } // determine whether to show last page views if ( GetAttributeValue( "PageViewCount" ).AsInteger() > 0 ) { string format = @" <li class='active-user {0}' data-toggle='tooltip' data-placement='top' title='{2}'> <i class='fa-li fa fa-circle'></i> {1} </li>"; sbUsers.Append( string.Format( format, className, personLink, pageViewsHtml ) ); } else { string format = @" <li class='active-user {0}'> <i class='fa-li fa fa-circle'></i> {1} </li>"; sbUsers.Append( string.Format( format, className, personLink ) ); } } } if ( sbUsers.Length > 0 ) { lUsers.Text = string.Format( @"<ul class='activeusers fa-ul'>{0}</ul>", sbUsers.ToString() ); } else { lMessages.Text = string.Format( "There are no logged in users on the {0} site.", site.Name ); } } }
/// <summary> /// Shows the active users. /// </summary> private void ShowActiveUsers() { int? siteId = GetAttributeValue( "Site" ).AsIntegerOrNull(); if ( !siteId.HasValue || SiteCache.Read(siteId.Value) == null ) { lMessages.Text = "<div class='alert alert-warning'>No site is currently configured.</div>"; return; } else { int pageViewCount = GetAttributeValue( "PageViewCount" ).AsIntegerOrNull() ?? 0; StringBuilder sbUsers = new StringBuilder(); var site = SiteCache.Read( siteId.Value ); lSiteName.Text = "<h4>" + site.Name + "</h4>"; lSiteName.Visible = GetAttributeValue( "ShowSiteNameAsTitle" ).AsBoolean(); if ( !site.EnablePageViews ) { lMessages.Text = "<div class='alert alert-warning'>Active " + site.Name + " users not available because page views are not enabled for site.</div>"; return; } lMessages.Text = string.Empty; string guestVisitorsStr = string.Empty; using ( var rockContext = new RockContext() ) { var qryPageViews = new PageViewService( rockContext ).Queryable(); var qryPersonAlias = new PersonAliasService( rockContext ).Queryable(); var pageViewQry = qryPageViews.Join( qryPersonAlias, pv => pv.PersonAliasId, pa => pa.Id, ( pv, pa ) => new { PersonAliasPersonId = pa.PersonId, pv.DateTimeViewed, pv.SiteId, pv.PageViewSessionId, PagePageTitle = pv.PageTitle } ); var last24Hours = RockDateTime.Now.AddDays( -1 ); int pageViewTakeCount = pageViewCount; if ( pageViewTakeCount == 0 ) { pageViewTakeCount = 1; } // Query to get who is logged in and last visit was to selected site var activeLogins = new UserLoginService( rockContext ).Queryable() .Where( l => l.PersonId.HasValue && l.IsOnLine == true ) .OrderByDescending( l => l.LastActivityDateTime ) .Select( l => new { login = new { l.UserName, l.LastActivityDateTime, l.PersonId, Person = new { l.Person.NickName, l.Person.LastName, l.Person.SuffixValueId } }, pageViews = pageViewQry .Where( v => v.PersonAliasPersonId == l.PersonId ) .Where( v => v.DateTimeViewed > last24Hours ) .OrderByDescending( v => v.DateTimeViewed ) .Take( pageViewTakeCount ) } ) .Select( a => new { a.login, pageViews = a.pageViews.ToList() } ); if ( CurrentUser != null ) { activeLogins = activeLogins.Where( m => m.login.UserName != CurrentUser.UserName ); } foreach ( var activeLogin in activeLogins ) { var login = activeLogin.login; if ( !activeLogin.pageViews.Any() || activeLogin.pageViews.FirstOrDefault().SiteId != site.Id ) { // only show active logins with PageViews and the most recent pageview is for the specified site continue; } var latestPageViewSessionId = activeLogin.pageViews.FirstOrDefault().PageViewSessionId; TimeSpan tsLastActivity = login.LastActivityDateTime.HasValue ? RockDateTime.Now.Subtract( login.LastActivityDateTime.Value ) : TimeSpan.MaxValue; string className = tsLastActivity.Minutes <= 5 ? "recent" : "not-recent"; // create link to the person string personFullName = Person.FormatFullName( login.Person.NickName, login.Person.LastName, login.Person.SuffixValueId ); string personLink = personFullName; if ( GetAttributeValue( "PersonProfilePage" ) != null ) { string personProfilePage = GetAttributeValue( "PersonProfilePage" ); var pageParams = new Dictionary<string, string>(); pageParams.Add( "PersonId", login.PersonId.ToString() ); var pageReference = new Rock.Web.PageReference( personProfilePage, pageParams ); personLink = string.Format( @"<a href='{0}'>{1}</a>", pageReference.BuildUrl(), personFullName ); } // determine whether to show last page views if ( GetAttributeValue( "PageViewCount" ).AsInteger() > 0 ) { string activeLoginFormat = @" <li class='active-user {0}' data-toggle='tooltip' data-placement='top' title='{2}'> <i class='fa-li fa fa-circle'></i> {1} </li>"; // define the formatting for each user entry if ( activeLogin.pageViews != null ) { string pageViewsHtml = activeLogin.pageViews .Where( v => v.PageViewSessionId == latestPageViewSessionId ) .Select( v => HttpUtility.HtmlEncode( v.PagePageTitle ) ).ToList().AsDelimited( "<br> " ); sbUsers.Append( string.Format( activeLoginFormat, className, personLink, pageViewsHtml ) ); } } else { string inactiveLoginFormat = @" <li class='active-user {0}'> <i class='fa-li fa fa-circle'></i> {1} </li>"; sbUsers.Append( string.Format( inactiveLoginFormat, className, personLink ) ); } } // get the 'show guests' attribute and if it's true, determine how many guests there are. bool showGuestVisitors = GetAttributeValue( "ShowGuestVisitors" ).AsBoolean(); if ( showGuestVisitors ) { // build a list of unique sessions views in the past 15 minutes. // We'll only take entries with a null personAliasID, which means they're not logged in, // and thus ARE guests. var last5Minutes = RockDateTime.Now.AddMinutes( -5 ); var last15Minutes = RockDateTime.Now.AddMinutes( -15 ); var qryGuests = new PageViewService( rockContext ).Queryable().AsNoTracking() .Where( p => p.SiteId == site.Id && p.DateTimeViewed > last15Minutes && p.PersonAliasId == null && p.PageViewSession.PageViewUserAgent.Browser != "Other" && p.PageViewSession.PageViewUserAgent.ClientType != "Crawler" ) .GroupBy( p => p.PageViewSessionId ) .Select( g => new { SessionId = g.Key, LastVisit = g.Max( p => p.DateTimeViewed ) } ) .ToList(); var numRecentGuests = qryGuests.Where( g => g.LastVisit >= last5Minutes ).Count(); var numInactiveGuests = qryGuests.Where( g => g.LastVisit < last5Minutes ).Count(); // now build the formatted entry, which is "Current Guests (0) (1)" where the first is a green badge, and the second yellow. if ( numRecentGuests > 0 || numInactiveGuests > 0 ) { guestVisitorsStr = "Current Guests:"; if ( numRecentGuests > 0 ) { guestVisitorsStr += string.Format( " <span class=\"badge badge-success\">{0}</span>", numRecentGuests ); } if ( numInactiveGuests > 0 ) { guestVisitorsStr += string.Format( " <span class=\"badge badge-warning\">{0}</span>", numInactiveGuests ); } } } } if ( sbUsers.Length > 0 ) { lUsers.Text = string.Format( @"<ul class='activeusers fa-ul'>{0}</ul>", sbUsers.ToString() ); lUsers.Text += string.Format( @"<p class='margin-l-sm'>{0}</p>", guestVisitorsStr ); } else { lMessages.Text = string.Format( "There are no logged in users on the {0} site.", site.Name ); lMessages.Text += "<br /><br />" + guestVisitorsStr; } } }