-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboardwindow.cpp
More file actions
1102 lines (880 loc) · 35.9 KB
/
Copy pathdashboardwindow.cpp
File metadata and controls
1102 lines (880 loc) · 35.9 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
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "dashboardwindow.h"
#include "ui_dashboardwindow.h"
#include <QMessageBox>
#include <QFile>
#include <QTextStream>
#include <QListWidgetItem>
#include <QDebug>
// CONSTRUCTOR & DESTRUCTOR
DashboardWindow::DashboardWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::DashboardWindow) {
ui->setupUi(this);
ui->managerControls->setVisible(false);
ui->studentControls->setVisible(false);
// Ensure necessary files exist
QFile file("programs.txt");
if (!file.exists()) {
QFile newFile("programs.txt");
newFile.open(QIODevice::WriteOnly | QIODevice::Text);
newFile.close();
}
QFile courseFile("courses.txt");
if (!courseFile.exists()) {
QFile newFile("courses.txt");
newFile.open(QIODevice::WriteOnly | QIODevice::Text);
newFile.close();
}
loadPrograms();
loadCourses(selectedProgramName); // Pass the selected Study Program name
loadTopics(selectedCourseName, false); // Manager Mode // Pass the selected Course name
}
DashboardWindow::~DashboardWindow() {
delete ui;
}
// MODE SWITCHING FUNCTIONS
// Enable Manager View
void DashboardWindow::enableManagerMode() {
ui->managerControls->setVisible(true);
ui->studentControls->setVisible(false);
// **Force update to show current topics & lessons**
loadPrograms();
loadCourses(selectedProgramName);
loadTopics(selectedCourseName, false); // Manager Mode
loadLessons(selectedSubTopicName);
}
// Enable Student View
void DashboardWindow::setStudentSession(QString studentID, QString studentName) {
loggedInStudentID = studentID;
loggedInStudentName = studentName;
qDebug() << "Stored Student ID: " << loggedInStudentID; // Check if ID is saved
}
void DashboardWindow::enableStudentMode() {
ui->managerControls->setVisible(false);
ui->studentControls->setVisible(true);
}
// GENERIC FILE I/O FUNCTIONS
// Generic function to save data
void DashboardWindow::saveToFile(QString fileName, QString data) {
QFile file(fileName);
if (!file.open(QIODevice::Append | QIODevice::Text)) {
QMessageBox::warning(this, "Error", "Could not save to file!");
return;
}
QTextStream out(&file);
out << data << "\n";
file.close();
}
// Generic function to load data
QStringList DashboardWindow::loadFromFile(QString fileName) {
QStringList dataList;
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return dataList;
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
dataList.append(line);
}
file.close();
return dataList;
}
// LOAD FUNCTIONS FOR DATA DISPLAY
// Load programs from file into UI
void DashboardWindow::loadPrograms() {
ui->programListWidget->clear(); // Manager's list
ui->availableProgramsListWidget->clear(); // Student's list
QStringList programs = loadFromFile("programs.txt");
if (programs.isEmpty()) {
QMessageBox::warning(this, "Error", "No study programs available! Please add programs first.");
return;
}
for (const QString &program : programs) {
ui->programListWidget->addItem(program); // Manager view
ui->availableProgramsListWidget->addItem(program); // Student view
}
}
// Load courses based on selected program
void DashboardWindow::loadCourses(QString programName) {
ui->courseListWidget->clear();
QFile file("courses.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
QStringList parts = line.split(" | ");
if (parts.size() == 2 && parts[0] == programName) {
ui->courseListWidget->addItem(parts[1]);
}
}
file.close();
}
// Load topics based on selected course
void DashboardWindow::loadTopics(QString courseName, bool isStudentMode) {
qDebug() << "Attempting to load topics for: " << courseName;
QStringList topics = loadFromFile("topics.txt");
ui->topicListWidget->clear();
for (const QString &topicEntry : topics) {
QStringList parts = topicEntry.split(" | ");
if (parts.size() == 2 && parts[0] == courseName) {
QString topicName = parts[1];
qDebug() << "Loading Topic: " << topicName;
QListWidgetItem *topicItem = new QListWidgetItem(topicName);
ui->topicListWidget->addItem(topicItem);
// Filter subtopics that belong to this topic
QStringList subTopics = loadFromFile("subtopics.txt");
for (const QString &subTopicEntry : subTopics) {
QStringList subParts = subTopicEntry.split(" | ");
if (subParts.size() == 2 && subParts[0] == topicName) {
QString subTopicName = subParts[1].trimmed();
qDebug() << "Loading SubTopic: " << subTopicName;
QListWidgetItem *subTopicItem = new QListWidgetItem(" ➥ " + subTopicName);
subTopicItem->setForeground(Qt::black);
ui->topicListWidget->addItem(subTopicItem);
}
}
}
}
}
// Load lessons from file into UI
void DashboardWindow::loadLessons(QString subTopicName) {
ui->lessonListWidget->clear();
QFile file(subTopicName + "_lessons.txt");
if (!file.exists()) {
qDebug() << "Lesson file not found: " << subTopicName + "_lessons.txt";
return;
}
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "Failed to open lesson file!";
return;
}
QTextStream in(&file);
while (!in.atEnd()) {
QString lessonTitle = in.readLine();
QString lessonDescription = in.readLine();
in.readLine(); // Skip separator "---"
ui->lessonListWidget->addItem(lessonTitle);
qDebug() << "Lesson Loaded: " << lessonTitle;
}
file.close();
}
// Load courses for student view
void DashboardWindow::loadStudentCourses(QString programName) {
ui->courseListWidget_2->clear();
QStringList courses = loadFromFile("courses.txt");
if (courses.isEmpty()) {
QMessageBox::warning(this, "Error", "No courses available for this program!");
return;
}
for (const QString &course : courses) {
QStringList parts = course.split(" | ");
if (parts.size() == 2 && parts[0] == programName) {
ui->courseListWidget_2->addItem(parts[1]);
}
}
}
// MANAGER MODE - PROGRAM MANAGEMENT
// **Add a Study Program**
void DashboardWindow::on_addProgramButton_clicked() {
QString programName = ui->programNameInput->text().trimmed();
if (programName.isEmpty()) {
QMessageBox::warning(this, "Error", "Please enter a valid program name!");
return;
}
// Prevent negative numbers or purely numeric names
if (programName.startsWith("-") || programName.contains(QRegularExpression("^-?\\d+$"))) {
QMessageBox::warning(this, "Error", "Program names cannot be negative or only numbers!");
return;
}
// Check if the program already exists in programs.txt
QFile file("programs.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::warning(this, "Error", "Could not access program database!");
return;
}
QTextStream in(&file);
while (!in.atEnd()) {
QString existingProgram = in.readLine().trimmed();
if (existingProgram.compare(programName, Qt::CaseInsensitive) == 0) {
file.close();
QMessageBox::warning(this, "Error", "Program '" + programName + "' already exists!");
return;
}
}
file.close();
// If program doesn't exist, add it
if (!file.open(QIODevice::Append | QIODevice::Text)) {
QMessageBox::warning(this, "Error", "Could not save program!");
return;
}
QTextStream out(&file);
out << programName << "\n";
file.close();
ui->programListWidget->addItem(programName);
QMessageBox::information(this, "Success", "Program added successfully!");
}
// **Remove a Study Program**
void DashboardWindow::on_removeProgramButton_clicked() {
QListWidgetItem *selectedItem = ui->programListWidget->currentItem();
if (!selectedItem) {
QMessageBox::warning(this, "Error", "Select a program to remove!");
return;
}
QString programToRemove = selectedItem->text();
delete selectedItem;
QStringList programs = loadFromFile("programs.txt");
programs.removeAll(programToRemove);
QFile file("programs.txt");
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&file);
for (const QString &program : programs) {
out << program << "\n";
}
file.close();
}
// **Edit a Study Program**
void DashboardWindow::on_editProgramButton_clicked() {
QListWidgetItem *selectedItem = ui->programListWidget->currentItem();
QString newProgramName = ui->programNameInput->text();
if (!selectedItem || newProgramName.isEmpty()) {
QMessageBox::warning(this, "Error", "Select a program and enter a new name!");
return;
}
QString oldProgramName = selectedItem->text();
selectedItem->setText(newProgramName);
QStringList programs = loadFromFile("programs.txt");
programs.replaceInStrings(oldProgramName, newProgramName);
QFile file("programs.txt");
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&file);
for (const QString &program : programs) {
out << program << "\n";
}
file.close();
ui->programNameInput->clear();
}
// MANAGER MODE - COURSE MANAGEMENT
// **Add a Course to a Selected Study Program**
void DashboardWindow::on_addCourseButton_clicked() {
QListWidgetItem *selectedProgram = ui->programListWidget->currentItem();
QString courseName = ui->courseNameInput->text().trimmed(); // Trim spaces
// Check if a program is selected
if (!selectedProgram) {
QMessageBox::warning(this, "Error", "Select a study program before adding a course!");
return;
}
// Check if course name is empty or contains only spaces
if (courseName.isEmpty()) {
QMessageBox::warning(this, "Invalid Input", "Course name cannot be empty or just spaces!");
return;
}
// Check if course name contains numbers or special characters (only allows letters and spaces)
QRegularExpression regex("^[A-Za-z ]+$"); // Allows only alphabet characters
if (!regex.match(courseName).hasMatch()) {
QMessageBox::warning(this, "Invalid Input", "Course name can only contain letters!");
return;
}
QString programName = selectedProgram->text();
saveToFile("courses.txt", programName + " | " + courseName); // Store course WITH program name
// Add course to the UI list immediately
ui->courseListWidget->addItem(courseName);
ui->courseNameInput->clear();
// Debugging output to verify success
qDebug() << "Added course: " << courseName << " under program: " << programName;
}
// **Remove a Course (Ensuring Correct Study Program Link)**
void DashboardWindow::on_removeCourseButton_clicked() {
QListWidgetItem *selectedItem = ui->courseListWidget->currentItem();
if (!selectedItem) {
QMessageBox::warning(this, "Error", "Select a course to remove!");
return;
}
QString courseToRemove = selectedItem->text();
delete selectedItem;
QStringList courses = loadFromFile("courses.txt");
courses.removeAll(selectedProgramName + " | " + courseToRemove); // Ensure correct course removal
QFile file("courses.txt");
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&file);
for (const QString &course : courses) {
out << course << "\n";
}
file.close();
}
// **Edit a Course (Keeping Study Program Link Intact)**
void DashboardWindow::on_editCourseButton_clicked() {
QListWidgetItem *selectedItem = ui->courseListWidget->currentItem();
QString newCourseName = ui->courseNameInput->text();
if (!selectedItem || newCourseName.isEmpty()) {
QMessageBox::warning(this, "Error", "Select a course and enter a new name!");
return;
}
QString oldCourseName = selectedItem->text();
selectedItem->setText(newCourseName);
QStringList courses = loadFromFile("courses.txt");
courses.replaceInStrings(selectedProgramName + " | " + oldCourseName, selectedProgramName + " | " + newCourseName); // Ensure program link remains
QFile file("courses.txt");
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&file);
for (const QString &course : courses) {
out << course << "\n";
}
file.close();
ui->courseNameInput->clear();
}
// MANAGER MODE - TOPIC MANAGEMENT
// Add a new topic
void DashboardWindow::on_addTopicButton_clicked() {
QListWidgetItem *selectedCourse = ui->courseListWidget->currentItem();
QString topicName = ui->topicNameInput->text().trimmed(); // Trim spaces
// Ensure a course is selected before adding a topic
if (!selectedCourse) {
QMessageBox::warning(this, "Error", "Select a course before adding a topic!");
return;
}
// Validate topic name: Cannot be empty, spaces-only, or contain numbers/symbols
if (topicName.isEmpty()) {
QMessageBox::warning(this, "Invalid Input", "Topic name cannot be empty or just spaces!");
return;
}
QRegularExpression regex("^[A-Za-z ]+$"); // Allows only alphabet characters
if (!regex.match(topicName).hasMatch()) {
QMessageBox::warning(this, "Invalid Input", "Topic name can only contain letters!");
return;
}
QString courseName = selectedCourse->text();
// Save topic data as "CourseName | TopicName" to ensure correct mapping
saveToFile("topics.txt", courseName + " | " + topicName);
// Add topic to the UI list immediately
ui->topicListWidget->addItem(topicName);
ui->topicNameInput->clear();
// Debug logging to verify success
qDebug() << "Added topic: " << topicName << " under course: " << courseName;
}
// Edit a topic
void DashboardWindow::on_editTopicButton_clicked() {
QListWidgetItem *selectedItem = ui->topicListWidget->currentItem();
QString newTopicName = ui->topicNameInput->text();
if (!selectedItem || newTopicName.isEmpty()) {
QMessageBox::warning(this, "Error", "Select a topic and enter a new name!");
return;
}
QString oldTopicName = selectedItem->text();
selectedItem->setText(newTopicName); // Update in the UI
// Read all topics from file and update all references from old to new name
QStringList topics = loadFromFile("topics.txt");
topics.replaceInStrings(oldTopicName, newTopicName);
QFile file("topics.txt");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::warning(this, "Error", "Could not open topics.txt for writing!");
return;
}
{
QTextStream out(&file);
for (const QString &topic : topics) {
out << topic << "\n";
}
}
file.close();
// Rename the subtopics file (if it exists) to match the new topic name
QFile oldSubTopicFile(oldTopicName + "_subtopics.txt");
QFile newSubTopicFile(newTopicName + "_subtopics.txt");
if (oldSubTopicFile.exists()) {
oldSubTopicFile.rename(newSubTopicFile.fileName());
}
ui->topicNameInput->clear();
}
// Remove a topic or a subtopic
void DashboardWindow::on_removeTopicButton_clicked() {
QListWidgetItem *selectedItem = ui->topicListWidget->currentItem();
if (!selectedItem) {
QMessageBox::warning(this, "Error", "Select a topic or subtopic to remove!");
return;
}
QString selectedText = selectedItem->text();
bool isSubTopic = selectedText.startsWith(" ➥ "); // Identify subtopics by the prefix
if (isSubTopic) {
// Removing a subtopic: find its parent topic first
QString subTopicName = selectedText.mid(4);
QListWidgetItem *parentTopic = ui->topicListWidget->item(ui->topicListWidget->row(selectedItem) - 1);
if (!parentTopic) {
QMessageBox::warning(this, "Error", "Could not find parent topic for removal!");
return;
}
QString topicName = parentTopic->text();
QString subTopicFile = topicName + "_subtopics.txt";
QStringList subTopics = loadFromFile(subTopicFile);
subTopics.removeAll(subTopicName);
QFile file(subTopicFile);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::warning(this, "Error", "Could not open subtopic file for writing!");
return;
}
{
QTextStream out(&file);
for (const QString &subTopic : subTopics) {
out << subTopic << "\n";
}
}
file.close();
} else {
// Removing a topic: remove the subtopic file and update topics.txt accordingly
QFile topicFile(selectedText + "_subtopics.txt");
topicFile.remove();
QStringList topics = loadFromFile("topics.txt");
topics.removeAll(selectedText);
QFile file("topics.txt");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::warning(this, "Error", "Could not open topics.txt for writing!");
return;
}
{
QTextStream out(&file);
for (const QString &topic : topics) {
out << topic << "\n";
}
}
file.close();
}
// Remove the item from the UI list
delete selectedItem;
}
// MANAGER MODE - SUBTOPIC MANAGEMENT
// Add a subtopic
void DashboardWindow::on_addSubTopicButton_clicked() {
QListWidgetItem *selectedTopic = ui->topicListWidget->currentItem();
QString subTopicName = ui->subTopicNameInput->text().trimmed(); // Trim spaces
if (!selectedTopic || subTopicName.isEmpty()) {
QMessageBox::warning(this, "Error", "Select a topic and enter a subtopic name!");
return;
}
QString topicName = selectedTopic->text();
QString subTopicFile = topicName + "_subtopics.txt";
QFile file(subTopicFile);
// Ensure the subtopic file exists before writing
if (!file.exists()) {
qDebug() << "Creating subtopic file: " << subTopicFile;
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::warning(this, "Error", "Failed to create subtopic file!");
return;
}
file.close();
}
// Append new subtopic to the file
if (!file.open(QIODevice::Append | QIODevice::Text)) {
QMessageBox::warning(this, "Error", "Could not open subtopic file for writing!");
return;
}
QTextStream out(&file);
out << subTopicName << "\n";
file.close();
// Update UI immediately
QListWidgetItem *subTopicItem = new QListWidgetItem(" ➥ " + subTopicName);
subTopicItem->setForeground(Qt::black);
ui->topicListWidget->addItem(subTopicItem);
ui->subTopicNameInput->clear();
}
// MANAGER MODE - LESSON MANAGEMENT
// Add a lesson
void DashboardWindow::on_addLessonButton_clicked() {
QListWidgetItem *selectedSubTopic = ui->topicListWidget->currentItem();
QString lessonTitle = ui->lessonTitleInput->text().trimmed(); // Trim spaces
QString lessonDescription = ui->lessonDescriptionInput->toPlainText().trimmed(); // Trim spaces
// Ensure a subtopic is selected before adding a lesson
if (!selectedSubTopic) {
QMessageBox::warning(this, "Error", "Select a subtopic before adding a lesson!");
return;
}
// Validate lesson title: Cannot be empty, spaces-only, or contain numbers/symbols
if (lessonTitle.isEmpty()) {
QMessageBox::warning(this, "Invalid Input", "Lesson title cannot be empty or just spaces!");
return;
}
QRegularExpression regex("^[A-Za-z ]+$"); // Allows only alphabet characters
if (!regex.match(lessonTitle).hasMatch()) {
QMessageBox::warning(this, "Invalid Input", "Lesson title can only contain letters!");
return;
}
// Ensure lesson description is not empty
if (lessonDescription.isEmpty()) {
QMessageBox::warning(this, "Invalid Input", "Lesson description cannot be empty!");
return;
}
QString subTopicName = selectedSubTopic->text().mid(4);
QString lessonFile = subTopicName + "_lessons.txt";
QFile file(lessonFile);
// Create lesson file if it doesn't exist
if (!file.exists()) {
qDebug() << "Lesson file does not exist, creating a new one...";
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::warning(this, "Error", "Failed to create lesson file!");
return;
}
file.close();
}
// Open file in append mode and write the lesson
if (!file.open(QIODevice::Append | QIODevice::Text)) {
QMessageBox::warning(this, "Error", "Could not open lesson file for writing!");
return;
}
QTextStream out(&file);
out << lessonTitle << "\n" << lessonDescription << "\n---\n";
file.close();
// Update UI immediately
ui->lessonListWidget->addItem(lessonTitle);
ui->lessonTitleInput->clear();
ui->lessonDescriptionInput->clear();
// Debug logging
qDebug() << "Added lesson: " << lessonTitle << " under subtopic: " << subTopicName;
}
// Remove a lesson from the selected subtopic
void DashboardWindow::on_removeLessonButton_clicked() {
QListWidgetItem *selectedLesson = ui->lessonListWidget->currentItem();
QListWidgetItem *selectedSubTopic = ui->topicListWidget->currentItem();
if (!selectedLesson || !selectedSubTopic) {
QMessageBox::warning(this, "Error", "Select a subtopic and a lesson to remove!");
return;
}
QString lessonTitle = selectedLesson->text();
QString subTopicName = selectedSubTopic->text().mid(4);
QString lessonFile = subTopicName + "_lessons.txt";
QStringList lessons;
QFile file(lessonFile);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::warning(this, "Error", "Could not open lesson file!");
return;
}
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
// When the lesson title matches, skip the next two lines (description and separator)
if (line == lessonTitle) {
in.readLine(); // Skip lesson description
in.readLine(); // Skip separator "---"
} else {
lessons.append(line);
}
}
file.close();
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::warning(this, "Error", "Could not open lesson file for writing!");
return;
}
{
QTextStream out(&file);
for (const QString &lesson : lessons) {
out << lesson << "\n";
}
}
file.close();
// Remove the selected lesson from the UI list
delete selectedLesson;
}
// EVENT HANDLERS - SELECTION CHANGES
// Load student data & registered program on login
void DashboardWindow::loadStudentData(QString studentID) {
QFile file("students.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
QStringList parts = line.split(" | ");
if (parts.size() == 5 && parts[0] == studentID) {
ui->registeredProgramsListWidget->clear();
// Only show the actual program, remove "NONE"
if (parts[4] != "NONE") {
ui->registeredProgramsListWidget->addItem(parts[4]);
}
break;
}
}
file.close();
}
// Student program registration & updating student file
void DashboardWindow::on_registerProgramButton_clicked() {
// Get the program that the student selects from the available programs list
QListWidgetItem *selectedProgram = ui->availableProgramsListWidget->currentItem();
if (!selectedProgram) {
QMessageBox::warning(this, "Error", "Please select a program to register!");
return;
}
QString programName = selectedProgram->text();
// Prevent multiple registrations (check against stored data instead of UI list)
QFile file("students.txt");
QString currentProgram = "NONE"; // Assume they haven't registered yet
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
QStringList parts = line.split(" | ");
if (parts.size() == 5 && parts[0] == loggedInStudentID) {
currentProgram = parts[4]; // Get their stored program
break;
}
}
file.close();
}
if (currentProgram != "NONE") {
QMessageBox::warning(this, "Error", "You are already registered in " + currentProgram + "!");
return;
}
// Update the file to store the registered program instead of "NONE"
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::warning(this, "Error", "Could not access student records!");
return;
}
QStringList fileLines;
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
QStringList parts = line.split(" | ");
if (parts.size() == 5 && parts[0] == loggedInStudentID) {
parts[4] = programName; // Replace "NONE" with the selected program
line = parts.join(" | ");
}
fileLines.append(line);
}
file.close();
// Rewrite `students.txt` with updated records
if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
QMessageBox::warning(this, "Error", "Could not update student records!");
return;
}
QTextStream out(&file);
for (const QString &entry : fileLines) {
out << entry << "\n";
}
file.close();
// Reflect the registration change in the UI
ui->registeredProgramsListWidget->clear();
ui->registeredProgramsListWidget->addItem(programName);
QMessageBox::information(this, "Registration Successful", "You have been registered for " + programName + "!");
}
//Withdraw button clicked
void DashboardWindow::on_withdrawProgramButton_clicked() {
// Check if the student is already registered in a program
QFile file("students.txt");
QStringList fileLines;
QString currentProgram = "NONE";
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::warning(this, "Error", "Could not access student records!");
return;
}
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
QStringList parts = line.split(" | ");
if (parts.size() == 5 && parts[0] == loggedInStudentID) {
currentProgram = parts[4]; // Get registered program
if (currentProgram == "NONE") {
QMessageBox::warning(this, "Error", "You are not registered in any program!");
file.close();
return;
}
parts[4] = "NONE"; // Reset program to "NONE"
line = parts.join(" | ");
}
fileLines.append(line);
}
file.close();
// Overwrite `students.txt` with updated data
if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
QMessageBox::warning(this, "Error", "Could not update student records!");
return;
}
QTextStream out(&file);
for (const QString &entry : fileLines) {
out << entry << "\n";
}
file.close();
// Update UI to reflect withdrawal
ui->registeredProgramsListWidget->clear();
QMessageBox::information(this, "Success", "You have withdrawn from " + currentProgram + ". You may now register for another program.");
}
// Program selection changed (Manager Mode)
void DashboardWindow::on_programListWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous) {
if (!current) return;
selectedProgramName = current->text();
loadCourses(selectedProgramName);
}
// Course selection changed (Manager Mode)
void DashboardWindow::on_courseListWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous) {
if (!current) return;
selectedCourseName = current->text();
qDebug() << "Course Selected: " << selectedCourseName; // Debug Check
loadTopics(selectedCourseName, false); // Load topics for the selected course
}
// Topic selection changed (Manager Mode)
void DashboardWindow::on_topicListWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous) {
if (!current) return;
if (current->text().startsWith(" ➥ ")) {
selectedSubTopicName = current->text().mid(4); // Store subtopic name
qDebug() << "Selected subtopic: " << selectedSubTopicName;
// Load lessons when a subtopic is clicked
loadLessons(selectedSubTopicName);
return;
}
QString topicName = current->text();
QString subTopicFile = topicName + "_subtopics.txt";
QFile file(subTopicFile);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "Subtopics file not found: " << subTopicFile;
return;
}
// Preserve existing topic structure while loading subtopics
ui->topicListWidget->clear();
QStringList topics = loadFromFile("topics.txt");
for (const QString &topic : topics) {
QStringList parts = topic.split(" | ");
if (parts.size() == 2 && parts[0] == selectedCourseName) {
QListWidgetItem *topicItem = new QListWidgetItem(parts[1]);
ui->topicListWidget->addItem(topicItem);
// Load subtopics directly beneath their respective topics
QFile subFile(parts[1] + "_subtopics.txt");
if (subFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&subFile);
QStringList subTopics;
while (!in.atEnd()) {
QString subTopic = in.readLine();
qDebug() << "Loading SubTopic: " << subTopic;
subTopics.append(subTopic);
}
subFile.close();
// Loop through subTopics to add them visually under topics
ui->topicListWidget->setUpdatesEnabled(false);
for (const QString &subTopic : subTopics) {
QListWidgetItem *subTopicItem = new QListWidgetItem(" ➥ " + subTopic);
subTopicItem->setForeground(Qt::black); // Ensures visibility
QFont font;
font.setBold(true); // Makes subtopics bold
subTopicItem->setFont(font);
ui->topicListWidget->addItem(subTopicItem);
}
ui->topicListWidget->setUpdatesEnabled(true);
ui->topicListWidget->update();
ui->topicListWidget->repaint(); // Force UI update
}
}
}
}
// Lesson selection changed (Manager Mode)
void DashboardWindow::on_lessonListWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous) {
if (!current) return;
QListWidgetItem *selectedSubTopic = ui->topicListWidget->currentItem();
if (!selectedSubTopic) return;
QString subTopicName = selectedSubTopic->text().mid(4);
QString lessonFile = subTopicName + "_lessons.txt";
QFile file(lessonFile);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::warning(this, "Error", "Could not open lesson file!");
return;
}
QTextStream in(&file);
QString lessonTitle, lessonDescription;
bool foundLesson = false;
while (!in.atEnd()) {
lessonTitle = in.readLine();
lessonDescription = in.readLine();
in.readLine(); // Skip separator "---"
if (lessonTitle == current->text()) {
foundLesson = true;
ui->lessonDescriptionInput->setText(lessonDescription);
break;
}
}
file.close();
if (!foundLesson) {
ui->lessonDescriptionInput->clear();
}
}
// Course selection changed (Student Mode)
void DashboardWindow::on_courseListWidget_2_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous) {
if (!current) return;
selectedCourseName = current->text();
qDebug() << "Student Selected Course: " << selectedCourseName;
ui->topicListWidget_2->clear(); // Clear previous topics/subtopics
QStringList topics = loadFromFile("topics.txt");
for (const QString &entry : topics) {
QStringList parts = entry.split(" | ");
if (parts.size() == 2 && parts[0] == selectedCourseName) {
QListWidgetItem *topicItem = new QListWidgetItem(parts[1]); // Topic Name