PreQuestionCandidate.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  6. class PreQuestionCandidate extends Model
  7. {
  8. use HasFactory;
  9. protected $fillable = [
  10. 'import_id',
  11. 'sequence',
  12. 'index',
  13. 'raw_markdown',
  14. 'stem',
  15. 'options',
  16. 'images',
  17. 'tables',
  18. 'is_question_candidate',
  19. 'ai_confidence',
  20. 'status',
  21. ];
  22. protected $casts = [
  23. 'is_question_candidate' => 'boolean',
  24. 'ai_confidence' => 'float',
  25. 'sequence' => 'integer',
  26. 'options' => 'array',
  27. 'images' => 'array',
  28. 'tables' => 'array',
  29. 'created_at' => 'datetime',
  30. 'updated_at' => 'datetime',
  31. ];
  32. public const STATUS_PENDING = 'pending';
  33. public const STATUS_REVIEWED = 'reviewed';
  34. public const STATUS_ACCEPTED = 'accepted';
  35. public const STATUS_REJECTED = 'rejected';
  36. public const STATUS_SUPERSEDED = 'superseded';
  37. public function import(): BelongsTo
  38. {
  39. return $this->belongsTo(MarkdownImport::class, 'import_id');
  40. }
  41. public function getConfidenceBadgeAttribute(): string
  42. {
  43. if ($this->ai_confidence === null) {
  44. return 'gray';
  45. }
  46. if ($this->ai_confidence >= 0.8) {
  47. return 'success';
  48. } elseif ($this->ai_confidence >= 0.5) {
  49. return 'warning';
  50. }
  51. return 'danger';
  52. }
  53. public function getStatusBadgeAttribute(): string
  54. {
  55. $badges = [
  56. self::STATUS_PENDING => 'gray',
  57. self::STATUS_REVIEWED => 'info',
  58. self::STATUS_ACCEPTED => 'success',
  59. self::STATUS_REJECTED => 'danger',
  60. self::STATUS_SUPERSEDED => 'gray',
  61. ];
  62. return $badges[$this->status] ?? 'gray';
  63. }
  64. public function getFirstImageAttribute(): ?string
  65. {
  66. if (empty($this->images)) {
  67. return null;
  68. }
  69. $images = is_string($this->images) ? json_decode($this->images, true) : $this->images;
  70. return $images[0] ?? null;
  71. }
  72. public function getStemPreviewAttribute(): string
  73. {
  74. if (!$this->stem) {
  75. return 'No stem extracted';
  76. }
  77. return \Str::limit($this->stem, 100);
  78. }
  79. }