From 8c953d0e4626068c8e6d87ffd10092a594370351 Mon Sep 17 00:00:00 2001 From: imadhigp Date: Mon, 24 Aug 2026 17:11:18 +0530 Subject: [PATCH] set my courses module 1 --- app/Http/Controllers/MyCoursesController.php | 65 +++ .../Controllers/adminmycoursesController.php | 124 ++++-- app/Models/CourseAssignStudent.php | 7 +- ...dent_id_to_course_assign_student_table.php | 30 ++ .../views/admin/adminmycourses.blade.php | 97 ++++- resources/views/mycourse.blade.php | 376 +++--------------- routes/web.php | 12 +- 7 files changed, 335 insertions(+), 376 deletions(-) create mode 100644 app/Http/Controllers/MyCoursesController.php create mode 100644 database/migrations/2026_08_24_064117_add_student_id_to_course_assign_student_table.php diff --git a/app/Http/Controllers/MyCoursesController.php b/app/Http/Controllers/MyCoursesController.php new file mode 100644 index 0000000..fbbd4b0 --- /dev/null +++ b/app/Http/Controllers/MyCoursesController.php @@ -0,0 +1,65 @@ +where('student_portal_log_id', $userId) + ->orWhere('id', $userId) + ->orWhere('student_id', $userId) + ->first(); + + + if (!$student) { + return view('mycourse', [ + 'courses' => collect([]), + 'student' => null + ]); + } + + + $courses = DB::table('courses') + ->join('course_assign_student', 'courses.id', '=', 'course_assign_student.course_id') + ->whereIn('course_assign_student.student_id', [ + $student->id, + $student->student_id, + $student->student_portal_log_id + ]) + ->select('courses.*', 'course_assign_student.assigned_at') + ->distinct() + ->get(); + + return view('mycourse', compact('courses', 'student')); + } + + + public function showModule($course_id) + { + $course = DB::table('courses')->where('id', $course_id)->first(); + + if (!$course) { + abort(404, 'Course not found'); + } + + $modules = DB::table('modules') + ->where('course_id', $course_id) + ->get(); + + return view('module', compact('course', 'modules')); + } + +} \ No newline at end of file diff --git a/app/Http/Controllers/adminmycoursesController.php b/app/Http/Controllers/adminmycoursesController.php index adf39f0..5a85818 100644 --- a/app/Http/Controllers/adminmycoursesController.php +++ b/app/Http/Controllers/adminmycoursesController.php @@ -5,6 +5,10 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Storage; +use App\Models\CourseAssignStudent; +use App\Models\Course; +use App\Models\studentDetails; +use Illuminate\Support\Facades\Schema; class adminmycoursesController extends Controller { @@ -25,7 +29,10 @@ class adminmycoursesController extends Controller ->get(); } - return view('admin.adminmycourses', compact('courses')); + // Needed for the "Assign Course to Students" modal on this page + $students = studentDetails::all(); + + return view('admin.adminmycourses', compact('courses', 'students')); } // --- COURSE CRUD METHODS --- @@ -69,50 +76,47 @@ class adminmycoursesController extends Controller } public function update(Request $request, $id) -{ - $request->validate([ - 'title' => 'required|string|max:255', - 'course_code' => 'required|string|max:50', - 'image' => 'nullable|image|mimes:jpeg,png,jpg,gif,webp|max:2048', - 'duration' => 'required|string', - 'level' => 'required|string', - 'status' => 'required|string', - ]); + { + $request->validate([ + 'title' => 'required|string|max:255', + 'course_code' => 'required|string|max:50', + 'image' => 'nullable|image|mimes:jpeg,png,jpg,gif,webp|max:2048', + 'duration' => 'required|string', + 'level' => 'required|string', + 'status' => 'required|string', + ]); - $course = DB::table('courses')->where('id', $id)->first(); + $course = DB::table('courses')->where('id', $id)->first(); - if (!$course) { - return redirect()->back()->with('error', 'Course not found!'); - } - - $imagePath = $course->image; - if ($request->hasFile('image')) { - if ($course->image && Storage::disk('public')->exists($course->image)) { - Storage::disk('public')->delete($course->image); + if (!$course) { + return redirect()->back()->with('error', 'Course not found!'); } - $imagePath = $request->file('image')->store('courses', 'public'); + + $imagePath = $course->image; + if ($request->hasFile('image')) { + if ($course->image && Storage::disk('public')->exists($course->image)) { + Storage::disk('public')->delete($course->image); + } + $imagePath = $request->file('image')->store('courses', 'public'); + } + + DB::table('courses')->where('id', $id)->update([ + 'title' => $request->title, + 'course_code' => $request->course_code, + 'image' => $imagePath, + 'duration' => $request->duration, + 'level' => $request->level, + 'author' => $request->author ?? $course->author ?? 'Admin', + 'desc' => $request->desc ?? $request->description ?? $course->desc, + 'badge' => $request->badge ?? $course->badge, + 'trending' => $request->has('trending') ? 1 : 0, + 'status' => $request->status, + 'updated_at' => now(), + ]); + + return redirect()->back()->with('success', 'Course updated successfully!'); } - DB::table('courses')->where('id', $id)->update([ - 'title' => $request->title, - 'course_code' => $request->course_code, - 'image' => $imagePath, - 'duration' => $request->duration, - 'level' => $request->level, - - 'author' => $request->author ?? $course->author ?? 'Admin', - - 'desc' => $request->desc ?? $request->description ?? $course->desc, - 'badge' => $request->badge ?? $course->badge, - 'trending' => $request->has('trending') ? 1 : 0, - 'status' => $request->status, - 'updated_at' => now(), - ]); - - return redirect()->back()->with('success', 'Course updated successfully!'); -} - - public function destroy($id) { $course = DB::table('courses')->where('id', $id)->first(); @@ -186,4 +190,42 @@ class adminmycoursesController extends Controller DB::table('course_modules')->where('id', $id)->delete(); return redirect()->back()->with('success', 'Module deleted successfully!'); } -} \ No newline at end of file + + // --- ASSIGN COURSE TO STUDENTS --- + + /** + * Handles the POST from the "Assign Course to Students" modal. + * Route name: assign.course + */ + public function assignCourse(Request $request) + { + $request->validate([ + 'course_id' => 'required|exists:courses,id', + 'student_ids' => 'required|array|min:1', + 'student_ids.*' => 'exists:student_details,id', + ]); + + foreach ($request->student_ids as $studentId) { + + CourseAssignStudent::updateOrCreate( + [ + 'course_id' => $request->course_id, + 'student_id' => $studentId, + ] +); + // CourseAssignStudent::updateOrCreate( + // [ + // 'course_id' => $request->course_id, + // 'student_id' => $studentId, + // ], + // [ + // 'assigned_at' => now(), + // ] + // ); + } + + return redirect()->back()->with('success', 'Course assigned to selected students successfully!'); + } +} + +Schema::getColumnListing('course_assign_student'); \ No newline at end of file diff --git a/app/Models/CourseAssignStudent.php b/app/Models/CourseAssignStudent.php index 668a7be..e5a7dc0 100644 --- a/app/Models/CourseAssignStudent.php +++ b/app/Models/CourseAssignStudent.php @@ -14,8 +14,9 @@ class CourseAssignStudent extends Model // Mass assignable attributes protected $fillable = [ - 'student_details_id', + 'student_id', 'course_id', + 'assigned_at', ]; /** @@ -23,7 +24,7 @@ class CourseAssignStudent extends Model */ public function studentDetail() { - return $this->belongsTo(StudentDetail::class, 'student_details_id'); + return $this->belongsTo(StudentDetail::class, 'student_id'); } /** @@ -33,4 +34,4 @@ class CourseAssignStudent extends Model { return $this->belongsTo(Course::class, 'course_id'); } -} +} \ No newline at end of file diff --git a/database/migrations/2026_08_24_064117_add_student_id_to_course_assign_student_table.php b/database/migrations/2026_08_24_064117_add_student_id_to_course_assign_student_table.php new file mode 100644 index 0000000..2c59be6 --- /dev/null +++ b/database/migrations/2026_08_24_064117_add_student_id_to_course_assign_student_table.php @@ -0,0 +1,30 @@ +foreignId('student_id')->constrained('student_details')->onDelete('cascade'); + }); + + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('course_assign_student', function (Blueprint $table) { + $table->dropForeign(['student_id']); + $table->dropColumn('student_id'); + }); + } +}; diff --git a/resources/views/admin/adminmycourses.blade.php b/resources/views/admin/adminmycourses.blade.php index 07faf60..8567ab2 100644 --- a/resources/views/admin/adminmycourses.blade.php +++ b/resources/views/admin/adminmycourses.blade.php @@ -5,11 +5,11 @@ Course Management - Admin Portal - + - + -
+
- @if($course) + + + Back to My Courses + - {{-- Dynamic Image URL Handler --}} - @php - $imageSrc = Str::startsWith($course->image, ['http://', 'https://']) - ? $course->image - : asset('storage/' . $course->image); - @endphp + +
+

