field('id,article_paper_id,score,create_time') ->where('user_id',$userId) ->order('id desc') ->page($offset,$size) ->select() ->toArray(); foreach ($ret as $k=>$v){ $title=Db::name('article_paper') ->field('title') ->where('id',$v['article_paper_id']) ->find(); $ret[$k]['title']=$title?$title['title']:''; $ret[$k]['score']=(int)$v['score']; } return $ret; } public function limits($userId,$paperId){ $totle=$this ->where([ 'user_id'=>$userId, 'article_paper_id'=>$paperId, ])->count(); $paperTotle=Db::name('article_paper') ->field('limits') ->where([ 'id'=>$paperId, 'del'=>0, 'status'=>2, ])->find(); $data = $paperTotle['limits'] - $totle; return $data > 0?$data:0; } public function saveData($userId,$orgId,$paperId,$jsonArray){ $score=0; foreach ($jsonArray as $k=>$value){ $info = Db::name('article_question') ->where('id',$value['questionId']) ->where('del',0) ->find(); if(!$info){ $this->error = '试题有变化,请重新答题'; return false; } $answer = $value['answer']?explode(',',$value['answer']):[]; $error = 0; $answers = json_decode($info['options'],true); if(!$answer){ $error = 1; }else{ foreach ($answer as $k=>$v){ $flag = 0; foreach ($answers as $kk=>$vv){ if($v == $vv['id']){ $flag = 1; if($vv['answer'] == 0){ $error = 1; break; } } } if($flag == 0){ $error = 0; break; } } } if($error == 0){ $score += $info['score']; } } $this->startTrans(); try{ $data=[ 'article_paper_id'=>$paperId, 'user_id'=>$userId, 'score'=>$score, 'create_time'=>date('Y-m-d H:i:s'), ]; $id=$this->insertGetId($data); if(!$id){ \exception('考试记录保存失败'); } $a =[]; foreach ($jsonArray as $k=>$val){ $data=[ 'article_record_id'=>$id, 'article_question_id'=>$val['questionId'], 'answer'=>$val['answer'], ]; $a[] = $data; } $res = Db::name('article_record_answer') ->insertAll($a); if(!$res){ \exception('问题保存失败'); } $this->commit(); return true; }catch (Exception $e){ $this->rollback(); $this->error = $e->getMessage(); return false; } } }