Film.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Support\Facades\DB;
  6. class Film extends Model
  7. {
  8. use HasFactory;
  9. public function comments() {
  10. return $this->hasMany(Comment::class, 'film');
  11. }
  12. public function votes()
  13. {
  14. return $this->hasMany(Vote::class, 'film');
  15. }
  16. public function suggester() {
  17. return $this->belongsTo(User::class, 'user');
  18. }
  19. public function getBewertung() {
  20. if(!is_null($this->seen)) {
  21. $count = Comment::where('film', $this->id)->where('evaluation', '>', 0)->count();
  22. return $count > 0 ? Comment::where('film', $this->id)->where('evaluation', '>', 0)->sum('evaluation') / $count : 0;
  23. } else return 0;
  24. }
  25. public function activeVotes() {
  26. return $this->votes()->leftJoin('users', 'votes.user', '=', 'users.id')->leftJoin('settings', function($join) {
  27. $join->on('users.id', '=', 'settings.user')->where('settings.key', '=', 'disabled');
  28. })->whereNull('settings.value');
  29. }
  30. public function nextfilm() {
  31. $id = Setting::where('key', 'nextfilm')->first()->value;
  32. return static::load($id);
  33. }
  34. public function isNext() {
  35. $next = Setting::where('key', 'nextfilm')->first()->value;
  36. return $next == $this->id;
  37. }
  38. public function scopePopular($query) {
  39. $nextfilm = Setting::where('key', 'nextfilm')->first()->value;
  40. return $query->addSelect(DB::raw('films.*, COUNT(case when votes.vote IS TRUE then 1 end) as upvotes, COUNT(*) as votes'))
  41. ->leftJoin('votes', 'votes.film', '=', 'films.id')
  42. ->leftJoin('users', 'users.id', '=', 'votes.user')
  43. ->whereNull('seen')->whereNull('rejected')
  44. ->whereNot('films.id', '=', $nextfilm)
  45. ->whereNot('users.disabled', '=', 1)
  46. ->groupBy('films.id')
  47. ->orderBy('upvotes', 'DESC')
  48. ->orderBy('suggested', 'ASC')
  49. ;
  50. }
  51. public function scopeSeen($query) {
  52. return $query->whereNull('rejected')->whereNotNull('seen')->orderBy('seen', 'DESC');
  53. }
  54. public function scopeRejected($query) {
  55. return $query->whereNull('seen')->whereNotNull('rejected')->orderBy('rejected', 'DESC');
  56. }
  57. public function scopeSuggested($query) {
  58. return $query->whereNull('seen')->whereNull('rejected')->orderBy('updated_at', 'DESC');
  59. }
  60. }