| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Models\Eloquent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Database\Eloquent\Factories\HasFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Database\Eloquent\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use App\Models\Eloquent\GroupHomework; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use App\Models\Eloquent\GroupHomeworkProblem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Carbon\Carbon; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use DateTimeInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use DB; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Log; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class Group extends Model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     use HasFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected $table='group'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected $primaryKey='gid'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     public function members() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         return $this->hasMany('App\Models\Eloquent\GroupMember', 'gid', 'gid'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function banneds() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         return $this->hasMany('App\Models\Eloquent\GroupBanned', 'group_id', 'gid'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function homework() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         return $this->hasMany('App\Models\Eloquent\GroupHomework', 'group_id', 'gid'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     protected function serializeDate(DateTimeInterface $date) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         return $date->format('Y-m-d H:i:s'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public static function boot() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         parent::boot(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         static::saving(function($model) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             if ($model->img!="" && $model->img!=null && $model->img[0]!="/") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 $model->img="/$model->img"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     public function getLeaderAttribute() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         return $this->members()->where('role', 3)->first()->user; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public function getLinkAttribute() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         return route('group.detail', ['gcode' => $this->gcode]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     public function addHomework($title, $description, $endedAt, $problems) | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         DB::beginTransaction(); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |             // Create Homework Itself | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |             $newHomework = new GroupHomework(); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |             $newHomework->title = $title; | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             $newHomework->description = $description; | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |             $newHomework->ended_at = $endedAt; | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             $newHomework->is_simple = 1; | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |             $this->homework()->save($newHomework); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |             // Created Related Problem List | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |             $problemIndex = 1; | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |             foreach ($problems as $problem) { | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |                 $newProblem = new GroupHomeworkProblem(); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |                 $newProblem->problem_id = $problem['pid']; | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |                 $newProblem->order_index = $problemIndex++; | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |                 $newHomework->problems()->save($newProblem); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |             // Commit Transaction | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |             DB::commit(); | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         } catch (Exception $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |             DB::rollback(); | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |             Log::alert($e); | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |             throw new Exception($e->getMessage()); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |         return $newHomework->id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 99 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |  |