|
@@ -0,0 +1,478 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace Question\Src;
|
|
|
+
|
|
|
+use Dever;
|
|
|
+
|
|
|
+class Api
|
|
|
+{
|
|
|
+ private $hidden = true;
|
|
|
+ private $exam = array();
|
|
|
+ private $question = array();
|
|
|
+ private $user = array();
|
|
|
+
|
|
|
+
|
|
|
+ * 获取某个答题规则的数据接口 api.get?id=1
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function get()
|
|
|
+ {
|
|
|
+ $result = array();
|
|
|
+ $id = Dever::input('id');
|
|
|
+ if ($id <= 0 || !$id) {
|
|
|
+ Dever::alert('规则不存在');
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->exam = Dever::db('question/exam')->one($id);
|
|
|
+
|
|
|
+ if (!$this->exam) {
|
|
|
+ Dever::alert('规则不存在');
|
|
|
+ }
|
|
|
+
|
|
|
+ $question = array();
|
|
|
+ if ($this->exam['type'] == 1) {
|
|
|
+
|
|
|
+ $question = $this->getQuestion($this->exam['cate_id'], $this->exam['num']);
|
|
|
+ } elseif ($this->exam['type'] == 3) {
|
|
|
+
|
|
|
+ $question = $this->getQuestion($this->exam['cate_id'], 0, $this->exam['info_ids']);
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->initUser($question);
|
|
|
+
|
|
|
+ $result['exam'] = $this->exam;
|
|
|
+ $result['user'] = $this->user;
|
|
|
+
|
|
|
+ $yes = Dever::input('yes', 1);
|
|
|
+
|
|
|
+ if ($yes == 1 && $this->user && $this->user['status'] < 3) {
|
|
|
+ $result['question'] = $this->getQuestionInfo();
|
|
|
+ }
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 从某个活动中抽取题目并保存下来
|
|
|
+ * $category 分类id
|
|
|
+ * $num 取多少题
|
|
|
+ * $ids 题目id
|
|
|
+ * $index 当前是第几题
|
|
|
+ * $level_ids 难度数量
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ private function getQuestion($category, $num, $ids = false, $index = 0, $level_ids = false)
|
|
|
+ {
|
|
|
+ $where = array();
|
|
|
+ $where['cate_id'] = $category;
|
|
|
+ if ($ids) {
|
|
|
+ $where['ids'] = $ids;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($level_ids) {
|
|
|
+
|
|
|
+ $level = explode(',', $level_ids);
|
|
|
+ $level_data = Dever::db('question/level')->getAll();
|
|
|
+ $index = $index + 1;
|
|
|
+ $level_info = array();
|
|
|
+ $i = 1;
|
|
|
+ foreach ($level as $k => $v) {
|
|
|
+ $start = $i;
|
|
|
+ $end = $v;
|
|
|
+ $i = $end + $i;
|
|
|
+ $end = $start + $end - 1;
|
|
|
+ if ($index >= $start && $index <= $end) {
|
|
|
+ if (isset($level_data[$k])) {
|
|
|
+ $level_info = $level_data[$k];
|
|
|
+ }
|
|
|
+
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isset($level_info)) {
|
|
|
+ $where['level_id'] = $level_info['id'];
|
|
|
+ } else {
|
|
|
+ Dever::alert('难度获取失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $data = Dever::db('question/info')->getAll($where);
|
|
|
+
|
|
|
+ $question_id = Dever::input('question_id');
|
|
|
+
|
|
|
+ if ($question_id > 0) {
|
|
|
+ if (isset($data[$question_id])) {
|
|
|
+ return array($data[$question_id]);
|
|
|
+ } else {
|
|
|
+ Dever::alert('错误的题目信息');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $result = array();
|
|
|
+ if ($data && $num) {
|
|
|
+ $count = count($data);
|
|
|
+ if ($num > $count) {
|
|
|
+ $num = $count;
|
|
|
+ }
|
|
|
+ $keys = array_rand($data, $num);
|
|
|
+
|
|
|
+ if (is_array($keys)) {
|
|
|
+ foreach ($keys as $k => $v) {
|
|
|
+ $result[$v] = $data[$v];
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $result[0] = $data[$keys];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * 初始化用户信息
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ private function initUser($question = array())
|
|
|
+ {
|
|
|
+ if (!$this->exam) {
|
|
|
+ Dever::alert('规则不存在');
|
|
|
+ }
|
|
|
+ $uid = $this->getUid();
|
|
|
+
|
|
|
+
|
|
|
+ if ($this->exam['continue'] == 1) {
|
|
|
+
|
|
|
+ $where['where_status'] = 4;
|
|
|
+ } elseif ($this->exam['continue'] == 2) {
|
|
|
+
|
|
|
+ $where['where_status'] = 3;
|
|
|
+ }
|
|
|
+ $where['where_uid'] = $uid;
|
|
|
+ $where['where_exam_id'] = $this->exam['id'];
|
|
|
+
|
|
|
+ $finish = Dever::db('question/user')->getFinish($where);
|
|
|
+
|
|
|
+ if ($this->exam['continue'] == 2 && $finish && $finish['status'] == 3) {
|
|
|
+
|
|
|
+ $this->user = $finish;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ $this->user = Dever::db('question/user')->getUnFinish($where);
|
|
|
+
|
|
|
+ $times = 1;
|
|
|
+ if($finish) {
|
|
|
+ $times = $finish['times'] + 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!$this->user) {
|
|
|
+ $insert = array();
|
|
|
+ $insert['num'] = $this->exam['num'];
|
|
|
+ if ($question) {
|
|
|
+ $question = array_keys($question);
|
|
|
+ $insert['num'] = count($question);
|
|
|
+ $insert['info_ids'] = implode(',', $question);
|
|
|
+ }
|
|
|
+ $insert['uid'] = $uid;
|
|
|
+ $insert['exam_id'] = $this->exam['id'];
|
|
|
+ $insert['index'] = 0;
|
|
|
+ $insert['score'] = 0;
|
|
|
+ $insert['status'] = 1;
|
|
|
+
|
|
|
+ $insert['times'] = $times;
|
|
|
+ $id = Dever::db('question/user')->insert($insert);
|
|
|
+ $this->user = Dever::db('question/user')->one($id);
|
|
|
+ } else {
|
|
|
+ $id = $this->user['id'];
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($this->user['status'] >= 3) {
|
|
|
+ $this->exam['content'] = Dever::array_decode($this->exam['content']);
|
|
|
+ $this->exam['result'] = array();
|
|
|
+ foreach ($this->exam['content'] as $k => $v) {
|
|
|
+ if (strstr($v['score'], '~')) {
|
|
|
+ $temp = explode('~', $v['score']);
|
|
|
+ if ($this->user['score'] >= $temp[0] && $this->user['score'] <= $temp[1]) {
|
|
|
+ unset($v['score']);
|
|
|
+ $this->exam['result'] = $v;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if ($this->user['score'] == $v['score']) {
|
|
|
+ unset($v['score']);
|
|
|
+ $this->exam['result'] = $v;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ unset($this->exam['content']);
|
|
|
+ } else {
|
|
|
+ unset($this->exam['content']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 获取用户id
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ private function getUid()
|
|
|
+ {
|
|
|
+ $id = Dever::input('uid');
|
|
|
+ if (!$id) {
|
|
|
+ $save = new \Dever\Session\Oper(DEVER_PROJECT, 'cookie');
|
|
|
+
|
|
|
+ $id = $save->get('uid');
|
|
|
+ if (!$id) {
|
|
|
+ $id = Dever::id();
|
|
|
+ $save->add('uid', $id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return $id;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 获取题目接口:api.getInfo?user_data_id=1&uid=1
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function getInfo()
|
|
|
+ {
|
|
|
+ $this->getUser();
|
|
|
+
|
|
|
+ $this->exam = Dever::db('question/exam')->one($this->user['exam_id']);
|
|
|
+
|
|
|
+ return $this->getQuestionInfo();
|
|
|
+ }
|
|
|
+
|
|
|
+ private function getUser()
|
|
|
+ {
|
|
|
+ $uid = $this->getUid();
|
|
|
+
|
|
|
+ $id = Dever::input('user_data_id');
|
|
|
+
|
|
|
+ $this->user = Dever::db('question/user')->one($id);
|
|
|
+
|
|
|
+ if (!$this->user) {
|
|
|
+ Dever::alert('错误的用户信息');
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($this->user && $uid != $this->user['uid']) {
|
|
|
+ Dever::alert('错误的用户信息');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * 获取题目
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ private function getQuestionInfo()
|
|
|
+ {
|
|
|
+ if ($this->user && $this->exam) {
|
|
|
+
|
|
|
+ if ($this->user['status'] == 4) {
|
|
|
+ Dever::alert('已经答完所有题目');
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($this->exam['continue'] == 2 && $this->user['status'] == 3) {
|
|
|
+ Dever::alert('不能继续答题');
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ Dever::db('question/user')->update(array('where_id' => $this->user['id'], 'status' => 2));
|
|
|
+
|
|
|
+ if ($this->exam['type'] == 1 && $this->user['info_ids']) {
|
|
|
+ $question = explode(",", $this->user['info_ids']);
|
|
|
+ } elseif ($this->exam['type'] == 2) {
|
|
|
+
|
|
|
+ $data = $this->getQuestion($this->exam['cate_id'], 1, false, $this->user['index']);
|
|
|
+ $question[$this->user['index']] = $data[0];
|
|
|
+ } elseif ($this->exam['type'] == 3 && $this->exam['info_ids']) {
|
|
|
+
|
|
|
+ $question = explode("\r\n", $this->exam['info_ids']);
|
|
|
+ } elseif ($this->exam['type'] == 4) {
|
|
|
+
|
|
|
+ $data = $this->getQuestion($this->exam['cate_id'], 1, false, $this->user['index'], $this->exam['level_ids']);
|
|
|
+ $question[$this->user['index']] = $data[0];
|
|
|
+ } else {
|
|
|
+ Dever::alert('错误的题目类型');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isset($question[$this->user['index']])) {
|
|
|
+ $data = $question[$this->user['index']];
|
|
|
+ if (!is_array($data)) {
|
|
|
+ $data = Dever::db('question/info')->one($data);
|
|
|
+ }
|
|
|
+ $data['index'] = $this->user['index'];
|
|
|
+ $data['num'] = $this->user['num'];
|
|
|
+
|
|
|
+ $data['content'] = Dever::array_decode($data['content']);
|
|
|
+ if ($this->hidden == true) {
|
|
|
+ foreach ($data['content'] as $k => $v) {
|
|
|
+ unset($data['content'][$k]['score']);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $data['user_info_ids'] = $this->user['user_info_ids'];
|
|
|
+ $data['score'] = $this->user['score'];
|
|
|
+ $data['total'] = $this->exam['num'];
|
|
|
+ }
|
|
|
+ $data['user_data_id'] = $this->user['id'];
|
|
|
+
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ Dever::alert('错误的题目信息');
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 开始答题接口: api.submit?question_id=1&option=0&user_data_id=1&uid=1&next=1 获取下一题的信息
|
|
|
+ * 返回参数:status 2为进行中 3答题错误 4答题正确
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function submit()
|
|
|
+ {
|
|
|
+ $id = Dever::input('user_data_id');
|
|
|
+ if (!$id) {
|
|
|
+ Dever::alert('错误的用户信息');
|
|
|
+ }
|
|
|
+
|
|
|
+ $question_id = Dever::input('question_id');
|
|
|
+ if (!$question_id) {
|
|
|
+ Dever::alert('错误的题目信息');
|
|
|
+ }
|
|
|
+ $option = Dever::input('option', 0);
|
|
|
+
|
|
|
+ $one['user_id'] = $id;
|
|
|
+ $one['uid'] = Dever::input('uid');
|
|
|
+ $one['info_id'] = Dever::input('question_id');
|
|
|
+ $one = Dever::db('question/user_answer')->one($one);
|
|
|
+ if ($one) {
|
|
|
+ $this->user = Dever::db('question/user')->one($id);
|
|
|
+ $update['status'] = $this->user['status'];
|
|
|
+ } else {
|
|
|
+
|
|
|
+ $this->hidden = false;
|
|
|
+ $data = $this->getInfo();
|
|
|
+
|
|
|
+ if (!isset($data['content'][$option])) {
|
|
|
+ Dever::alert('错误的选项信息');
|
|
|
+ }
|
|
|
+
|
|
|
+ $update['user_info_ids'] = $data['user_info_ids'];
|
|
|
+ if ($update['user_info_ids']) {
|
|
|
+ $update['user_info_ids'] = explode(',', $update['user_info_ids']);
|
|
|
+ $update['user_info_ids'][] = $data['id'];
|
|
|
+ $update['user_info_ids'] = implode(',', $update['user_info_ids']);
|
|
|
+ } else {
|
|
|
+ $update['user_info_ids'] = $data['id'];
|
|
|
+ }
|
|
|
+
|
|
|
+ $update['score'] = $data['score'] + $data['content'][$option]['score'];
|
|
|
+ $update['index'] = $data['index'] + 1;
|
|
|
+ $update['where_id'] = $data['user_data_id'];
|
|
|
+
|
|
|
+ $update['status'] = 2;
|
|
|
+
|
|
|
+ if ($update['index'] >= $data['num']) {
|
|
|
+
|
|
|
+ $update['status'] = 4;
|
|
|
+
|
|
|
+ $this->setRanking($update['score']);
|
|
|
+ }
|
|
|
+ if ($this->exam['continue'] == 2 && $data['content'][$option]['score'] == 0) {
|
|
|
+
|
|
|
+ $update['status'] = 3;
|
|
|
+
|
|
|
+ $this->setRanking($update['score']);
|
|
|
+ }
|
|
|
+
|
|
|
+ Dever::db('question/user')->update($update);
|
|
|
+
|
|
|
+ $insert['user_id'] = $data['user_data_id'];
|
|
|
+ $insert['info_id'] = $data['id'];
|
|
|
+ $insert['score'] = $data['content'][$option]['score'];
|
|
|
+ $insert['option'] = $option;
|
|
|
+ $insert['uid'] = $this->user['uid'];
|
|
|
+ $insert['option_name'] = $data['content'][$option]['title'];
|
|
|
+ Dever::db('question/user_answer')->insert($insert);
|
|
|
+ }
|
|
|
+
|
|
|
+ $result = array();
|
|
|
+ $result['status'] = $update['status'];
|
|
|
+ $result['question_id'] = $question_id;
|
|
|
+ $result['option'] = $option;
|
|
|
+ $result['user_data_id'] = $id;
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function setRanking($score)
|
|
|
+ {
|
|
|
+ $ranking = Dever::db('question/ranking')->one(array
|
|
|
+ (
|
|
|
+ 'uid' => $this->user['uid'],
|
|
|
+ 'exam_id' => $this->user['exam_id'],
|
|
|
+ ));
|
|
|
+
|
|
|
+ if ($ranking) {
|
|
|
+ if ($ranking['score'] < $score) {
|
|
|
+ $update['uid'] = $this->user['uid'];
|
|
|
+ $update['exam_id'] = $this->user['exam_id'];
|
|
|
+ $update['score'] = $score;
|
|
|
+ $update['times'] = $this->user['times'];
|
|
|
+ $update['where_id'] = $ranking['id'];
|
|
|
+ Dever::db('question/ranking')->update($update);
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ $insert['uid'] = $this->user['uid'];
|
|
|
+ $insert['exam_id'] = $this->user['exam_id'];
|
|
|
+ $insert['score'] = $score;
|
|
|
+ $insert['times'] = $this->user['times'];
|
|
|
+ Dever::db('question/ranking')->insert($insert);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 复活接口 把当前的status状态值为2即可
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function continue()
|
|
|
+ {
|
|
|
+ $this->getUser();
|
|
|
+ $update['where_id'] = $this->user['id'];
|
|
|
+ $update['status'] = 2;
|
|
|
+ Dever::db('question/user')->update($update);
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 重新挑战接口,重新开始,当前结束
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function reset()
|
|
|
+ {
|
|
|
+ $this->getUser();
|
|
|
+ $update['where_id'] = $this->user['id'];
|
|
|
+ $update['status'] = 4;
|
|
|
+ Dever::db('question/user')->update($update);
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+}
|