Library.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. namespace addons\yexam\service;
  3. use app\admin\model\yexam\Answer;
  4. use app\admin\model\yexam\Fav;
  5. use app\admin\model\yexam\Question;
  6. use app\admin\model\yexam\QuestionLog;
  7. use think\Exception;
  8. class Library
  9. {
  10. public $model;
  11. public function __construct()
  12. {
  13. $this->model = new \app\admin\model\yexam\Library();
  14. }
  15. /**
  16. * 获取历年真题列表
  17. * @param $page
  18. * @param $limit
  19. * @throws \think\Exception
  20. */
  21. public function getLibraryList($subject_id,$page,$limit){
  22. $count = $this->model->where(['status'=>1,'subject_id'=>$subject_id])->count();
  23. if($page){
  24. $data = $this->model->field("id,library_name as name,question_ids")->where(['status'=>1,'subject_id'=>$subject_id])->page($page,$limit)->order("weigh asc")->select();
  25. }else{
  26. $data = $this->model->field("id,library_name as name,question_ids")->where(['status'=>1,'subject_id'=>$subject_id])->order("weigh asc")->select();
  27. }
  28. return ['total'=>$count,'data'=>$data];
  29. }
  30. /**
  31. * 获取历年真题答题卡
  32. */
  33. public function getCard($library_id,$user_id){
  34. $libraryInfo = $this->model->where(['id'=>$library_id])->find();
  35. if(empty($libraryInfo)){
  36. return;
  37. }
  38. $questionModel = new \app\admin\model\yexam\Question();
  39. $data = $questionModel
  40. ->alias("question")
  41. ->field("question.*,ifnull(log.state,3) as state")
  42. ->join("yexam_question_log log","question.id=log.question_id and log.user_id=".$user_id,"left")
  43. ->where(['question.id'=>['in',$libraryInfo['question_ids']]])->order("question.type asc,question.id asc")->select();
  44. $return = [];
  45. foreach($data as $v){
  46. $_return['id'] = $v['id'];
  47. $_return['state'] = $v['state'] ==3?3:($v['state']==0?1:2); //1是错2是对3是未答过此题
  48. $return[] = $_return;
  49. }
  50. return $return;
  51. }
  52. /**
  53. * 获取章节练习错题答题卡
  54. */
  55. public function getFavCard($library_id,$user_id){
  56. $favModel = new Fav();
  57. $data = collection($favModel
  58. ->alias("fav")
  59. ->field("question.id")
  60. ->join("yexam_question question","fav.question_id=question.id",'inner')
  61. ->join("yexam_library library","fav.library_id=library.id",'inner')
  62. ->where(['fav.library_id'=>$library_id,'fav.user_id'=>$user_id])
  63. ->order("question.type asc,question.id asc")
  64. ->select());
  65. return $data;
  66. }
  67. /**
  68. * 获取历年真题错题答题卡
  69. */
  70. public function getErrorCard($library_id,$user_id){
  71. $logModel = new QuestionLog();
  72. $data = collection($logModel
  73. ->alias("log")
  74. ->field("question.id")
  75. ->join("yexam_question question","log.question_id=question.id",'inner')
  76. ->join("yexam_library library","log.library_id=library.id",'inner')
  77. ->where(['log.library_id'=>$library_id,'log.user_id'=>$user_id,'log.state'=>0])
  78. ->order("question.type asc,question.id asc")
  79. ->select());
  80. return $data;
  81. }
  82. /**
  83. * 移除历年真题错题
  84. * @param $unit_id
  85. * @param $id
  86. * @param $user_id
  87. */
  88. public function removeErrorQuestion($id,$user_id){
  89. $logModel = new QuestionLog();
  90. $row = $logModel
  91. ->where(['library_id'=>['neq',0],'user_id'=>$user_id,'question_id'=>$id,'state'=>0])
  92. ->find();
  93. if(empty($row)){
  94. return true;
  95. }
  96. $row->delete();
  97. return true;
  98. }
  99. /**
  100. * 获取上次答题定位num
  101. */
  102. public function getLastPosition($library_id,$user_id){
  103. //获取当前单元已经练习的最后的题目ID
  104. $logModel = new QuestionLog();
  105. $data = $logModel
  106. ->field("a.question_id")
  107. ->alias("a")
  108. ->join("yexam_question b","a.question_id=b.id",'inner')
  109. ->where(['a.library_id'=>$library_id,'user_id'=>$user_id])->order('a.lasttime desc')->find();
  110. if(empty($data)){
  111. return 1;
  112. }else{
  113. $questionModel = new \app\admin\model\yexam\Question();
  114. $questionList = $questionModel->field("id")->where(['library_id'=>$library_id])->order("type asc,id asc")->select();
  115. $num = 1;
  116. foreach($questionList as $k=>$v){
  117. $num++;
  118. if($data['question_id'] == $v['id']){
  119. break;
  120. }
  121. }
  122. if($num>count($questionList)){
  123. $num = 1;
  124. }
  125. return $num;
  126. }
  127. }
  128. /**
  129. * 历年真题答题
  130. */
  131. public function doanswer($id,$answer,$user_id){
  132. $questionModel = new \app\admin\model\yexam\Question();
  133. if($questionInfo = $questionModel->where(['id'=>$id])->find()){
  134. if (preg_replace('# #', '', $questionInfo['right_answer']) != preg_replace('# #', '', $answer)) {
  135. $result = 0;
  136. }else{
  137. $result = 1;
  138. }
  139. }else{
  140. return;
  141. }
  142. //写入答题记录
  143. $logModel = new QuestionLog();
  144. if($log = $logModel->where(['question_id'=>$id,'user_id'=>$user_id])->find()){
  145. $log->save(['lasttime'=>time(),'state'=>$result]);
  146. $log->setInc('total_num');
  147. if(empty($result)){
  148. $log->setInc('error_num');
  149. }else{
  150. $log->save(['error_num'=>0]);
  151. }
  152. }else{
  153. $logModel = new QuestionLog();
  154. $logModel->save([
  155. 'subject_id'=>$questionInfo['subject_id'],
  156. 'library_id'=>$questionInfo['library_id'],
  157. 'question_id'=>$id,
  158. 'user_id'=>$user_id,
  159. 'createtime'=>time(),
  160. 'lasttime'=>time(),
  161. 'state'=>$result
  162. ]);
  163. }
  164. return $result;
  165. }
  166. /**
  167. * 获取历年真题错题列表
  168. */
  169. public function getErrorLogList($subject_id,$user_id,$page,$limit){
  170. $logModel = new QuestionLog();
  171. $count = $logModel
  172. ->alias("log")
  173. ->join("yexam_library library","log.library_id=library.id",'inner')
  174. ->where(['log.subject_id'=>$subject_id,'log.library_id'=>['neq',0],'log.user_id'=>$user_id,'log.state'=>0])->group("library.id")->count();
  175. if($page){
  176. $data = $logModel
  177. ->alias("log")
  178. ->field("library.id,library.library_name,count(log.id) as error_num")
  179. ->join("yexam_library library","log.library_id=library.id",'inner')
  180. ->where(['log.subject_id'=>$subject_id,'library_id'=>['neq',0],'log.user_id'=>$user_id,'log.state'=>0])
  181. ->page($page,$limit)
  182. ->group("library.id")
  183. ->select();
  184. }else{
  185. $data = $logModel
  186. ->alias("log")
  187. ->field("library.id,library.library_name,count(log.id) as error_num")
  188. ->join("yexam_library library","log.library_id=library.id",'inner')
  189. ->where(['log.subject_id'=>$subject_id,'library_id'=>['neq',0],'log.user_id'=>$user_id,'log.state'=>0])
  190. ->group("library.id")
  191. ->select();
  192. }
  193. return ['total'=>$count,'data'=>$data];
  194. }
  195. /**
  196. * 获取当前题库的题目数量
  197. * @throws Exception
  198. */
  199. public function getLibraryQuestionCount($libraryInfo){
  200. $questionModel = new Question();
  201. return $questionModel->where(['id'=>['in',$libraryInfo['question_ids']]])->count();
  202. }
  203. /**
  204. * 获取指定历年真题下的题目数量
  205. * @throws Exception
  206. */
  207. public function getLibraryQuestionTestNum($libraryInfo,$user_id){
  208. $questionModel = new Question();
  209. return $questionModel
  210. ->alias("question")
  211. ->join("yexam_question_log log","question.id=log.question_id","inner")
  212. ->where(['question.id'=>['in',$libraryInfo['question_ids']],"log.user_id"=>$user_id])->count();
  213. }
  214. /**
  215. * 获取指定历年真题下的题目正确数量
  216. * @throws Exception
  217. */
  218. public function getLibraryQuestionRightNum($libraryInfo,$user_id){
  219. $questionModel = new Question();
  220. return $questionModel
  221. ->alias("question")
  222. ->join("yexam_question_log log","question.id=log.question_id","inner")
  223. ->where(['question.id'=>['in',$libraryInfo['question_ids']],"log.state"=>1,"log.user_id"=>$user_id])->count();
  224. }
  225. /**
  226. * 获取题库正确率
  227. * @throws Exception
  228. */
  229. public function right_scale($libraryInfo,$user_id){
  230. //获取正确数量
  231. $test_num = self::getLibraryQuestionTestNum($libraryInfo,$user_id);
  232. $right_num = self::getLibraryQuestionRightNum($libraryInfo,$user_id);
  233. return empty($test_num)?0:(bcdiv($right_num,$test_num,2)*100);
  234. }
  235. /**
  236. * 获取章节练习收藏列表
  237. */
  238. public function getFavList($subject_id,$user_id,$page,$limit){
  239. $favModel = new Fav();
  240. $count = $favModel
  241. ->alias("fav")
  242. ->join("yexam_library library","fav.library_id=library.id",'inner')
  243. ->where(['library.subject_id'=>$subject_id,'fav.user_id'=>$user_id])
  244. ->count();
  245. if($page){
  246. $data = $favModel
  247. ->alias("fav")
  248. ->field("library.id,library.library_name,count(fav.id) as num")
  249. ->join("yexam_library library","fav.library_id=library.id",'inner')
  250. ->where(['library.subject_id'=>$subject_id,'fav.user_id'=>$user_id])
  251. ->group("library.id")
  252. ->page($page,$limit)
  253. ->select();
  254. }else{
  255. $data = $favModel
  256. ->alias("fav")
  257. ->field("library.id,library.library_name,count(fav.id) as num")
  258. ->join("yexam_library library","fav.library_id=library.id",'inner')
  259. ->where(['library.subject_id'=>$subject_id,'fav.user_id'=>$user_id])
  260. ->group("library.id")
  261. ->select();
  262. }
  263. return ['total'=>$count,'data'=>$data];
  264. }
  265. }