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