PageRenderTime 19ms CodeModel.GetById 1ms app.highlight 13ms RepoModel.GetById 2ms app.codeStats 0ms

/CmsCheckin/ListClasses.cs

https://bitbucket.org/mahalowe/bvcms
C# | 260 lines | 242 code | 18 blank | 0 comment | 25 complexity | 25d266f91a2d17df2cf1e7f1b424e8c7 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.ComponentModel;
  4using System.Drawing;
  5using System.Data;
  6using System.Linq;
  7using System.Text;
  8using System.Windows.Forms;
  9using System.Net;
 10using System.Xml.Linq;
 11using System.Configuration;
 12using System.IO;
 13using System.Collections.Specialized;
 14
 15namespace CmsCheckin
 16{
 17    public partial class ListClasses : UserControl
 18    {
 19        public ListClasses()
 20        {
 21            InitializeComponent();
 22        }
 23
 24        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
 25        {
 26            const int WM_KEYDOWN = 0x100;
 27            const int WM_SYSKEYDOWN = 0x104;
 28
 29            if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
 30            {
 31                switch (keyData)
 32                {
 33                    case Keys.PageUp:
 34                        if (pgup.Visible)
 35                            ShowPage(page - 1);
 36                        return true;
 37                    case Keys.PageDown:
 38                        if (pgdn.Visible)
 39                            ShowPage(page + 1);
 40                        return true;
 41                    case Keys.Escape:
 42                        this.Swap(Program.home.family);
 43                        Program.home.family.ShowFamily(FamilyId);
 44                        return true;
 45                    case Keys.S | Keys.Alt:
 46                        Program.TimerReset();
 47                        Program.CursorShow();
 48                        return true;
 49                }
 50            }
 51            return base.ProcessCmdKey(ref msg, keyData);
 52        }
 53
 54        private List<ClassInfo> list;
 55        private float points;
 56        private Font Labfont;
 57        private string Verdana;
 58        private XDocument x;
 59        DateTime time;
 60        int FamilyId;
 61        int PeopleId;
 62        int page;
 63        List<Control> controls = new List<Control>();
 64        bool ShowAllClasses;
 65        public bool JoiningNotAttending = false;
 66
 67        public void ShowResults(int pid)
 68        {
 69            var url = "Checkin2/Classes/" + pid + Program.QueryString;
 70            if (ShowAllClasses)
 71                url += "&noagecheck=true";
 72            x = this.GetDocument(url);
 73
 74            time = DateTime.Now;
 75
 76            points = 14F;
 77
 78            Verdana = "Verdana";
 79            FamilyId = x.Root.Attribute("fid").Value.ToInt();
 80            PeopleId = x.Root.Attribute("pid").Value.ToInt();
 81
 82            if (x.Descendants("class").Count() == 0)
 83            {
 84                ClearControls();
 85                var lab = new Label();
 86                lab.Font = new Font(Verdana, points, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
 87                lab.Location = new Point(15, 200);
 88                lab.AutoSize = true;
 89                lab.Text = "Sorry, no classes found";
 90                this.Controls.Add(lab);
 91                controls.Add(lab);
 92                GoBackButton.Text = "Go Back";
 93                return;
 94            }
 95            list = new List<ClassInfo>();
 96            foreach (var e in x.Descendants("class"))
 97            {
 98                var hr = DateTime.Today;
 99                DateTime.TryParse(e.Attribute("hour").Value, out hr);
100
101                var ci = new ClassInfo
102                {
103                    display = e.Attribute("display").Value,
104                    oid = e.Attribute("orgid").Value.ToInt(),
105                    pid = PeopleId,
106                    nlabels = e.Attribute("nlabels").Value.ToInt(),
107                    hour = hr
108                };
109                var leadtime = double.Parse(e.Attribute("leadtime").Value);
110                double howlate = -(Program.EarlyCheckin / 60d);
111                if (ci.oid != 0 && leadtime <= Program.LeadTime && leadtime >= howlate)
112                    list.Add(new ClassInfo
113                    {
114                        display = e.Attribute("display").Value,
115                        oid = e.Attribute("orgid").Value.ToInt(),
116                        pid = PeopleId,
117                        nlabels = e.Attribute("nlabels").Value.ToInt(),
118                        hour = hr
119                    });
120            }
121            ShowPage(1);
122        }
123        public void ShowPage(int page)
124        {
125            ClearControls();
126            this.page = page;
127            var g = this.CreateGraphics();
128            while (true)
129            {
130                var wid = 0;
131                Labfont = new Font(Verdana, points, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
132                foreach (var c in list)
133                {
134                    var size = g.MeasureString(c.display, Labfont);
135                    wid = Math.Max(wid, (int)size.Width);
136                }
137                if (wid > 1000)
138                {
139                    points -= 1F;
140                    continue;
141                }
142                break;
143            }
144            const int PageSize = 10;
145
146            int srow = (page - 1) * PageSize;
147            int erow = srow + PageSize;
148            if (erow > list.Count)
149                erow = list.Count;
150            pgdn.Visible = list.Count > erow;
151            pgup.Visible = srow > 0;
152            int rowheight = 50;
153            int top = 50;
154
155            for (var r = srow; r < erow; r++)
156            {
157                var c = list[r];
158                var ab = new Button();
159                controls.Add(ab);
160                ab.BackColor = SystemColors.ControlLight;
161                ab.Font = Labfont;
162                top += rowheight;
163                ab.Location = new Point(10, top);
164                ab.Size = new Size(1000, 45);
165                ab.TextAlign = ContentAlignment.MiddleLeft;
166                ab.UseVisualStyleBackColor = false;
167                ab.Tag = c;
168                ab.Text = c.display;
169                this.Controls.Add(ab);
170                ab.Click += new EventHandler(ab_Click);
171            }
172            Program.TimerStart(timer1_Tick);
173        }
174
175        void timer1_Tick(object sender, EventArgs e)
176        {
177            Program.TimerStop();
178            Util.UnLockFamily();
179            Program.ClearFields();
180            ShowAllClasses = false;
181            this.GoHome("");
182        }
183
184        void ab_Click(object sender, EventArgs e)
185        {
186            var ab = sender as Button;
187            var c = ab.Tag as ClassInfo;
188            if (JoiningNotAttending)
189                Util.JoinUnJoin(c, true);
190            else
191                Util.AttendUnAttend(new Util.ClassCheckedInfo { c = c, ischecked = true });
192            ShowAllClasses = false;
193            JoiningNotAttending = false;
194			if (Program.baseform.textbox.Parent is Home)
195			{
196				this.Swap(Program.home.family);
197				Program.home.family.classlist.Add(c);
198				Program.home.family.ShowFamily(FamilyId);
199			}
200			else if (Program.baseform.textbox.Parent is Home2)
201			{
202				this.Swap(Program.home2.family);
203				Program.home2.family.classlist.Add(c);
204				Program.home2.family.ShowFamily(FamilyId);
205			}
206        }
207
208        private void GoBack_Click(object sender, EventArgs e)
209        {
210			JoiningNotAttending = false;
211			if (Program.baseform.textbox.Parent is Home)
212			{
213				this.Swap(Program.home.family);
214				Program.home.family.ShowFamily(FamilyId);
215			}
216			else if (Program.baseform.textbox.Parent is Home2)
217			{
218				this.Swap(Program.home2.family);
219				Program.home2.family.ShowFamily(FamilyId);
220			}
221            ShowAllClasses = false;
222        }
223        private void ClearControls()
224        {
225			JoiningNotAttending = false;
226            foreach (var c in controls)
227            {
228                this.Controls.Remove(c);
229                c.Dispose();
230            }
231            controls.Clear();
232        }
233        private void pgdn_Click(object sender, EventArgs e)
234        {
235            ShowPage(page + 1);
236        }
237
238        private void pgup_Click(object sender, EventArgs e)
239        {
240            ShowPage(page - 1);
241        }
242
243        private void allclasses_Click(object sender, EventArgs e)
244        {
245            ShowAllClasses = true;
246            ShowResults(PeopleId);
247        }
248
249    }
250    public class ClassInfo
251    {
252        public string display { get; set; }
253        public int oid { get; set; }
254        public int pid { get; set; }
255        public string mv { get; set; }
256        public string bdays { get; set; }
257        public int nlabels { get; set; }
258        public DateTime? hour { get; set; }
259    }
260}