[Back]

//
// DotNetNuke - http:'www.dotnetnuke.com
// Copyright (c) 2002-2009
// by DotNetNuke Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Web.UI.WebControls;
using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Security;
using DotNetNuke.Security.Roles;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules.Actions;

namespace DotNetNuke.Modules.DNNSilverlightChat
{
    public partial class View : PortalModuleBase, IActionable
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (DotNetNuke.Framework.AJAX.IsInstalled())
            {
                DotNetNuke.Framework.AJAX.RegisterScriptManager();

                DotNetNuke.Entities.Users.UserInfo objUser;
                objUser = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo();
                int intPortalID = objUser.PortalID;
                int intUserID = objUser.UserID;
                string DisplayName = objUser.DisplayName;
                string strPassword = IWebXAMLSupport.SetXAMLCall(objUser, ModuleId, 1);
                string strWebServiceURL = "http://" + this.PortalAlias.HTTPAlias + "/DesktopModules/DNNSilverlightChat/WebService.asmx";
                string strRefresh = Convert.ToString(GetRefresh());
                int intMaxUsers = GetMaxUsers();

                if (GetCurrentUserCount(ModuleId) > GetMaxUsers())
                {
                    intUserID = 0;
                    strPassword = "";
                }

                SilverlightControl.InitParameters = String.Format("PortalID={0},ModuleId={1},UserID={2},Password={3},Refresh={4},DisplayName={5},WebServiceURL={6}", intPortalID.ToString(), ModuleId.ToString(), intUserID.ToString(), strPassword, strRefresh, DisplayName, strWebServiceURL);
            }
        }

        #region IActionable Members
        public DotNetNuke.Entities.Modules.Actions.ModuleActionCollection ModuleActions
        {
            get
            {
                ModuleActionCollection Actions = new ModuleActionCollection();
                Actions.Add(GetNextActionID(), "Chat Log", ModuleActionType.AddContent, "", "", EditUrl(), false, SecurityAccessLevel.Edit, true, false);
                return Actions;
            }
        }
        #endregion

        #region GetRefresh
        int GetRefresh()
        {
            int intRefresh = 3;

            if ((Settings["SilverlightChat_Refresh"]) != null)
            {
                intRefresh = Convert.ToInt32(Settings["SilverlightChat_Refresh"]);
            }

            return intRefresh;
        }
        #endregion

        #region GetMaxUsers
        int GetMaxUsers()
        {
            int intMaxUsers = 3;

            if ((Settings["SilverlightChat_MaxUsers"]) != null)
            {
                intMaxUsers = Convert.ToInt32(Settings["SilverlightChat_MaxUsers"]);
            }

            return intMaxUsers;
        }
        #endregion

        #region GetCurrentUserCount
        private static int GetCurrentUserCount(int ModuleId)
        {
            int intCurrentUserCount = 1;

            try
            {
                string key = String.Format("{0}_DNNChatUser", ModuleId.ToString());
                List<DNNChatUser> colDNNChatUser;

                if (!CacheHelper.Get(key, out colDNNChatUser))
                {
                    colDNNChatUser = new List<DNNChatUser>();
                    CacheHelper.Add(colDNNChatUser, key);
                }

                var loggedInUsers = from objDNNChatUser in colDNNChatUser
                                    group objDNNChatUser by objDNNChatUser.UserID into ChatUsers
                                    select new { ChatUsers.Key };

                intCurrentUserCount = loggedInUsers.Count();
            }
            catch
            {
            }

            return intCurrentUserCount;
        }
        #endregion
    }
}