7e7a0e7614eb0294ce2ff9ce835ba5c633233831
[CPE_learningsite] / CPE / CPE.App / CPE.App.Api / Helpers / TinCanHelper.cs
1 using CPE.App.Api.Models;
2 using CPE.App.Web.Code;
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 using System.Web;
7
8 namespace CPE.App.Api.Helpers
9 {
10     public class TinCanHelper
11     {
12         public static bool HandleStatement(TinCanStatementModel statement)
13         {
14             // statement.Verb.Id=http://adlnet.gov/expapi/verbs/answered
15             // statement.Verb.Id=http://adlnet.gov/expapi/verbs/experienced
16
17             if (statement.Verb.Id.IndexOf("passed")>-1)
18             {
19                 Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} statement.Verb.Id={1}", DateTime.UtcNow.ToString("yyyyMMdd_HHmmss"), statement.Verb.Id);
20
21                 //parse statement to db variables
22                 var email = statement.Actor.Mbox.Substring(7);
23                 var fname = statement.Actor.Name.Substring(0, statement.Actor.Name.IndexOf(" ") + 1);
24                 var lname = statement.Actor.Name.Substring(statement.Actor.Name.LastIndexOf(" ") + 1);
25                 var courseUrl = statement.Object.Id.Substring(statement.Object.Id.LastIndexOf("/") + 1);
26                 var courseName = statement.Object.Definition.Name.EnUs;
27
28                 //cpe has test data with multiple, currently valid purchases of the same course for one user, which causes problems but is not a real-world problem.
29                 //elucidat tracks all users globally by email address so a tincan statement won't identify a specific purchase here 
30                 //unless we create unique email addy (e.g. email+ticket@mail.com) when originally logging them in to content
31                 var course = BaseController.Database.PurchasedCourses.FirstOrDefault(c => c.Email == email  && c.ContentUrl == courseUrl && c.CertificateDate == null);
32                 if (course == null)
33                 {
34                     Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} no action, cert already earned email={1} courseName={2}", DateTime.UtcNow.ToString("yyyyMMdd_HHmmss"), email, courseName);
35                     //no action, cert already earned
36                     return true;
37                 }
38
39                 course.CertificateDate = Convert.ToDateTime(statement.Timestamp);
40                 try
41                 {
42                     BaseController.Database.SubmitChanges();
43                 }
44                 catch (Exception exception)
45                 {
46                     Utilities.LogWrapper.Error("[TinCanHelper][HandleStatement] {0} email={1} courseName={2} exception={3}", DateTime.UtcNow.ToString("yyyyMMdd_HHmmss"), email, courseName, exception.ToString());
47                 }
48
49                 var done = SendEmailHelper.SendCertificateEmail(course);
50
51                 Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} cert sent email={1} courseName={2}", DateTime.UtcNow.ToString("yyyyMMdd_HHmmss"), email, courseName);
52
53                 //umm why am i not returning done here? 
54                 return true;
55             }
56
57
58             if (statement.Verb.Id.IndexOf("failed") > -1)
59             {
60                 Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} statement.Verb.Id={1}", DateTime.UtcNow.ToString("yyyyMMdd_HHmmss"), statement.Verb.Id);
61
62                 //parse statement to db variables
63                 var email = statement.Actor.Mbox.Substring(7);
64                 var fname = statement.Actor.Name.Substring(0, statement.Actor.Name.IndexOf(" ") + 1);
65                 var lname = statement.Actor.Name.Substring(statement.Actor.Name.LastIndexOf(" ") + 1);
66                 var courseUrl = statement.Object.Id.Substring(statement.Object.Id.LastIndexOf("/") + 1);
67                 var courseName = statement.Object.Definition.Name.EnUs;
68
69                 //only logging failed attempts prior to earning a cert
70                 //selecting first record for a user for a course that is not expired. To allow for if a user fails to earn a cert within a year and re-purchases course
71
72                 var course = BaseController.Database.PurchasedCourses.FirstOrDefault(c => c.Email == email && c.ContentUrl == courseUrl && c.CertificateDate == null && ((c.PurchaseDate).AddYears(1)) > DateTime.UtcNow);
73                 if (course == null)
74                 {
75                     //no action, cert already earned
76                     return true;
77                 }
78
79                 Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} course.Ticket={1}", DateTime.UtcNow.ToString("yyyyMMdd_HHmmss"), course.Ticket);
80
81                 if (!course.FailedAttempts.HasValue)
82                 {
83                     course.FailedAttempts = 0;
84                 }
85                 course.FailedAttempts++;
86                 try
87                 {
88                     BaseController.Database.SubmitChanges();
89                 }
90                 catch (Exception exception)
91                 {
92                     Utilities.LogWrapper.Error("[TinCanHelper][HandleStatement] {0} email={1} courseName={2} exception={3}", DateTime.UtcNow.ToString("yyyyMMdd_HHmmss"), email, courseName, exception.ToString());
93                 }
94
95                 Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} failure recorded email={1} courseName={2}", DateTime.UtcNow.ToString("yyyyMMdd_HHmmss"), email, courseName);
96
97                 return true;
98             }
99             return true;
100         }
101     }
102 }