<repo_name>phanvantan73/roll-system<file_sep>/database/seeds/UserTableSeeder.php
<?php

use Illuminate\Database\Seeder;
use App\User;
use App\Subject;

class UserTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $faker = Faker\Factory::create();
        factory(User::class)->create([
            'email' => '<EMAIL>',
            'name' => 'Admin',
            'password' => bcrypt('<PASSWORD>'),
            'is_admin' => 1,
        ]);

        for ($i = 0; $i <= 10; $i++) {
        	$mssv = "$i$i$i$i$i$i$i";
        	$user = User::create([
        		'name' => $faker->name,
        		'email' => $mssv . '@sv.com.vn',
        		'password' => bcrypt('<PASSWORD>'),
        	]);
        	$user->student()->create([
        		'name' => $user->name,
        		'mssv' => $mssv,
        		'class' => ($i > 0 && $i < 10 ? '1' . $i . 'T1' : '11T4'),

        	]);
        }

        Subject::create([
    		'name' => 'Môn học 1',
    		'start_time' => '07:00:00',
    		'end_time' => '12:00:00',
    		'learn_time' => 1,
    	]);
    	Subject::create([
    		'name' => 'Môn học 2',
    		'start_time' => '13:00:00',
    		'end_time' => '17:00:00',
    		'learn_time' => 1,
    	]);
    	Subject::create([
    		'name' => 'Môn học 3',
    		'start_time' => '07:00:00',
    		'end_time' => '12:00:00',
    		'learn_time' => 2,
    	]);
    	Subject::create([
    		'name' => '<NAME>',
    		'start_time' => '13:00:00',
    		'end_time' => '17:00:00',
    		'learn_time' => 2,
    	]);
    	Subject::create([
    		'name' => '<NAME>',
    		'start_time' => '07:00:00',
    		'end_time' => '12:00:00',
    		'learn_time' => 3,
    	]);
    	Subject::create([
    		'name' => '<NAME>',
    		'start_time' => '13:00:00',
    		'end_time' => '17:00:00',
    		'learn_time' => 3,
    	]);
    	Subject::create([
    		'name' => '<NAME>',
    		'start_time' => '07:00:00',
    		'end_time' => '12:00:00',
    		'learn_time' => 4,
    	]);
    	Subject::create([
    		'name' => '<NAME>',
    		'start_time' => '13:00:00',
    		'end_time' => '17:00:00',
    		'learn_time' => 4,
    	]);
    	Subject::create([
    		'name' => '<NAME>',
    		'start_time' => '07:00:00',
    		'end_time' => '12:00:00',
    		'learn_time' => 5,
    	]);
    	Subject::create([
    		'name' => '<NAME>',
    		'start_time' => '13:00:00',
    		'end_time' => '17:00:00',
    		'learn_time' => 5,
    	]);
    }
}
<file_sep>/app/Student.php
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Subject;
use App\User;

class Student extends Model
{
    protected $fillable = [
    	'mssv',
    	'name',
    	'class',
        'user_id',
    ];

    public function subjects()
    {
    	return $this->belongsToMany(Subject::class)->withPivot('status')->withTimestamps();
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public static function getStudentFromMssv($mssv)
    {
    	return Student::where('mssv', $mssv)->first();
    }

    public function checkRollcallByDay($day, $subject_id)
    {
        return $this->subjects()->whereDate('student_subject.created_at', $day)
            ->where('student_subject.subject_id', $subject_id)
            ->exists();
    }
}
<file_sep>/app/Http/Controllers/Admin/AdminController.php
<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Student;
use App\Subject;
use App\User;
use Carbon\Carbon;
use File;
use Auth;
use DB;

class AdminController extends Controller
{
    public function index()
    {
        return view('admin.index');
    }

