Api.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. <?php
  2. namespace Question\Src;
  3. use Dever;
  4. class Api
  5. {
  6. private $hidden = true;
  7. private $exam = array();
  8. private $question = array();
  9. private $user = array();
  10. private $redis;
  11. private $rank = DEVER_PROJECT . '_score';
  12. /**
  13. * 获取某个答题规则的数据接口 api.get?id=1
  14. *
  15. * @return mixed
  16. */
  17. public function get()
  18. {
  19. $result = array();
  20. $id = Dever::input('id');
  21. if ($id <= 0 || !$id) {
  22. Dever::alert('规则不存在');
  23. }
  24. $this->exam = Dever::db('question/exam')->one($id);
  25. if (!$this->exam) {
  26. Dever::alert('规则不存在');
  27. }
  28. $question = array();
  29. if ($this->exam['type'] == 1) {
  30. # 首次抽取题目,题目保存在user表中,user表保存答题进度
  31. $question = $this->getQuestion($this->exam['cate_id'], $this->exam['num']);
  32. } elseif ($this->exam['type'] == 3) {
  33. # 固定id得到题目,题目保存在user表中,user表保存答题进度
  34. $question = $this->getQuestion($this->exam['cate_id'], 0, $this->exam['info_ids']);
  35. }
  36. $this->initUser($question);
  37. $result['exam'] = $this->exam;
  38. $result['user'] = $this->user;
  39. $yes = Dever::input('yes', 1);
  40. # 未完成,并且设置了yes==1,则立即开始答题
  41. if ($yes == 1 && $this->user && $this->user['status'] < 3) {
  42. $result['question'] = $this->getQuestionInfo();
  43. }
  44. $result['exam']['continue_num'] = $this->getOverContinueNum();
  45. unset($result['exam']['level_ids']);
  46. return $result;
  47. }
  48. private function getOverContinueNum()
  49. {
  50. $continue = $this->getContinueNum();
  51. if ($continue['id'] > 0) {
  52. $this->exam['continue_num'] = $this->exam['continue_num'] - $continue['num'];
  53. }
  54. return $this->exam['continue_num'];
  55. }
  56. /**
  57. * 获取用户信息接口 api.getUserInfo?uid=1
  58. *
  59. * @return mixed
  60. */
  61. public function getUserInfo()
  62. {
  63. $this->getUser(true);
  64. if ($this->user['uid'] > 0) {
  65. $info = Dever::db('question/ranking')->one(array('uid' => $this->user['uid']));
  66. $this->user['ranking_score'] = $this->user['score'];
  67. if ($info) {
  68. $this->user['ranking_score'] = $info['score'];
  69. }
  70. $this->result();
  71. $result['user'] = $this->user;
  72. $where['score'] = $this->user['score'];
  73. $result['total'] = Dever::db('question/ranking')->getTotal($where);
  74. } else {
  75. $this->user['ranking_score'] = $this->user['score'];
  76. $result['user'] = $this->user;
  77. $result['total'] = 0;
  78. }
  79. $result['exam'] = $this->exam;
  80. return $result;
  81. }
  82. /**
  83. * 排行榜接口
  84. *
  85. * @return mixed
  86. */
  87. public function ranking()
  88. {
  89. $uid = Dever::input('uid');
  90. $this->redis();
  91. $result = array();
  92. if ($this->redis) {
  93. $ranking = $this->redis->zRevRange($this->rank, 0, 7, true);
  94. $rank = -1;
  95. $data = array();
  96. if ($ranking) {
  97. $i = 0;
  98. foreach ($ranking as $k => $v) {
  99. if ($k) {
  100. $data[$i] = Dever::db('passport/user')->one($k);
  101. $data[$i]['score'] = $v;
  102. $data[$i]['rank'] = $i;
  103. # 这个用户是不是自己
  104. $data[$i]['my'] = 2;
  105. if ($k == $uid) {
  106. $data[$i]['my'] = 1;
  107. $rank = $i;
  108. }
  109. $i++;
  110. }
  111. }
  112. }
  113. $result['list'] = $data;
  114. if ($uid) {
  115. if ($rank < 0) {
  116. # 获取当前用户的排名
  117. $rank = $this->redis->zRevRank($this->rank, $uid);
  118. }
  119. $user = Dever::db('passport/user')->one($uid);
  120. $ranking = Dever::db('question/ranking')->one(array('option_uid' => $uid));
  121. $user['score'] = $ranking['score'];
  122. $result['user'] = $user;
  123. $result['user']['rank'] = $rank;
  124. }
  125. }
  126. return $result;
  127. }
  128. /**
  129. * 定时更新所有排行榜数据接口 可以将这个放到cron中,定时执行 question/api.upRanking
  130. *
  131. * @return mixed
  132. */
  133. public function upRanking()
  134. {
  135. $this->redis();
  136. if ($this->redis) {
  137. $data = Dever::db('question/ranking')->state();
  138. if ($data) {
  139. foreach ($data as $k => $v) {
  140. $this->redis->zAdd($this->rank, $v['score'], $v['uid']);
  141. }
  142. }
  143. }
  144. return true;
  145. }
  146. /**
  147. * 从某个活动中抽取题目并保存下来
  148. * $category 分类id
  149. * $num 取多少题
  150. * $ids 题目id
  151. * $index 当前是第几题
  152. * $level_ids 难度数量
  153. *
  154. * @return mixed
  155. */
  156. private function getQuestion($category, $num, $ids = false, $index = 0, $level_ids = false)
  157. {
  158. $where = array();
  159. $where['cate_id'] = $category;
  160. if ($ids) {
  161. $where['ids'] = $ids;
  162. }
  163. if ($level_ids) {
  164. $where['level_id'] = $this->getLevel($level_ids, $index);
  165. }
  166. $question_id = Dever::input('question_id');
  167. $data = Dever::db('question/info')->getAll($where);
  168. if ($question_id > 0) {
  169. if (isset($data[$question_id])) {
  170. return array($data[$question_id]);
  171. } else {
  172. Dever::alert('错误的题目信息');
  173. }
  174. }
  175. $result = array();
  176. if ($data && $num) {
  177. $result = $this->getQuestionOne($question_id, $data, $num);
  178. }
  179. return $result;
  180. }
  181. private function getQuestionOne($question_id, $data, $num)
  182. {
  183. $one['user_id'] = $this->user['id'];
  184. $one['uid'] = Dever::input('uid');
  185. if (!$question_id && $num == 1) {
  186. $answer = Dever::db('question/user_answer')->getAll($one);
  187. # 去除已经答过的题
  188. if ($answer) {
  189. $data = array_diff_key($data, $answer);
  190. }
  191. }
  192. $count = count($data);
  193. if ($num > $count) {
  194. $num = $count;
  195. }
  196. $result = array();
  197. $keys = array_rand($data, $num);
  198. if (is_array($keys)) {
  199. foreach ($keys as $k => $v) {
  200. $result[$v] = $data[$v];
  201. }
  202. } else {
  203. $result[0] = $data[$keys];
  204. }
  205. return $result;
  206. }
  207. /**
  208. * 获取等级id
  209. *
  210. * @return mixed
  211. */
  212. private function getLevel($level_ids, $index)
  213. {
  214. # 如果有难度 比较自由的模式 1,3 换行 2,3
  215. $level = explode("\r\n", $level_ids);
  216. $level_data = Dever::db('question/level')->all();
  217. $index = $index + 1;
  218. $level_info = array();
  219. $i = 1;
  220. foreach ($level as $v) {
  221. $v = explode(',', $v);
  222. $start = $i;
  223. $end = $v[1];
  224. $i = $end + $i;
  225. $end = $start + $end - 1;
  226. if ($index >= $start && $index <= $end) {
  227. if (isset($level_data[$v[0]])) {
  228. $level_info = $level_data[$v[0]];
  229. }
  230. break;
  231. }
  232. }
  233. /*
  234. 3,3,4形式,已废弃
  235. $level = explode(',', $level_ids);
  236. $level_data = Dever::db('question/level')->getAll();
  237. $index = $index + 1;
  238. $level_info = array();
  239. $i = 1;
  240. foreach ($level as $k => $v) {
  241. $start = $i;
  242. $end = $v;
  243. $i = $end + $i;
  244. $end = $start + $end - 1;
  245. if ($index >= $start && $index <= $end) {
  246. if (isset($level_data[$k])) {
  247. $level_info = $level_data[$k];
  248. }
  249. break;
  250. }
  251. }
  252. */
  253. if (isset($level_info)) {
  254. return $level_info['id'];
  255. } else {
  256. Dever::alert('难度获取失败');
  257. }
  258. }
  259. /**
  260. * 初始化用户信息
  261. *
  262. * @return mixed
  263. */
  264. private function initUser($question = array())
  265. {
  266. if (!$this->exam) {
  267. Dever::alert('规则不存在');
  268. }
  269. $uid = $this->getUid();
  270. # 获取当前最新的一条已完成记录
  271. if ($this->exam['continue'] == 1) {
  272. # 答错可以继续答题 则答完所有题才算完成
  273. $where['where_status'] = 4;
  274. } elseif ($this->exam['continue'] >= 2) {
  275. # 答错不可以继续答题 则错误也算完成
  276. $where['where_status'] = 3;
  277. }
  278. $where['where_uid'] = $uid;
  279. $where['where_exam_id'] = $this->exam['id'];
  280. # 获取已完成记录
  281. $finish = Dever::db('question/user')->getFinish($where);
  282. if ($this->exam['continue'] == 3 && $finish && $finish['status'] == 3) {
  283. # 最新一次如果是错误的,则停止继续
  284. $this->user = $finish;
  285. return;
  286. }
  287. # 获取正在进行中的记录
  288. $this->user = Dever::db('question/user')->getUnFinish($where);
  289. $times = 1;
  290. if($finish) {
  291. if ($this->user && $finish['times'] >= $this->user['times']) {
  292. $update['where_id'] = $this->user['id'];
  293. $update['status'] = 3;
  294. Dever::db('question/user')->update($update);
  295. return $this->initUser($question);
  296. }
  297. $times = $finish['times'] + 1;
  298. }
  299. if (!$this->user) {
  300. $insert = array();
  301. $insert['num'] = $this->exam['num'];
  302. if ($question) {
  303. $question = array_keys($question);
  304. $insert['num'] = count($question);
  305. $insert['info_ids'] = implode(',', $question);
  306. }
  307. $insert['uid'] = $uid;
  308. $insert['exam_id'] = $this->exam['id'];
  309. $insert['index'] = 0;
  310. $insert['score'] = 0;
  311. $insert['status'] = 1;
  312. # 检查答题次数 第一次
  313. $insert['times'] = $times;
  314. $id = Dever::db('question/user')->insert($insert);
  315. $this->user = Dever::db('question/user')->one($id);
  316. } else {
  317. $id = $this->user['id'];
  318. }
  319. # 获取规则
  320. $this->result();
  321. }
  322. private function result()
  323. {
  324. if ($this->user['status'] >= 3) {
  325. $this->exam['content'] = Dever::array_decode($this->exam['content']);
  326. $this->exam['result'] = array();
  327. foreach ($this->exam['content'] as $k => $v) {
  328. if (strstr($v['score'], '~')) {
  329. $temp = explode('~', $v['score']);
  330. if ($this->user['score'] >= $temp[0] && $this->user['score'] <= $temp[1]) {
  331. unset($v['score']);
  332. $this->exam['result'] = $v;
  333. break;
  334. }
  335. } else {
  336. if ($this->user['score'] == $v['score']) {
  337. unset($v['score']);
  338. $this->exam['result'] = $v;
  339. break;
  340. }
  341. }
  342. }
  343. if (isset($this->exam['result']['info']) && $this->exam['result']['info']) {
  344. $this->exam['result']['info_text'] = strip_tags($this->exam['result']['info']);
  345. }
  346. if (isset($this->exam['result']['pic']) && $this->exam['result']['pic']) {
  347. $this->exam['result']['pic_https'] = str_replace('http://' , 'https://', $this->exam['result']['pic']);
  348. }
  349. unset($this->exam['content']);
  350. } else {
  351. unset($this->exam['content']);
  352. }
  353. }
  354. /**
  355. * 获取用户id
  356. *
  357. * @return mixed
  358. */
  359. private function getUid()
  360. {
  361. $id = Dever::input('uid');
  362. if (!$id) {
  363. $save = new \Dever\Session\Oper(DEVER_PROJECT, 'cookie');
  364. $id = $save->get('uid');
  365. if (!$id) {
  366. $id = Dever::id();
  367. $save->add('uid', $id);
  368. }
  369. }
  370. return $id;
  371. }
  372. /**
  373. * 获取题目接口:api.getInfo?user_data_id=1&uid=1
  374. *
  375. * @return mixed
  376. */
  377. public function getInfo()
  378. {
  379. $this->getUser();
  380. return $this->getQuestionInfo();
  381. }
  382. /**
  383. * 获取最新一条用户参与信息
  384. *
  385. * @return mixed
  386. */
  387. private function getUser($state = false)
  388. {
  389. $uid = $this->getUid();
  390. //$id = Dever::input('user_data_id');
  391. $this->user = Dever::db('question/user')->get(array('where_uid' => $uid));
  392. if (!$this->user) {
  393. if ($state == false) {
  394. Dever::alert('错误的用户信息');
  395. } else {
  396. $this->user['id'] = -1;
  397. $this->user['uid'] = -1;
  398. $this->user['score'] = 0;
  399. }
  400. }
  401. $this->exam = Dever::db('question/exam')->one($this->user['exam_id']);
  402. }
  403. /**
  404. * 获取题目
  405. *
  406. * @return mixed
  407. */
  408. private function getQuestionInfo()
  409. {
  410. if ($this->user && $this->exam) {
  411. # 验证是否答完
  412. if ($this->user['status'] == 4) {
  413. Dever::alert('已经答完所有题目');
  414. }
  415. # 验证是否答错
  416. if ($this->exam['continue'] >= 2 && $this->user['status'] == 3) {
  417. Dever::alert('不能继续答题');
  418. }
  419. # 进行中
  420. Dever::db('question/user')->update(array('where_id' => $this->user['id'], 'status' => 2));
  421. if ($this->exam['type'] == 1 && $this->user['info_ids']) {
  422. $question = explode(",", $this->user['info_ids']);
  423. } elseif ($this->exam['type'] == 2) {
  424. # 随机抽取
  425. $data = $this->getQuestion($this->exam['cate_id'], 1, false, $this->user['index']);
  426. $question[$this->user['index']] = $data[0];
  427. } elseif ($this->exam['type'] == 3 && $this->exam['info_ids']) {
  428. # 固定的
  429. $question = explode("\r\n", $this->exam['info_ids']);
  430. } elseif ($this->exam['type'] == 4) {
  431. # 随机抽取并且按照难度抽取
  432. $data = $this->getQuestion($this->exam['cate_id'], 1, false, $this->user['index'], $this->exam['level_ids']);
  433. $question[$this->user['index']] = $data[0];
  434. } else {
  435. Dever::alert('错误的题目类型');
  436. }
  437. }
  438. if (isset($question[$this->user['index']])) {
  439. $data = $question[$this->user['index']];
  440. if (!is_array($data)) {
  441. $data = Dever::db('question/info')->one($data);
  442. }
  443. $data['index'] = $this->user['index'];
  444. $data['num'] = $this->user['num'];
  445. $data['content'] = Dever::array_decode($data['content']);
  446. if ($this->hidden == true) {
  447. foreach ($data['content'] as $k => $v) {
  448. unset($data['content'][$k]['score']);
  449. }
  450. } else {
  451. //$data['user_info_ids'] = $this->user['user_info_ids'];
  452. $data['score'] = $this->user['score'];
  453. $data['total'] = $this->exam['num'];
  454. }
  455. $data['user_data_id'] = $this->user['id'];
  456. return $data;
  457. }
  458. Dever::alert('错误的题目信息');
  459. }
  460. /**
  461. * 开始答题接口: api.submit?question_id=1&option=0&uid=1
  462. * 返回参数:status 2为进行中 3答题错误 4答题正确
  463. *
  464. * @return mixed
  465. */
  466. public function submit()
  467. {
  468. $this->getUser();
  469. $question_id = Dever::input('question_id');
  470. if (!$question_id) {
  471. Dever::alert('错误的题目信息');
  472. }
  473. $option = Dever::input('option', 0);
  474. $one['user_id'] = $this->user['id'];
  475. $one['uid'] = Dever::input('uid');
  476. $one['info_id'] = Dever::input('question_id');
  477. $one = Dever::db('question/user_answer')->one($one);
  478. if ($one) {
  479. $update['status'] = $this->user['status'];
  480. } else {
  481. /*
  482. if ($this->exam['continue'] >= 2 && $this->exam['continue_times'] > 0) {
  483. $cur = time();
  484. # 后端超时设置
  485. $times = $this->exam['continue_times'];
  486. if ($cur - $this->user['cdate'] > $times) {
  487. # 超时
  488. $option = -1;
  489. }
  490. }
  491. */
  492. $this->hidden = false;
  493. $data = $this->getQuestionInfo();
  494. if ($option == -1) {
  495. # 超时
  496. $data['content'][$option] = array();
  497. $data['content'][$option]['score'] = 0;
  498. $data['content'][$option]['title'] = '超时';
  499. }
  500. if (!isset($data['content'][$option])) {
  501. Dever::alert('错误的选项信息');
  502. }
  503. /*
  504. $update['user_info_ids'] = $data['user_info_ids'];
  505. if ($update['user_info_ids']) {
  506. $update['user_info_ids'] = explode(',', $update['user_info_ids']);
  507. $update['user_info_ids'][] = $data['id'];
  508. $update['user_info_ids'] = implode(',', $update['user_info_ids']);
  509. } else {
  510. $update['user_info_ids'] = $data['id'];
  511. }
  512. */
  513. $update['score'] = $data['score'] + $data['content'][$option]['score'];
  514. $update['where_id'] = $data['user_data_id'];
  515. $update['status'] = 2;
  516. if ($this->exam['continue'] >= 2 && $data['content'][$option]['score'] == 0) {
  517. # 答题错误 进度不变
  518. $update['status'] = 3;
  519. # 将当前分数同步到排行榜中去
  520. $this->setRanking($update['score']);
  521. # 答题完成后,进度变化
  522. if ($this->exam['continue_index'] == 2) {
  523. $update['index'] = $data['index'] + 1;
  524. } else {
  525. $update['index'] = $data['index'];
  526. }
  527. } else {
  528. $update['index'] = $data['index'] + 1;
  529. }
  530. if ($update['index'] >= $data['num'] && $update['status'] == 2) {
  531. # 答题完成
  532. $update['status'] = 4;
  533. # 将当前分数同步到排行榜中去
  534. $this->setRanking($update['score']);
  535. }
  536. Dever::db('question/user')->update($update);
  537. if ($update['index'] > $data['index']) {
  538. $insert['user_id'] = $data['user_data_id'];
  539. $insert['info_id'] = $data['id'];
  540. $insert['score'] = $data['content'][$option]['score'];
  541. $insert['option'] = $option;
  542. $insert['uid'] = $this->user['uid'];
  543. $insert['option_name'] = $data['content'][$option]['title'];
  544. Dever::db('question/user_answer')->insert($insert);
  545. }
  546. }
  547. $result = array();
  548. $result['status'] = $update['status'];
  549. $result['question_id'] = $question_id;
  550. $result['option'] = $option;
  551. $result['continue_num'] = $this->getOverContinueNum();
  552. return $result;
  553. }
  554. /**
  555. * 积分榜单
  556. *
  557. * @return mixed
  558. */
  559. private function setRanking($score)
  560. {
  561. $ranking = Dever::db('question/ranking')->one(array
  562. (
  563. 'uid' => $this->user['uid'],
  564. 'exam_id' => $this->user['exam_id'],
  565. ));
  566. $state = false;
  567. if ($ranking) {
  568. if ($ranking['score'] < $score) {
  569. $state = true;
  570. $update['uid'] = $this->user['uid'];
  571. $update['exam_id'] = $this->user['exam_id'];
  572. $update['score'] = $score;
  573. $update['times'] = $this->user['times'];
  574. $update['where_id'] = $ranking['id'];
  575. Dever::db('question/ranking')->update($update);
  576. }
  577. } else {
  578. $state = true;
  579. $insert['uid'] = $this->user['uid'];
  580. $insert['exam_id'] = $this->user['exam_id'];
  581. $insert['score'] = $score;
  582. $insert['times'] = $this->user['times'];
  583. Dever::db('question/ranking')->insert($insert);
  584. }
  585. if ($state == true) {
  586. $this->redis();
  587. if ($this->redis) {
  588. $this->redis->zAdd($this->rank, $score, $this->user['uid']);
  589. }
  590. }
  591. }
  592. /**
  593. * 复活接口 把当前的status状态值为2即可
  594. *
  595. * @return mixed
  596. */
  597. public function go_continue()
  598. {
  599. $this->getUser();
  600. # 先验证当前的状态是否是3
  601. if ($this->user['status'] == 3) {
  602. $this->checkContinue();
  603. $update['where_id'] = $this->user['id'];
  604. if ($this->user['index'] >= $this->user['num']) {
  605. $update['status'] = 4;
  606. } else {
  607. $update['status'] = 2;
  608. }
  609. Dever::db('question/user')->update($update);
  610. }
  611. return true;
  612. }
  613. /**
  614. * 无限复活接口 慎用
  615. *
  616. * @return mixed
  617. */
  618. public function go_all_continue()
  619. {
  620. $this->getUser();
  621. # 先验证当前的状态是否是3
  622. if ($this->user['status'] == 3) {
  623. $update['where_id'] = $this->user['id'];
  624. if ($this->user['index'] >= $this->user['num']) {
  625. $update['status'] = 4;
  626. } else {
  627. $update['status'] = 2;
  628. }
  629. Dever::db('question/user')->update($update);
  630. }
  631. return true;
  632. }
  633. /**
  634. * 验证当前的复活次数够不够
  635. *
  636. * @return mixed
  637. */
  638. private function checkContinue()
  639. {
  640. $continue_num = $this->exam['continue_num'];
  641. if ($continue_num > 0) {
  642. $continue = $this->getContinueNum();
  643. if ($continue['id'] > 0) {
  644. if ($continue['num'] < $continue_num) {
  645. #可以继续复活
  646. $update['where_id'] = $continue['id'];
  647. $update['num'] = $continue['num'] + 1;
  648. Dever::db('question/continue')->update($update);
  649. } else {
  650. Dever::alert('无法复活');
  651. }
  652. } else {
  653. $insert['exam_id'] = $this->user['exam_id'];
  654. $insert['uid'] = $this->user['uid'];
  655. $insert['user_id'] = $this->user['id'];
  656. $insert['num'] = 1;
  657. Dever::db('question/continue')->insert($insert);
  658. }
  659. }
  660. }
  661. private function getContinueNum()
  662. {
  663. $continue_num_user = $this->exam['continue_num_user'];
  664. $where['exam_id'] = $this->user['exam_id'];
  665. if ($continue_num_user == 1) {
  666. $where['uid'] = $this->user['uid'];
  667. } elseif ($continue_num_user == 2) {
  668. $where['user_id'] = $this->user['id'];
  669. }
  670. $continue = Dever::db('question/continue')->one($where);
  671. if ($continue) {
  672. $time = time();
  673. $continue_num_type = $this->exam['continue_num_type'];
  674. $continue_num_type_times = $this->exam['continue_num_type_times'];
  675. # 查看时间间隔
  676. if ($continue_num_type == 1) {
  677. # 按照自然天
  678. $continue_num_type_times = 0;
  679. $date = Dever::maketime(date('Y-m-d 00:00:00', strtotime("+1 day", $continue['cdate'])));
  680. if ($time >= $date) {
  681. $continue['num'] = 0;
  682. }
  683. } elseif ($continue_num_type == 2) {
  684. # 按照小时
  685. $continue_num_type_times = $continue_num_type_times * 3600;
  686. } elseif ($continue_num_type == 3) {
  687. # 按照天
  688. $continue_num_type_times = $continue_num_type_times * 86400;
  689. }
  690. if ($continue_num_type_times > 0 && $time - $continue['cdate'] >= $continue_num_type_times) {
  691. $continue['num'] = 0;
  692. }
  693. } else {
  694. $continue['id'] = 0;
  695. $continue['num'] = 0;
  696. }
  697. return $continue;
  698. }
  699. /**
  700. * 重新挑战接口,重新开始,当前结束
  701. *
  702. * @return mixed
  703. */
  704. public function reset()
  705. {
  706. $this->getUser();
  707. $update['where_id'] = $this->user['id'];
  708. $update['status'] = 4;
  709. Dever::db('question/user')->update($update);
  710. return true;
  711. }
  712. /**
  713. * 初始化redis
  714. *
  715. * @return mixed
  716. */
  717. private function redis()
  718. {
  719. $this->redis = false;
  720. if ($this->redis) {
  721. return $this->redis;
  722. }
  723. $config = Dever::config('database')->redis;
  724. if ($config) {
  725. if (class_exists('\\Redis')) {
  726. $this->redis = new \Redis;
  727. $this->redis->connect($config['host'], $config['port']);
  728. }
  729. }
  730. return $this->redis;
  731. }
  732. }