[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.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Security;
using System.IO;
using DotNetNuke.Entities.Users;

namespace AdefWebserver.Modules.SimpleSurvey
{
    public partial class AdminResults : PortalModuleBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if ((!PortalSecurity.IsInRole("Administrators")))
                {
                    Response.Redirect(DotNetNuke.Common.Globals.NavigateURL());
                }

                if (!Page.IsPostBack)
                {
                    DisplayUsers();
                    LoadUserResults();
                }
            }
            catch (Exception ex)
            {
                Exceptions.ProcessModuleLoadException(this, ex);
            }
        }

        #region DisplayUsers
        private void DisplayUsers()
        {
            SimpleSurveyDataContext SimpleSurveyDataContext = new SimpleSurveyDataContext();

            var results = from SurveyDataItems in SimpleSurveyDataContext.SurveyDataItems
                          from SurveyDataAnswers in SimpleSurveyDataContext.SurveyDataAnswers
                          where SurveyDataItems.SurveyDataItemID == SurveyDataAnswers.SurveyDataItemID
                          where SurveyDataItems.ModuleID == ModuleId
                          where SurveyDataItems.Visible == true
                          group SurveyDataAnswers by SurveyDataAnswers.UserID into Users
                          select new
                          {
                              UserID = Users.Key,
                              UserName = String.Format("{0} ({1})", 
                                 UserController.GetUser(PortalId, Convert.ToInt32(Users.Key), false).DisplayName, 
                                 UserController.GetUser(PortalId, Convert.ToInt32(Users.Key), false).Username)
                          };

            if (results.Count() > 0)
            {
                ddlUser.DataSource = results;
                ddlUser.DataBind();
            }
        }
        #endregion

        #region lnkBack_Click
        protected void lnkBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL());
        }
        #endregion

        #region ddlUser_SelectedIndexChanged
        protected void ddlUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadUserResults();
        }
        #endregion

        #region LoadUserResults
        private void LoadUserResults()
        {
            if (ddlUser.Items.Count > 0)
            {
                SimpleSurveyDataContext SimpleSurveyDataContext = new SimpleSurveyDataContext();

                var results = from SurveyDataItems in SimpleSurveyDataContext.SurveyDataItems
                              from SurveyDataAnswers in SimpleSurveyDataContext.SurveyDataAnswers
                              where SurveyDataItems.SurveyDataItemID == SurveyDataAnswers.SurveyDataItemID
                              where SurveyDataItems.ModuleID == ModuleId
                              where SurveyDataAnswers.UserID == Convert.ToInt32(ddlUser.SelectedValue)
                              orderby SurveyDataItems.SortOrder
                              select new
                              {
                                  Question = SurveyDataItems.DataName,
                                  Answer = SurveyDataAnswers.Value
                              };

                if (results.Count() > 0)
                {
                    gvAnswers.DataSource = results;
                    gvAnswers.DataBind();
                }
            }
        }
        #endregion

        #region gvAnswers_RowDataBound
        protected void gvAnswers_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if ((e.Row.RowType == DataControlRowType.DataRow))
            {
                TableRow objTableRow = (TableRow)e.Row;
                TableCell objTableCell = (TableCell)objTableRow.Cells[1];
                if (objTableCell.Text.Contains(".jpg"))
                {
                    HyperLink tempHyperLink = new HyperLink();
                    tempHyperLink.Text = " [View Image]";
                    tempHyperLink.NavigateUrl = string.Format(@"uploads/{0}", objTableCell.Text);
                    tempHyperLink.Target = "_new";
                    objTableCell.Controls.Add(tempHyperLink);
                }
            }
        }
        #endregion
    }
}