Abiyasa_Putra_Prasetya/Penilaian Literasi/iClOP-V2/app/Http/Controllers/React/ReactDosenController.php
2025-03-22 15:18:38 +07:00

70 lines
2.5 KiB
PHP

<?php
namespace App\Http\Controllers\React;
use App\Http\Controllers\Controller;
use App\Models\React\ReactTopic;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Yajra\DataTables\Facades\DataTables;
use App\Models\PHP\Topic;
use App\Models\React\ReactTopic_detail;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Auth;
class ReactDosenController extends Controller{
function topics(){
$topics = ReactTopic::all();
return view('react.teacher.topics',[
'topics' => $topics,
]);
}
function add_topics( Request $request, $id ){
$detail = ReactTopic::findorfail($id);
$results = DB::select('SELECT * FROM react_topics_detail WHERE id_topics = ?', [$id]);
return view('react.teacher.topics_detail',[
'results' => $results,
'detail' => $detail->title,
'id' => $id,
]);
}
function simpan( Request $request){
$validatedData = $request->validate([
'title' => 'required|string|max:255',
'caption' => 'required|string|max:255',
'editor' => 'required|string',
'id' => 'required|int|max:11',
'materials' => 'required|file|max:10240', // Example: max 10MB
]);
// Periksa apakah file yang diunggah bukan PDF
if ($request->file('materials')->getClientOriginalExtension() !== 'pdf') {
return redirect()->back()->with('error', 'Materi harus berupa file PDF.');
}
$originName = $request->file('materials')->getClientOriginalName();
$fileName = pathinfo($originName, PATHINFO_FILENAME);
$extension = $request->file('materials')->getClientOriginalExtension();
$fileName = $fileName . '_' . time() . '.' . $extension;
$new_name = str_replace(" ",'_',$fileName);
$path = $request->file('materials')->move(public_path('react/document/A1_BASIC_PHP'), $new_name);
ReactTopic_detail::create([
'title' => $validatedData['title'],
'id_topics' => $validatedData['id'],
'controller' => $validatedData['caption'],
'description' => $validatedData['editor'],
'folder_path' => $path, // Save the path to the uploaded file
'file_name' => $new_name,
]);
$id = $validatedData['id'];
return Redirect::to("/react/teacher/topics/add/$id")->with('message', 'Data Berhasil Diinputkan');
}
}