initial commit
[CPE_learningsite] / CPE / CPE.App / CPE.App.Web / Controllers / AccessController.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Configuration;
4 using System.Data.Entity;
5 using System.Linq;
6 using System.Web;
7 using System.Web.Mvc;
8 using CPE.App.Web.Models;
9 using CPE.App.Web.Code;
10
11 namespace CPE.App.Web.Controllers
12 {
13     public class AccessController : Controller
14     {
15
16         [HttpPost]
17         public ActionResult Revoke(BlockedPurchase blockedCourse)
18         {
19             BaseController.Database.BlockedPurchases.InsertOnSubmit(blockedCourse);
20             try
21             {
22                 BaseController.Database.SubmitChanges();
23             }
24             catch
25             {
26                 return null;
27             }
28
29             return null;
30         }
31
32         [HttpPost]
33         public ActionResult Restore(BlockedPurchase blockedCourse)
34         {
35             BlockedPurchase course =
36                 BaseController.Database.BlockedPurchases.SingleOrDefault(
37                     bc =>
38                         bc.Ticket == blockedCourse.Ticket && bc.ContentUrl == blockedCourse.ContentUrl &&
39                         bc.Email == blockedCourse.Email);
40             if (course != null)
41             {
42                 BaseController.Database.BlockedPurchases.DeleteOnSubmit(course);
43                 try
44                 {
45                     BaseController.Database.SubmitChanges();
46                 }
47                 catch
48                 {
49                     return null;
50                 }
51             }
52             return null;
53         }
54
55         [HttpPost]
56         public ActionResult OverrideCert(PurchasedCourse purchasedCourse)
57         {
58             PurchasedCourse pCourse =
59                 BaseController.Database.PurchasedCourses.FirstOrDefault(
60                     pc =>
61                         pc.Ticket == purchasedCourse.Ticket && pc.ContentUrl == purchasedCourse.ContentUrl &&
62                         pc.Email == purchasedCourse.Email && pc.CertificateDate == null);
63             if (pCourse != null)
64             {
65                 pCourse.CertificateDate = DateTime.Now;
66
67                 pCourse.isManualCertificate = true;
68             }
69             try
70             {
71                 BaseController.Database.SubmitChanges();
72             }
73             catch
74             {
75                 return null;
76             }
77
78             return null;
79         }
80
81         [HttpPost]
82         public ActionResult OverridePoll(int mpsk, int msk, double creds)
83         {
84             var meeting = BaseController.Database.MeetingSessions.FirstOrDefault(m => m.MeetingSessionKey == msk);
85             if (meeting == null)
86             {
87                 return Content("Error retrieving meeting session details.");
88             }
89             var scoId = meeting.SCO_ID;
90
91             
92             List<ParticipantEngagement> missedEngagements =
93                 BaseController.Database.ParticipantEngagements.Where(
94                     pe => pe.MeetingParticipantSessionKey == mpsk && pe.ResponseTime == null)
95                     .ToList();
96
97             var principalId = 0;
98             foreach (var missed in missedEngagements)
99             {
100                 if (principalId == 0) //updating principalid on the first loop, it will be the same for each item
101                 {
102                     principalId = missed.Principal_ID;
103                 }
104                 missed.ResponseTime = DateTime.UtcNow;
105             }
106             try
107             {
108                 BaseController.Database.SubmitChanges();
109             }
110             catch
111             {
112                 return null;
113             }
114
115             var purchase =
116                 BaseController.Database.ParticipantPurchases.FirstOrDefault(
117                     p => p.PrincipalID == principalId && p.MeetingSco == scoId && p.EarnedCertificate == false);
118             if (purchase == null)
119             {
120                 return
121                     Content(
122                         "This participant is not eligible for an Autmomated Certificate, because they logged in using a magic ticket instead of a unique hashed-ticket, but their Poll Responses have been updated.");
123             }
124
125             var meetingSession = BaseController.Database.MeetingSessionsData().FirstOrDefault(m => m.MeetingSessionKey == msk);
126
127             var courseLength = meetingSession.ActualSessionTime.GetValueOrDefault(); //in minutes
128             double courseCredits = courseLength / 50.0; //1 credit per 50 minutes
129             List<double> maxCourseCredits = ConfigurationManager.AppSettings["MaxCourseCreditList"].Split(';').Select(s => double.Parse(s)).ToList(); //max credits cut list
130             int mx = maxCourseCredits.BinarySearch(courseCredits); //find appropriate max for this course
131             double maxCredits = maxCourseCredits[mx >= 0 ? mx : ~mx - 1];//...by rounding down
132             double realCredits = creds;
133             purchase.Credits = Math.Min(maxCredits, realCredits); //user awarded no more than max possible credits for the course
134
135             purchase.EarnedCertificate = true;
136             purchase.IsForced = true;
137   
138
139             try
140             {
141                 BaseController.Database.SubmitChanges();
142             }
143             catch
144             {
145                 return null;
146             }
147             return Content("Poll responses successfully updated. User should now be eligible for a certificate.");
148
149         }
150     }
151 }