-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGroupViewFactory.php
More file actions
435 lines (400 loc) · 16.3 KB
/
GroupViewFactory.php
File metadata and controls
435 lines (400 loc) · 16.3 KB
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
<?php
namespace App\Model\View;
use App\Helpers\EvaluationStatus\EvaluationStatus;
use App\Helpers\GroupBindings\GroupBindingAccessor;
use App\Helpers\PermissionHints;
use App\Model\Entity\Assignment;
use App\Model\Entity\AssignmentSolution;
use App\Model\Entity\Group;
use App\Model\Entity\GroupExamLock;
use App\Model\Entity\GroupExternalAttribute;
use App\Model\Entity\GroupMembership;
use App\Model\Entity\ShadowAssignment;
use App\Model\Entity\ShadowAssignmentPoints;
use App\Model\Entity\User;
use App\Model\Repository\AssignmentSolutions;
use App\Model\Repository\ShadowAssignmentPointsRepository;
use App\Security\ACL\IAssignmentPermissions;
use App\Security\ACL\IShadowAssignmentPermissions;
use App\Security\ACL\IGroupPermissions;
use Nette\Utils\Arrays;
use Doctrine\Common\Collections\Collection;
/**
* Factory for group views which somehow do not fit into json serialization of
* entities.
*/
class GroupViewFactory
{
/** @var AssignmentSolutions */
private $assignmentSolutions;
/** @var IGroupPermissions */
private $groupAcl;
/** @var IAssignmentPermissions */
private $assignmentAcl;
/** @var IShadowAssignmentPermissions */
private $shadowAssignmentAcl;
/** @var GroupBindingAccessor */
private $bindings;
/** @var ShadowAssignmentPointsRepository */
private $shadowAssignmentPointsRepository;
public function __construct(
AssignmentSolutions $assignmentSolutions,
IGroupPermissions $groupAcl,
IAssignmentPermissions $assignmentAcl,
IShadowAssignmentPermissions $shadowAssignmentAcl,
GroupBindingAccessor $bindings,
ShadowAssignmentPointsRepository $shadowAssignmentPointsRepository
) {
$this->assignmentSolutions = $assignmentSolutions;
$this->groupAcl = $groupAcl;
$this->assignmentAcl = $assignmentAcl;
$this->shadowAssignmentAcl = $shadowAssignmentAcl;
$this->bindings = $bindings;
$this->shadowAssignmentPointsRepository = $shadowAssignmentPointsRepository;
}
/**
* Get total sum of points which given user gained in given solutions.
* @param AssignmentSolution[] $solutions list of assignment solutions
* @return int
*/
private function getPointsGainedByStudentForSolutions(array $solutions)
{
return array_reduce(
$solutions,
function ($carry, AssignmentSolution $solution) {
return $carry + $solution->getTotalPoints();
},
0
);
}
/**
* Get total sum of points which given user gained in given shadow assignments.
* @param ShadowAssignmentPoints[] $shadowPointsList
* @return int
*/
private function getPointsForShadowAssignments(array $shadowPointsList): int
{
return array_reduce(
$shadowPointsList,
function ($carry, ShadowAssignmentPoints $points) {
return $carry + $points->getPoints();
},
0
);
}
/**
* Get assignments of the group which current user can view.
*/
private function getAssignments(Group $group): Collection
{
return $group->getAssignments()->filter(
function (Assignment $assignment) {
return $this->assignmentAcl->canViewDetail($assignment);
}
);
}
/**
* Get shadow assignments of the group which current user can view.
*/
private function getShadowAssignments(Group $group): Collection
{
return $group->getShadowAssignments()->filter(
function (ShadowAssignment $assignment) {
return $this->shadowAssignmentAcl->canViewDetail($assignment);
}
);
}
/**
* Get the statistics of an individual student.
* @param Group $group
* @param User $student Student of this group
* @param array $assignmentSolutions Loaded assignment solutions of the student for all assignments in the group.
* This variable allows us bulk-load optimizations for the solutions.
* @return array Students statistics
*/
private function getStudentStatsInternal(
Group $group,
User $student,
array $assignmentSolutions,
array $reviewRequests
) {
$maxPoints = 0;
$shadowPointsMap = $this->shadowAssignmentPointsRepository->findPointsForAssignments(
$this->getShadowAssignments($group)->getValues(),
$student
);
$gainedPoints = $this->getPointsGainedByStudentForSolutions($assignmentSolutions);
$gainedPoints += $this->getPointsForShadowAssignments($shadowPointsMap);
$studentReviewRequests = $reviewRequests[$student->getId()] ?? [];
$assignments = [];
foreach ($this->getAssignments($group) as $assignment) {
/**
* @var Assignment $assignment
*/
$maxPoints += $assignment->getGroupPoints();
$best = Arrays::get($assignmentSolutions, $assignment->getId(), null);
$submission = $best ? $best->getLastSubmission() : null;
$assignments[] = [
"id" => $assignment->getId(),
"status" => $submission ? EvaluationStatus::getStatus($submission) : null,
"points" => [
"total" => $assignment->getMaxPointsBeforeFirstDeadline(),
"gained" => $best ? $best->getPoints() : null,
"bonus" => $best ? $best->getBonusPoints() : null,
],
"bestSolutionId" => $best ? $best->getId() : null,
"accepted" => $best ? $best->isAccepted() : null,
"reviewRequest" => !empty($studentReviewRequests[$assignment->getId()])
];
}
$shadowAssignments = [];
foreach ($this->getShadowAssignments($group) as $shadowAssignment) {
/**
* @var ShadowAssignment $shadowAssignment
*/
$maxPoints += $shadowAssignment->getGroupPoints();
$shadowPoints = Arrays::get($shadowPointsMap, $shadowAssignment->getId(), null);
$shadowAssignments[] = [
"id" => $shadowAssignment->getId(),
"points" => [
"total" => $shadowAssignment->getMaxPoints(),
"gained" => $shadowPoints ? $shadowPoints->getPoints() : null
]
];
}
$passesLimit = null; // null = no limit
$limit = null;
if ($group->getPointsLimit() !== null && $group->getPointsLimit() > 0) {
$limit = $group->getPointsLimit();
$passesLimit = $gainedPoints >= $limit;
} elseif ($group->getThreshold() !== null && $group->getThreshold() > 0) {
$limit = $maxPoints * $group->getThreshold();
$passesLimit = $gainedPoints >= $limit;
}
return [
"userId" => $student->getId(),
"groupId" => $group->getId(),
"points" => [
"total" => $maxPoints,
"limit" => $limit,
"gained" => $gainedPoints,
],
"hasLimit" => $passesLimit !== null,
"passesLimit" => $passesLimit ?? true,
"assignments" => $assignments,
"shadowAssignments" => $shadowAssignments
];
}
/**
* Get the statistics of an individual student.
* @param Group $group
* @param User $student Student of this group
* @return array Students statistics
*/
public function getStudentsStats(Group $group, User $student)
{
$assignmentSolutions = $this->assignmentSolutions->findBestUserSolutionsForAssignments(
$this->getAssignments($group)->getValues(),
$student
);
$reviewRequestSolutions = $this->assignmentSolutions->findReviewRequestSolutionsIndexed($group, $student);
return $this->getStudentStatsInternal($group, $student, $assignmentSolutions, $reviewRequestSolutions);
}
/**
* Get the statistics of all students.
* @param Group $group
* @return array Statistics for all students of the group
*/
public function getAllStudentsStats(Group $group)
{
$assignmentSolutions = $this->assignmentSolutions->findBestSolutionsForAssignments(
$this->getAssignments($group)->getValues()
);
$reviewRequestSolutions = $this->assignmentSolutions->findReviewRequestSolutionsIndexed($group);
return array_map(
function ($student) use ($group, $assignmentSolutions, $reviewRequestSolutions) {
$solutions = array_key_exists(
$student->getId(),
$assignmentSolutions
) ? $assignmentSolutions[$student->getId()] : [];
return $this->getStudentStatsInternal($group, $student, $solutions, $reviewRequestSolutions);
},
$group->getStudents()->getValues()
);
}
/**
* Get as much group detail info as your permissions grants you.
* @param Group $group
* @param bool $ignoreArchived
* @return array
*/
public function getGroup(Group $group, bool $ignoreArchived = true): array
{
$canView = $this->groupAcl->canViewDetail($group);
$privateData = null;
if ($canView) {
$privateData = [
"admins" => $group->getAdminsIds(),
"supervisors" => $group->getSupervisorsIds(),
"observers" => $group->getObserversIds(),
"students" => $this->groupAcl->canViewStudents($group) ? $group->getStudentsIds() : [],
"instanceId" => $group->getInstance() ? $group->getInstance()->getId() : null,
"hasValidLicence" => $group->hasValidLicense(),
"assignments" => $this->getAssignments($group)->map(
function (Assignment $assignment) {
return $assignment->getId();
}
)->getValues(),
"shadowAssignments" => $this->getShadowAssignments($group)->map(
function (ShadowAssignment $assignment) {
return $assignment->getId();
}
)->getValues(),
"publicStats" => $group->getPublicStats(),
"detaining" => $group->isDetaining(),
"threshold" => $group->getThreshold(),
"pointsLimit" => $group->getPointsLimit(),
"bindings" => $this->bindings->getBindingsForGroup($group),
"examBegin" => $group->hasExamPeriodSet() ? $group->getExamBegin()?->getTimestamp() : null,
"examEnd" => $group->hasExamPeriodSet() ? $group->getExamEnd()?->getTimestamp() : null,
"examLockStrict" => $group->hasExamPeriodSet() ? $group->isExamLockStrict() : null,
"exams" => $group->getExams()->getValues(),
];
}
$childGroups = $group->getChildGroups()->filter(
function (Group $group) use ($ignoreArchived) {
return !($ignoreArchived && $group->isArchived()) && $this->groupAcl->canViewPublicDetail($group);
}
);
return [
"id" => $group->getId(),
"externalId" => $group->getExternalId(),
"organizational" => $group->isOrganizational(),
"exam" => $group->isExam(),
"archived" => $group->isArchived(),
"public" => $group->isPublic(),
"directlyArchived" => $group->isDirectlyArchived(),
"localizedTexts" => $group->getLocalizedTexts()->getValues(),
"primaryAdminsIds" => $group->getPrimaryAdminsIds(),
"parentGroupId" => $group->getParentGroup() ? $group->getParentGroup()->getId() : null,
"parentGroupsIds" => $group->getParentGroupsIds(),
"childGroups" => $childGroups->map(
function (Group $group) {
return $group->getId();
}
)->getValues(),
"privateData" => $privateData,
"permissionHints" => PermissionHints::get($this->groupAcl, $group)
];
}
/**
* Get group data which current user can view about given groups.
* @param Group[] $groups
* @param bool $ignoreArchived
* @return array
*/
public function getGroups(array $groups, bool $ignoreArchived = true): array
{
return array_map(
function (Group $group) use ($ignoreArchived) {
return $this->getGroup($group, $ignoreArchived);
},
$groups
);
}
/**
* Preprocess an array of exam locks based on group ACLs.
* @param Group $group
* @param GroupExamLock[] $locks
* @return array
*/
public function getGroupExamLocks(Group $group, array $locks): array
{
$ips = $this->groupAcl->canViewExamLocksIPs($group);
return array_map(function ($lock) use ($ips) {
$res = $lock->jsonSerialize();
if (!$ips) {
unset($res['remoteAddr']);
}
return $res;
}, $locks);
}
/**
* Get a subset of group data relevant (available) for ReCodEx extensions (plugins).
* @param Group $group to be rendered
* @param array $injectAttributes [service][key] => value
* @param string|null $membershipType GroupMembership::TYPE_* value for user who made the search
* @return array
*/
public function getGroupForExtension(
Group $group,
array $injectAttributes = [],
?string $membershipType = null,
array $adminUsers = [],
): array {
$admins = [];
foreach ($group->getPrimaryAdminsIds() as $id) {
$admins[$id] = null;
if (array_key_exists($id, $adminUsers)) {
$admins[$id] = $adminUsers[$id]->getNameParts(); // an array with name and title components
$admins[$id]['email'] = $adminUsers[$id]->getEmail();
}
}
return [
"id" => $group->getId(),
"parentGroupId" => $group->getParentGroup() ? $group->getParentGroup()->getId() : null,
"admins" => $admins,
"localizedTexts" => $group->getLocalizedTexts()->getValues(),
"organizational" => $group->isOrganizational(),
"exam" => $group->isExam(),
"public" => $group->isPublic(),
"detaining" => $group->isDetaining(),
"attributes" => $injectAttributes,
"membership" => $membershipType,
];
}
/**
* Get a subset of groups data relevant (available) for ReCodEx extensions (plugins).
* @param Group[] $groups
* @param GroupExternalAttribute[] $attributes
* @param GroupMembership[] $memberships GroupMembership[] to filter by
*/
public function getGroupsForExtension(array $groups, array $attributes = [], array $memberships = []): array
{
// create a multi-dimensional map [groupId][attr-service][attr-key] => attr-value
$attributesMap = [];
foreach ($attributes as $attribute) {
$gid = $attribute->getGroup()->getId();
$attributesMap[$gid] = $attributesMap[$gid] ?? [];
$service = $attribute->getService();
$attributesMap[$gid][$service] = $attributesMap[$gid][$service] ?? [];
$key = $attribute->getKey();
$attributesMap[$gid][$service][$key] = $attributesMap[$gid][$service][$key] ?? [];
$attributesMap[$gid][$service][$key][] = $attribute->getValue();
}
// create membership mapping group-id => membership-type
$membershipsMap = [];
foreach ($memberships as $membership) {
$gid = $membership->getGroup()->getId();
$membershipsMap[$gid] = $membership->getType();
}
$admins = [];
foreach ($groups as $group) {
foreach ($group->getPrimaryAdmins() as $admin) {
$admins[$admin->getId()] = $admin;
}
}
return array_map(
function (Group $group) use ($attributesMap, $membershipsMap, $admins) {
$id = $group->getId();
return $this->getGroupForExtension(
$group,
$attributesMap[$id] ?? [],
$membershipsMap[$id] ?? null,
$admins
);
},
$groups
);
}
}