-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblems.php
More file actions
793 lines (692 loc) · 33.4 KB
/
Copy pathproblems.php
File metadata and controls
793 lines (692 loc) · 33.4 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
<?php
require 'config.php';
if (!isset($_SESSION['name']) && !isset($_COOKIE['name'])) {
echo "Session Error";
header("Location: login.php");
exit;
}
if (isset($_SESSION['name']) && isset($_SESSION['email'])) {
$name = $_SESSION['name'];
$email = $_SESSION['email'];
$stmt = $conn->prepare("SELECT codechef_id, leetcode_id, codeforces_id FROM profile WHERE Uemail = ?");
if ($stmt) {
$stmt->bind_param("s", $email); // Bind the email as a string
$stmt->execute();
$stmt->bind_result($codechef_id, $leetcode_id, $codeforces_id);
$stmt->fetch();
$stmt->close();
} else {
die("Query preparation failed: " . $conn->error);
}
}
// Handle AJAX actions
if (isset($_POST['action'])) {
if (!isset($_SESSION['email'])) {
echo json_encode(['success' => false, 'message' => 'User not logged in']);
exit;
}
$user_email = $_SESSION['email'];
// Handle bookmark toggle action
if ($_POST['action'] === 'toggle_bookmark') {
if (!isset($_POST['problem_id'])) {
echo json_encode(['success' => false, 'message' => 'Missing problem ID']);
exit;
}
$problem_id = $_POST['problem_id'];
// Check if bookmark already exists
$check_stmt = $conn->prepare("SELECT id FROM bookmarks WHERE user_email = ? AND problem_id = ?");
$check_stmt->bind_param("si", $user_email, $problem_id);
$check_stmt->execute();
$check_stmt->store_result();
if ($check_stmt->num_rows > 0) {
// Bookmark exists, so remove it
$delete_stmt = $conn->prepare("DELETE FROM bookmarks WHERE user_email = ? AND problem_id = ?");
$delete_stmt->bind_param("si", $user_email, $problem_id);
$result = $delete_stmt->execute();
$delete_stmt->close();
echo json_encode(['success' => $result, 'bookmarked' => false]);
} else {
// Bookmark doesn't exist, so add it
$insert_stmt = $conn->prepare("INSERT INTO bookmarks (user_email, problem_id) VALUES (?, ?)");
$insert_stmt->bind_param("si", $user_email, $problem_id);
$result = $insert_stmt->execute();
$insert_stmt->close();
echo json_encode(['success' => $result, 'bookmarked' => true]);
}
$check_stmt->close();
exit;
}
// Handle add new problem action
if ($_POST['action'] === 'add_problem') {
// Validate required fields
$required_fields = ['title', 'difficulty', 'platform', 'problem_url', 'description'];
$missing_fields = [];
foreach ($required_fields as $field) {
if (!isset($_POST[$field]) || empty($_POST[$field])) {
$missing_fields[] = $field;
}
}
if (!empty($missing_fields)) {
echo json_encode(['success' => false, 'message' => 'Missing required fields: ' . implode(', ', $missing_fields)]);
exit;
}
// Get form data
$title = $_POST['title'];
$difficulty = $_POST['difficulty'];
$platform = $_POST['platform'] === 'other' ? 'Custom' : $_POST['platform'];
$problem_url = $_POST['problem_url'];
$description = $_POST['description'];
$tags = isset($_POST['tags']) ? $_POST['tags'] : '';
// Insert the problem
$stmt = $conn->prepare("INSERT INTO problems (title, difficulty, platform, problem_url, description, tags) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bind_param("ssssss", $title, $difficulty, $platform, $problem_url, $description, $tags);
if ($stmt->execute()) {
$new_problem_id = $conn->insert_id;
echo json_encode([
'success' => true,
'message' => 'Problem added successfully',
'problem_id' => $new_problem_id
]);
} else {
echo json_encode(['success' => false, 'message' => 'Failed to add problem: ' . $stmt->error]);
}
$stmt->close();
exit;
}
// Handle mark as solved action
if ($_POST['action'] === 'mark_solved') {
if (!isset($_POST['problem_id'])) {
echo json_encode(['success' => false, 'message' => 'Missing problem ID']);
exit;
}
$problem_id = $_POST['problem_id'];
$solution_code = $_POST['solution_code'] ?? '';
$language = $_POST['language'] ?? '';
$time_taken = $_POST['time_taken'] ?? null;
// Check if problem is already marked as solved
$check_stmt = $conn->prepare("SELECT id FROM solved_problems WHERE user_email = ? AND problem_id = ?");
$check_stmt->bind_param("si", $user_email, $problem_id);
$check_stmt->execute();
$check_stmt->store_result();
if ($check_stmt->num_rows > 0) {
echo json_encode(['success' => false, 'message' => 'Problem already marked as solved']);
$check_stmt->close();
exit;
}
$check_stmt->close();
// Get problem details for statistics update
$problem_stmt = $conn->prepare("SELECT difficulty, platform FROM problems WHERE id = ?");
$problem_stmt->bind_param("i", $problem_id);
$problem_stmt->execute();
$problem_stmt->bind_result($difficulty, $platform);
$problem_stmt->fetch();
$problem_stmt->close();
// Start transaction
$conn->begin_transaction();
try {
// Insert into solved_problems
$insert_stmt = $conn->prepare("INSERT INTO solved_problems (user_email, problem_id, solution_code, language, time_taken) VALUES (?, ?, ?, ?, ?)");
$insert_stmt->bind_param("sissi", $user_email, $problem_id, $solution_code, $language, $time_taken);
$insert_stmt->execute();
$insert_stmt->close();
// Update user statistics
$today = date('Y-m-d');
// Check if user has statistics record
$stats_check = $conn->prepare("SELECT id, last_solved_date FROM user_statistics WHERE user_email = ?");
$stats_check->bind_param("s", $user_email);
$stats_check->execute();
$stats_check->store_result();
if ($stats_check->num_rows === 0) {
// Create new statistics record
$stats_insert = $conn->prepare("INSERT INTO user_statistics (user_email, total_solved, last_solved_date, streak_days) VALUES (?, 1, ?, 1)");
$stats_insert->bind_param("ss", $user_email, $today);
$stats_insert->execute();
$stats_insert->close();
} else {
// Update existing statistics
$stats_check->bind_result($stats_id, $last_solved_date);
$stats_check->fetch();
// Calculate streak
$streak_update = '';
if ($last_solved_date) {
$yesterday = date('Y-m-d', strtotime('-1 day'));
if ($last_solved_date == $today) {
// Already solved today, no streak update needed
$streak_update = '';
} elseif ($last_solved_date == $yesterday) {
// Solved yesterday, increment streak
$streak_update = ', streak_days = streak_days + 1';
} else {
// Streak broken, reset to 1
$streak_update = ', streak_days = 1';
}
} else {
// First time solving, set streak to 1
$streak_update = ', streak_days = 1';
}
// Update statistics based on difficulty and platform
$difficulty_col = strtolower($difficulty) . '_solved';
$platform_col = strtolower($platform) . '_solved';
$stats_update = $conn->prepare("UPDATE user_statistics SET
total_solved = total_solved + 1,
$difficulty_col = $difficulty_col + 1,
$platform_col = $platform_col + 1,
last_solved_date = ? $streak_update
WHERE user_email = ?");
$stats_update->bind_param("ss", $today, $user_email);
$stats_update->execute();
$stats_update->close();
}
$stats_check->close();
// Commit transaction
$conn->commit();
echo json_encode(['success' => true, 'message' => 'Problem marked as solved']);
} catch (Exception $e) {
// Rollback transaction on error
$conn->rollback();
echo json_encode(['success' => false, 'message' => 'Error: ' . $e->getMessage()]);
}
exit;
}
}
// Get filter parameters
$platform_filter = $_GET['platform'] ?? 'all';
$difficulty_filter = $_GET['difficulty'] ?? 'all';
$bookmarked_filter = isset($_GET['bookmarked']) ? ($_GET['bookmarked'] === '1') : false;
// Build the SQL query based on filters
$sql = "SELECT p.id, p.title, p.difficulty, p.platform, p.problem_url, p.description, p.tags,
CASE WHEN b.id IS NOT NULL THEN 1 ELSE 0 END AS bookmarked,
CASE WHEN sp.id IS NOT NULL THEN 1 ELSE 0 END AS solved
FROM problems p
LEFT JOIN bookmarks b ON p.id = b.problem_id AND b.user_email = ?
LEFT JOIN solved_problems sp ON p.id = sp.problem_id AND sp.user_email = ?";
$where_clauses = [];
$params = [$email, $email];
$types = "ss";
if ($platform_filter !== 'all') {
$where_clauses[] = "p.platform = ?";
$params[] = $platform_filter;
$types .= "s";
}
if ($difficulty_filter !== 'all') {
$where_clauses[] = "p.difficulty = ?";
$params[] = $difficulty_filter;
$types .= "s";
}
if ($bookmarked_filter) {
$where_clauses[] = "b.id IS NOT NULL";
}
// Add solved filter if specified
$solved_filter = isset($_GET['solved']) ? ($_GET['solved'] === '1') : false;
if ($solved_filter) {
$where_clauses[] = "sp.id IS NOT NULL";
}
if (!empty($where_clauses)) {
$sql .= " WHERE " . implode(" AND ", $where_clauses);
}
// Prepare and execute the query
$stmt = $conn->prepare($sql);
if (!$stmt) {
die("Query preparation failed: " . $conn->error);
}
// Bind parameters dynamically
if (!empty($params)) {
$stmt->bind_param($types, ...$params);
}
$stmt->execute();
$result = $stmt->get_result();
// Fetch all problems
$problems = [];
while ($row = $result->fetch_assoc()) {
// Convert tags string to array
$row['tags'] = explode(',', $row['tags']);
$row['url'] = $row['problem_url']; // Rename for compatibility with existing code
$row['bookmarked'] = (bool)$row['bookmarked'];
$problems[] = $row;
}
$stmt->close();
// Use the fetched problems as filtered problems
$filtered_problems = $problems;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Problems - CodeKeep</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<link rel="stylesheet" href="dashboard.css">
<link rel="stylesheet" href="problems.css">
</head>
<body>
<!-- Sidebar -->
<div class="sidebar">
<div class="logo">
<h1><span class="logo-text"><Code<span style="color: #1a4eaf">Case></span></span></h1>
</div>
<div class="nav-menu">
<a href="dashboard.php" class="nav-item">
<i class="fas fa-home"></i>
<span>Dashboard</span>
</a>
<a href="allusers.php" class="nav-item">
<i class="fas fa-users"></i>
<span>All Users</span>
</a>
<a href="contests.php" class="nav-item">
<i class="fas fa-calendar"></i>
<span>Contests</span>
</a>
<a href="problems.php" class="nav-item active">
<i class="fas fa-code"></i>
<span>Problems</span>
</a>
<a href="notes.php" class="nav-item">
<i class="fas fa-sticky-note"></i>
<span>Notes</span>
</a>
<a href="cp_helper.php" class="nav-item">
<i class="fas fa-robot"></i>
<span>CP Helper</span>
</a>
</div>
<div class="configure-profiles">
<a href="profile.php" class="configure-btn">
<i class="fas fa-user-cog"></i>
<span>Configure Profiles</span>
</a>
</div>
<div class="user-profile">
<div class="user-avatar">
<?php echo substr($name, 0, 1); ?>
</div>
<div class="user-info">
<div class="user-name"><?php echo htmlspecialchars($name); ?></div>
</div>
<button class="logout-btn" onclick="confirmLogout()">
<i class="fas fa-sign-out-alt"></i>
</button>
</div>
</div>
<div class="extra" style="width: 220px;
background-color: #171923;
padding: 20px 0;
display: flex;
flex-direction: column;
border-right: 1px solid #2d3748;">
</div>
<!-- Main Content -->
<div class="main-content">
<div class="header-with-button">
<div>
<a href="dashboard.php" class="back-to-dashboard-btn">
<i class="fas fa-arrow-left"></i> Back to Dashboard
</a>
<h1>Coding Problems</h1>
<p>Browse, solve, and bookmark coding problems from various platforms.</p>
</div>
</div>
<div class="search-container">
<input type="text" class="search-input" placeholder="Search problems..." id="problemSearch">
</div>
<div class="filter-container">
<div class="filter-group">
<div class="filter-label">Platform</div>
<div class="filter-options">
<a href="?platform=all<?php echo $difficulty_filter !== 'all' ? '&difficulty=' . $difficulty_filter : ''; ?><?php echo $bookmarked_filter ? '&bookmarked=1' : ''; ?>" class="filter-btn <?php echo $platform_filter === 'all' ? 'active' : ''; ?>">All</a>
<a href="?platform=leetcode<?php echo $difficulty_filter !== 'all' ? '&difficulty=' . $difficulty_filter : ''; ?><?php echo $bookmarked_filter ? '&bookmarked=1' : ''; ?>" class="filter-btn <?php echo $platform_filter === 'leetcode' ? 'active' : ''; ?>">LeetCode</a>
<a href="?platform=codechef<?php echo $difficulty_filter !== 'all' ? '&difficulty=' . $difficulty_filter : ''; ?><?php echo $bookmarked_filter ? '&bookmarked=1' : ''; ?>" class="filter-btn <?php echo $platform_filter === 'codechef' ? 'active' : ''; ?>">CodeChef</a>
<a href="?platform=codeforces<?php echo $difficulty_filter !== 'all' ? '&difficulty=' . $difficulty_filter : ''; ?><?php echo $bookmarked_filter ? '&bookmarked=1' : ''; ?>" class="filter-btn <?php echo $platform_filter === 'codeforces' ? 'active' : ''; ?>">Codeforces</a>
<a href="?platform=custom<?php echo $difficulty_filter !== 'all' ? '&difficulty=' . $difficulty_filter : ''; ?><?php echo $bookmarked_filter ? '&bookmarked=1' : ''; ?>" class="filter-btn <?php echo $platform_filter === 'custom' ? 'active' : ''; ?>">Custom</a>
</div>
</div>
<div class="filter-group">
<div class="filter-label">Difficulty</div>
<div class="filter-options">
<a href="?difficulty=all<?php echo $platform_filter !== 'all' ? '&platform=' . $platform_filter : ''; ?><?php echo $bookmarked_filter ? '&bookmarked=1' : ''; ?>" class="filter-btn <?php echo $difficulty_filter === 'all' ? 'active' : ''; ?>">All</a>
<a href="?difficulty=easy<?php echo $platform_filter !== 'all' ? '&platform=' . $platform_filter : ''; ?><?php echo $bookmarked_filter ? '&bookmarked=1' : ''; ?>" class="filter-btn <?php echo $difficulty_filter === 'easy' ? 'active' : ''; ?>">Easy</a>
<a href="?difficulty=medium<?php echo $platform_filter !== 'all' ? '&platform=' . $platform_filter : ''; ?><?php echo $bookmarked_filter ? '&bookmarked=1' : ''; ?>" class="filter-btn <?php echo $difficulty_filter === 'medium' ? 'active' : ''; ?>">Medium</a>
<a href="?difficulty=hard<?php echo $platform_filter !== 'all' ? '&platform=' . $platform_filter : ''; ?><?php echo $bookmarked_filter ? '&bookmarked=1' : ''; ?>" class="filter-btn <?php echo $difficulty_filter === 'hard' ? 'active' : ''; ?>">Hard</a>
</div>
</div>
<div class="filter-group">
<div class="filter-label">Bookmarked</div>
<div class="filter-options">
<a href="?<?php echo $platform_filter !== 'all' ? 'platform=' . $platform_filter . '&' : ''; ?><?php echo $difficulty_filter !== 'all' ? 'difficulty=' . $difficulty_filter . '&' : ''; ?>bookmarked=1" class="filter-btn <?php echo $bookmarked_filter ? 'active' : ''; ?>">Bookmarked Only</a>
<a href="?<?php echo $platform_filter !== 'all' ? 'platform=' . $platform_filter : ''; ?><?php echo $difficulty_filter !== 'all' ? '&difficulty=' . $difficulty_filter : ''; ?>" class="filter-btn <?php echo !$bookmarked_filter ? 'active' : ''; ?>">All Problems</a>
</div>
</div>
<div class="filter-group">
<div class="filter-label">Solved Status</div>
<div class="filter-options">
<a href="?<?php echo $platform_filter !== 'all' ? 'platform=' . $platform_filter . '&' : ''; ?><?php echo $difficulty_filter !== 'all' ? 'difficulty=' . $difficulty_filter . '&' : ''; ?>solved=1" class="filter-btn <?php echo $solved_filter ? 'active' : ''; ?>">Solved Only</a>
<a href="?<?php echo $platform_filter !== 'all' ? 'platform=' . $platform_filter : ''; ?><?php echo $difficulty_filter !== 'all' ? '&difficulty=' . $difficulty_filter : ''; ?>" class="filter-btn <?php echo !$solved_filter ? 'active' : ''; ?>">All Problems</a>
</div>
</div>
</div>
<div class="problems-container">
<?php if (empty($filtered_problems)): ?>
<div class="dashboard-card">
<p>No problems found matching your filters.</p>
</div>
<?php else: ?>
<?php foreach ($filtered_problems as $problem): ?>
<div class="problem-card <?php echo strtolower($problem['difficulty']); ?>">
<div class="problem-header">
<div class="problem-title">
<?php echo htmlspecialchars($problem['title']); ?>
<?php if (strtolower($problem['platform']) === 'custom'): ?>
<span class="custom-badge"><i class="fas fa-user-edit"></i> User Added</span>
<?php endif; ?>
</div>
<div class="problem-platform platform-<?php echo strtolower($problem['platform']); ?>">
<?php echo htmlspecialchars($problem['platform']); ?>
</div>
</div>
<div class="problem-description">
<?php echo htmlspecialchars($problem['description']); ?>
</div>
<div class="problem-tags">
<?php foreach ($problem['tags'] as $tag): ?>
<span class="tag"><?php echo htmlspecialchars($tag); ?></span>
<?php endforeach; ?>
</div>
<div class="problem-actions">
<div class="difficulty difficulty-<?php echo strtolower($problem['difficulty']); ?>">
<?php echo htmlspecialchars($problem['difficulty']); ?>
<?php if ($problem['solved']): ?>
<span class="solved-badge"><i class="fas fa-check-circle"></i> Solved</span>
<?php endif; ?>
</div>
<div class="action-buttons">
<button class="problem-btn bookmark-btn <?php echo $problem['bookmarked'] ? 'active' : ''; ?>" onclick="toggleBookmark(<?php echo $problem['id']; ?>)">
<i class="fas <?php echo $problem['bookmarked'] ? 'fa-bookmark' : 'fa-bookmark'; ?>"></i>
<?php echo $problem['bookmarked'] ? 'Bookmarked' : 'Bookmark'; ?>
</button>
<?php if (!$problem['solved']): ?>
<button class="problem-btn solved-btn" onclick="markAsSolved(<?php echo $problem['id']; ?>)">
<i class="fas fa-check-circle"></i>
Mark as Solved
</button>
<?php endif; ?>
<a href="<?php echo htmlspecialchars($problem['url']); ?>" target="_blank" class="problem-btn">
<i class="fas fa-external-link-alt"></i>
Solve
</a>
</div>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
<script>
function confirmLogout() {
let logoutConfirm = confirm("Are you sure you want to logout?");
if (logoutConfirm) {
window.location.href = 'logout.php';
}
}
function toggleBookmark(problemId) {
const button = event.currentTarget;
const formData = new FormData();
formData.append('action', 'toggle_bookmark');
formData.append('problem_id', problemId);
// Send AJAX request
fetch('problems.php', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
// Update UI based on server response
if (data.bookmarked) {
button.classList.add('active');
button.innerHTML = '<i class="fas fa-bookmark"></i> Bookmarked';
} else {
button.classList.remove('active');
button.innerHTML = '<i class="fas fa-bookmark"></i> Bookmark';
}
} else {
console.error('Failed to toggle bookmark');
}
})
.catch(error => {
console.error('Error:', error);
});
}
function markAsSolved(problemId) {
// Show solution form in a modal
const modal = document.createElement('div');
modal.className = 'modal';
modal.innerHTML = `
<div class="modal-content">
<div class="modal-header">
<h3>Mark Problem as Solved</h3>
<button class="close-btn" onclick="closeModal(this.parentNode.parentNode.parentNode)">×</button>
</div>
<div class="modal-body">
<form id="solutionForm">
<div class="form-group">
<label for="language">Programming Language</label>
<select id="language" name="language" class="form-control">
<option value="C++">C++</option>
<option value="Java">Java</option>
<option value="Python">Python</option>
<option value="JavaScript">JavaScript</option>
<option value="Other">Other</option>
</select>
</div>
<div class="form-group">
<label for="timeTaken">Time Taken (minutes)</label>
<input type="number" id="timeTaken" name="timeTaken" class="form-control" min="1">
</div>
<div class="form-group">
<label for="solutionCode">Solution Code (optional)</label>
<textarea id="solutionCode" name="solutionCode" class="form-control" rows="10"></textarea>
</div>
<div class="form-actions">
<button type="button" class="cancel-btn" onclick="closeModal(this.parentNode.parentNode.parentNode.parentNode.parentNode)">Cancel</button>
<button type="button" class="submit-btn" onclick="submitSolution(${problemId})">Mark as Solved</button>
</div>
</form>
</div>
</div>
`;
document.body.appendChild(modal);
// Add modal styles if not already added
if (!document.getElementById('modalStyles')) {
const style = document.createElement('style');
style.id = 'modalStyles';
style.textContent = `
.modal {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
display: flex;
justify-content: center;
align-items: center;
z-index: 1000;
backdrop-filter: blur(5px);
animation: fadeIn 0.3s ease-out;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
@keyframes slideIn {
from { transform: translateY(-20px); opacity: 0; }
to { transform: translateY(0); opacity: 1; }
}
.modal-content {
background-color: #1e2130;
border-radius: 12px;
width: 90%;
max-width: 650px;
max-height: 90vh;
overflow-y: auto;
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.5);
animation: slideIn 0.3s ease-out;
border: 1px solid #3f4865;
}
.modal-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 20px 25px;
border-bottom: 1px solid #3f4865;
background-color: #171923;
}
.modal-header h3 {
margin: 0;
font-size: 20px;
color: #e2e8f0;
font-weight: 600;
}
.close-btn {
background: none;
border: none;
font-size: 28px;
color: #a0aec0;
cursor: pointer;
transition: color 0.2s;
line-height: 1;
padding: 0;
margin: 0;
}
.close-btn:hover {
color: #e2e8f0;
}
.modal-body {
padding: 25px;
}
.form-group {
margin-bottom: 24px;
}
.form-group label {
display: block;
margin-bottom: 10px;
color: #e2e8f0;
font-weight: 500;
font-size: 15px;
}
.form-control {
width: 100%;
padding: 12px 15px;
background-color: #171923;
border: 1px solid #3f4865;
border-radius: 8px;
color: white;
font-size: 15px;
transition: border-color 0.3s, box-shadow 0.3s;
}
.form-control:focus {
border-color: #4299e1;
outline: none;
box-shadow: 0 0 0 2px rgba(66, 153, 225, 0.3);
}
textarea.form-control {
resize: vertical;
min-height: 120px;
line-height: 1.5;
font-family: inherit;
}
.form-actions {
display: flex;
justify-content: flex-end;
gap: 15px;
margin-top: 30px;
}
.cancel-btn, .submit-btn {
padding: 12px 24px;
border-radius: 8px;
cursor: pointer;
font-size: 15px;
font-weight: 600;
transition: all 0.3s;
}
.cancel-btn {
background-color: transparent;
border: 1px solid #3f4865;
color: #e2e8f0;
}
.cancel-btn:hover {
background-color: #2d3748;
}
.submit-btn {
background-color: #4299e1;
color: white;
border: none;
box-shadow: 0 4px 6px rgba(66, 153, 225, 0.2);
}
.submit-btn:hover {
background-color: #3182ce;
transform: translateY(-2px);
box-shadow: 0 6px 8px rgba(66, 153, 225, 0.3);
}
.submit-btn:active {
transform: translateY(0);
}
`;
document.head.appendChild(style);
}
}
function submitSolution(problemId) {
const language = document.getElementById('language').value;
const timeTaken = document.getElementById('timeTaken').value;
const solutionCode = document.getElementById('solutionCode').value;
if (!language || !timeTaken) {
alert('Please fill in all required fields');
return;
}
// Create form data
const formData = new FormData();
formData.append('action', 'mark_solved');
formData.append('problem_id', problemId);
formData.append('language', language);
formData.append('time_taken', timeTaken);
formData.append('solution_code', solutionCode);
// Send AJAX request
fetch('problems.php', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert(data.message);
// Close the modal
const modal = document.querySelector('.modal');
document.body.removeChild(modal);
// Reload the page to update the UI
window.location.reload();
} else {
alert('Error: ' + data.message);
}
})
.catch(error => {
console.error('Error:', error);
alert('An error occurred while marking the problem as solved.');
});
}
// Simple client-side search functionality
document.getElementById('problemSearch').addEventListener('input', function(e) {
const searchTerm = e.target.value.toLowerCase();
const problemCards = document.querySelectorAll('.problem-card');
problemCards.forEach(card => {
const title = card.querySelector('.problem-title').textContent.toLowerCase();
const description = card.querySelector('.problem-description').textContent.toLowerCase();
const tags = Array.from(card.querySelectorAll('.tag')).map(tag => tag.textContent.toLowerCase());
const matchesSearch = title.includes(searchTerm) ||
description.includes(searchTerm) ||
tags.some(tag => tag.includes(searchTerm));
card.style.display = matchesSearch ? 'block' : 'none';
});
});
</script>
</body>
</html>