Skip to content

Commit c73bef7

Browse files
committed
Internal: Stricter code in attendancelink.class.php
1 parent 8ecfada commit c73bef7

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

public/main/gradebook/lib/be/attendancelink.class.php

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class AttendanceLink extends AbstractLink
1414
{
1515
private $attendance_table = null;
1616

17+
private $attendance_data = array();
18+
1719
public function __construct()
1820
{
1921
parent::__construct();
@@ -23,15 +25,15 @@ public function __construct()
2325
/**
2426
* @return string
2527
*/
26-
public function get_type_name()
28+
public function get_type_name(): string
2729
{
2830
return get_lang('Attendance');
2931
}
3032

3133
/**
3234
* @return bool
3335
*/
34-
public function is_allowed_to_change_name()
36+
public function is_allowed_to_change_name(): bool
3537
{
3638
return false;
3739
}
@@ -76,8 +78,9 @@ public function get_all_links(): array
7678

7779
/**
7880
* Has anyone done this exercise yet ?
81+
* @throws Exception
7982
*/
80-
public function has_results()
83+
public function has_results(): bool
8184
{
8285
$tbl_attendance_result = Database::get_course_table(TABLE_ATTENDANCE_RESULT);
8386
$sessionId = $this->get_session_id();
@@ -93,11 +96,12 @@ public function has_results()
9396
}
9497

9598
/**
96-
* @param int $studentId
97-
*
98-
* @return array|null
99+
* @param ?int $studentId
100+
* @param ?string $type
101+
* @return array
102+
* @throws Exception
99103
*/
100-
public function calc_score($studentId = null, $type = null)
104+
public function calc_score(?int $studentId = null, ?string $type = null): array
101105
{
102106
$tbl_attendance_result = Database::get_course_table(TABLE_ATTENDANCE_RESULT);
103107
$sessionId = $this->get_session_id();
@@ -133,7 +137,7 @@ public function calc_score($studentId = null, $type = null)
133137
// all students -> get average
134138
$students = []; // user list, needed to make sure we only
135139
// take first attempts into account
136-
$rescount = 0;
140+
$resultCount = 0;
137141
$sum = 0;
138142
$sumResult = 0;
139143
$bestResult = 0;
@@ -142,7 +146,7 @@ public function calc_score($studentId = null, $type = null)
142146
if (!(array_key_exists($data['user_id'], $students))) {
143147
if (0 != $attendance['attendance_qualify_max']) {
144148
$students[$data['user_id']] = $data['score'];
145-
$rescount++;
149+
$resultCount++;
146150
$sum += $data['score'] / $attendance['attendance_qualify_max'];
147151
$sumResult += $data['score'];
148152
if ($data['score'] > $bestResult) {
@@ -153,51 +157,52 @@ public function calc_score($studentId = null, $type = null)
153157
}
154158
}
155159

156-
if (0 == $rescount) {
160+
if (0 == $resultCount) {
157161
return [null, null];
158162
} else {
159163
switch ($type) {
160164
case 'best':
161165
return [$bestResult, $weight];
162166
break;
163167
case 'average':
164-
return [$sumResult / $rescount, $weight];
168+
return [$sumResult / $resultCount, $weight];
165169
break;
166170
case 'ranking':
167171
return AbstractLink::getCurrentUserRanking($studentId, $students);
168172
break;
169173
default:
170-
return [$sum, $rescount];
174+
return [$sum, $resultCount];
171175
break;
172176
}
173177
}
174178
}
175179
}
176180

177-
public function needs_name_and_description()
181+
public function needs_name_and_description(): bool
178182
{
179183
return false;
180184
}
181185

182-
public function needs_max()
186+
public function needs_max(): bool
183187
{
184188
return false;
185189
}
186190

187-
public function needs_results()
191+
public function needs_results(): bool
188192
{
189193
return false;
190194
}
191195

192196
/**
193197
* @return string
198+
* @throws \Doctrine\DBAL\Exception
194199
*/
195-
public function get_name()
200+
public function get_name(): string
196201
{
197202
$this->get_attendance_data();
198-
$attendance_title = isset($this->attendance_data['name']) ? $this->attendance_data['name'] : '';
199-
$attendance_qualify_title = isset($this->attendance_data['attendance_qualify_title']) ? $this->attendance_data['attendance_qualify_title'] : '';
200-
if (isset($attendance_qualify_title) && '' != $attendance_qualify_title) {
203+
$attendance_title = $this->attendance_data['name'] ?? '';
204+
$attendance_qualify_title = $this->attendance_data['attendance_qualify_title'] ?? '';
205+
if ('' != $attendance_qualify_title) {
201206
return $this->attendance_data['attendance_qualify_title'];
202207
} else {
203208
return $attendance_title;
@@ -207,15 +212,16 @@ public function get_name()
207212
/**
208213
* @return string
209214
*/
210-
public function get_description()
215+
public function get_description(): string
211216
{
212217
return '';
213218
}
214219

215220
/**
216221
* Check if this still links to an exercise.
222+
* @throws Exception
217223
*/
218-
public function is_valid_link()
224+
public function is_valid_link(): bool
219225
{
220226
$sql = 'SELECT count(iid) FROM '.$this->get_attendance_table().'
221227
WHERE iid = '.$this->get_ref_id();
@@ -225,7 +231,10 @@ public function is_valid_link()
225231
return 0 != $number[0];
226232
}
227233

228-
public function get_link()
234+
/**
235+
* @throws Exception
236+
*/
237+
public function get_link(): string
229238
{
230239
// it was extracts the attendance id
231240
$sessionId = $this->get_session_id();
@@ -243,25 +252,26 @@ public function get_link()
243252
/**
244253
* @return string
245254
*/
246-
public function get_icon_name()
255+
public function get_icon_name(): string
247256
{
248257
return 'attendance';
249258
}
250259

251260
/**
252261
* Lazy load function to get the database table of the student publications.
253262
*/
254-
private function get_attendance_table()
263+
private function get_attendance_table(): string
255264
{
256265
$this->attendance_table = Database::get_course_table(TABLE_ATTENDANCE);
257266

258267
return $this->attendance_table;
259268
}
260269

261270
/**
262-
* @return array|bool
271+
* @return array
272+
* @throws \Doctrine\DBAL\Exception
263273
*/
264-
private function get_attendance_data()
274+
private function get_attendance_data(): array
265275
{
266276
if (!isset($this->attendance_data)) {
267277
$sql = 'SELECT * FROM '.$this->get_attendance_table().' att

0 commit comments

Comments
 (0)