-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-list.php
More file actions
177 lines (157 loc) · 5.67 KB
/
Copy pathtest-list.php
File metadata and controls
177 lines (157 loc) · 5.67 KB
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
<?php
require 'backend/ud/session.php'; // Load session data
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdn.tailwindcss.com"></script>
<link
rel="icon"
type="image/x-icon"
href="assets/img/background/fav-logo.png"
/>
<title>Ongoing Contests - CodeCraft</title>
<style>
@import url("https://fonts.googleapis.com/css2?family=Syne:wght@400..800&display=swap");
* {
font-family: "Syne", serif;
font-optical-sizing: auto;
font-style: normal;
}
.hover-scale {
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.hover-scale:hover {
transform: scale(1.05);
box-shadow: 0px 10px 20px rgba(0, 0, 0, 0.1);
}
</style>
</head>
<body class="bg-gray-100 min-h-screen font-sans">
<div class="container mx-auto p-8">
<!-- Header -->
<header class="flex justify-between items-center mb-8">
<h1 class="text-3xl font-bold text-purple-600">Ongoing Contests</h1>
<div class="flex items-center space-x-4">
<div class="relative flex items-center">
<img
src="assets/img/background/search.png"
alt="Search Icon"
class="absolute left-3 w-4 h-4"
/>
<input
type="text"
placeholder="Search contests..."
class="pl-10 pr-4 py-2 border border-gray-300 rounded-lg w-full"
oninput="searchContests(this.value)"
/>
</div>
</div>
</header>
<!-- Ongoing Contests Section -->
<div id="ongoing-contests" class="grid grid-cols-3 gap-6">
<!-- Contest cards will be dynamically populated here -->
</div>
<!-- No Contests Message -->
<p id="no-contests" class="text-center text-gray-500 hidden">
No ongoing contests found.
</p>
</div>
<script>
// Fetch and display ongoing contests
async function fetchOngoingContests(search = "") {
try {
const response = await fetch(
`backend/contest/ongoing.php?search=${encodeURIComponent(search)}`
);
const contests = await response.json();
// Get the container and no-contests message
const container = document.getElementById("ongoing-contests");
const noContestsMessage = document.getElementById("no-contests");
// Clear any existing content
container.innerHTML = "";
if (contests.length === 0) {
// Show no-contests message if no contests are found
noContestsMessage.classList.remove("hidden");
return;
}
// Hide the no-contests message
noContestsMessage.classList.add("hidden");
// Populate the container with contest cards
contests.forEach((contest) => {
const contestCard = `
<div class="hover-scale bg-white p-4 border border-gray-300 rounded-lg">
<img
src="${contest.banner || 'https://via.placeholder.com/150'}"
alt="${contest.contest_name} Banner"
class="rounded-lg mb-4 w-full h-40 object-cover"
/>
<h3 class="text-lg font-bold text-purple-600 mb-2">
${contest.contest_name}
</h3>
<p class="text-gray-500 mb-4">${contest.description}</p>
<p class="text-sm text-gray-700 mb-2">
<strong>Start:</strong> ${new Date(
contest.start_time
).toLocaleString()}
</p>
<p class="text-sm text-gray-700 mb-2">
<strong>End:</strong> ${new Date(
contest.end_time
).toLocaleString()}
</p>
<button
class="mt-4 px-6 py-2 bg-purple-500 text-white rounded-full hover:bg-purple-600"
onclick="viewContestDetails(${contest.contest_id})"
>
View Details
</button>
</div>
`;
container.innerHTML += contestCard;
});
} catch (error) {
console.error("Error fetching contests:", error);
alert("Failed to load contests. Please try again later.");
}
}
// Redirect to contest details page
// Redirect to contest details page after checking registration
async function viewContestDetails(contestId) {
try {
if (!userId) {
alert("User is not logged in.");
return;
}
// Call the PHP backend to check registration
const response = await fetch(`backend/contest/check.php?contestId=${contestId}&userId=${userId}`);
const data = await response.json();
if (data.error) {
alert(data.error);
return;
}
if (data.isRegistered) {
// User is registered, redirect to the contest page
window.location.href = `ongoing-contest.php?contest_id=${contestId}`;
} else {
// User is not registered, show an alert or take appropriate action
alert("You are not registered for this contest. ");
}
} catch (error) {
console.error("Error checking registration:", error);
alert("An error occurred while checking registration. Please try again later.");
}
}
// Handle search input
function searchContests(query) {
fetchOngoingContests(query);
}
// Initialize the page
document.addEventListener("DOMContentLoaded", () => {
fetchOngoingContests();
});
</script>
</body>
</html>