0
0

ArticleRecord.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Exception;
  5. class ArticleRecord extends Base
  6. {
  7. public $table = 'article_record';
  8. protected $validateName = 'ArticleRecord';
  9. public function lists($userId,$page,$size){
  10. $offset = ($page - 1) * $size;
  11. $ret=$this
  12. ->field('id,article_paper_id,score,create_time')
  13. ->where('user_id',$userId)
  14. ->order('id desc')
  15. ->page($offset,$size)
  16. ->select()
  17. ->toArray();
  18. foreach ($ret as $k=>$v){
  19. $title=Db::name('article_paper')
  20. ->field('title')
  21. ->where('id',$v['article_paper_id'])
  22. ->find();
  23. $ret[$k]['title']=$title?$title['title']:'';
  24. $ret[$k]['score']=(int)$v['score'];
  25. }
  26. return $ret;
  27. }
  28. public function limits($userId,$paperId){
  29. $totle=$this
  30. ->where([
  31. 'user_id'=>$userId,
  32. 'article_paper_id'=>$paperId,
  33. ])->count();
  34. $paperTotle=Db::name('article_paper')
  35. ->field('limits')
  36. ->where([
  37. 'id'=>$paperId,
  38. 'del'=>0,
  39. 'status'=>2,
  40. ])->find();
  41. $data = $paperTotle['limits'] - $totle;
  42. return $data > 0?$data:0;
  43. }
  44. public function saveData($userId,$orgId,$paperId,$jsonArray){
  45. $score=0;
  46. foreach ($jsonArray as $k=>$value){
  47. $info = Db::name('article_question')
  48. ->where('id',$value['questionId'])
  49. ->where('del',0)
  50. ->find();
  51. if(!$info){
  52. $this->error = '试题有变化,请重新答题';
  53. return false;
  54. }
  55. $answer = $value['answer']?explode(',',$value['answer']):[];
  56. $error = 0;
  57. $answers = json_decode($info['options'],true);
  58. if(!$answer){
  59. $error = 1;
  60. }else{
  61. foreach ($answer as $k=>$v){
  62. $flag = 0;
  63. foreach ($answers as $kk=>$vv){
  64. if($v == $vv['id']){
  65. $flag = 1;
  66. if($vv['answer'] == 0){
  67. $error = 1;
  68. break;
  69. }
  70. }
  71. }
  72. if($flag == 0){
  73. $error = 0;
  74. break;
  75. }
  76. }
  77. }
  78. if($error == 0){
  79. $score += $info['score'];
  80. }
  81. }
  82. $this->startTrans();
  83. try{
  84. $data=[
  85. 'article_paper_id'=>$paperId,
  86. 'user_id'=>$userId,
  87. 'score'=>$score,
  88. 'create_time'=>date('Y-m-d H:i:s'),
  89. ];
  90. $id=$this->insertGetId($data);
  91. if(!$id){
  92. \exception('考试记录保存失败');
  93. }
  94. $a =[];
  95. foreach ($jsonArray as $k=>$val){
  96. $data=[
  97. 'article_record_id'=>$id,
  98. 'article_question_id'=>$val['questionId'],
  99. 'answer'=>$val['answer'],
  100. ];
  101. $a[] = $data;
  102. }
  103. $res = Db::name('article_record_answer')
  104. ->insertAll($a);
  105. if(!$res){
  106. \exception('问题保存失败');
  107. }
  108. $this->commit();
  109. return true;
  110. }catch (Exception $e){
  111. $this->rollback();
  112. $this->error = $e->getMessage();
  113. return false;
  114. }
  115. }
  116. }