/**
  * Get SubClassSubjects by maLMH
  *
  * @param $maLMH
  *
  * @return null|static
  */
 public static function getSubClassSubjectsBymaLMH($maLMH)
 {
     $lmhs = ClassSubject::all()->where('maLMH', $maLMH);
     if ($lmhs->count() == 0) {
         return null;
     }
     $lmh = $lmhs->first();
     $lmh_id = $lmh->id;
     $subs = SubClassSubject::all()->where('classSubject', $lmh_id);
     return $subs;
 }
 /**
  * API Register
  *
  * @param Request $request
  *
  * @return \Illuminate\Http\JsonResponse
  */
 public function register(Request $request)
 {
     onlyAllowPostRequest($request);
     $all = $request->only(['email', 'password', 'mssv', 'lop']);
     /**
      * Dữ liệu trả về
      */
     $response = new stdClass();
     if (!filter_var($all['email'], FILTER_VALIDATE_EMAIL)) {
         $response->error = true;
         $response->error_msg = 'Email không hợp lệ!';
         return response()->json($response);
     }
     /**
      * Kiểm tra password
      */
     if (strlen($all['password']) < 6) {
         $response->error = true;
         $response->error_msg = 'Password quá ngắn! Yêu cầu tối thiểu trên 6 kí tự';
         return response()->json($response);
     }
     /**
      * Tìm user đã tồn tại chưa?
      */
     $user = User::all()->where('email', $all['email']);
     if ($user->count() > 0) {
         //Đã tồn tại người dùng
         $response->error = true;
         $response->error_msg = 'Đã tồn tại người dùng với email ' . $all['email'];
         return response()->json($response);
     }
     /**
      * Get timetable UET
      */
     $res = getTimeTableUET($all['mssv']);
     /**
      * Dữ liệu trả về
      */
     if ($res == false) {
         //Không tồn tại MSV
         $response->error = true;
         $response->error_msg = 'Mã số sinh viên không hợp lệ!';
         return response()->json($response);
     }
     $name = $res['name'];
     $qh = $res['qh'];
     $timetable = $res['timetable'];
     /**
      * Tìm kiếm lớp khóa học
      */
     $classXes = ClassX::all()->where('name', $qh);
     if ($classXes->count() > 0) {
         $classX_id = $classXes->first()->id;
     } else {
         $classX = ClassX::create(['name' => $qh]);
         $classX_id = $classX->id;
     }
     $type = 'student';
     //Mặc định người dùng đăng ký là sinh viên
     $user = User::create(['email' => $all['email'], 'password' => md5($all['password']), 'msv' => $all['mssv'], 'class' => $classX_id, 'type' => $type, 'name' => $name]);
     /**
      * Import timetable
      */
     foreach ($timetable as $index => $t) {
         $maLMH = $t->maLMH;
         $nhom = intval($t->nhom);
         if ($nhom == 0) {
             //Nhóm lý thuyết
             $lmhs = ClassSubject::all()->where('maLMH', $maLMH);
             if ($lmhs->count() > 0) {
                 $lmh = $lmhs->first();
                 $lmh_id = $lmh->id;
                 $subs = SubClassSubject::all()->where('classSubject', $lmh_id);
                 foreach ($subs as $s) {
                     $sub_id = $s->id;
                     $tt = TimeTable::create(['user' => $user->id, 'subClass' => $sub_id]);
                 }
             }
         } else {
             //Nhóm thực hành
             $lmhs = ClassSubject::all()->where('maLMH', $maLMH);
             if ($lmhs->count() > 0) {
                 $lmh = $lmhs->first();
                 $lmh_id = $lmh->id;
                 $subs = SubClassSubject::all()->where('classSubject', $lmh_id);
                 if ($subs->count() > 0) {
                     foreach ($subs as $s) {
                         $sub_id = $s->id;
                         if (intval($s->nhom) == 0 || intval($s->nhom == $nhom)) {
                             $tt = TimeTable::create(['user' => $user->id, 'subClass' => $sub_id]);
                         }
                     }
                 }
             }
         }
     }
     $response->error = false;
     $response->uid = $user->getAttribute('id');
     $response->user = User::getInfoById($user->id);
     return response()->json($response);
 }
 public function getTimetable(Request $request)
 {
     $user_id = $request->input('id');
     /**
      * Dữ liệu trả về
      */
     $response = new stdClass();
     $user = User::getInfoById($user_id);
     if ($user == null) {
         $response->error = true;
         $response->error_msg = 'Đã có vấn đề xảy ra! Bạn vui long quay lại sau.';
         return response()->json($response);
     }
     /**
      * Giáo viên
      */
     if ($user->type == 'teacher') {
         $u_x_id = $user->id;
         $classSubXS = SubClassSubject::all()->where('teacher', intval($u_x_id))->where('nhom', 0);
         if ($classSubXS->count() == 0) {
             $response->error = true;
             $response->error_msg = 'Đã có vấn đề xảy ra! Bạn vui long quay lại sau.';
             return response()->json($response);
         }
         $arr_items = [];
         foreach ($classSubXS as $i => $s) {
             $s_id = $s->id;
             $subClassSubject = SubClassSubject::all()->where('id', intval($s_id))->first();
             $class_id = $subClassSubject->classSubject;
             $classSubject = ClassSubject::all()->where('id', intval($class_id))->first();
             $subject_id = $classSubject->subject;
             $subject = Subject::all()->where('id', intval($subject_id))->first();
             $item_s = new stdClass();
             $item_s->maMH = $subject->maMH;
             $item_s->maLMH = $classSubject->maLMH;
             $item_s->name = $subject->name;
             $item_s->soTin = $subject->soTin;
             $item_s->viTri = $subClassSubject->viTri;
             $item_s->soTiet = $subClassSubject->soTiet;
             $item_s->soSV = $subClassSubject->soSV;
             $item_s->nhom = $subClassSubject->nhom;
             $item_s->address = $subClassSubject->address;
             $item_s->teacher = User::getInfoById($subClassSubject->teacher);
             $arr_items[] = $item_s;
         }
     } else {
         $timetable = TimeTable::all()->where('user', intval($user_id));
         if ($timetable->count() == 0) {
             $response->error = true;
             $response->error_msg = 'Bạn chưa đồng bộ thời khóa biểu!';
             return response()->json($response);
         }
         $arr_items = [];
         foreach ($timetable as $i => $s) {
             $s_id = $s->subClass;
             $subClassSubject = SubClassSubject::all()->where('id', intval($s_id))->first();
             $class_id = $subClassSubject->classSubject;
             $classSubject = ClassSubject::all()->where('id', intval($class_id))->first();
             $subject_id = $classSubject->subject;
             $subject = Subject::all()->where('id', intval($subject_id))->first();
             $item_s = new stdClass();
             $item_s->maMH = $subject->maMH;
             $item_s->maLMH = $classSubject->maLMH;
             $item_s->name = $subject->name;
             $item_s->soTin = $subject->soTin;
             $item_s->viTri = $subClassSubject->viTri;
             $item_s->soTiet = $subClassSubject->soTiet;
             $item_s->soSV = $subClassSubject->soSV;
             $item_s->nhom = $subClassSubject->nhom;
             $item_s->address = $subClassSubject->address;
             $item_s->teacher = User::getInfoById($subClassSubject->teacher);
             $arr_items[] = $item_s;
         }
     }
     $filter = [];
     $filter[0] = $arr_items[0];
     $j = 0;
     for ($i = 1; $i < count($arr_items); $i++) {
         if ($filter[$j]->maLMH != $arr_items[$i]->maLMH || $filter[$j]->nhom != $arr_items[$i]->nhom) {
             $j++;
             $filter[$j] = $arr_items[$i];
         }
     }
     $response->error = false;
     $response->timetable = $filter;
     return response()->json($response);
 }
 public function getGroup(Request $request)
 {
     onlyAllowPostRequest($request);
     $id_user = intval($request->input('id'));
     $base = $request->input('base');
     /**
      * Dữ liệu trả về
      */
     $response = new stdClass();
     $users = User::all()->where('id', $id_user);
     if ($users->count() == 0) {
         //
         $response->error = true;
         $response->error_msg = 'Không tồn tại người dùng này!';
         return response()->json($response);
     }
     $user = $users->first();
     $arrGroup = [];
     if ($base == 'class_xes') {
         $id_class = $user->class;
         $classX = ClassX::all()->where('id', $id_class)->first();
         $class_x = new stdClass();
         $class_x->id = $classX->id;
         $class_x->base = $base;
         $class_x->name = $classX->name;
         $class_x->soSV = ClassX::getCountStudentByClassId($id_class);
         $class_x->teacher = User::getInfoById($classX->teacher);
         $arrGroup = [$class_x];
     }
     if ($base == 'classSubject') {
         /**
          * Giáo viên
          */
         if ($user->type == 'teacher') {
             $u_x_id = $user->id;
             $classSubXS = SubClassSubject::all()->where('teacher', intval($u_x_id))->where('nhom', 0);
             if ($classSubXS->count() == 0) {
                 $response->error = true;
                 $response->error_msg = 'Đã có vấn đề xảy ra! Bạn vui long quay lại sau.';
                 return response()->json($response);
             }
             $arrGroup = [];
             foreach ($classSubXS as $k => $cls) {
                 $sub_id = $cls->id;
                 $subClassSubject = SubClassSubject::all()->where('id', intval($sub_id))->first();
                 $teacher_id = $subClassSubject->teacher;
                 $lmh_id = $subClassSubject->classSubject;
                 $classSubject = ClassSubject::all()->where('id', intval($lmh_id))->first();
                 $maLMH = $classSubject->maLMH;
                 $subject_id = $classSubject->subject;
                 $subject = Subject::all()->where('id', intval($subject_id))->first();
                 $cl = new stdClass();
                 $cl->base = 'classSubject';
                 $cl->id = $classSubject->id;
                 $cl->maLMH = $maLMH;
                 $cl->name = $subject->name;
                 $cl->soSV = $subClassSubject->soSV;
                 $cl->teacher = User::getInfoById($teacher_id);
                 $arrGroup[] = $cl;
             }
         } else {
             $timeTables = TimeTable::all()->where('user', $user->id);
             if ($timeTables->count() == 0) {
                 $response->error = true;
                 $response->error_msg = 'Tài khoản chưa có lớp môn học nào!';
                 return response()->json($response);
             }
             foreach ($timeTables as $tt) {
                 $sub_id = $tt->subClass;
                 $subClassSubject = SubClassSubject::all()->where('id', intval($sub_id))->first();
                 $teacher_id = $subClassSubject->teacher;
                 $lmh_id = $subClassSubject->classSubject;
                 $classSubject = ClassSubject::all()->where('id', intval($lmh_id))->first();
                 $maLMH = $classSubject->maLMH;
                 $subject_id = $classSubject->subject;
                 $subject = Subject::all()->where('id', intval($subject_id))->first();
                 $cl = new stdClass();
                 $cl->base = 'classSubject';
                 $cl->id = $classSubject->id;
                 $cl->maLMH = $maLMH;
                 $cl->name = $subject->name;
                 $cl->soSV = $subClassSubject->soSV;
                 $cl->teacher = User::getInfoById($teacher_id);
                 if ($subClassSubject->nhom == 0) {
                     $arrGroup[] = $cl;
                 }
             }
         }
     }
     $filter = [];
     $filter[0] = $arrGroup[0];
     $j = 0;
     for ($i = 1; $i < count($arrGroup); $i++) {
         if ($filter[$j]->maLMH != $arrGroup[$i]->maLMH) {
             $j++;
             $filter[$j] = $arrGroup[$i];
         }
     }
     $arrGroup = $filter;
     $response->error = false;
     $response->group = $arrGroup;
     return response()->json($response);
 }