FrmAnalyzeRateOption.cs 19.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
/*-------------------------------------------------------------------------------------------
 * 答题正确率
 * 创建:杨斌 2011-12-01
 * 修改:杨斌 2012-03-01
 * ----------------------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeneralLib;
using Microsoft.Office.Interop.PowerPoint;

namespace SunVoteARSPPT
{
    public partial class FrmAnalyzeRateOption : Form
    {
        /// <summary>
        /// 图表类
        /// </summary>
        public ManageChart Chart = null;

        public FrmAnalyzeRateOption()
        {
            InitializeComponent();
        }

        //退出
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 是否正在加载窗体
        /// </summary>
        private bool IsLoading = false;

        //窗体加载
        private void FrmAnalyzeRateOption_Load(object sender, EventArgs e)
        {
            GlobalInfo.SysLanguage.SetLanguage(this.Name, this);
            lvwOptionRate.Columns[0].Text = "   " + lvwOptionRate.Columns[0].Text;

            IsLoading = true;

            ResponseDB.UpdateSlideIndexToDB();

            Chart = new ManageChart();
            Chart.MsChart = this.MSChart;
            MSChart.Dock = DockStyle.Fill;

            ControlOper.SetPosCheckBoxToListViewHead(lvwOptionRate, chkSelectAllOptionRate);//杨斌 2012-11-16

            LoadSlide();

            ShowTopicInfo();

            LoadSlideOptionRate();

            ShowChartRateOption();

            IsLoading = false;

            //杨斌 2014-07-25
            List<string> lstRadio = new List<string>();
            lstRadio.Add(GlobalInfo.IniCtlSystem.ReadString(this.Name, "ChartDataLabel", ""));
            lstRadio.Add(GlobalInfo.IniCtlSystem.ReadString(this.Name, "ChartType", ""));
            ControlOper.SetControlCheck(this, lstRadio);

            //杨斌 2019-07-04
            if (SelectSlideID > 0)
            {
                foreach (ListViewItem v in lvwSlide.Items)
                {
                    if ((v.Tag + "") == ("'" + SelectSlideID + "'"))
                    {
                        v.Selected = true;
                    }
                }
            }
        }

        /// <summary>
        /// 查询的反馈题目类型
        /// </summary>
        private const string cResponseType = "'Group','Choice','Judge','Vote','Grade'";//,'Number','Text' 因没选项报错。杨斌 2015-01-16

        /// <summary>
        /// 加载反馈幻灯片信息
        /// </summary>
        private void LoadSlide()
        {
            try
            {
                lvwSlide.Items.Clear();

                //加载数据
                string sql = "Select * From ST_Topic" +
                            " WHERE (((ST_Topic.TT_ID) IN(" + cResponseType + ")) AND (Len(ST_Topic.T_ID)<12))" +
                            " Order By T_Index Asc";

                GlobalInfo.DBOperation.OpenDataSet(sql);
                System.Data.DataTable tb = GlobalInfo.DBOperation.DataSet.Tables[0];
                for (int iRow = 0; iRow < tb.Rows.Count; iRow++)
                {
                    DataRow row = tb.Rows[iRow];

                    ListViewItem item = lvwSlide.Items.Add(row["T_Index"].ToString());
                    item.SubItems.Add(row["T_Title"].ToString());

                    item.Tag = "'" + row["T_ID"].ToString() + "'";//存放题目ID

                    item.SubItems[0].Tag = row["TT_ID"] + "";//杨斌 2018-06-20
                }
                GlobalInfo.DBOperation.CloseDataSet();

                //默认选中题目
                if (lvwSlide.Items.Count > 0) lvwSlide.Items[0].Selected = true;

            }
            catch (Exception ex)
            {
                SystemLog.WriterLog(ex);
            }
        }

        /// <summary>
        /// 加载题目反馈结果的选项比率,按高比率低排序
        /// </summary>
        private void LoadSlideOptionRate()
        {
            try
            {
                lvwOptionRate.Items.Clear();//杨斌 空白的需清理,放在return前面

                ListViewItem selItem = null;
                if (lvwSlide.SelectedItems.Count > 0)
                    selItem = lvwSlide.SelectedItems[0];

                if (selItem == null) return;

                string topicID = selItem.Tag.ToString();
                //杨斌 2018-06-20
                string topicType = selItem.SubItems[0].Tag + "";
                bool haveCorrect = ((topicType == "Choice") || (topicType == "Judge"));//"'Group','Choice','Judge','Vote','Grade'"

                //杨斌 2019-10-10。修正没有正确答案的题目显示错误选项百分比
                if (haveCorrect)
                {
                    bool hasCorrectAsw = false;
                    string responseType = "'Choice','Judge'";
                    Dictionary<string, Dictionary<string, string>> dicTopicPar = ReportEx.GetTopicPar(responseType);
                    string tid = topicID.Trim('\'');
                    if (dicTopicPar[tid].ContainsKey("CorrectAnswer"))
                        hasCorrectAsw = !string.IsNullOrEmpty(dicTopicPar[tid]["CorrectAnswer"]);
                    haveCorrect = hasCorrectAsw;
                }
                
                //杨斌 2012-10-24
                string sql = "SELECT ST_Response.R_Result, ST_Response.R_Correct, Count(ST_Response.R_Result) AS R_ResultCount" +
                        " FROM ST_Response" +
                        " GROUP BY ST_Response.R_Result, ST_Response.R_Correct, ST_Response.T_ID" +
                        " HAVING (((ST_Response.T_ID)=" + topicID + "))" +
                        " ORDER BY ST_Response.R_Correct DESC, Count(ST_Response.R_Result) DESC, ST_Response.R_Result ASC";

                GlobalInfo.DBOperation.OpenDataSet(sql);
                System.Data.DataTable tb = GlobalInfo.DBOperation.DataSet.Tables[0];
                int countResult = 0;

                //杨斌 2012-06-29
                bool bABCD = PPTOper.IsABCDItemBySlideId(ConvertOper.Convert(topicID.Replace("'", "")).ToInt);

                for (int iRow = 0; iRow < tb.Rows.Count; iRow++)
                {
                    DataRow row = tb.Rows[iRow];

                    string result = row["R_Result"].ToString();
                    //杨斌 2012-06-29
                    if (bABCD)
                        result = PPTOper.FormatNumABC(result);
                    result = PublicFunction.TrimEndStr(result, ",");

                    //杨斌 2012-11-14
                    string sR = GlobalInfo.SysLanguage.LPT.ReadString("FrmAnalyzeRateCorrect", "lblLegentCorrect", "Right");
                    string sW = GlobalInfo.SysLanguage.LPT.ReadString("FrmAnalyzeRateCorrect", "lblLegentWrong", "Wrong");
                    byte[] aR = System.Text.Encoding.Unicode.GetBytes(sR);
                    byte[] aW = System.Text.Encoding.Unicode.GetBytes(sW);
                    if (aR.Length > aW.Length)
                    {
                        sW = sW.PadRight(aR.Length - aW.Length + 1, ' ');
                    }
                    else if (aR.Length < aW.Length)
                    {
                        sR = sR.PadRight(aW.Length - sR.Length + 1, ' ');
                    }
                    if (haveCorrect)//杨斌 2018-06-20
                    {
                        if (ConvertOper.Convert(row["R_Correct"].ToString()).ToInt > 0)
                            result = sR + " " + result;
                        else
                            result = sW + " " + result;
                    }

                    ListViewItem item = lvwOptionRate.Items.Add(result);
                    int countItem = ConvertOper.Convert(row["R_ResultCount"].ToString()).ToInt;
                    countResult += countItem;
                    item.SubItems.Add(row["R_ResultCount"].ToString());

                    item.Tag = ConvertOper.Convert(row["R_ResultCount"].ToString()).ToInt;
                }
                for (int i = 0; i < lvwOptionRate.Items.Count; i++)
                {
                    ListViewItem item = lvwOptionRate.Items[i];
                    item.SubItems[1].Text += " / " + (ConvertOper.Convert(item.SubItems[1].Text).ToInt / (double)countResult).ToString("P2");
                }
                lvwOptionRate.Tag = countResult;//记住分母
                GlobalInfo.DBOperation.CloseDataSet();

                //全选
                chkSelectAllOptionRate.Checked = (lvwOptionRate.Items.Count > 0);
                ControlOper.SelectAllListViewItem(lvwOptionRate, chkSelectAllOptionRate.Checked);
            }
            catch (Exception ex)
            {
                SystemLog.WriterLog(ex);
            }
        }

        /// <summary>
        /// 显示选项选择率图表
        /// </summary>
        private void ShowChartRateOption()
        {
            try
            {
                MSChart.Visible = false;

                ListViewItem selItem = null;
                if (lvwSlide.SelectedItems.Count > 0)
                    selItem = lvwSlide.SelectedItems[0];

                int selectCount = lvwOptionRate.CheckedItems.Count;

                if ((selItem == null) || (selectCount < 1)) return;

                //取值
                double countResult = 0;
                string[] lab = new string[selectCount];
                double[] data = new double[selectCount];
                for (int i = 0; i < lvwOptionRate.CheckedItems.Count; i++)
                {
                    ListViewItem item = lvwOptionRate.CheckedItems[i];
                    lab[i] = item.Text;//图例文本
                    data[i] = ConvertOper.Convert(item.Tag.ToString()).ToInt;//数据值
                    countResult += data[i];
                }

                //图例文本
                Chart.LegentText = lab;
                Chart.XValue = lab;

                //数据值
                Chart.YValues = new List<double[]>();
                Chart.YValues.Add(data);

                //分母
                Chart.LabelDenominator = ConvertOper.Convert(lvwOptionRate.Tag.ToString()).ToInt;

                //图表类型
                RadioButton rbtChartType = ControlOper.GetChecked(rbtChartType1);
                Chart.ChartType = EnumName<ChartTypes>.GetEnum(rbtChartType.Tag.ToString());
                Chart.Is3D = (rbtChartType.Name.IndexOf("3D") > 0);

                //图表数据标签样式             
                RadioButton rbtChartDataLabel = ControlOper.GetChecked(rbtChartDataLabel1);
                Chart.LabelType = EnumName<LabelTypes>.GetEnum(rbtChartDataLabel.Tag.ToString());

                //图表图例显示
                switch (Chart.ChartType)
                {
                    case ChartTypes.ctPie://饼图则显示图例
                        Chart.LegendEnable = true;
                        Chart.LegentDocking = LegentDockings.ldRight;
                        break;
                    default:
                        Chart.LegendEnable = false;
                        break;
                }

                //图表高度
                Chart.Width = MSChart.Width;
                Chart.Height = MSChart.Height;

                //图表标题显示题目标题。杨斌 2015-07-07
                if (lvwSlide.SelectedItems.Count > 0)
                {
                    System.Windows.Forms.DataVisualization.Charting.Chart mChart = Chart.MsChart;
                    mChart.Titles.Clear();
                    mChart.Titles.Add(lvwSlide.SelectedItems[0].SubItems[1].Text);
                    mChart.Titles[0].Alignment = ContentAlignment.TopCenter;
                    mChart.Titles[0].Font = Chart.ItemLabelFont;
                }

                Chart.UpdateChart();//刷新图表

                MSChart.Visible = true;

            }
            catch (Exception ex)
            {
                SystemLog.WriterLog(ex);
            }
        }

        /// <summary>
        /// 加载题目信息
        /// </summary>
        private void ShowTopicInfo()
        {
            try
            {
                //获取题目ID和标题
                ListViewItem selItem = null;
                if (lvwSlide.SelectedItems.Count > 0)
                    selItem = lvwSlide.SelectedItems[0];

                if (selItem == null) return;

                string topicText = selItem.SubItems[1].Text;
                string topicID = selItem.Tag.ToString();

                //获取选项个数
                int optionCount = 0;
                string sql = "SELECT ST_TopicPar.T_ID, ST_TopicPar.TP_Value" +
                        " FROM ST_TopicPar" +
                        " WHERE (((ST_TopicPar.T_ID) In (" + topicID + ")) AND ((ST_TopicPar.TP_Name)='OptionCount'))";
                GlobalInfo.DBOperation.OpenDataSet(sql);
                System.Data.DataTable tb = GlobalInfo.DBOperation.DataSet.Tables[0];
                for (int iRow = 0; iRow < tb.Rows.Count; iRow++)
                {
                    DataRow row = tb.Rows[iRow];
                    optionCount = ConvertOper.Convert(row["TP_Value"].ToString()).ToInt;
                }
                GlobalInfo.DBOperation.CloseDataSet();

                //获取选项文本                
                string[] aryWherePar = new string[optionCount];
                for (int i = 0; i < optionCount; i++)
                {
                    aryWherePar[i] = "'OptionText_" + (i + 1).ToString() + "'";
                }
                string wherePar = string.Join(",", aryWherePar);
                string[] aryOptionText = new string[optionCount];//Key=题目ID,Item=选项文本[]
                sql = "SELECT DISTINCT ST_TopicPar.T_ID, ST_TopicPar.TP_Name, ST_TopicPar.TP_Value" +
                        " FROM ST_TopicPar" +
                        " WHERE (((ST_TopicPar.TP_Name) In (" + wherePar + ")) AND ((ST_TopicPar.T_ID) In (" + topicID + ")))" +
                        " ORDER BY ST_TopicPar.T_ID, ST_TopicPar.TP_Name";
                GlobalInfo.DBOperation.OpenDataSet(sql);
                tb = GlobalInfo.DBOperation.DataSet.Tables[0];
                for (int iRow = 0; iRow < tb.Rows.Count; iRow++)
                {
                    DataRow row = tb.Rows[iRow];
                    string id = "'" + row["T_ID"].ToString() + "'";
                    string tName = row["TP_Name"].ToString();
                    string tValue = row["TP_Value"].ToString();
                    int index = ConvertOper.Convert(tName.Replace("OptionText_", "")).ToInt - 1;
                    //杨斌 2012-06-29
                    //aryOptionText[index] = (iRow + 1).ToString() + "." + tValue;
                    bool bABCD = PPTOper.IsABCDItemBySlideId(ConvertOper.Convert(row["T_ID"].ToString()).ToInt);
                    if (bABCD)
                        aryOptionText[index] = PPTOper.FormatNumABC(iRow + 1, false) + ". " + tValue;
                    else
                        aryOptionText[index] = (iRow + 1).ToString() + "." + tValue;
                }
                GlobalInfo.DBOperation.CloseDataSet();

                if (topicText.Length < 1) topicText = "Slide " + selItem.Text;
                string text = topicText;
                text += "\r\n\r\n";
                text += string.Join("\r\n", aryOptionText);
                txtTopicInfo.Text = text;
            }
            catch (Exception ex)
            {
                SystemLog.WriterLog(ex);
            }
        }

        int SelectSlideID = 0;
        public void ShowDialogAndTrySelect(int slideID)
        {
            //ShowChartFull();
            SelectSlideID = slideID;
            this.ShowDialog();
        }

        /// <summary>
        /// 是否全屏
        /// </summary>
        private bool IsFullScreen = false;
        private System.Drawing.Point OldFrmPoint;
        private Size OldFrmSize;
        private System.Drawing.Point OldPnlPoint;
        private Size OldPnlSize;

        /// <summary>
        /// 全屏显示图表
        /// </summary>
        private void ShowChartFull()
        {
            if (IsFullScreen)
            {
                IsFullScreen = false;
                this.ControlBox = true;
                this.Text = this.Tag.ToString();
                pnlChart.Dock = DockStyle.None;
                this.Location = OldFrmPoint;
                this.Size = OldFrmSize;
                pnlChart.Location = OldPnlPoint;
                pnlChart.Size = OldPnlSize;
                pnlChart.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
            }
            else
            {
                IsFullScreen = true;

                OldFrmPoint = this.Location;
                OldFrmSize = this.Size;
                OldPnlPoint = pnlChart.Location;
                OldPnlSize = pnlChart.Size;

                this.Tag = this.Text;
                this.Text = "";
                this.ControlBox = false;
                pnlChart.Dock = DockStyle.Fill;
                pnlChart.BringToFront();
                this.Location = new System.Drawing.Point(0, 0);

                //杨斌 2014-09-17
                Screen scrShow = ControlOper.GetSecondScreen();
                this.Size = new Size(scrShow.Bounds.Width, scrShow.Bounds.Height);
            }

            ControlOper.CenterForm(this, IsFullScreen);//杨斌 2014-09-17
        }

        //选择题目时加载选项选择率排序,并自动选中选择率,显示图表
        private void lvwSlide_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsLoading) return;

            ShowTopicInfo();

            LoadSlideOptionRate();

            ShowChartRateOption();
        }

        //选择显示的结果选项
        private void lvwOptionRate_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            if (IsLoading) return;

            IsLoading = true;
            chkSelectAllOptionRate.Checked = ((lvwOptionRate.CheckedItems.Count > 0) && (lvwOptionRate.CheckedItems.Count == lvwOptionRate.Items.Count));
            IsLoading = false;

            ShowChartRateOption();
        }

        //全选结果选项
        private void chkSelectAllOptionRate_CheckedChanged(object sender, EventArgs e)
        {
            if (IsLoading) return;

            IsLoading = true;
            ControlOper.SelectAllListViewItem(lvwOptionRate, chkSelectAllOptionRate.Checked);
            IsLoading = false;

            ShowChartRateOption();
        }

        //选择图表数据标签类型
        private void rbtChartDataLabel_CheckedChanged(object sender, EventArgs e)
        {
            if (IsLoading) return;

            ShowChartRateOption();

            GlobalInfo.IniCtlSystem.WriteValue(this.Name, "ChartDataLabel", (sender as Control).Name);//杨斌 2014-07-25
        }

        //选择图表样式
        private void rbtChartType_CheckedChanged(object sender, EventArgs e)
        {
            if (IsLoading) return;

            ShowChartRateOption();

            GlobalInfo.IniCtlSystem.WriteValue(this.Name, "ChartType", (sender as Control).Name);//杨斌 2014-07-25
        }

        //双击全屏
        private void pnlChart_DoubleClick(object sender, EventArgs e)
        {
            ShowChartFull();
        }

        //窗体按键响应
        private void FrmAnalyzeRateOption_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    if (IsFullScreen)
                        ShowChartFull();
                    else
                        this.Close();
                    break;
            }
        }

        private void btnShowFull_Click(object sender, EventArgs e)
        {

        }
    }
}