Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | 3x 3x 3x 3x 3x 3x 3x 3x 3x | const express = require('express');
const pool = require('../config/database');
const { authenticate, authorize } = require('../middleware/auth');
const { body, validationResult } = require('express-validator');
const router = express.Router();
// Generate student progress report
router.post('/student-progress', authenticate, [
body('student_id').isInt()
], async (req, res) => {
try {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
const { student_id } = req.body;
const currentUser = req.user;
// Check access
if (currentUser.role === 'student') {
const [student] = await pool.query(
'SELECT id FROM students WHERE user_id = ?',
[currentUser.id]
);
if (student.length === 0 || student[0].id !== student_id) {
return res.status(403).json({ error: 'Access denied' });
}
}
// Get student data
const [students] = await pool.query(
`SELECT
s.*, u.name, u.email, u.phone,
sup_user.name as supervisor_name
FROM students s
JOIN users u ON s.user_id = u.id
LEFT JOIN supervisors sup ON s.supervisor_id = sup.id
LEFT JOIN users sup_user ON sup.user_id = sup_user.id
WHERE s.id = ?`,
[student_id]
);
if (students.length === 0) {
return res.status(404).json({ error: 'Student not found' });
}
const student = students[0];
// Get milestones
const [milestones] = await pool.query(
'SELECT * FROM milestones WHERE student_id = ? ORDER BY created_at ASC',
[student_id]
);
// Get meetings count
const [meetings] = await pool.query(
'SELECT COUNT(*) as count FROM meetings WHERE student_id = ? AND status = "completed"',
[student_id]
);
// Get documents count
const [documents] = await pool.query(
'SELECT COUNT(*) as count FROM documents WHERE student_id = ?',
[student_id]
);
// Get pending reviews
const [pendingReviews] = await pool.query(
'SELECT COUNT(*) as count FROM documents WHERE student_id = ? AND status = "pending_review"',
[student_id]
);
// Generate report data
const reportData = {
student: {
name: student.name,
email: student.email,
program: student.program,
startDate: student.start_date,
expectedCompletion: student.expected_completion,
progress: student.progress,
supervisor: student.supervisor_name
},
milestones,
statistics: {
meetingsCompleted: meetings[0].count,
documentsSubmitted: documents[0].count,
pendingReviews: pendingReviews[0].count
},
generatedAt: new Date().toISOString(),
generatedBy: currentUser.name
};
// Save report to database
await pool.query(
'INSERT INTO system_reports (generated_by, report_type, report_data) VALUES (?, ?, ?)',
[currentUser.id, 'student_progress', JSON.stringify(reportData)]
);
res.json({ report: reportData });
} catch (error) {
console.error('Generate report error:', error);
res.status(500).json({ error: 'Failed to generate report' });
}
});
// Generate system analytics report (admin only)
router.post('/analytics', authenticate, authorize('administrator'), async (req, res) => {
try {
// Get total students
const [totalStudents] = await pool.query('SELECT COUNT(*) as count FROM students');
// Get active supervisors
const [activeSupervisors] = await pool.query(
'SELECT COUNT(*) as count FROM supervisors s JOIN users u ON s.user_id = u.id WHERE u.status = "active"'
);
// Get pending reviews
const [pendingReviews] = await pool.query(
'SELECT COUNT(*) as count FROM documents WHERE status = "pending_review"'
);
// Get average progress
const [avgProgress] = await pool.query(
'SELECT AVG(progress) as avg FROM students'
);
// Get students by program
const [studentsByProgram] = await pool.query(
'SELECT program, COUNT(*) as count FROM students GROUP BY program'
);
// Get meetings statistics
const [meetingsStats] = await pool.query(
`SELECT
COUNT(*) as total,
SUM(CASE WHEN status = 'completed' THEN 1 ELSE 0 END) as completed,
SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) as pending
FROM meetings`
);
const reportData = {
statistics: {
totalStudents: totalStudents[0].count,
activeSupervisors: activeSupervisors[0].count,
pendingReviews: pendingReviews[0].count,
averageProgress: Math.round(avgProgress[0].avg || 0)
},
studentsByProgram,
meetings: meetingsStats[0],
generatedAt: new Date().toISOString(),
generatedBy: req.user.name
};
// Save report
await pool.query(
'INSERT INTO system_reports (generated_by, report_type, report_data) VALUES (?, ?, ?)',
[req.user.id, 'analytics', JSON.stringify(reportData)]
);
res.json({ report: reportData });
} catch (error) {
console.error('Generate analytics error:', error);
res.status(500).json({ error: 'Failed to generate analytics report' });
}
});
// Get report history
router.get('/history', authenticate, async (req, res) => {
try {
const currentUser = req.user;
let query = 'SELECT * FROM system_reports';
const params = [];
// Non-admins can only see their own reports
if (currentUser.role !== 'administrator') {
query += ' WHERE generated_by = ?';
params.push(currentUser.id);
}
query += ' ORDER BY created_at DESC LIMIT 50';
const [reports] = await pool.query(query, params);
res.json({ reports });
} catch (error) {
console.error('Get report history error:', error);
res.status(500).json({ error: 'Failed to fetch report history' });
}
});
module.exports = router;
|