-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLMS._class.User.pas
205 lines (176 loc) · 4.93 KB
/
LMS._class.User.pas
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
unit LMS._class.User;
interface
uses
System.JSON,
Generics.Collections,
LMS._interface.LMS;
type
TUser = class(TInterfacedObject, IUser)
strict private
fCourse: ICourse;
fEmail: string;
fFirstName: string;
fFullName: string;
fid: integer;
flastcourseaccess: TDateTime;
fLastName: string;
fLMS: ILMS;
fOtherEnrolledCourses: TList<ICourse>;
fRoles: string;
fUserName: string;
function GetCourse: ICourse;
function GetEmail: string;
function getFilterContent: string;
function GetFirstName: string;
function GetFullName: string;
function GetId: integer;
function GetLastAccessAsString: string;
function GetLastAccessFromAsString: string;
function GetLastName: string;
function getLMS: ILMS;
function GetOtherEnrolledCourses: TList<ICourse>;
function GetRoles: string;
function GetUserName: string;
procedure SetCourse(const value: ICourse);
procedure SetRoles(const value: string);
public
constructor Create(const LMS: ILMS; const aJSONValue: TJSONValue);
property Course: ICourse read GetCourse write SetCourse;
property Email: string read GetEmail;
property FilterContent: string read getFilterContent;
property First_Name: string read GetFirstName;
property Full_Name: string read GetFullName;
property Id: integer read GetId;
property Last_access: string read GetLastAccessAsString;
property Last_access_from: string read GetLastAccessFromAsString;
property Last_Name: string read GetLastName;
// procedure AssignByJson(const aJsonValue: TJSONValue);
// Properties for view components, do not resource
property LMS: ILMS read getLMS;
property OtherEnrolledCourses: TList<ICourse> read GetOtherEnrolledCourses;
property Roles: string read GetRoles write SetRoles;
property UserName: string read GetUserName;
end;
implementation
uses
DateUtils, sysutils,
LMS.Helper.Utils,
LMS.Helper.Log;
constructor TUser.Create(const LMS: ILMS; const aJSONValue: TJSONValue);
var
timestamp: Int64;
enrolledcourses: TJSONArray;
aOtherCourse: ICourse;
begin
fOtherEnrolledCourses := TList<ICourse>.Create;
fLMS := LMS;
fid := aJSONValue.GetValue<cardinal>('id');
fUserName := aJSONValue.GetValue<string>('username');
fFirstName := aJSONValue.GetValue<string>('firstname');
fLastName := aJSONValue.GetValue<string>('lastname');
fFullName := aJSONValue.GetValue<string>('fullname');
fEmail := aJSONValue.GetValue<string>('email');
// Some rest query does not return some fields so we have to check
if aJSONValue.TryGetValue<Int64>('lastcourseaccess', timestamp) then
flastcourseaccess := unixtodatetime(timestamp);
if aJSONValue.TryGetValue<TJSONArray>('enrolledcourses', enrolledcourses) then
for var acourse in enrolledcourses do
begin
aOtherCourse := fLMS.getCourseById(acourse.GetValue<integer>('id'));
OtherEnrolledCourses.add(aOtherCourse);
end;
end;
function TUser.GetCourse: ICourse;
begin
result := fCourse;
end;
function TUser.GetEmail: string;
begin
result := shadowtext(fEmail);
end;
function TUser.getFilterContent: string;
begin
result := shadowtext(fEmail + ' ' + fFullName);
end;
function TUser.GetFirstName: string;
begin
result := shadowtext(fFirstName);
end;
function TUser.GetFullName: string;
begin
result := shadowtext(fFullName);
end;
function TUser.GetId: integer;
begin
result := fid;
end;
function TUser.GetLastAccessAsString: string;
begin
result := FormatDateTimeNever(flastcourseaccess);
end;
function TUser.GetLastAccessFromAsString: string;
const
DaysPerWeek = 7;
DaysPerMonth = 30;
var
DifInDays: integer;
begin
if datetimetounix(flastcourseaccess) = 0 then
result := 'never'
else
begin
DifInDays := Round(Now - flastcourseaccess);
if DifInDays < DaysPerWeek then
begin
if DifInDays = 1 then
result := 'a day ago'
else if DifInDays > 1 then
result := IntToStr(DifInDays) + ' days ago'
else
result := 'today';
end
else if DifInDays < DaysPerMonth then
begin
if DifInDays div DaysPerWeek = 1 then
result := 'a week ago'
else
result := Format('%d weeks ago', [DifInDays div DaysPerWeek]);
end
else
begin
if DifInDays div DaysPerMonth = 1 then
result := 'a month ago'
else
result := Format('%d months ago', [DifInDays div DaysPerMonth]);
end;
end;
end;
function TUser.GetLastName: string;
begin
result := shadowtext( fLastName);
end;
function TUser.getLMS: ILMS;
begin
result := fLMS;
end;
function TUser.GetOtherEnrolledCourses: TList<ICourse>;
begin
result := fOtherEnrolledCourses;
end;
function TUser.GetRoles: string;
begin
result := fRoles;
end;
function TUser.GetUserName: string;
begin
result := fUserName;
end;
procedure TUser.SetCourse(const value: ICourse);
begin
fCourse := value;
end;
procedure TUser.SetRoles(const value: string);
begin
fRoles := value;
end;
end.