Skip to content

Commit 7591019

Browse files
authored
feat(tracker): manage optional subjects and excluded subjects in tracker and profile (#419)
* fix(forum): filter subjects and chapters by is_trackable * feat(tracker): manage optional subjects and excluded subjects in tracker and profile * refactor(tracker): switch to include-first optional subjects selection and seed by slugs * feat(tracker): add eye-catching optional subjects callout banner and status bar
1 parent d918287 commit 7591019

9 files changed

Lines changed: 808 additions & 131 deletions

File tree

‎app/Http/Controllers/ForumController.php‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ public function index(Request $request): Response
6565
}
6666

6767
$subjects = Cache::remember('forum_filter_subjects', now()->addDay(), function () {
68-
return Subject::select('id', 'name', 'course', 'slug')
69-
->with(['nodes' => fn ($q) => $q->whereNull('parent_id')->select('id', 'subject_id', 'name', 'slug')->orderBy('sort_order')])
68+
return Subject::where('is_trackable', true)
69+
->select('id', 'name', 'course', 'slug')
70+
->with(['nodes' => fn ($q) => $q->where('is_trackable', true)->select('id', 'subject_id', 'name', 'slug')->orderBy('sort_order')])
7071
->orderBy('sort_order')
7172
->get()
7273
->toArray();
@@ -182,8 +183,9 @@ public function create(): Response|RedirectResponse
182183
}
183184

184185
$subjects = Cache::remember('forum_filter_subjects', now()->addDay(), function () {
185-
return Subject::select('id', 'name', 'course', 'slug')
186-
->with(['nodes' => fn ($q) => $q->whereNull('parent_id')->select('id', 'subject_id', 'name', 'slug')->orderBy('sort_order')])
186+
return Subject::where('is_trackable', true)
187+
->select('id', 'name', 'course', 'slug')
188+
->with(['nodes' => fn ($q) => $q->where('is_trackable', true)->select('id', 'subject_id', 'name', 'slug')->orderBy('sort_order')])
187189
->orderBy('sort_order')
188190
->get()
189191
->toArray();

‎app/Http/Controllers/StudyTrackerController.php‎

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,50 @@ public function index(Request $request): Response
1818
$user = $request->user();
1919
$course = $user?->curriculum ?: 'hsc';
2020
$group = $user?->group ?: 'science';
21+
$optionalSubjectIds = (array) ($user?->optional_subject_ids ?? []);
2122

22-
$subjects = Subject::where('course', $course)
23+
$subjectsQuery = Subject::where('course', $course)
2324
->where('is_trackable', true)
24-
->whereIn('group', [$group, 'common'])
25-
->orderBy('sort_order', 'asc')
25+
->where(function ($query) use ($group, $optionalSubjectIds) {
26+
$query->where(function ($q) use ($group) {
27+
$q->where('is_optional', false)
28+
->whereIn('group', [$group, 'common']);
29+
});
30+
31+
if (! empty($optionalSubjectIds)) {
32+
$query->orWhere(function ($q) use ($group, $optionalSubjectIds) {
33+
$q->where('is_optional', true)
34+
->whereIn('group', [$group, 'common'])
35+
->whereIn('id', $optionalSubjectIds);
36+
});
37+
}
38+
})
39+
->orderBy('sort_order', 'asc');
40+
41+
$subjects = $subjectsQuery
2642
->with(['nodes' => function ($query) {
2743
$query->where('is_trackable', true)
44+
->with('parent:id,name,sort_order')
2845
->orderBy('sort_order', 'asc')
29-
->select('id', 'subject_id', 'name', 'slug', 'sort_order', 'weight');
46+
->select('id', 'subject_id', 'parent_id', 'name', 'slug', 'sort_order', 'weight');
3047
}])
31-
->get(['id', 'name', 'slug', 'course', 'group', 'sort_order'])
48+
->get(['id', 'name', 'slug', 'course', 'group', 'sort_order', 'is_optional'])
49+
->toArray();
50+
51+
// All trackable optional subjects for curriculum modal (so user can toggle optional subjects on/off)
52+
$groupSubjects = Subject::where('is_trackable', true)
53+
->where('is_optional', true)
54+
->orderBy('sort_order', 'asc')
55+
->get(['id', 'name', 'course', 'group', 'is_optional'])
3256
->toArray();
3357

3458
if (! $user) {
3559
return Inertia::render('Tracker/Index', [
3660
'course' => $course,
3761
'group' => $group,
3862
'subjects' => $subjects,
63+
'optionalSubjectIds' => [],
64+
'optionalSubjects' => $groupSubjects,
3965
'completedNodeIds' => [],
4066
'todaySeconds' => 0,
4167
'stats' => [
@@ -64,6 +90,8 @@ public function index(Request $request): Response
6490
'course' => $course,
6591
'group' => $group,
6692
'subjects' => $subjects,
93+
'optionalSubjectIds' => $optionalSubjectIds,
94+
'optionalSubjects' => $groupSubjects,
6795
'completedNodeIds' => $completedNodeIds,
6896
'todaySeconds' => $todaySeconds,
6997
'stats' => $studyData['stats'],
@@ -147,9 +175,15 @@ public function updateCurriculum(Request $request)
147175
$validated = $request->validate([
148176
'curriculum' => 'required|string|in:hsc,ssc',
149177
'group' => 'required|string|in:science,humanities,commerce',
178+
'optional_subject_ids' => 'nullable|array',
179+
'optional_subject_ids.*' => 'integer|exists:subjects,id',
150180
]);
151181

152-
$request->user()->update($validated);
182+
$request->user()->update([
183+
'curriculum' => $validated['curriculum'],
184+
'group' => $validated['group'],
185+
'optional_subject_ids' => $validated['optional_subject_ids'] ?? [],
186+
]);
153187

154188
return redirect()->route('tracker.index')
155189
->with('success', 'Curriculum updated to '.strtoupper($validated['curriculum']).' ('.ucfirst($validated['group']).').');

‎app/Http/Controllers/UserProfileController.php‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,14 +393,32 @@ private function getSyllabusProgress(User $user): array
393393
{
394394
$course = $user->curriculum ?: 'hsc';
395395
$group = $user->group ?: 'science';
396-
$trackableSubjects = Subject::where('course', $course)
396+
$optionalSubjectIds = (array) ($user->optional_subject_ids ?? []);
397+
398+
$subjectsQuery = Subject::where('course', $course)
397399
->where('is_trackable', true)
398-
->whereIn('group', [$group, 'common'])
399-
->orderBy('sort_order', 'asc')
400+
->where(function ($query) use ($group, $optionalSubjectIds) {
401+
$query->where(function ($q) use ($group) {
402+
$q->where('is_optional', false)
403+
->whereIn('group', [$group, 'common']);
404+
});
405+
406+
if (! empty($optionalSubjectIds)) {
407+
$query->orWhere(function ($q) use ($group, $optionalSubjectIds) {
408+
$q->where('is_optional', true)
409+
->whereIn('group', [$group, 'common'])
410+
->whereIn('id', $optionalSubjectIds);
411+
});
412+
}
413+
})
414+
->orderBy('sort_order', 'asc');
415+
416+
$trackableSubjects = $subjectsQuery
400417
->with(['nodes' => function ($query) {
401418
$query->where('is_trackable', true)
419+
->with('parent:id,name,sort_order')
402420
->orderBy('sort_order', 'asc')
403-
->select('id', 'subject_id', 'name', 'slug', 'sort_order', 'weight');
421+
->select('id', 'subject_id', 'parent_id', 'name', 'slug', 'sort_order', 'weight');
404422
}])
405423
->get(['id', 'name', 'slug', 'course', 'tailwind_format', 'icon', 'sort_order']);
406424

@@ -439,6 +457,12 @@ private function getSyllabusProgress(User $user): array
439457
'name' => $node->name,
440458
'weight' => $nodeWeight,
441459
'is_completed' => $isDone,
460+
'parent_id' => $node->parent_id,
461+
'parent' => $node->parent ? [
462+
'id' => $node->parent->id,
463+
'name' => $node->parent->name,
464+
'sort_order' => $node->parent->sort_order,
465+
] : null,
442466
];
443467
}
444468

‎app/Models/Subject.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ protected function casts(): array
1212
{
1313
return [
1414
'is_trackable' => 'boolean',
15+
'is_optional' => 'boolean',
1516
];
1617
}
1718

‎app/Models/User.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class User extends Authenticatable
5959
'facebook',
6060
'instagram',
6161
'github',
62+
'optional_subject_ids',
6263
'last_active_at',
6364
'last_seen_at',
6465
];
@@ -121,6 +122,7 @@ protected function casts(): array
121122
'is_verified' => 'boolean',
122123
'priority' => 'integer',
123124
'allow_pokes' => 'boolean',
125+
'optional_subject_ids' => 'array',
124126
];
125127
}
126128

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\DB;
6+
use Illuminate\Support\Facades\Schema;
7+
8+
return new class extends Migration
9+
{
10+
/**
11+
* Run the migrations.
12+
*/
13+
public function up(): void
14+
{
15+
Schema::table('subjects', function (Blueprint $table) {
16+
$table->boolean('is_optional')->default(false)->after('is_trackable');
17+
});
18+
19+
Schema::table('users', function (Blueprint $table) {
20+
$table->json('optional_subject_ids')->nullable()->after('group');
21+
});
22+
23+
// Seed initial known optional subjects by unique slug:
24+
$optionalSlugs = [
25+
// HSC Science
26+
'hsc-hmath-1st',
27+
'hsc-hmath-2nd',
28+
'hsc-biology-1st',
29+
'hsc-biology-2nd',
30+
// SSC Science
31+
'ssc-biology',
32+
'ssc-higher-math',
33+
// HSC Commerce
34+
'hsc-finance-1st',
35+
'hsc-finance-2nd',
36+
'hsc-marketing-1st',
37+
'hsc-marketing-2nd',
38+
// HSC Humanities
39+
'hsc-economics-1st',
40+
'hsc-economics-2nd',
41+
'hsc-logic-1st',
42+
'hsc-logic-2nd',
43+
'hsc-islamic-history-1st',
44+
'hsc-islamic-history-2nd',
45+
];
46+
47+
DB::table('subjects')->whereIn('slug', $optionalSlugs)->update(['is_optional' => true]);
48+
}
49+
50+
/**
51+
* Reverse the migrations.
52+
*/
53+
public function down(): void
54+
{
55+
Schema::table('users', function (Blueprint $table) {
56+
$table->dropColumn('optional_subject_ids');
57+
});
58+
59+
Schema::table('subjects', function (Blueprint $table) {
60+
$table->dropColumn('is_optional');
61+
});
62+
}
63+
};

0 commit comments

Comments
 (0)