| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Models; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Webpatser\Uuid\Uuid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Support\Facades\Log; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Support\Facades\Auth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Database\Eloquent\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class Institution_class_student extends Model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * The database table used by the model. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $table = 'institution_class_students'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * Attributes that should be mass-assignable. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     protected $fillable = ['student_id', 'institution_class_id', 'education_grade_id', 'academic_period_id', 'institution_id', 'student_status_id', 'modified_user_id', 'modified', 'created_user_id', 'created']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * The attributes excluded from the model's JSON form. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     protected $hidden = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * The attributes that should be casted to native types. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     protected $casts = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * The attributes that should be mutated to dates. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     protected $dates = ['date_of_birth', 'date_of_death', 'last_login', 'modified', 'created', 'start_date', 'end_date', 'modified', 'created', 'modified', 'created']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     public $timestamps = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     public static function boot() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         parent::boot(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         self::creating(function ($model) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             $model->id = (string) Uuid::generate(4); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $model->created = now(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     public function student() | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         return $this->belongsTo('App\Models\Security_user', 'student_id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     public  static function getStudentsCount($institution_class_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $total_male_students = self::with(['student' => function ($query) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $query->where('student.gender_id', '=', 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         }])->whereHas('student', function ($query) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $query->where('gender_id', '=', 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         })->where('institution_class_id', '=', $institution_class_id)->count(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $total_female_students = self::with(['student' => function ($query) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $query->where('student.gender_id', '=', 2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         }])->whereHas('student', function ($query) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $query->where('gender_id', '=', 2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         })->where('institution_class_id', '=', $institution_class_id)->count(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $totalStudents = $total_female_students + $total_male_students; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             'total' => $totalStudents, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             'total_female_students' => $total_female_students, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             'total_male_students' => $total_male_students | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     public static function  isDuplicated($inputs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $exists = self::where('student_id', '=', $inputs['student_id']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             ->where('institution_class_id', $inputs['institution_class_id']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             ->count(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         return $exists; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     public function getStudentNewClass($student) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         return self::query() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             ->where('student_id', $student['student_id']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             ->join('institution_classes', 'institution_class_students.institution_class_id', '=', 'institution_classes.id') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             ->where('institution_class_students.student_id', $student['student_id']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             ->get()->last(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * Create new Institution class entry for student from examination data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @param [type] $student | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @param [type] $admissionInfo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     public static function createExaminationData($student, $admissionInfo) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             self::updateOrcreate([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 'student_id' => $student['id'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                 'institution_class_id' => $admissionInfo['instituion_class']['id'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 'education_grade_id' => $admissionInfo['education_grade']->id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                 'academic_period_id' => $admissionInfo['academic_period']->id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                 'institution_id' => $admissionInfo['instituion']->id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                 'student_status_id' => 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } catch (\Throwable $th) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             Log::error($th); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 133 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 134 |  |  |  |