/** * Run the database seeds. * * @return void */ public function run() { Model::unguard(); echo 'start'; DB::statement('SET FOREIGN_KEY_CHECKS=0;'); if (Student::count()) { // return; } Student::where('id', '!=', 10000)->delete(); // DB::table('students')->where('id', '!=',10000)->delete(); // StudentHistory::truncate(); // StudentClassroomHistory::truncate(); // StudentClassroomSession::truncate(); StudentHistory::where('student_id', '!=', 10000)->delete(); $demo_students = [1268, 1276, 1284, 45, 2772, 2803, 2827]; $active_semester = 9; $summer_semesters = [5, 8]; $quran_subjects = [8, 6, 21, 28, 39, 52]; $student_status = ['-3', '-4', '-5', '-6']; $student_study_states = [1 => 'active', 2 => 'success', 3 => 'fail', 4 => 'stop', 5 => 'repeat', 6 => 'success']; $student_states = [1 => 'active', 2 => 'delayed', 3 => 'withdrawn', 4 => 'fired', 5 => 'discontinuous', 6 => 'graduate']; // $all_registrations = DB::connection('old')->table('registration') // ->select('reg_specialist', 'reg_nationalid') // ->orderBy('reg_id', 'DESC') // ->get(); $all_user_subjects = DB::connection('old')->table('user_subject')->select('us_userid', 'us_subid', 'us_semid', 'us_state', 'us_result', 'us_status')->get(); $new_departments = $new_department = Department::select('ad.id', 'ad.parent_id', 'ay.id AS ay_id', 'as.code')->join('academystructure_terms as at', 'at.id', '=', 'ad.term_id')->join('academystructure_years as ay', function ($j) { $j->on('at.year_id', '=', 'ay.id'); // ->where('ay.id', '=',$academy['academy_year']); })->join('academystructure_specialties as as', function ($j) { $j->on('as.id', '=', 'ad.spec_id'); // ->where('as.code', '=', $dep_sepcialty_code); })->from('academystructure_departments as ad')->get()->toArray(); $all_old_academies = $academy = DB::connection('old')->table('sub_academy')->select('academy.*', 'sub_academy.sub_acad_subid')->join('academy', 'sub_acad_academyid', '=', 'academy_id')->get(); $all_old_academies = array_map(function ($a) { return (array) $a; }, $all_old_academies); $students = DB::connection('old')->table('users')->select('users.*', 'r.reg_id')->where('user_groupid', 3)->where('user_edu_state', '>=', 1)->where('user_study_state', '>=', 1)->where('user_code', '!=', 'student')->leftJoin('registration as r', 'r.reg_nationalid', '=', 'users.user_nationalid')->groupBy('users.user_id')->get(); $new_students = []; $new_histories = []; echo 'will loop' . "\n"; foreach ($students as $student) { $gender = substr($student->user_code, 5, 1) == '1' ? 'm' : 'f'; $new_student = []; $new_student['id'] = $student->user_id; $new_student['name'] = $student->user_fullname; $new_student['email'] = $student->user_email; $new_student['mobile'] = $student->user_phone; $new_student['national_id'] = $student->user_nationalid; $new_student['gender'] = $gender; $new_student['state'] = 'active'; $new_student['study_state'] = 'first'; $new_student['academystructure_department_id'] = NULL; $new_student['registration_id'] = $student->reg_id ? $student->reg_id : NULL; $new_student['username'] = $student->user_code; $new_student['password'] = bcrypt($student->user_phone); // $new_student['created_at'] = new DateTime; // $new_student['updated_at'] = new DateTime; $success = false; $semesters = array_unique(array_pluck(array_filter($all_user_subjects, function ($user_subject) use($student) { return $user_subject->us_userid == $student->user_id; }), 'us_semid')); asort($semesters); $i = 0; foreach ($semesters as $semester) { // exit(var_dump($semesters)); $history = ['student_id' => $student->user_id, 'state' => 'active', 'study_state' => 'stop', 'academycycle_semester_id' => $semester]; $subjects = array_filter($all_user_subjects, function ($user_subject) use($semester, $student) { return $user_subject->us_semid == $semester && $user_subject->us_userid == $student->user_id; }); $subjects = array_map(function ($item) { return (array) $item; }, $subjects); $subjects = array_map(function ($item) { return array_only($item, ['us_userid', 'us_state', 'us_result', 'us_status', 'us_subid']); }, $subjects); if (empty($subjects)) { continue; } $one_subject = []; foreach ($subjects as $subject) { if (isset($one_subject['us_subid']) and $subject['us_subid'] > $one_subject['us_subid']) { $one_subject = $subject; } elseif (empty($one_subject)) { $one_subject = $subject; } } // exit(var_dump($one_subject)); // $one_subject = end($subjects); $academy_where = ['sub_acad_subid' => $one_subject['us_subid']]; // if($student->user_yearid==3 && $semester>5) { // if($i>4 && $semester>5) { switch ($student->user_department) { case 1: $academy_department = 'اصول الفقة'; break; case 2: $academy_department = 'الدرسات الاسلامية'; break; case 3: $academy_department = 'عام'; break; } $academy_where['academy_department'] = $academy_department; // } if ($semester == $active_semester) { $academy = array_filter($all_old_academies, function ($a) use($student) { return $student->user_academyid == $a['academy_id']; }); } else { $academy = array_filter($all_old_academies, function ($a) use($one_subject, $academy_where) { return empty($academy_where['academy_department']) or $a['academy_department'] == $academy_where['academy_department']; }); } // var_dump($academy_department); $academy = current($academy); if (empty($academy)) { continue; } // $dep_sepcialty_code = 'G'; // new specialty code switch ($student->user_department) { case 1: $dep_sepcialty_code = 'O'; break; case 2: $dep_sepcialty_code = 'I'; break; case 3: $dep_sepcialty_code = 'G'; break; } // var_dump($dep_sepcialty_code). "\n"; // if($student->user_de) // // $dep_sepcialty_code = 'G'; // if($academy['academy_department']=='اصول الفقة') { // $dep_sepcialty_code = 'O'; // } elseif($academy['academy_department']=='الدرسات الاسلامية') { // $dep_sepcialty_code = 'I'; // } // if($academy['academy_id']<=4) { // $student_nationalid = $student->user_nationalid; // $registration = array_filter($all_registrations, function($reg) use ($student_nationalid){ // return $reg->reg_nationalid == $student_nationalid; // }); // $registration = (array)array_shift($registration); // if(!empty($registration)) { // switch ($registration['reg_specialist']) { // case 'islam': // $dep_sepcialty_code = 'I'; // break; // case 'feqh': // $dep_sepcialty_code = 'O'; // break; // default: // $dep_sepcialty_code = 'G'; // break; // } // } // } $new_departments_filtered = array_filter($new_departments, function ($d) use($academy, $dep_sepcialty_code) { return $d['ay_id'] == $academy['academy_year'] && $d['code'] == $dep_sepcialty_code; }); if ($academy['academy_term'] == 2) { $new_department = array_pop($new_departments_filtered); } else { $new_department = array_shift($new_departments_filtered); } $history['academystructure_department_id'] = $new_department['id']; if (in_array($one_subject['us_result'], $student_status)) { switch ($one_subject['us_result']) { case "-3": $history['state'] = 'withdrawn'; break; case "-4": $history['state'] = 'discontinuous'; break; case "-5": $history['state'] = 'fired'; break; case "-6": $history['state'] = 'delayed'; break; } } $success_total = count(array_filter($subjects, function ($user_subject) { return $user_subject['us_state'] == 'sucess'; })); $failed_total = count(array_filter($subjects, function ($user_subject) { return $user_subject['us_state'] == 'fail'; })); if (!in_array($semester, array_merge($summer_semesters, [$active_semester]))) { if ($failed_total >= 4) { $history['study_state'] = 'fail'; } else { $history['study_state'] = 'success'; } } elseif (in_array($semester, $summer_semesters) && $failed_total > 0) { $history['study_state'] = 'fail'; } elseif (in_array($semester, $summer_semesters) && $failed_total == 0) { $history['study_state'] = 'success'; } elseif ($semester == $active_semester) { $history['study_state'] = 'active'; } if ($one_subject['us_status'] == '-1') { $history['study_state'] = 'repeat'; } if ($one_subject['us_result'] == '-6') { $history['study_state'] = 'stop'; } if ($i == count($semesters) - 1) { $history['study_state'] = $student_study_states[$student->user_study_state]; $history['state'] = $student_states[$student->user_edu_state]; } // var_dump($history); $new_histories[] = $history; $i++; // end foreach $semesters as $semester } if (empty($semesters)) { $new_department = array_filter($new_departments, function ($d) { return $d['id'] == 3; }); if (!empty($new_department)) { $new_student['academystructure_department_id'] = current($new_department)['id']; } } else { if (isset($new_department['id'])) { $new_student['academystructure_department_id'] = $new_department['id']; } } $new_student['study_state'] = $student_study_states[$student->user_edu_state]; $new_student['state'] = $student_states[$student->user_study_state]; $new_students[] = $new_student; } // var_dump($new_histories); foreach (array_chunk($new_students, 1000) as $chunk) { DB::table('students')->insert($chunk); } foreach (array_chunk($new_histories, 1000) as $chunk) { DB::table('student_histories')->insert($chunk); } }