{{ $course->title ?? $course->name ?? 'Course Modules' }}

+

{{ $course->course_code ?? '' }} {{ isset($course->desc) ? '| '.$course->desc : '' }}

+
- -
-
-
-

{{ $course->title }}

-

Welcome back! Continue your learning journey and complete all modules.

- - Continue Learning - -
-
- -
-
-
+

Modules List

- -
- - -
- - -
- {{ $course->title }} -
-
+ @if(isset($modules) && count($modules) > 0) +
+ @foreach($modules as $index => $module) +
+
+
-

{{ $course->course_code ?? $course->title }}

+ @if(!empty($module->semester)) + + Semester {{ $module->semester }} + + @endif + +
+ Module {{ $index + 1 }}: {{ $module->title }} +
+

- {{ $course->author ?? 'Instructor' }} + {{ $module->description ?? 'No description provided.' }}

- - {{ $course->badge ?? 'Active Course' }} - -
- -

- {{ $course->desc ?? 'No description available for this course.' }} -

- -
-
- Overall Progress - 0% -
-
- - -
-

- Course Modules -

-
- - -
-
-
-
-
-
-
Introduction & Fundamentals
- In Progress -
-

Basic concepts and core theories.

-
-
- -
-
-
-
-
- -
- - -
-
-
-
-
-
-
{{ $course->level ?? 'General' }}
- Course Level -
-
-
- -
-
-
-
-
{{ $course->duration ?? 'N/A' }}
- Course Duration -
-
-
- -
-
-
-
-
{{ $course->status ?? 'Enrolled' }}
- Status -
-
-
-
-
- -
- - @else - - -
-
-
- -