    public function getAll()
    {
        $requestDay = Carbon::now();
        $day = $requestDay->dayOfWeek;
        $requestDay = $requestDay->format('d-m-Y');
        $path = public_path('file/' . Carbon::now()->toDateString() . '.txt');
        $data = [];
        $errors = [];
        if (File::exists($path)) {
            $contents = File::get($path);
            $contents = explode(PHP_EOL, $contents);
            $subjectsInDay = Subject::where('learn_time', $day)->get();
            for ($i = 0; $i < count($contents) - 1; $i++) {
                $content = explode(',', $contents[$i]);
                $student = Student::getStudentFromMssv($content[0]);
                if ($student) {
                    $rollTime = $content[1];
                    $data[$i] = [
                        'student' => $student,
                        'rollTime' => $rollTime,
                    ];
                    foreach ($subjectsInDay as $key => $subject) {
                        $check = DB::table('student_subject')->where('student_id', $student->id)->where('subject_id', $subject->id)
                            ->whereDate('created_at', Carbon::today()->toDateString())->oldest()->first();
                        
                        if ($rollTime >= $subject->start_time && $rollTime <= $subject->end_time ) {

                            if (!$check) {
                                $student->subjects()->attach($subject->id, [
                                    'created_at' => Carbon::today()->toDateString() . ' ' . $rollTime,
                                    'status' => 1,
                                ]);
                            } else {
                                
                                if ($rollTime < Carbon::create($check->created_at)->format('H:i:s')) {
                                    DB::table('student_subject')->delete($check->id);
                                    $student->subjects()->attach($subject->id, [
                                        'created_at' => Carbon::today()->toDateString() . ' ' . $rollTime,
                                        'status' => 1,
                                    ]);
                                }
                            }
                        }
                    }
                }
            }
        } else {
            $errors['error'] = 'Camera chưa cập nhật kết quả!';
        }
        
        
        return view('sumary', compact('requestDay', 'data', 'errors'));

    }

    public function addStudent(Request $request)
    {
        $name = $request->name;
        $mssv = $request->mssv;
        $user = User::create([
            'name' => $name,
            'email' => $mssv . '@sv.com.vn',
            'password' => bcrypt('<PASSWORD>'),
        ]);

        $student = $user->student()->create([
            'name' => $name,
            'mssv' => $mssv,
            'class' => '13T1',
        ]);
        $user = $user->load('student');

        return response()->json([
            'success' => true,
            'user' => $user,
        ]);
    }

    public function getAllStudent(Request $request)
    {
        $users = User::where('id', '!=', Auth::user()->id)->get();

        return view('admin.student', compact('users'));
    }
}
<file_sep>/readme.md
Sau khi đã clone project từ github, làm theo những bước sau:<br>
	+ Mở terminal và chạy lệnh "composer install" hoặc "composer update"<br>
	+ Tạo file .env bằng cách copy file .env.example và đổi tên.<br>
	+ Vào file .env chỉnh sửa claij cấu hình database như sau:<br>
		+ DB_DATABASE = tên cơ sở dữ liệu<br>
		+ DB_USERNAME = tên đăng nhập mySql<br>
		+ DB_PASSWORD = <PASSWORD><br>
	+ Tiếp đó mở terminal và chạy lệnh "php artisan key:generate".<br>
	+ Tiếp theo nhập vào dòng lệnh "php artisan migrate" để migration cơ sở dữ liệu.<br>
	+ Sau đó chạy tiếp "php artisan db:seed" để seed dữ liệu.<br>
	+ Cuối cùng chạy lệnh "php artisan make:auth" để tạo authentication.<br>

Sau khi hoàn thành các bước trên, chạy lệnh "php artisan serve" rồi truy cập vào địa chỉ<br>
http://127.0.0.1:8000 để xem kết quả.<br>
<file_sep>/app/Http/Controllers/StudentController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Student;
use App\Subject;
use Carbon\Carbon;
use File;
use Auth;

class StudentController extends Controller
{
    public function getResult(Request $request)
    {
        $student = Auth::user()->student;
        $subjects = Subject::pluck('name', 'id')->toArray();
        $month = Carbon::today()->format('m');
        $year = Carbon::today()->format('Y');
        $date = Carbon::today()->format('d');
        $defaultSubjectId = Subject::orderBy('created_at')->first()->id;
        $defaultSubjectName = Subject::orderBy('created_at')->first()->name;
        $defaultDay = $year . '-' . $month. '-' . $date;
        $data = $this->getData($student, $defaultSubjectId, $defaultDay);
        $defaultDay = Carbon::create($defaultDay)->format('d-m-Y');

        if ($request->ajax()) {
            $defaultSubjectId = $request->subject_id;
            $defaultDay = $request->day;
            $data = $this->getData($student, $defaultSubjectId, $defaultDay);

            return response()->json([
                'html' => count($data) ? view('table', compact('data'))->render() : [],
                'message' => 'successfully',
            ]);
        }

        return view('result', compact('data', 'student', 'subjects', 'defaultDay', 'defaultSubjectName'));
    }

    public function getData($student, $subjectId, $day)
    {
        $data = [];
        
        foreach ($student->subjects as $subject) {
            if ($subject->pivot->subject_id == $subjectId && $subject->pivot->created_at->format('Y-m-d') == $day) {
                array_push($data, $subject);
            }
        }

        return $data;
    }
}
