1 using CPE.App.Api.Models;
2 using CPE.App.Web.Code;
4 using System.Collections.Generic;
9 namespace CPE.App.Api.Helpers
11 public class TinCanHelper
13 public static bool HandleStatement(TinCanStatementModel statement)
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)
20 Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} statement.Verb.Id={1}. statement={2}", dtNow, statement.Verb.Id, JsonConvert.SerializeObject(statement));
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;
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);
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
41 // course = BaseController.Database.PurchasedCourses.FirstOrDefault(c => c.Email == email && c.ContentUrl == courseUrl && c.CertificateDate!=null);
42 // if (course == null)
44 // Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} no action, course == null, select courseName={1} from db with CertificateDate.", dtNow, courseName);
47 // Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} no action, select courseName={1} from db with CertificateDate. course={2}", dtNow, courseName, JsonConvert.SerializeObject(course));
49 // done = SendEmailHelper.SendCertificateEmail(course);
51 // Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} no action, send courseName={1} from db to email={2} successfully", dtNow, courseName, email);
53 //catch (Exception exception)
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);
60 //Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} cert sent email={1} courseName={2}", dtNow, email, courseName);
65 course.CertificateDate = Convert.ToDateTime(statement.Timestamp);
68 BaseController.Database.SubmitChanges();
70 catch (Exception exception)
72 Utilities.LogWrapper.Error("[TinCanHelper][HandleStatement] {0} email={1} courseName={2} exception={3}", dtNow, email, courseName, exception.ToString());
75 done = SendEmailHelper.SendCertificateEmail(course);
77 Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} cert sent email={1} courseName={2}", dtNow, email, courseName);
79 //umm why am i not returning done here?
84 if (statement.Verb.Id.IndexOf("failed") > -1)
86 Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} statement.Verb.Id={1}. statement={2}", dtNow, statement.Verb.Id, JsonConvert.SerializeObject(statement));
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;
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
98 var course = BaseController.Database.PurchasedCourses.FirstOrDefault(c => c.Email == email && c.ContentUrl == courseUrl && c.CertificateDate == null && ((c.PurchaseDate).AddYears(1)) > DateTime.UtcNow);
101 //no action, cert already earned
105 Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} course.Ticket={1}", dtNow, course.Ticket);
107 if (!course.FailedAttempts.HasValue)
109 course.FailedAttempts = 0;
111 course.FailedAttempts++;
114 BaseController.Database.SubmitChanges();
116 catch (Exception exception)
118 Utilities.LogWrapper.Error("[TinCanHelper][HandleStatement] {0} email={1} courseName={2} exception={3}", dtNow, email, courseName, exception.ToString());
121 Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} failure recorded email={1} courseName={2}", dtNow, email, courseName);