@@ -101,20 +101,32 @@ public function calc_score(?int $studentId = null, ?string $type = null): array
101101
102102 // Load attendance settings (qualify max + require unique mode)
103103 $ sql = 'SELECT attendance_qualify_max, require_unique
104- FROM ' .$ this ->get_attendance_table ().'
105- WHERE iid = ' .$ attendanceId ;
104+ FROM ' .$ this ->get_attendance_table ().'
105+ WHERE iid = ' .( int ) $ attendanceId ;
106106 $ query = Database::query ($ sql );
107107 $ attendance = Database::fetch_assoc ($ query );
108108
109109 $ qualifyMax = (float ) ($ attendance ['attendance_qualify_max ' ] ?? 0 );
110110 $ requireUnique = !empty ($ attendance ['require_unique ' ]);
111111
112112 // Avoid gradebook showing 0/0 in require-unique mode when qualify max was never set.
113- if ($ requireUnique && $ qualifyMax <= 0 ) {
113+ if ($ requireUnique && $ qualifyMax <= 0.0 ) {
114114 // Using 100 matches the UI expectation ("100% when present at least once").
115115 $ qualifyMax = 100.0 ;
116116 }
117117
118+ if (!$ requireUnique && $ qualifyMax <= 0.0 ) {
119+ $ sqlTotal = 'SELECT COUNT(*) AS total
120+ FROM ' .$ tbl_calendar .'
121+ WHERE attendance_id = ' .(int ) $ attendanceId ;
122+ $ rowTotal = Database::fetch_assoc (Database::query ($ sqlTotal ));
123+ $ totalDates = (int ) ($ rowTotal ['total ' ] ?? 0 );
124+
125+ if ($ totalDates > 0 ) {
126+ $ qualifyMax = (float ) $ totalDates ;
127+ }
128+ }
129+
118130 // ------------------------------------------------------------
119131 // Require-unique mode: 100% score if present at least once
120132 // This is calculated from attendance sheets to avoid relying on
@@ -130,8 +142,8 @@ public function calc_score(?int $studentId = null, ?string $type = null): array
130142 $ sqlHasPresence = 'SELECT 1
131143 FROM ' .$ tbl_sheet .' s
132144 INNER JOIN ' .$ tbl_calendar .' c ON c.iid = s.attendance_calendar_id
133- WHERE c.attendance_id = ' .$ attendanceId .'
134- AND s.user_id = ' .$ studentId .'
145+ WHERE c.attendance_id = ' .( int ) $ attendanceId .'
146+ AND s.user_id = ' .( int ) $ studentId .'
135147 AND s.presence IN (1, 2, 3)
136148 LIMIT 1 ' ;
137149
@@ -156,7 +168,7 @@ public function calc_score(?int $studentId = null, ?string $type = null): array
156168 MAX(CASE WHEN s.presence IN (1, 2, 3) THEN 1 ELSE 0 END) AS has_presence
157169 FROM ' .$ tbl_sheet .' s
158170 INNER JOIN ' .$ tbl_calendar .' c ON c.iid = s.attendance_calendar_id
159- WHERE c.attendance_id = ' .$ attendanceId .'
171+ WHERE c.attendance_id = ' .( int ) $ attendanceId .'
160172 GROUP BY s.user_id ' ;
161173
162174 $ rs = Database::query ($ sqlAll );
@@ -173,7 +185,7 @@ public function calc_score(?int $studentId = null, ?string $type = null): array
173185
174186 // Compute stats (keep legacy behavior when qualifyMax is 0)
175187 foreach ($ students as $ uid => $ finalScore ) {
176- if (0 != $ qualifyMax ) {
188+ if (0.0 != $ qualifyMax ) {
177189 $ resultCount ++;
178190 $ sumRatio += $ finalScore / $ qualifyMax ;
179191 $ sumScore += $ finalScore ;
@@ -205,8 +217,8 @@ public function calc_score(?int $studentId = null, ?string $type = null): array
205217 }
206218
207219 $ sql = 'SELECT user_id, score
208- FROM ' .$ tbl_attendance_result .'
209- WHERE attendance_id = ' .$ attendanceId ;
220+ FROM ' .$ tbl_attendance_result .'
221+ WHERE attendance_id = ' .( int ) $ attendanceId ;
210222
211223 if (null !== $ studentId ) {
212224 $ sql .= ' AND user_id = ' .(int ) $ studentId ;
@@ -219,7 +231,7 @@ public function calc_score(?int $studentId = null, ?string $type = null): array
219231 if ($ row = Database::fetch_assoc ($ scores )) {
220232 $ score = (float ) ($ row ['score ' ] ?? 0 );
221233 if ('default ' === $ type ) {
222- return [$ qualifyMax > 0 ? ($ score / $ qualifyMax ) : 0.0 , 1 ];
234+ return [$ qualifyMax > 0.0 ? ($ score / $ qualifyMax ) : 0.0 , 1 ];
223235 }
224236
225237 return [$ score , $ qualifyMax ];
@@ -256,7 +268,7 @@ public function calc_score(?int $studentId = null, ?string $type = null): array
256268
257269 // Compute stats
258270 foreach ($ students as $ uid => $ finalScore ) {
259- if (0 != $ qualifyMax ) {
271+ if (0.0 != $ qualifyMax ) {
260272 $ resultCount ++;
261273 $ sumRatio += $ finalScore / $ qualifyMax ;
262274 $ sumScore += $ finalScore ;
0 commit comments