No Assigned Course Found

-

You do not have any course assigned to your profile yet. Please contact the administration for assistance.

-
-
+ @endforeach +
+ @else + +
+ +
No Modules Found
+

There are no modules found for this course.

- @endif
- - @endsection \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index e46fed4..2b73c5a 100644 --- a/routes/web.php +++ b/routes/web.php @@ -20,7 +20,7 @@ use App\Http\Controllers\AdminTimetableController; use App\Http\Controllers\AdminDashboardController; use App\Http\Controllers\welcomeController; use App\Http\Controllers\DocumentDownloadController; - + use App\Http\Controllers\MyCoursesController; /* |-------------------------------------------------------------------------- | Public Routes & General Pages @@ -117,6 +117,14 @@ Route::middleware(['web'])->group(function () { Route::get('/mycourse', [CoureseAssignController::class, 'showMyCourse'])->name('mycourse'); Route::get('/module/{id}', [CoureseAssignController::class, 'showModule'])->name('module.show'); + Route::get('/mycourse', [MyCoursesController::class, 'showMyCourses'])->name('mycourse'); + + + Route::get('/course/{course_id}/modules', [MyCoursesController::class, 'showModule'])->name('course.modules'); + + + Route::get('/mycourse', [MyCoursesController::class, 'showMyCourses'])->name('mycourse'); + // Route::get('/module/{course_id}', [MyCoursesController::class, 'showModule'])->name('course.modules'); /* Document Downloads */ Route::get('/student/download/calendar', [DocumentDownloadController::class, 'downloadAcademicCalendar'])->name('student.download.calendar'); Route::get('/student/download/handbook', [DocumentDownloadController::class, 'downloadStudentHandbook'])->name('student.download.handbook'); @@ -165,6 +173,8 @@ Route::middleware(['web'])->prefix('admin')->group(function () { Route::post('/adminmycourses/store', [adminmycoursesController::class, 'store'])->name('admin.courses.store'); Route::post('/adminmycourses/update/{id}', [adminmycoursesController::class, 'update'])->name('admin.courses.update'); Route::delete('/courses/{id}', [adminmycoursesController::class, 'destroy'])->name('admin.courses.delete'); + Route::post('/admin/assign-course', [adminmycoursesController::class, 'assignCourse']) + ->name('assign.course'); // Admin Timetable Routes Route::get('/timetable', [AdminTimetableController::class, 'index'])->name('admin.timetable');