-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLMS.TreeView.CourseCategory.pas
347 lines (288 loc) · 8.12 KB
/
LMS.TreeView.CourseCategory.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
unit LMS.TreeView.CourseCategory;
interface
uses
System.SysUtils,
System.Classes,
System.Types,
System.UITypes,
vcl.Menus,
winapi.messages,
VirtualTrees,
LMS._interface.LMS,
LMS.TreeView.Custom;
type
TLMSCategoryTreeView = class(TLMSCustomLMSVirtualStringTree)
private
fLMSCategory: ICategory;
function GetSelectedUser: IUser;
procedure setLMSCategory(const Value: ICategory);
protected
procedure DoInitNode(Parent, Node: PVirtualNode;
var InitStates: TVirtualNodeInitStates); override;
procedure MyDoGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
Column: TColumnIndex; TextType: TVSTTextType; var CellText: string);
procedure MyDoInitChildren(Sender: TBaseVirtualTree; Node: PVirtualNode;
var ChildCount: cardinal);
procedure NodeClick(Sender: TBaseVirtualTree; const HitInfo: THitInfo);
public
constructor Create(Owner: TComponent); override;
procedure FilterByText(const text: string);
property Category: ICategory read fLMSCategory write setLMSCategory;
property SelectedUser: IUser read GetSelectedUser;
end;
implementation
uses
vcl.Graphics,
vcl.ImgList, windows,
dialogs,
ShellApi,
generics.Collections,
LMS.Helper.Consts,
LMS.Helper.RTTI,
LMS.Helper.Browser,
LMS.Helper.Utils,
LMS.Form.Course;
constructor TLMSCategoryTreeView.Create(Owner: TComponent);
begin
inherited;
OnGetText := MyDoGetText;
OnInitChildren := MyDoInitChildren;
OnNodeClick := NodeClick;
end;
procedure TLMSCategoryTreeView.DoInitNode(Parent, Node: PVirtualNode;
var InitStates: TVirtualNodeInitStates);
var
data, parentdata: PTreeData;
begin
data := GetNodeData(Node);
parentdata := GetNodeData(Parent);
begin
if parentdata = nil then
begin
data^.node_type := ntCourse;
data^.Course := fLMSCategory.Courses[Node.Index];
data^.Course.RefreshEnrolledUsers;
if (data^.Course.UserGroups.count > 0) or (data^.Course.Users.count > 0)
then
begin
Include(Node.States, vsHasChildren);
Include(Node.States, vsExpanded);
end
end
else if parentdata.node_type = ntCourse then
begin
if (parentdata.Course <> nil) and (parentdata.Course.UserGroups.count > 0)
then
begin
data^.node_type := ntGroup;
data^.Group := parentdata.Course.UserGroups[Node.Index];
Include(Node.States, vsHasChildren);
Include(Node.States, vsExpanded);
end
// Just users list
else
begin
data^.node_type := ntUser;
data^.User := parentdata.Course.Users[Node.Index];
Exclude(Node.States, vsHasChildren);
end;
{ if (data^.aLMS.autoconnect) then
Include(Node.States, vsExpanded); }
end
else if parentdata.node_type = ntGroup then
begin
data^.node_type := ntUser;
data^.User := parentdata.Group.UsersInGroup[Node.Index];
Exclude(Node.States, vsHasChildren);
end;
// Has a group
{ *if (LMSCourse <> nil) and (LMSCourse.fUserGroups.count > 0) then
begin
data^.node_type := ntGroup;
data^.Group := fLMSCourse.fUserGroups[Node.Index];
Include(Node.States, vsHasChildren);
Include(Node.States, vsExpanded);
end
// Just users list
else
begin
data^.node_type := ntUser;
data^.User := fLMSCourse.fUsers[Node.Index];
Exclude(Node.States, vsHasChildren);
end;
* }
{ if (data^.aLMS.autoconnect) then
Include(Node.States, vsExpanded); }
end
end;
procedure TLMSCategoryTreeView.FilterByText(const text: string);
var
aVirtualNodeEnumerator: TVTVirtualNodeEnumerator;
data: PTreeData;
aCompare: string;
aParent: PVirtualNode;
begin
aVirtualNodeEnumerator := initializednodes().GetEnumerator;
// Not paint until finished
BeginUpdate;
while aVirtualNodeEnumerator.MoveNext do
begin
data := GetNodeData(aVirtualNodeEnumerator.Current);
case data^.node_type of
ntGroup:
aCompare := data^.Group.FilterContent;
ntUser:
aCompare := data^.User.FilterContent;
end;
if (Pos(UpperCase(text), UpperCase(aCompare)) > 0) or (text = '') then
begin
IsVisible[aVirtualNodeEnumerator.Current] := true;
aParent := aVirtualNodeEnumerator.Current.Parent;
while RootNode <> aParent do
begin
IsVisible[aParent] := true;
aParent := aParent.Parent;
end;
end
else
IsVisible[aVirtualNodeEnumerator.Current] := false;
end;
// Please refresh
EndUpdate;
end;
function TLMSCategoryTreeView.GetSelectedUser: IUser;
var
aVirtualNodeEnumerator: TVTVirtualNodeEnumerator;
data: PTreeData;
begin
result := nil;
aVirtualNodeEnumerator := SelectedNodes().GetEnumerator;
while aVirtualNodeEnumerator.MoveNext do
begin
data := GetNodeData(aVirtualNodeEnumerator.Current);
if data^.node_type = ntUser then
begin
result := data^.User;
// refactor ... exit if not more
end;
end;
end;
procedure TLMSCategoryTreeView.MyDoGetText(Sender: TBaseVirtualTree;
Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
var CellText: string);
var
data: PTreeData;
begin
data := GetNodeData(Node);
case data^.node_type of
ntCourse:
if Column = 0 then
CellText := data^.Course.displayname
else
CellText := '';
ntGroup:
if Column = 1 then
CellText := data^.Group.Group_Name
else
CellText := '';
ntUser:
CellText := GetPropertyValue(tobject(data^.User),
TextToPropertyName(Header.Columns[Column].text));
end;
end;
procedure TLMSCategoryTreeView.MyDoInitChildren(Sender: TBaseVirtualTree;
Node: PVirtualNode; var ChildCount: cardinal);
var
data: PTreeData;
begin
data := GetNodeData(Node);
if data <> nil then
begin
case data^.node_type of
ntCourse:
if data^.Course.UserGroups.count > 0 then
ChildCount := data^.Course.UserGroups.count
else
ChildCount := data^.Course.Users.count;
ntGroup:
ChildCount := data^.Group.UsersInGroup.count;
end;
end;
end;
procedure TLMSCategoryTreeView.NodeClick(Sender: TBaseVirtualTree;
const HitInfo: THitInfo);
var
aVirtualNodeEnumerator: TVTVirtualNodeEnumerator;
data: PTreeData;
CtrlPressed: boolean;
// ShiftPressed: boolean;
begin
CtrlPressed := (GetKeyState(VK_CONTROL) and $8000) = $8000;
// ShiftPressed := (GetKeyState(VK_SHIFT) and $8000) = $8000;
aVirtualNodeEnumerator := SelectedNodes.GetEnumerator;
while aVirtualNodeEnumerator.MoveNext do
begin
data := GetNodeData(aVirtualNodeEnumerator.Current);
case data^.node_type of
ntUser:
begin
if CtrlPressed then
begin
OpenInBrowser(data^.User, data^.User.Course);
end
else
{ with TLMSForm.Create(self) do
begin
LMS := data^.aLMS;
show();
end; }
end;
end;
end;
end;
procedure TLMSCategoryTreeView.setLMSCategory(const Value: ICategory);
begin
fLMSCategory := Value;
Header.Columns.Clear;
with Header do
with Columns.add do
begin
text := 'Course';
Options := Options + [coAutoSpring, coResizable, coSmartResize];
end;
with Header do
with Columns.add do
begin
text := 'Group';
Options := Options + [coAutoSpring, coResizable, coSmartResize];
end;
with Header do
begin
with Columns.add do
begin
text := 'Full name';
Options := Options + [coAutoSpring, coResizable, coSmartResize];
end;
with Columns.add do
begin
text := 'Email';
Options := Options + [coAutoSpring, coResizable, coEditable];
end;
with Columns.add do
begin
text := 'Roles';
Options := Options + [coAutoSpring, coResizable];
end;
with Columns.add do
begin
text := 'Last access';
Options := Options + [coAutoSpring, coResizable];
end;
Options := Options + [hovisible, hoAutoSpring, hoAutoResize,
hoDblClickResize];
AutoSizeIndex := Columns.GetLastVisibleColumn;
RootNodeCount := fLMSCategory.CoursesCount;
end;
Header.AutoFitColumns(false, smaAllColumns, 0);
end;
end.