/home/u764571690/domains/savitrfoundation.com/public_html/scholar/pages
NameSizeModeActions
cache/-0755rm
css/-0755rm
.htaccess26180644editdlrm
ack.php252830644editdlrm
bank.php190330644editdlrm
daily_quiz.php1073980644editdlrm
dashboard.php272430644editdlrm
distributed.php270430644editdlrm
help.php228580644editdlrm
institute.php169280644editdlrm
invite.php262230644editdlrm
leaderboard.php376840644editdlrm
personal.php154320644editdlrm
status.php302530644editdlrm
tasks.php710750644editdlrm
Edit: /home/u764571690/domains/savitrfoundation.com/public_html/scholar/pages/leaderboard.php (37684B)
'/', 'domain' => '', // Empty for localhost compatibility 'secure' => false, // Allow HTTP for localhost 'httponly' => true, 'samesite' => 'Lax', 'lifetime' => 2592000 // 30 days cookie lifetime ]); session_start(); } // Correct path to db.php require_once __DIR__ . '/../db.php'; // ✅ PERFORMANCE: Local cache class for pages/cache directory class LocalSimpleCache extends SimpleCache { private static $cacheDir = __DIR__ . '/cache'; private static $defaultTTL = 300; public static function init() { if (!file_exists(self::$cacheDir)) { @mkdir(self::$cacheDir, 0755, true); } } public static function get($key) { self::init(); $file = self::$cacheDir . '/' . md5($key) . '.cache'; if (!file_exists($file)) return false; $data = @unserialize(file_get_contents($file)); if ($data === false) return false; if (time() > $data['expires']) { @unlink($file); return false; } return $data['value']; } public static function set($key, $value, $ttl = null) { self::init(); $ttl = $ttl ?? self::$defaultTTL; $file = self::$cacheDir . '/' . md5($key) . '.cache'; $data = ['value' => $value, 'expires' => time() + $ttl, 'created' => time()]; $fp = @fopen($file, 'c+'); if ($fp === false) return @file_put_contents($file, serialize($data)) !== false; $lockAcquired = false; for ($i = 0; $i < 10; $i++) { if (flock($fp, LOCK_EX | LOCK_NB)) { $lockAcquired = true; break; } usleep(500000); } if ($lockAcquired) { ftruncate($fp, 0); fwrite($fp, serialize($data)); flock($fp, LOCK_UN); fclose($fp); return true; } else { fclose($fp); return @file_put_contents($file, serialize($data)) !== false; } } public static function delete($key) { self::init(); $file = self::$cacheDir . '/' . md5($key) . '.cache'; return @unlink($file); } } // Check if user is logged in if (!isset($_SESSION['user_logged_in']) || $_SESSION['user_logged_in'] !== true) { header('Location: ../student'); exit(); } $applicantId = (int)($_SESSION['applicant_id'] ?? 0); $userId = $_SESSION['user_id'] ?? ''; $userName = $_SESSION['user_name'] ?? 'Student'; // Get current month-year for leaderboard $currentMonthYear = date('Y-m'); $previousMonthYear = date('Y-m', strtotime('-1 month')); try { // ✅ OPTIMIZED: Cache leaderboard data for 2 minutes (frequently accessed) $currentCacheKey = 'leaderboard_current_' . $currentMonthYear; $previousCacheKey = 'leaderboard_previous_' . $previousMonthYear; $leaderboardEntries = LocalSimpleCache::get($currentCacheKey); $previousLeaderboardEntries = LocalSimpleCache::get($previousCacheKey); // Get top 10 leaderboard entries for current month if ($leaderboardEntries === false) { $stmt = $pdo->prepare(" SELECT l.*, u.name as user_name FROM quiz_leaderboard_monthly l JOIN users u ON l.user_id = u.user_id WHERE l.month_year = ? ORDER BY l.total_score DESC LIMIT 10 "); $stmt->execute([$currentMonthYear]); $leaderboardEntries = $stmt->fetchAll(PDO::FETCH_ASSOC); LocalSimpleCache::set($currentCacheKey, $leaderboardEntries, 120); // Cache for 2 minutes } // Get top 10 leaderboard entries for previous month if ($previousLeaderboardEntries === false) { $stmt = $pdo->prepare(" SELECT l.*, u.name as user_name FROM quiz_leaderboard_monthly l JOIN users u ON l.user_id = u.user_id WHERE l.month_year = ? ORDER BY l.total_score DESC LIMIT 10 "); $stmt->execute([$previousMonthYear]); $previousLeaderboardEntries = $stmt->fetchAll(PDO::FETCH_ASSOC); LocalSimpleCache::set($previousCacheKey, $previousLeaderboardEntries, 120); // Cache for 2 minutes } // ✅ OPTIMIZED: Single query to get user data and position for both months $userPosition = 0; $userScore = 0; $userQuizzesTaken = 0; $previousUserPosition = 0; $previousUserScore = 0; $previousUserQuizzesTaken = 0; if ($userId) { // Single query to get user's data for both months and calculate positions $stmt = $pdo->prepare(" SELECT month_year, total_score, quizzes_taken, (SELECT COUNT(*) + 1 FROM quiz_leaderboard_monthly l2 WHERE l2.month_year = l.month_year AND l2.total_score > l.total_score) as position FROM quiz_leaderboard_monthly l WHERE l.user_id = ? AND l.month_year IN (?, ?) "); $stmt->execute([$userId, $currentMonthYear, $previousMonthYear]); $userDataRows = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($userDataRows as $row) { if ($row['month_year'] === $currentMonthYear) { $userScore = $row['total_score']; $userQuizzesTaken = $row['quizzes_taken']; $userPosition = $row['position'] ?? 0; } else { $previousUserScore = $row['total_score']; $previousUserQuizzesTaken = $row['quizzes_taken']; $previousUserPosition = $row['position'] ?? 0; } } } } catch (Exception $e) { $error_message = "Error loading leaderboard: " . $e->getMessage(); error_log($error_message); $leaderboardEntries = []; $previousLeaderboardEntries = []; $userPosition = 0; $userScore = 0; $userQuizzesTaken = 0; $previousUserPosition = 0; $previousUserScore = 0; $previousUserQuizzesTaken = 0; } ?> Leaderboard - Student Dashboard - Savitr Foundation Scholarship
Savitr Foundation
Welcome, Scholar ID:

Current Month Leaderboard -

0): ?>
Your Current Position
Rank
Points Quizzes
= 3): ?>
ID:
ID:
ID:
= 1): ?>

Top Performers

$entry): ?>
Scholar ID:
points quizzes

Previous Month Leaderboard -

= 3): ?>
ID:
ID:
ID:
= 1): ?>

Top Performers

$entry): ?>
Scholar ID:
points quizzes
Rank 0 ? $userPosition : ($previousUserPosition > 0 ? $previousUserPosition : 'N/A'); ?>
0 ? $userScore : $previousUserScore; ?> Points 0 ? $userQuizzesTaken : $previousUserQuizzesTaken; ?> Quizzes