File size: 3,480 Bytes
0dff816
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php
// upload-functions.php
// Include database connection
require_once __DIR__ . '../../db.php';

// ==============================
// Get today's uploads for a user
// ==============================
function getTodaysUploads($userId) {
    global $conn;
    $today = date('Y-m-d 00:00:00');

    $stmt = $conn->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;
}
?>