prepare("SELECT * FROM uploads WHERE user_id = ? AND created_at >= ? ORDER BY created_at DESC"); $stmt->bind_param("is", $userId, $today); $stmt->execute(); $result = $stmt->get_result(); $uploads = []; while ($row = $result->fetch_assoc()) { $uploads[] = $row; } return $uploads; } // =================================== // Get today's upload stats for a user // =================================== function getTodaysUploadStats($userId) { global $conn; $today = date('Y-m-d 00:00:00'); $stmt = $conn->prepare(" SELECT COUNT(*) AS total_uploads, SUM(CASE WHEN status = 'approved' THEN 1 ELSE 0 END) AS approved_uploads, IFNULL(SUM(reward_amount), 0) AS total_earnings FROM uploads WHERE user_id = ? AND created_at >= ? "); $stmt->bind_param("is", $userId, $today); $stmt->execute(); $result = $stmt->get_result(); return $result->fetch_assoc(); } // =================================== // Process file upload // =================================== function processFileUpload($userId, $fileData) { global $conn; $uploadDir = __DIR__ . '/../uploads/'; if (!is_dir($uploadDir)) { if (!mkdir($uploadDir, 0775, true)) { return ['success' => false, 'error' => 'Failed to create upload directory.']; } } $fileName = uniqid() . '_' . basename($fileData['name']); $filePath = $uploadDir . $fileName; if (move_uploaded_file($fileData['tmp_name'], $filePath)) { // Calculate reward $rewardAmount = calculateRewardAmount($fileData['type'], $fileData['size']); // Save upload record $stmt = $conn->prepare(" INSERT INTO uploads (user_id, file_name, file_type, file_size, status, reward_amount, created_at) VALUES (?, ?, ?, ?, 'approved', ?, NOW()) "); $stmt->bind_param("issid", $userId, $fileName, $fileData['type'], $fileData['size'], $rewardAmount); $stmt->execute(); // Update user earnings $stmt2 = $conn->prepare("UPDATE users SET meta_earnings = meta_earnings + ? WHERE id = ?"); $stmt2->bind_param("di", $rewardAmount, $userId); $stmt2->execute(); return [ 'success' => true, 'upload_id' => $conn->insert_id, 'reward' => $rewardAmount ]; } else { return ['success' => false, 'error' => 'File upload failed. Check permissions.']; } } // =================================== // Calculate reward amount // =================================== function calculateRewardAmount($fileType, $fileSize) { $baseReward = 50; // KES per file $typeBonus = 0; if (strpos($fileType, 'image/') === 0) { $typeBonus = 10; } elseif (strpos($fileType, 'video/') === 0) { $typeBonus = 20; } $sizeBonus = 0; if ($fileSize > 10 * 1024 * 1024) { $sizeBonus = 15; } elseif ($fileSize > 5 * 1024 * 1024) { $sizeBonus = 10; } elseif ($fileSize > 1 * 1024 * 1024) { $sizeBonus = 5; } return $baseReward + $typeBonus + $sizeBonus; } ?>