Conditions | 10 |
Paths | 933 |
Total Lines | 73 |
Code Lines | 49 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
48 | public function cacheStatistics() |
||
49 | { |
||
50 | try { |
||
51 | $statistics = []; |
||
52 | $homeworkProblems = $this->problems->sortBy('order_index'); |
||
53 | $users = $this->group->members()->where('role', '>=', 1)->get(); |
||
54 | $userIDArr = $users->pluck('uid'); |
||
55 | $defaultVerdict = []; |
||
56 | |||
57 | foreach ($homeworkProblems as $homeworkProblem) { |
||
58 | $statistics['problems'][] = [ |
||
59 | 'pid' => $homeworkProblem->problem_id, |
||
60 | 'readable_name' => $homeworkProblem->problem->readable_name, |
||
61 | ]; |
||
62 | $defaultVerdict[$homeworkProblem->problem_id] = [ |
||
63 | "icon" => "checkbox-blank-circle-outline", |
||
64 | "color" => "wemd-grey-text" |
||
65 | ]; |
||
66 | } |
||
67 | |||
68 | foreach ($users as $user) { |
||
69 | $statistics['data'][$user->uid] = [ |
||
70 | 'name' => $user->user->name, |
||
71 | 'nick_name' => blank($user->nick_name) ? null : $user->nick_name, |
||
72 | 'solved' => 0, |
||
73 | 'attempted' => 0, |
||
74 | 'verdict' => $defaultVerdict |
||
75 | ]; |
||
76 | } |
||
77 | |||
78 | $endedAt = Carbon::parse($this->ended_at); |
||
79 | |||
80 | foreach ($homeworkProblems as $homeworkProblem) { |
||
81 | $userProbIDArr = []; |
||
82 | |||
83 | foreach ($homeworkProblem->problem->users_latest_submission($userIDArr->diff($userProbIDArr), null, $endedAt, ['Accepted'])->get() as $acceptedRecord) { |
||
84 | $statistics['data'][$acceptedRecord['uid']]['verdict'][$homeworkProblem->problem_id] = [ |
||
85 | "icon" => "checkbox-blank-circle", |
||
86 | "color" => $acceptedRecord['color'] |
||
87 | ]; |
||
88 | $statistics['data'][$acceptedRecord['uid']]['solved']++; |
||
89 | $userProbIDArr[] = $acceptedRecord['uid']; |
||
90 | } |
||
91 | |||
92 | foreach ($homeworkProblem->problem->users_latest_submission($userIDArr->diff($userProbIDArr), null, $endedAt, ['Partially Accepted'])->get() as $acceptedRecord) { |
||
93 | $statistics['data'][$acceptedRecord['uid']]['verdict'][$homeworkProblem->problem_id] = [ |
||
94 | "icon" => "cisco-webex", |
||
95 | "color" => $acceptedRecord['color'] |
||
96 | ]; |
||
97 | $statistics['data'][$acceptedRecord['uid']]['attempted']++; |
||
98 | $userProbIDArr[] = $acceptedRecord['uid']; |
||
99 | } |
||
100 | |||
101 | foreach ($homeworkProblem->problem->users_latest_submission($userIDArr->diff($userProbIDArr), null, $endedAt)->get() as $acceptedRecord) { |
||
102 | $statistics['data'][$acceptedRecord['uid']]['verdict'][$homeworkProblem->problem_id] = [ |
||
103 | "icon" => "cisco-webex", |
||
104 | "color" => $acceptedRecord['color'] |
||
105 | ]; |
||
106 | $statistics['data'][$acceptedRecord['uid']]['attempted']++; |
||
107 | $userProbIDArr[] = $acceptedRecord['uid']; |
||
108 | } |
||
109 | } |
||
110 | |||
111 | usort($statistics['data'], function ($a, $b) { |
||
112 | return $b["solved"] == $a["solved"] ? $b["attempted"] <=> $a["attempted"] : $b["solved"] <=> $a["solved"]; |
||
113 | }); |
||
114 | |||
115 | $statistics['timestamp'] = time(); |
||
116 | Cache::tags(['homework', 'statistics'])->put($this->id, $statistics, 360); |
||
117 | return $statistics; |
||
118 | } catch (Exception $e) { |
||
119 | Log::alert($e); |
||
|
|||
120 | return false; |
||
121 | } |
||
124 |