update signutare
[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 using Newtonsoft.Json;
8
9 namespace CPE.App.Api.Helpers
10 {
11     public class TinCanHelper
12     {
13         public static bool HandleStatement(TinCanStatementModel statement)
14         {
15             // statement.Verb.Id=http://adlnet.gov/expapi/verbs/answered
16             // statement.Verb.Id=http://adlnet.gov/expapi/verbs/experienced
17             var dtNow = DateTime.UtcNow.ToString("yyyyMMdd_HHmmss");
18             if (statement.Verb.Id.IndexOf("passed")>-1)
19             {
20                 Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} statement.Verb.Id={1}. statement={2}", dtNow, statement.Verb.Id, JsonConvert.SerializeObject(statement));
21
22                 //parse statement to db variables
23                 var email = statement.Actor.Mbox.Substring(7);
24                 var fname = statement.Actor.Name.Substring(0, statement.Actor.Name.IndexOf(" ") + 1);
25                 var lname = statement.Actor.Name.Substring(statement.Actor.Name.LastIndexOf(" ") + 1);
26                 var courseUrl = statement.Object.Id.Substring(statement.Object.Id.LastIndexOf("/") + 1);
27                 var courseName = statement.Object.Definition.Name.EnUs;
28                 bool done;
29
30                 //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.
31                 //elucidat tracks all users globally by email address so a tincan statement won't identify a specific purchase here 
32                 //unless we create unique email addy (e.g. email+ticket@mail.com) when originally logging them in to content
33                 var course = BaseController.Database.PurchasedCourses.FirstOrDefault(c =>
34                     c.Email == email && c.ContentUrl == courseUrl && c.CertificateDate == null);
35                 if (course == null)
36                 {
37                     Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} no action 1, cert already earned email={1} courseName={2}", dtNow, email, courseName);
38                     //no action, cert already earned
39                     //try
40                     //{
41                     //    course = BaseController.Database.PurchasedCourses.FirstOrDefault(c => c.Email == email && c.ContentUrl == courseUrl && c.CertificateDate!=null);
42                     //    if (course == null)
43                     //    {
44                     //        Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} no action, course == null, select courseName={1} from db with CertificateDate.", dtNow, courseName);
45                     //    }
46
47                     //    Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} no action, select courseName={1} from db with CertificateDate. course={2}", dtNow,  courseName, JsonConvert.SerializeObject(course));
48
49                     //    done = SendEmailHelper.SendCertificateEmail(course);
50
51                     //    Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} no action, send courseName={1} from db to email={2} successfully", dtNow, courseName, email);
52                     //}
53                     //catch (Exception exception)
54                     //{
55                     //    var innerException = "";
56                     //    if (exception.InnerException != null) innerException = exception.InnerException.ToString();
57                     //    Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} email={1} courseName={2} exception={3}", dtNow, email, courseName, exception.ToString()+ innerException);
58                     //}
59
60                     //Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} cert sent email={1} courseName={2}", dtNow, email, courseName);
61
62                     return true;
63                 }
64
65                 course.CertificateDate = Convert.ToDateTime(statement.Timestamp);
66                 try
67                 {
68                     BaseController.Database.SubmitChanges();
69                 }
70                 catch (Exception exception)
71                 {
72                     Utilities.LogWrapper.Error("[TinCanHelper][HandleStatement] {0} email={1} courseName={2} exception={3}", dtNow, email, courseName, exception.ToString());
73                 }
74
75                 done = SendEmailHelper.SendCertificateEmail(course);
76
77                 Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} cert sent email={1} courseName={2}", dtNow, email, courseName);
78
79                 //umm why am i not returning done here? 
80                 return true;
81             }
82
83
84             if (statement.Verb.Id.IndexOf("failed") > -1)
85             {
86                 Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} statement.Verb.Id={1}. statement={2}", dtNow, statement.Verb.Id, JsonConvert.SerializeObject(statement));
87
88                 //parse statement to db variables
89                 var email = statement.Actor.Mbox.Substring(7);
90                 var fname = statement.Actor.Name.Substring(0, statement.Actor.Name.IndexOf(" ") + 1);
91                 var lname = statement.Actor.Name.Substring(statement.Actor.Name.LastIndexOf(" ") + 1);
92                 var courseUrl = statement.Object.Id.Substring(statement.Object.Id.LastIndexOf("/") + 1);
93                 var courseName = statement.Object.Definition.Name.EnUs;
94
95                 //only logging failed attempts prior to earning a cert
96                 //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
97
98                 var course = BaseController.Database.PurchasedCourses.FirstOrDefault(c => c.Email == email && c.ContentUrl == courseUrl && c.CertificateDate == null && ((c.PurchaseDate).AddYears(1)) > DateTime.UtcNow);
99                 if (course == null)
100                 {
101                     //no action, cert already earned
102                     return true;
103                 }
104
105                 Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} course.Ticket={1}", dtNow, course.Ticket);
106
107                 if (!course.FailedAttempts.HasValue)
108                 {
109                     course.FailedAttempts = 0;
110                 }
111                 course.FailedAttempts++;
112                 try
113                 {
114                     BaseController.Database.SubmitChanges();
115                 }
116                 catch (Exception exception)
117                 {
118                     Utilities.LogWrapper.Error("[TinCanHelper][HandleStatement] {0} email={1} courseName={2} exception={3}", dtNow, email, courseName, exception.ToString());
119                 }
120
121                 Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} failure recorded email={1} courseName={2}", dtNow, email, courseName);
122
123                 return true;
124             }
125             return true;
126         }
127     }
128 }