PaperModel.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace app\admin\model\exam;
  3. use addons\exam\model\BaseModel;
  4. use traits\model\SoftDelete;
  5. class PaperModel extends BaseModel
  6. {
  7. use SoftDelete;
  8. // 表名
  9. protected $name = 'exam_paper';
  10. // 自动写入时间戳字段
  11. protected $autoWriteTimestamp = 'int';
  12. // 定义时间戳字段名
  13. protected $createTime = 'createtime';
  14. protected $updateTime = 'updatetime';
  15. protected $deleteTime = 'deletetime';
  16. // 追加属性
  17. protected $append
  18. = [
  19. 'mode_text',
  20. 'status_text',
  21. 'start_time_text',
  22. 'end_time_text',
  23. ];
  24. public function getModeList()
  25. {
  26. return ['RANDOM' => __('Random'), 'FIX' => __('Fix')];
  27. }
  28. public function getKindList()
  29. {
  30. return ['RANDOM' => __('Random'), 'FIX' => __('Fix')];
  31. }
  32. public function getStatusList()
  33. {
  34. return ['NORMAL' => __('Normal'), 'HIDDEN' => __('Hidden')];
  35. }
  36. public function getModeTextAttr($value, $data)
  37. {
  38. $value = $value ? $value : ($data['mode'] ?? '');
  39. $list = $this->getModeList();
  40. return $list[$value] ?? '';
  41. }
  42. public function getStatusTextAttr($value, $data)
  43. {
  44. $value = $value ? $value : ($data['status'] ?? '');
  45. $list = $this->getStatusList();
  46. return $list[$value] ?? '';
  47. }
  48. public function getStartTimeTextAttr($value, $data)
  49. {
  50. $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
  51. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  52. }
  53. public function getEndTimeTextAttr($value, $data)
  54. {
  55. $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
  56. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  57. }
  58. protected function setStartTimeAttr($value)
  59. {
  60. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  61. }
  62. protected function setEndTimeAttr($value)
  63. {
  64. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  65. }
  66. public function cate()
  67. {
  68. return $this->belongsTo(CateModel::class, 'cate_id', 'id', [], 'LEFT')->setEagerlyType(0);
  69. }
  70. public function cates()
  71. {
  72. return $this->belongsTo(CateModel::class, 'cate_id', 'id');
  73. }
  74. /**
  75. * 试卷不同类型或难度题的分数
  76. * @param $configs
  77. * @param $kind
  78. * @param $difficulty
  79. * @return mixed
  80. */
  81. public static function getSingleScore($configs, $kind, $difficulty)
  82. {
  83. $config = $configs[$kind];
  84. if ($config['use_difficulty'] && !empty($config['difficulty'][$difficulty]) && !empty($config['difficulty'][$difficulty]['score'])) {
  85. return $config['difficulty'][$difficulty]['score'];
  86. }
  87. return $config['score'];
  88. }
  89. }