2020use App \Security \ACL \IShadowAssignmentPermissions ;
2121use App \Security \ACL \IGroupPermissions ;
2222use Doctrine \Common \Collections \Collection ;
23+ use Nette \Utils \Arrays ;
2324
2425
2526/**
@@ -63,19 +64,14 @@ public function __construct(AssignmentSolutions $assignmentSolutions,
6364
6465 /**
6566 * Get total sum of points which given user gained in given solutions.
66- * @param Pair [] $solutions list of pairs, second item is solution, can be null
67+ * @param AssignmentSolution [] $solutions list of assignment solutions
6768 * @return int
6869 */
6970 private function getPointsGainedByStudentForSolutions (array $ solutions ) {
7071 return array_reduce (
7172 $ solutions ,
72- function ($ carry , Pair $ solutionPair ) {
73- $ best = $ solutionPair ->value ;
74- if ($ best !== null ) {
75- $ carry += $ best ->getTotalPoints ();
76- }
77-
78- return $ carry ;
73+ function ($ carry , AssignmentSolution $ solution ) {
74+ return $ carry + $ solution ->getTotalPoints ();
7975 },
8076 0
8177 );
@@ -88,12 +84,8 @@ function ($carry, Pair $solutionPair) {
8884 */
8985 private function getPointsForShadowAssignments (array $ shadowPointsList ): int {
9086 return array_reduce ($ shadowPointsList ,
91- function ($ carry , Pair $ pointsPair ) {
92- $ points = $ pointsPair ->value ;
93- if ($ points !== null ) {
94- $ carry += $ points ->getPoints ();
95- }
96- return $ carry ;
87+ function ($ carry , ShadowAssignmentPoints $ points ) {
88+ return $ carry + $ points ->getPoints ();
9789 },
9890 0 );
9991 }
@@ -107,18 +99,16 @@ function ($carry, Pair $pointsPair) {
10799 public function getStudentsStats (Group $ group , User $ student ) {
108100 $ maxPoints = $ group ->getMaxPoints ();
109101 $ assignmentSolutions = $ this ->assignmentSolutions ->findBestSolutionsForAssignments ($ group ->getAssignments ()->getValues (), $ student );
110- $ shadowPointsList = $ this ->shadowAssignmentPointsRepository ->findPointsForAssignments ($ group ->getShadowAssignments ()->getValues (), $ student );
102+ $ shadowPointsMap = $ this ->shadowAssignmentPointsRepository ->findPointsForAssignments ($ group ->getShadowAssignments ()->getValues (), $ student );
111103 $ gainedPoints = $ this ->getPointsGainedByStudentForSolutions ($ assignmentSolutions );
112- $ gainedPoints += $ this ->getPointsForShadowAssignments ($ shadowPointsList );
104+ $ gainedPoints += $ this ->getPointsForShadowAssignments ($ shadowPointsMap );
113105
114106 $ assignments = [];
115- foreach ($ assignmentSolutions as $ solutionPair ) {
107+ foreach ($ group -> getAssignments ()-> getValues () as $ assignment ) {
116108 /**
117109 * @var Assignment $assignment
118- * @var AssignmentSolution $best
119110 */
120- $ assignment = $ solutionPair ->key ;
121- $ best = $ solutionPair ->value ;
111+ $ best = Arrays::get ($ assignmentSolutions , $ assignment ->getId (), null );
122112 $ submission = $ best ? $ best ->getLastSubmission () : null ;
123113
124114 $ assignments [] = [
@@ -133,14 +123,16 @@ public function getStudentsStats(Group $group, User $student) {
133123 }
134124
135125 $ shadowAssignments = [];
136- foreach ($ shadowPointsList as $ pointsPair ) {
137- $ assignment = $ pointsPair ->key ;
138- $ shadowPoints = $ pointsPair ->value ;
126+ foreach ($ group ->getShadowAssignments ()->getValues () as $ shadowAssignment ) {
127+ /**
128+ * @var ShadowAssignment $shadowAssignment
129+ */
130+ $ shadowPoints = Arrays::get ($ shadowPointsMap , $ shadowAssignment ->getId (), null );
139131
140132 $ shadowAssignments [] = [
141- "id " => $ assignment ->getId (),
133+ "id " => $ shadowAssignment ->getId (),
142134 "points " => [
143- "total " => $ assignment ->getMaxPoints (),
135+ "total " => $ shadowAssignment ->getMaxPoints (),
144136 "gained " => $ shadowPoints ? $ shadowPoints ->getPoints () : null
145137 ]
146138 ];
0 commit comments