academico-sis /
academico
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Models; |
||
| 4 | |||
| 5 | use App\Mail\ResultNotification; |
||
| 6 | use Backpack\CRUD\app\Models\Traits\CrudTrait; |
||
| 7 | use Illuminate\Database\Eloquent\Model; |
||
| 8 | use Illuminate\Support\Facades\Mail; |
||
| 9 | use Spatie\Activitylog\Traits\LogsActivity; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * @mixin IdeHelperResult |
||
| 13 | */ |
||
| 14 | class Result extends Model |
||
| 15 | { |
||
| 16 | use CrudTrait; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 17 | use LogsActivity; |
||
| 18 | |||
| 19 | protected $guarded = ['id']; |
||
| 20 | |||
| 21 | protected static bool $logUnguarded = true; |
||
| 22 | |||
| 23 | protected static function boot() |
||
| 24 | { |
||
| 25 | parent::boot(); |
||
| 26 | |||
| 27 | if (config('app.send_emails_for_results')) { |
||
| 28 | // when a result is added, send a notification |
||
| 29 | static::saved(function (self $result) { |
||
| 30 | Mail::to($result->enrollment->student->user->email)->locale($result->enrollment->student->user->locale)->queue(new ResultNotification($result->enrollment->course, $result->enrollment->student->user)); |
||
| 31 | }); |
||
| 32 | } |
||
| 33 | } |
||
| 34 | |||
| 35 | public function comments() |
||
| 36 | { |
||
| 37 | return $this->morphMany(Comment::class, 'commentable'); |
||
| 38 | } |
||
| 39 | |||
| 40 | public function result_name() |
||
| 41 | { |
||
| 42 | return $this->belongsTo(ResultType::class, 'result_type_id'); |
||
| 43 | } |
||
| 44 | |||
| 45 | public function getResultTypeAttribute() |
||
| 46 | { |
||
| 47 | return $this->result_name->name; |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * A Result is linked to an Enrollment. |
||
| 52 | */ |
||
| 53 | public function enrollment() |
||
| 54 | { |
||
| 55 | return $this->belongsTo(Enrollment::class); |
||
| 56 | } |
||
| 57 | |||
| 58 | public function getStudentNameAttribute() |
||
| 59 | { |
||
| 60 | return $this->enrollment['student']['firstname'].' '.$this->enrollment['student']['lastname']; |
||
| 61 | } |
||
| 62 | |||
| 63 | public function getCourseNameAttribute() |
||
| 64 | { |
||
| 65 | return $this->enrollment['course']['name']; |
||
| 66 | } |
||
| 67 | |||
| 68 | public function getCoursePeriodAttribute() |
||
| 69 | { |
||
| 70 | return $this->enrollment['course']['period']['name']; |
||
| 71 | } |
||
| 72 | } |
||
| 73 |