-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLMS._class.Course.pas
453 lines (378 loc) · 10.7 KB
/
LMS._class.Course.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
unit LMS._class.Course;
interface
uses
System.Classes,
Generics.Collections,
LMS._interface.LMS;
type
TLMSCourse = class(TInterfacedObject, ICourse)
strict private
fdisplayname: string;
fEndDate: TDateTime;
fFullName: string;
fGradeItems: TList<IGradeItem>;
fGroupMode: cardinal;
fid: cardinal;
fLMS: ILMS;
fSections: TList<ISection>;
fshortname: string;
fStartDate: TDateTime;
fTimeCreated: TDateTime;
fTimeModified: TDateTime;
// All course groups
fUserGroups: TList<IUsersGroup>;
// All users enrolled in this course
fUsers: TList<IUser>;
function GetCategory: ICategory;
function GetDisplayContent: string;
function GetDisplayName: string;
function GetEndDate: TDateTime;
function getFilterContent: string;
function GetFullName: string;
function GetGradeItems: TList<IGradeItem>;
function GetGroupMode: cardinal;
function getId: cardinal;
function GetLMS: ILMS;
function GetSections: TList<ISection>;
function GetShortName: string;
function GetStartDate: TDateTime;
function GetTimeCreated: TDateTime;
function GetTimeModified: TDateTime;
function GetUserGroups: TList<IUsersGroup>;
function GetUsers: TList<IUser>;
procedure RefreshUserGroups;
procedure SetDisplayName(const Value: string);
procedure SetEndDate(const Value: TDateTime);
procedure SetFullName(const Value: string);
procedure SetGroupMode(const Value: cardinal);
procedure SetId(const Value: cardinal);
procedure SetShortName(const Value: string);
procedure SetStartDate(const Value: TDateTime);
procedure SetTimeCreated(const Value: TDateTime);
procedure SetTimeModified(const Value: TDateTime);
procedure SetUserGroups(const Value: TList<IUsersGroup>);
procedure SetUsers(const Value: TList<IUser>);
public
constructor Create(const LMS: ILMS);
destructor Destroy; override;
procedure GetCourseContent;
procedure GetCourseRoles(aCourseRoles: TStringlist);
procedure GetGradeBook;
function GetUserCountByRol(const aRole: string): cardinal;
procedure RefreshEnrolledUsers;
// Returns the apropiated text to show information about courses
property DisplayContent: string read GetDisplayContent;
property DisplayName: string read GetDisplayName write SetDisplayName;
property End_Date: TDateTime read GetEndDate write SetEndDate;
// Return the course information that can be filtered from
property FilterContent: string read getFilterContent;
property FullName: string read GetFullName write SetFullName;
property GroupMode: cardinal read GetGroupMode write SetGroupMode;
property Id: cardinal read getId write SetId;
// Pointer to the LMS parent
property LMS: ILMS read GetLMS;
property shortname: string read GetShortName write SetShortName;
property Start_Date: TDateTime read GetStartDate write SetStartDate;
property Time_Created: TDateTime read GetTimeCreated write SetTimeCreated;
property Time_Modified: TDateTime read GetTimeModified write SetTimeModified;
// All course groups
property UserGroups: TList<IUsersGroup> read GetUserGroups write SetUserGroups;
property Users: TList<IUser> read GetUsers write SetUsers;
end;
implementation
uses
System.JSON,
System.SysUtils,
LMS.Helper.Log,
LMS._class.GradeItem,
LMS._class.User,
LMS._class.Section,
LMS._class.Module,
LMS._class.Content,
LMS._class.UsersGroup;
constructor TLMSCourse.Create(const LMS: ILMS);
begin
inherited Create;
fUsers := TList<IUser>.Create;
fUserGroups := TList<IUsersGroup>.Create;
fGradeItems := TList<IGradeItem>.Create;
fSections := TList<ISection>.Create;
fLMS := LMS;
end;
destructor TLMSCourse.Destroy;
begin
fSections.free;
fGradeItems.free;
fUserGroups.free;
fUsers.free;
inherited;
end;
function TLMSCourse.GetCategory: ICategory;
var
cat: ICategory;
cour: ICourse;
begin
result := nil;
for cat in fLMS.Categories do
begin
for cour in cat.Courses do
if (cour.Id = Id) then
begin
result := cat;
break;
end;
end;
end;
procedure TLMSCourse.GetCourseContent;
var
aSectionItems, aModuleItems, aContentItems: TJSonArray;
aSection: ISection;
aModule: IModule;
aContent: IContent;
aContentFileType: string;
aJsonSection, aJsonModule, aJsonContent: TJSONValue;
aContentsInfo: TJsonObject;
aMimeType: string;
begin
fSections.clear;
aSectionItems := fLMS.GetLMSConnection.GetCourseContent(fid);
for aJsonSection in aSectionItems do
begin
aSection := TSection.Create(self);
aSection.Name := aJsonSection.GetValue<string>('name');
aModuleItems := aJsonSection.GetValue<TJSonArray>('modules');
for aJsonModule in aModuleItems do
begin
aModule := tmodule.Create(aSection);
aModule.Name := aJsonModule.GetValue<string>('name');
aModule.ModName := aJsonModule.GetValue<string>('modname');
if aJsonModule.TryGetValue<TJsonObject>('contentsinfo', aContentsInfo) then
begin
// here we have the all contents info
end;
if aJsonModule.TryGetValue<TJSonArray>('contents', aContentItems) then
for aJsonContent in aContentItems do
begin
aContent := TContent.Create(aModule);
aContent.FileType := aJsonContent.GetValue<string>('type');
aContent.FileName := aJsonContent.GetValue<string>('filename');
if aJsonContent.TryGetValue<string>('mimetype', aMimeType) then
aContent.MimeType := aMimeType;
aContent.fileurl := aJsonContent.GetValue<string>('fileurl');
aModule.Contents.add(aContent);
end;
aSection.Modules.add(aModule);
end;
fSections.add(aSection);
end;
// log(aSectionItems.ToString);
end;
procedure TLMSCourse.GetCourseRoles(aCourseRoles: TStringlist);
var
aUser: IUser;
begin
for aUser in fUsers do
if aCourseRoles.IndexOf(aUser.Roles) < 0 then
aCourseRoles.add(aUser.Roles);
end;
function TLMSCourse.GetDisplayContent: string;
begin
result := format('%s - %s - (%d)', [shortname, DisplayName, Id]);
end;
function TLMSCourse.GetDisplayName: string;
begin
result := fdisplayname;
end;
function TLMSCourse.GetEndDate: TDateTime;
begin
result := fEndDate;
end;
function TLMSCourse.getFilterContent: string;
begin
result := shortname + ' ' + FullName + ' ' + DisplayName + ' ' + Id.ToString;
end;
function TLMSCourse.GetFullName: string;
begin
result := fFullName;
end;
procedure TLMSCourse.GetGradeBook;
var
aUsers, aGradeItems: TJSonArray;
aUser, aGradeItem: TJSONValue;
aGradeItemC: TGradeItem;
begin
fGradeItems.clear;
aGradeItems := fLMS.GetLMSConnection.GetUsersGradeBook(self.fid);
end;
function TLMSCourse.GetGradeItems: TList<IGradeItem>;
begin
result := fGradeItems;
end;
function TLMSCourse.GetGroupMode: cardinal;
begin
result := fGroupMode;
end;
function TLMSCourse.getId: cardinal;
begin
result := fid;
end;
function TLMSCourse.GetLMS: ILMS;
begin
result := fLMS;
end;
function TLMSCourse.GetSections: TList<ISection>;
begin
result := fSections;
end;
function TLMSCourse.GetShortName: string;
begin
result := fshortname;
end;
function TLMSCourse.GetStartDate: TDateTime;
begin
result := fStartDate;
end;
function TLMSCourse.GetTimeCreated: TDateTime;
begin
result := fTimeCreated;
end;
function TLMSCourse.GetTimeModified: TDateTime;
begin
result := fTimeModified;
end;
function TLMSCourse.GetUserCountByRol(const aRole: string): cardinal;
var
aUser: IUser;
begin
result := 0;
for aUser in fUsers do
begin
if aUser.Roles = aRole then
inc(result);
end;
end;
function TLMSCourse.GetUserGroups: TList<IUsersGroup>;
begin
result := fUserGroups;
end;
function TLMSCourse.GetUsers: TList<IUser>;
begin
result := fUsers;
end;
procedure TLMSCourse.RefreshEnrolledUsers;
var
aUser: IUser;
aUsers: TJSonArray;
User: TJSONValue;
groups: TJSonArray;
Group: TJSONValue;
rol: TJSONValue;
arealrol: string;
begin
// Populate user groups first
RefreshUserGroups;
fUsers.clear;
aUsers := fLMS.aLMSConnection.GetEnrolledUsersByCourseId(self.Id);
if aUsers <> nil then
begin
// log(aUsers.ToString);
for User in aUsers do
begin
aUser := TUser.Create(fLMS, User);
aUser.Course := self;
// Get user roles
for rol in User.GetValue<TJSonArray>('roles') do
begin
arealrol := rol.GetValue<string>('name');
if arealrol = '' then
aUser.Roles := aUser.Roles + rol.GetValue<string>('shortname')
else
aUser.Roles := aUser.Roles + arealrol;
end;
// Add user to users list
fUsers.add(aUser);
// Include the user in the corresponding group
if User.TryGetValue<TJSonArray>('groups', groups) then
begin
for Group in groups do
begin
for var agroup in self.fUserGroups do
begin
if agroup.Id = Group.GetValue<cardinal>('id') then
agroup.UsersInGroup.add(aUser)
end;
end;
end;
//
end;
end;
end;
procedure TLMSCourse.RefreshUserGroups;
var
aUserGroups: TJSonArray;
agroup: TJSONValue;
aUserGroup: IUsersGroup;
begin
fUserGroups.clear;
aUserGroups := fLMS.aLMSConnection.GetUserGroupsByCourseId(self.Id);
if aUserGroups <> nil then
begin
// log(aUserGroups.ToString);
for agroup in aUserGroups do
begin
aUserGroup := TUsersGroup.Create;
aUserGroup.Id := agroup.GetValue<cardinal>('id');
aUserGroup.Group_Name := agroup.GetValue<string>('name');
{ aUser.fFirstName := User.GetValue<string>('firstname');
aUser.fLastName := User.GetValue<string>('lastname');
aUser.fFullName := User.GetValue<string>('fullname');
}
fUserGroups.add(aUserGroup)
end;
end;
end;
procedure TLMSCourse.SetDisplayName(const Value: string);
begin
fdisplayname := Value;
end;
procedure TLMSCourse.SetEndDate(const Value: TDateTime);
begin
fEndDate := Value;
end;
procedure TLMSCourse.SetFullName(const Value: string);
begin
fFullName := Value;
end;
procedure TLMSCourse.SetGroupMode(const Value: cardinal);
begin
fGroupMode := Value;
end;
procedure TLMSCourse.SetId(const Value: cardinal);
begin
fid := Value;
end;
procedure TLMSCourse.SetShortName(const Value: string);
begin
fshortname := Value;
end;
procedure TLMSCourse.SetStartDate(const Value: TDateTime);
begin
fStartDate := Value;
end;
procedure TLMSCourse.SetTimeCreated(const Value: TDateTime);
begin
fTimeCreated := Value;
end;
procedure TLMSCourse.SetTimeModified(const Value: TDateTime);
begin
fTimeModified := Value;
end;
procedure TLMSCourse.SetUserGroups(const Value: TList<IUsersGroup>);
begin
fUserGroups := Value;
end;
procedure TLMSCourse.SetUsers(const Value: TList<IUser>);
begin
fUsers := Value;
end;
end.