ExaminationRecord.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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\wap\model\topic;
  12. use traits\ModelTrait;
  13. use basic\ModelBasic;
  14. use app\wap\model\topic\ExaminationTestRecord;
  15. use app\wap\model\topic\ExaminationWrongBank;
  16. use app\wap\model\topic\TestPaper;
  17. use app\wap\model\topic\TestPaperObtain;
  18. use app\admin\model\questions\TestPaperScoreGrade;
  19. /**
  20. * 用户考试记录 Model
  21. * Class ExaminationRecord
  22. */
  23. class ExaminationRecord extends ModelBasic
  24. {
  25. use ModelTrait;
  26. /**添加考试记录
  27. * @param $id
  28. * @param $type
  29. * @param $uid
  30. */
  31. public static function addExaminationRecord($id, $type, $uid, $txamination_time)
  32. {
  33. $data['test_id'] = $id;
  34. $data['type'] = $type;
  35. $data['uid'] = $uid;
  36. $data['add_time'] = time();
  37. if ($type == 2) {
  38. $data['start_time'] = time();
  39. $data['end_time'] = bcadd(time(), bcmul($txamination_time, 60, 0), 0);
  40. }
  41. return self::insertGetId($data);
  42. }
  43. /**清除上次考试结果
  44. * @param $id
  45. * @param $type
  46. * @param $uid
  47. */
  48. public static function clearLastExamResults($id, $type, $uid)
  49. {
  50. $record = self::where(['test_id' => $id, 'type' => $type, 'uid' => $uid])->order('id desc')->find();
  51. if (!$record) return true;
  52. $testCount = ExaminationTestRecord::where(['e_id' => $record['id'], 'type' => $record['type']])->count();
  53. if (!$testCount) return true;
  54. self::where(['test_id' => $id, 'type' => $type, 'uid' => $uid, 'is_submit' => 0])->update(['is_submit' => 1]);
  55. $res = ExaminationTestRecord::where(['e_id' => $record['id'], 'type' => $record['type']])->delete();
  56. return $res;
  57. }
  58. /**提交考试记录
  59. * @param $data
  60. * @param $uid
  61. */
  62. public static function submitExaminationRecord($data, $uid)
  63. {
  64. $record = self::where(['id' => $data['examination_id'], 'type' => $data['type'], 'uid' => $uid])->find();
  65. if (!$record || $record['is_submit']) return self::setErrorInfo('记录不存在或已提交!');
  66. $yes_questions = ExaminationTestRecord::where(['e_id' => $record['id'], 'type' => $record['type'], 'is_correct' => 2])->count();
  67. $wrong_question = ExaminationTestRecord::where(['e_id' => $record['id'], 'type' => $record['type'], 'is_correct' => 1])->count();
  68. $testPaper = TestPaper::where(['id' => $record['test_id'], 'is_show' => 1, 'is_del' => 0])->field('is_score,item_number')->find();
  69. $array['accuracy'] = bcmul(bcdiv($yes_questions, $testPaper['item_number'], 2), 100, 0);
  70. $array['yes_questions'] = $yes_questions;
  71. $array['wrong_question'] = $wrong_question;
  72. $array['is_submit'] = 1;
  73. $array['duration'] = $data['duration'];
  74. $array['score'] = ExaminationTestRecord::where(['e_id' => $record['id'], 'type' => $record['type'], 'is_correct' => 2])->sum('score');
  75. $array['grade'] = TestPaperScoreGrade::getTestPaperScoreGrade($record['test_id'], $array['score']);
  76. $res = self::edit($array, $record['id']);
  77. if (!$res) return self::setErrorInfo('记录修改错误!');
  78. $res1 = ExaminationWrongBank::addWrongBank($record['id'], $record['test_id'], $record['type'], $uid);
  79. $res2 = TestPaper::PreExercisesWhere()->where(['id' => $record['test_id']])->setInc('answer');
  80. $res3 = TestPaperObtain::where(['test_id' => $record['test_id'], 'type' => $data['type'], 'uid' => $uid, 'is_del' => 0])->setInc('number');
  81. if ($res && $res1 && $res2 && $res3) return true;
  82. else return false;
  83. }
  84. }