Fix student grade registration: set grade_10 as default, add auto-heal for legacy tawjihi_2008, add student grade selector and update-grade API

This commit is contained in:
Hamza-Ayed
2026-09-09 01:06:35 +03:00
parent 8183e53b86
commit 0a2ca71cb4
13 changed files with 440 additions and 41 deletions
@@ -514,6 +514,10 @@ class AuthController
[$userId]
);
if ($user) {
if (($user['grade_level'] ?? '') === 'tawjihi_2008') {
Database::query("UPDATE students SET grade_level = 'grade_10', updated_at = NOW() WHERE id = ?", [$userId]);
$user['grade_level'] = 'grade_10';
}
$isCompleted = !empty($user['full_name']) && $user['full_name'] !== 'طالب جديد' && $user['full_name'] !== 'الطالب المتميز' && !empty($user['grade_level']);
$userData = [
'id' => $user['id'],
@@ -579,6 +583,11 @@ class AuthController
$student = Database::selectOne("SELECT * FROM students WHERE national_id_hash = ? LIMIT 1", [$nationalIdHash]);
if ($student) {
if (($student['grade_level'] ?? '') === 'tawjihi_2008') {
Database::query("UPDATE students SET grade_level = 'grade_10', updated_at = NOW() WHERE id = ?", [$student['id']]);
$student['grade_level'] = 'grade_10';
}
// Student exists. Verify identity linkage.
if ($student['identity_id'] === null) {
// Pre-registered by Guardian, link to this identity phone now
@@ -752,6 +761,78 @@ class AuthController
}
}
/**
* Update Student Grade & Academic Stream
* POST /api/student/profile/update-grade
*/
public function updateStudentGrade(Request $request, Response $response): void
{
$studentId = (int)$request->user_id;
if (!$studentId) {
$authHeader = $request->getHeader('authorization', '');
if ($authHeader && preg_match('/Bearer\s(\S+)/i', $authHeader, $matches)) {
$payload = Security::verifyJWT($matches[1]);
if ($payload && isset($payload['user_id'])) {
$studentId = (int)$payload['user_id'];
}
}
}
if (!$studentId) {
$response->status(401)->json(['status' => 'error', 'message' => 'غير مصرح']);
return;
}
$body = $request->getBody();
$rawGrade = trim((string)($body['grade_level'] ?? 'grade_10'));
$stream = trim((string)($body['stream'] ?? 'general'));
$normalizedGrade = \App\Services\StudentAccessControlService::normalizeGrade($rawGrade);
Database::query(
"UPDATE students SET grade_level = ?, stream = ?, updated_at = NOW() WHERE id = ?",
[$normalizedGrade, $stream, $studentId]
);
$student = Database::selectOne(
"SELECT s.id, s.uuid, s.full_name, s.national_id, s.grade_level, s.stream, s.readiness_score, s.school_id, ai.phone_number, ai.status, s.created_at
FROM students s
JOIN auth_identities ai ON s.identity_id = ai.id
WHERE s.id = ? LIMIT 1",
[$studentId]
);
if (!$student) {
$response->status(404)->json(['status' => 'error', 'message' => 'طالب غير موجود']);
return;
}
$displayName = $this->readStoredValue((string)$student['full_name']);
$response->json([
'status' => 'success',
'message' => 'تم تحديث الصف الدراسي بنجاح',
'data' => [
'grade_level' => $student['grade_level'],
'stream' => $student['stream'],
'user' => [
'id' => (int)$student['id'],
'uuid' => $student['uuid'],
'full_name' => $displayName,
'name' => $displayName,
'role' => 'student',
'national_id' => $this->readStoredValue((string)$student['national_id']),
'grade_level' => $student['grade_level'],
'stream' => $student['stream'],
'readiness_score' => $student['readiness_score'] ? (float)$student['readiness_score'] : 0.0,
'phone' => Security::decrypt($student['phone_number']),
'status' => $student['status'],
'is_completed' => true,
'is_student' => true,
'is_teacher' => false,
]
]
]);
}
/**
* Logout and destroy Redis active session
* POST /api/auth/logout
+7 -2
View File
@@ -520,15 +520,20 @@ class VideoController
CurriculumService::ensureSchema();
$curriculumKey = trim((string)($request->getQuery('curriculum_key') ?? ''));
$curriculumKeyNoExt = preg_replace('/\.md$/i', '', $curriculumKey);
$rawId = $curriculumKey !== '' ? $curriculumKey : ($request->getParam('id') ?? '');
$lesson = null;
if ($curriculumKey !== '') {
$lesson = Database::selectOne("SELECT * FROM lessons WHERE curriculum_key = ? LIMIT 1", [$curriculumKey]);
$lesson = Database::selectOne(
"SELECT * FROM lessons WHERE curriculum_key = ? OR curriculum_key = ? OR local_path LIKE ? OR markdown_content LIKE ? LIMIT 1",
[$curriculumKey, $curriculumKeyNoExt, '%' . $curriculumKeyNoExt . '%', '%' . $curriculumKeyNoExt . '%']
);
} elseif (is_numeric($rawId) && (int)$rawId > 0) {
$lesson = Database::selectOne("SELECT * FROM lessons WHERE id = ? LIMIT 1", [(int)$rawId]);
} elseif (!empty($rawId)) {
$lesson = Database::selectOne("SELECT * FROM lessons WHERE curriculum_key = ? OR title LIKE ? OR local_path LIKE ? OR markdown_content LIKE ? LIMIT 1", [$rawId, "%{$rawId}%", "%{$rawId}%", "%{$rawId}%"]);
$rawIdNoExt = preg_replace('/\.md$/i', '', $rawId);
$lesson = Database::selectOne("SELECT * FROM lessons WHERE curriculum_key = ? OR curriculum_key = ? OR title LIKE ? OR local_path LIKE ? OR markdown_content LIKE ? LIMIT 1", [$rawId, $rawIdNoExt, "%{$rawId}%", "%{$rawIdNoExt}%", "%{$rawIdNoExt}%"]);
// Flutter curriculum lessons use the manifest slug (e.g. u1_l1_*),
// while the database stores the canonical lesson title.
if (!$lesson) {