ExaminationRecord.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\merchant\model\questions;
  12. use traits\ModelTrait;
  13. use basic\ModelBasic;
  14. use app\merchant\model\questions\TestPaperScoreGrade;
  15. use service\PhpSpreadsheetService;
  16. /**
  17. * 用户考试记录 Model
  18. * Class ExaminationRecord
  19. * @package app\admin\model\questions
  20. */
  21. class ExaminationRecord extends ModelBasic
  22. {
  23. use ModelTrait;
  24. public static function setWhere($where)
  25. {
  26. $model = self::alias('r')->join('TestPaper t', 'r.test_id=t.id')
  27. ->join('User u', 'r.uid=u.uid')->order('r.add_time desc')->where(['r.is_submit' => 1, 't.is_del' => 0]);
  28. if (isset($where['test_id']) && $where['test_id']) $model = $model->where('r.test_id', $where['test_id']);
  29. if (isset($where['type']) && $where['type']) $model = $model->where('r.type', $where['type']);
  30. if (isset($where['uid']) && $where['uid']) $model = $model->where('r.uid', $where['uid']);
  31. if (isset($where['mer_id']) && $where['mer_id']) $model = $model->where('t.mer_id', $where['mer_id']);
  32. if (isset($where['title']) && $where['title'] != '') $model = $model->where('r.uid', 'like', "%$where[title]%");
  33. return $model;
  34. }
  35. /**用户答题结果
  36. * @param $where
  37. * @return array
  38. * @throws \think\Exception
  39. */
  40. public static function getExaminationRecord($where)
  41. {
  42. $model = self::setWhere($where)->field('r.id,r.test_id,r.type,r.uid,r.score,r.accuracy,t.title,u.nickname,r.add_time');
  43. if (isset($where['excel']) && $where['excel'] == 1) {
  44. $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
  45. } else {
  46. $data = ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
  47. }
  48. foreach ($data as $key => &$value) {
  49. if ($where['type'] == 2) {
  50. $value['grade'] = TestPaperScoreGrade::getTestPaperScoreGrade($value['test_id'], $value['score']);
  51. }
  52. $value['add_time'] = date('Y-m-d H:i:s', $value['add_time']);
  53. }
  54. if (isset($where['excel']) && $where['excel'] == 1) {
  55. self::SaveExcel($data, $where['type']);
  56. }
  57. $count = self::setWhere($where)->count();
  58. return compact('data', 'count');
  59. }
  60. /**
  61. * 保存并下载excel
  62. * $list array
  63. * return
  64. */
  65. public static function SaveExcel($list, $type)
  66. {
  67. $export = [];
  68. $filename = '答题记录' . time() . '.xlsx';
  69. if ($type == 1) {
  70. $array = ['ID', 'UID', '昵称', '标题', '正确率%', '提交时间'];
  71. foreach ($list as $index => $item) {
  72. $export[] = [
  73. $item['id'],
  74. $item['uid'],
  75. $item['nickname'],
  76. $item['title'],
  77. $item['accuracy'],
  78. $item['add_time']
  79. ];
  80. }
  81. $filename = '练习记录' . time() . '.xlsx';
  82. } else if ($type == 2) {
  83. $array = ['ID', 'UID', '昵称', '标题', '分数', '正确率%', '分数等级', '提交时间'];
  84. foreach ($list as $index => $item) {
  85. $export[] = [
  86. $item['id'],
  87. $item['uid'],
  88. $item['nickname'],
  89. $item['title'],
  90. $item['score'],
  91. $item['accuracy'],
  92. $item['grade'],
  93. $item['add_time']
  94. ];
  95. }
  96. $filename = '考试记录' . time() . '.xlsx';
  97. }
  98. PhpSpreadsheetService::outdata($filename, $export, $array);
  99. }
  100. }