File: D:/HostingSpaces/TDijk1/erp-apps.eu/wwwroot/App_Code/ERPApps/Tasks/AppPopularityTask.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CMS.EventLog;
using CMS.Scheduler;
using CMS.CMSHelper;
using System.Data;
using CMS.GlobalHelper;
using CMS.TreeEngine;
using CMS.DataEngine;
using CMS.SettingsProvider;
using CMS.DocumentEngine;
using CMS.SiteProvider;
using CMS.Helpers;
using CMS;
[assembly: RegisterCustomClass("ERP.AppPopularityTask", typeof(ERP.AppPopularityTask))]
namespace ERP
{
public class AppPopularityTask : ITask
{
string DOCTYPE = ERPConfig.DOCTYPE_APPLICATION;
/// <summary>
/// Executes the task.
/// </summary>
/// <param name="ti">Task info</param>
public string Execute(TaskInfo ti)
{
EventLogProvider.LogEvent(EventType.INFORMATION, "AppPopularityTask", "Start", null, "This task was executed from '~/App_Code/ERPApps/Tasks/AppPopularityTask.cs'.");
int updatedCount = 0;
GeneralConnection con = ConnectionHelper.GetConnection();
// Check the site
int siteId = SiteContext.CurrentSiteID;
if ( siteId > 0 )
{
// select all documents of given doctype
InfoDataSet<TreeNode> ds = TreeHelper.SelectNodes("/%", false, DOCTYPE);
if ( !DataHelper.DataSourceIsEmpty(ds) )
{
foreach ( DataRow item in ds.Tables[0].Rows )
{
// set date interval
DateTime toDate = DateTime.Now;
DateTime fromDate = toDate.AddDays(-30); // take previous 30 days
CMS.WebAnalytics.HitsIntervalEnum interval = CMS.WebAnalytics.HitsIntervalEnum.Day;
TreeNode node = TreeNode.New(DOCTYPE,item);
if ( node == null )
continue;
// calculate hits
int hits = CMS.WebAnalytics.HitsInfoProvider.GetObjectHitCount(siteId, node.NodeID, interval, "pageviews", fromDate, toDate);
// calculate rating from reviews
QueryDataParameters parameters = new QueryDataParameters();
parameters.Add("@AppID", node.GetValue("AppID"));
var queryInfo = QueryInfoProvider.GetQueryInfo(ERPConfig.DOCTYPE_REVIEW + ".applicationrating");
DataSet reviewDs = con.ExecuteQuery(new QueryParameters(queryInfo, parameters,null));
if ( !DataHelper.DataSourceIsEmpty(reviewDs) )
{
TreeProvider.SetRating(node, ValidationHelper.GetInteger(reviewDs.Tables[0].Rows[0]["Total"], 0)/10.0, ValidationHelper.GetInteger(reviewDs.Tables[0].Rows[0]["RecordsCount"], 0));
}
else
{
TreeProvider.SetRating(node, 0, 0);
}
// update application
node.SetValue("AppMonthPageviews", hits);
node.Update();
updatedCount++;
}
}
}
EventLogProvider.LogEvent(EventType.INFORMATION, "AppPopularityTask", "Finished", null, "This task was executed from '~/App_Code/ERPApps/Tasks/AppPopularityTask.cs'. Updated " + updatedCount + " documents.");
return null;
}
}
}