Git Repository Public Repository

CPE_learningsite

URLs

Copy to Clipboard

This repository has no backups
This repository's network speed is throttled to 100KB/sec

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using CPE.App.Api.Models;
using CPE.App.Web.Code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;

namespace CPE.App.Api.Helpers
{
    public class TinCanHelper
    {
        public static bool HandleStatement(TinCanStatementModel statement)
        {
            // statement.Verb.Id=http://adlnet.gov/expapi/verbs/answered
            // statement.Verb.Id=http://adlnet.gov/expapi/verbs/experienced
            var dtNow = DateTime.UtcNow.ToString("yyyyMMdd_HHmmss");
            if (statement.Verb.Id.IndexOf("passed")>-1)
            {
                Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} statement.Verb.Id={1}. statement={2}", dtNow, statement.Verb.Id, JsonConvert.SerializeObject(statement));

                //parse statement to db variables
                var email = statement.Actor.Mbox.Substring(7);
                var fname = statement.Actor.Name.Substring(0, statement.Actor.Name.IndexOf(" ") + 1);
                var lname = statement.Actor.Name.Substring(statement.Actor.Name.LastIndexOf(" ") + 1);
                var courseUrl = statement.Object.Id.Substring(statement.Object.Id.LastIndexOf("/") + 1);
                var courseName = statement.Object.Definition.Name.EnUs;
                bool done;

                //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.
                //elucidat tracks all users globally by email address so a tincan statement won't identify a specific purchase here 
                //unless we create unique email addy (e.g. email+ticket@mail.com) when originally logging them in to content
                var course = BaseController.Database.PurchasedCourses.FirstOrDefault(c =>
                    c.Email == email && c.ContentUrl == courseUrl && c.CertificateDate == null);
                if (course == null)
                {
                    Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} no action 1, cert already earned email={1} courseName={2}", dtNow, email, courseName);
                    //no action, cert already earned
                    //try
                    //{
                    //    course = BaseController.Database.PurchasedCourses.FirstOrDefault(c => c.Email == email && c.ContentUrl == courseUrl && c.CertificateDate!=null);
                    //    if (course == null)
                    //    {
                    //        Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} no action, course == null, select courseName={1} from db with CertificateDate.", dtNow, courseName);
                    //    }

                    //    Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} no action, select courseName={1} from db with CertificateDate. course={2}", dtNow,  courseName, JsonConvert.SerializeObject(course));

                    //    done = SendEmailHelper.SendCertificateEmail(course);

                    //    Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} no action, send courseName={1} from db to email={2} successfully", dtNow, courseName, email);
                    //}
                    //catch (Exception exception)
                    //{
                    //    var innerException = "";
                    //    if (exception.InnerException != null) innerException = exception.InnerException.ToString();
                    //    Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} email={1} courseName={2} exception={3}", dtNow, email, courseName, exception.ToString()+ innerException);
                    //}

                    //Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} cert sent email={1} courseName={2}", dtNow, email, courseName);

                    return true;
                }

                course.CertificateDate = Convert.ToDateTime(statement.Timestamp);
                try
                {
                    BaseController.Database.SubmitChanges();
                }
                catch (Exception exception)
                {
                    Utilities.LogWrapper.Error("[TinCanHelper][HandleStatement] {0} email={1} courseName={2} exception={3}", dtNow, email, courseName, exception.ToString());
                }

                done = SendEmailHelper.SendCertificateEmail(course);

                Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} cert sent email={1} courseName={2}", dtNow, email, courseName);

                //umm why am i not returning done here? 
                return true;
            }


            if (statement.Verb.Id.IndexOf("failed") > -1)
            {
                Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} statement.Verb.Id={1}. statement={2}", dtNow, statement.Verb.Id, JsonConvert.SerializeObject(statement));

                //parse statement to db variables
                var email = statement.Actor.Mbox.Substring(7);
                var fname = statement.Actor.Name.Substring(0, statement.Actor.Name.IndexOf(" ") + 1);
                var lname = statement.Actor.Name.Substring(statement.Actor.Name.LastIndexOf(" ") + 1);
                var courseUrl = statement.Object.Id.Substring(statement.Object.Id.LastIndexOf("/") + 1);
                var courseName = statement.Object.Definition.Name.EnUs;

                //only logging failed attempts prior to earning a cert
                //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

                var course = BaseController.Database.PurchasedCourses.FirstOrDefault(c => c.Email == email && c.ContentUrl == courseUrl && c.CertificateDate == null && ((c.PurchaseDate).AddYears(1)) > DateTime.UtcNow);
                if (course == null)
                {
                    //no action, cert already earned
                    return true;
                }

                Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} course.Ticket={1}", dtNow, course.Ticket);

                if (!course.FailedAttempts.HasValue)
                {
                    course.FailedAttempts = 0;
                }
                course.FailedAttempts++;
                try
                {
                    BaseController.Database.SubmitChanges();
                }
                catch (Exception exception)
                {
                    Utilities.LogWrapper.Error("[TinCanHelper][HandleStatement] {0} email={1} courseName={2} exception={3}", dtNow, email, courseName, exception.ToString());
                }

                Utilities.LogWrapper.Info("[TinCanHelper][HandleStatement] {0} failure recorded email={1} courseName={2}", dtNow, email, courseName);

                return true;
            }
            return true;
        }
    }
}

Commits for CPE_learningsite/CPE/CPE.App/CPE.App.Api/Helpers/TinCanHelper.cs

Diff revisions: vs.
Revision Author Commited Message
d90493 ... Diff Diff v.shishlov Sun 29 Aug, 2021 15:28:47 +0000

add ssl 1.2

4cd176 ... v.shishlov Fri 27 Aug, 2021 14:33:17 +0000

initial commit