|
@@ -10,6 +10,8 @@ class Api
|
|
|
private $exam = array();
|
|
|
private $question = array();
|
|
|
private $user = array();
|
|
|
+ private $redis;
|
|
|
+ private $rank = DEVER_PROJECT . '_score';
|
|
|
|
|
|
|
|
|
* 获取某个答题规则的数据接口 api.get?id=1
|
|
@@ -50,9 +52,24 @@ class Api
|
|
|
$result['question'] = $this->getQuestionInfo();
|
|
|
}
|
|
|
|
|
|
+ $result['exam']['continue_num'] = $this->getOverContinueNum();
|
|
|
+
|
|
|
+ unset($result['exam']['level_ids']);
|
|
|
+
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
+ private function getOverContinueNum()
|
|
|
+ {
|
|
|
+ $continue = $this->getContinueNum();
|
|
|
+
|
|
|
+ if ($continue['id'] > 0) {
|
|
|
+ $this->exam['continue_num'] = $this->exam['continue_num'] - $continue['num'];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->exam['continue_num'];
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
* 获取用户信息接口 api.getUserInfo?uid=1
|
|
|
*
|
|
@@ -60,15 +77,106 @@ class Api
|
|
|
*/
|
|
|
public function getUserInfo()
|
|
|
{
|
|
|
- $this->getUser();
|
|
|
- $this->result();
|
|
|
+ $this->getUser(true);
|
|
|
+
|
|
|
+ if ($this->user['uid'] > 0) {
|
|
|
+ $info = Dever::db('question/ranking')->one(array('uid' => $this->user['uid']));
|
|
|
+ $this->user['ranking_score'] = $this->user['score'];
|
|
|
+ if ($info) {
|
|
|
+ $this->user['ranking_score'] = $info['score'];
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->result();
|
|
|
+
|
|
|
+
|
|
|
+ $result['user'] = $this->user;
|
|
|
+
|
|
|
+ $where['score'] = $this->user['score'];
|
|
|
+ $result['total'] = Dever::db('question/ranking')->getTotal($where);
|
|
|
+ } else {
|
|
|
+ $this->user['ranking_score'] = $this->user['score'];
|
|
|
+ $result['user'] = $this->user;
|
|
|
+ $result['total'] = 0;
|
|
|
+ }
|
|
|
|
|
|
$result['exam'] = $this->exam;
|
|
|
- $result['user'] = $this->user;
|
|
|
-
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 排行榜接口
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function ranking()
|
|
|
+ {
|
|
|
+ $uid = Dever::input('uid');
|
|
|
+ $this->redis();
|
|
|
+ $result = array();
|
|
|
+ if ($this->redis) {
|
|
|
+ $ranking = $this->redis->zRevRange($this->rank, 0, 7, true);
|
|
|
+
|
|
|
+ $rank = -1;
|
|
|
+ $data = array();
|
|
|
+ if ($ranking) {
|
|
|
+ $i = 0;
|
|
|
+ foreach ($ranking as $k => $v) {
|
|
|
+ if ($k) {
|
|
|
+ $data[$i] = Dever::db('passport/user')->one($k);
|
|
|
+ $data[$i]['score'] = $v;
|
|
|
+ $data[$i]['rank'] = $i;
|
|
|
+
|
|
|
+ $data[$i]['my'] = 2;
|
|
|
+ if ($k == $uid) {
|
|
|
+ $data[$i]['my'] = 1;
|
|
|
+ $rank = $i;
|
|
|
+ }
|
|
|
+ $i++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $result['list'] = $data;
|
|
|
+
|
|
|
+ if ($uid) {
|
|
|
+ if ($rank < 0) {
|
|
|
+
|
|
|
+ $rank = $this->redis->zRevRank($this->rank, $uid);
|
|
|
+ }
|
|
|
+
|
|
|
+ $user = Dever::db('passport/user')->one($uid);
|
|
|
+ $ranking = Dever::db('question/ranking')->one(array('option_uid' => $uid));
|
|
|
+ $user['score'] = $ranking['score'];
|
|
|
+
|
|
|
+ $result['user'] = $user;
|
|
|
+ $result['user']['rank'] = $rank;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * 定时更新所有排行榜数据接口 可以将这个放到cron中,定时执行 question/api.upRanking
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function upRanking()
|
|
|
+ {
|
|
|
+ $this->redis();
|
|
|
+ if ($this->redis) {
|
|
|
+ $data = Dever::db('question/ranking')->state();
|
|
|
+
|
|
|
+ if ($data) {
|
|
|
+ foreach ($data as $k => $v) {
|
|
|
+ $this->redis->zAdd($this->rank, $v['score'], $v['uid']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
* 从某个活动中抽取题目并保存下来
|
|
|
* $category 分类id
|
|
@@ -91,10 +199,10 @@ class Api
|
|
|
$where['level_id'] = $this->getLevel($level_ids, $index);
|
|
|
}
|
|
|
|
|
|
- $data = Dever::db('question/info')->getAll($where);
|
|
|
-
|
|
|
$question_id = Dever::input('question_id');
|
|
|
|
|
|
+ $data = Dever::db('question/info')->getAll($where);
|
|
|
+
|
|
|
if ($question_id > 0) {
|
|
|
if (isset($data[$question_id])) {
|
|
|
return array($data[$question_id]);
|
|
@@ -105,19 +213,39 @@ class Api
|
|
|
|
|
|
$result = array();
|
|
|
if ($data && $num) {
|
|
|
- $count = count($data);
|
|
|
- if ($num > $count) {
|
|
|
- $num = $count;
|
|
|
+ $result = $this->getQuestionOne($question_id, $data, $num);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function getQuestionOne($question_id, $data, $num)
|
|
|
+ {
|
|
|
+ $one['user_id'] = $this->user['id'];
|
|
|
+ $one['uid'] = Dever::input('uid');
|
|
|
+ if (!$question_id && $num == 1) {
|
|
|
+ $answer = Dever::db('question/user_answer')->getAll($one);
|
|
|
+
|
|
|
+ if ($answer) {
|
|
|
+ $data = array_diff_key($data, $answer);
|
|
|
}
|
|
|
- $keys = array_rand($data, $num);
|
|
|
+ }
|
|
|
|
|
|
- if (is_array($keys)) {
|
|
|
- foreach ($keys as $k => $v) {
|
|
|
- $result[$v] = $data[$v];
|
|
|
- }
|
|
|
- } else {
|
|
|
- $result[0] = $data[$keys];
|
|
|
+ $count = count($data);
|
|
|
+ if ($num > $count) {
|
|
|
+ $num = $count;
|
|
|
+ }
|
|
|
+
|
|
|
+ $result = array();
|
|
|
+
|
|
|
+ $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;
|
|
@@ -215,6 +343,12 @@ class Api
|
|
|
|
|
|
$times = 1;
|
|
|
if($finish) {
|
|
|
+ if ($this->user && $finish['times'] >= $this->user['times']) {
|
|
|
+ $update['where_id'] = $this->user['id'];
|
|
|
+ $update['status'] = 3;
|
|
|
+ Dever::db('question/user')->update($update);
|
|
|
+ return $this->initUser($question);
|
|
|
+ }
|
|
|
$times = $finish['times'] + 1;
|
|
|
}
|
|
|
|
|
@@ -265,6 +399,14 @@ class Api
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ if (isset($this->exam['result']['info']) && $this->exam['result']['info']) {
|
|
|
+ $this->exam['result']['info_text'] = strip_tags($this->exam['result']['info']);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isset($this->exam['result']['pic']) && $this->exam['result']['pic']) {
|
|
|
+ $this->exam['result']['pic_https'] = str_replace('http://' , 'https://', $this->exam['result']['pic']);
|
|
|
+ }
|
|
|
+
|
|
|
unset($this->exam['content']);
|
|
|
} else {
|
|
|
unset($this->exam['content']);
|
|
@@ -310,14 +452,20 @@ class Api
|
|
|
*
|
|
|
* @return mixed
|
|
|
*/
|
|
|
- private function getUser()
|
|
|
+ private function getUser($state = false)
|
|
|
{
|
|
|
$uid = $this->getUid();
|
|
|
|
|
|
$this->user = Dever::db('question/user')->get(array('where_uid' => $uid));
|
|
|
|
|
|
if (!$this->user) {
|
|
|
- Dever::alert('错误的用户信息');
|
|
|
+ if ($state == false) {
|
|
|
+ Dever::alert('错误的用户信息');
|
|
|
+ } else {
|
|
|
+ $this->user['id'] = -1;
|
|
|
+ $this->user['uid'] = -1;
|
|
|
+ $this->user['score'] = 0;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
$this->exam = Dever::db('question/exam')->one($this->user['exam_id']);
|
|
@@ -367,6 +515,7 @@ class Api
|
|
|
if (!is_array($data)) {
|
|
|
$data = Dever::db('question/info')->one($data);
|
|
|
}
|
|
|
+
|
|
|
$data['index'] = $this->user['index'];
|
|
|
$data['num'] = $this->user['num'];
|
|
|
|
|
@@ -376,7 +525,7 @@ class Api
|
|
|
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'];
|
|
|
}
|
|
@@ -411,6 +560,8 @@ class Api
|
|
|
if ($one) {
|
|
|
$update['status'] = $this->user['status'];
|
|
|
} else {
|
|
|
+
|
|
|
+
|
|
|
if ($this->exam['continue'] >= 2 && $this->exam['continue_times'] > 0) {
|
|
|
$cur = time();
|
|
|
|
|
@@ -421,6 +572,7 @@ class Api
|
|
|
$option = -1;
|
|
|
}
|
|
|
}
|
|
|
+ */
|
|
|
|
|
|
$this->hidden = false;
|
|
|
$data = $this->getQuestionInfo();
|
|
@@ -436,6 +588,7 @@ class Api
|
|
|
Dever::alert('错误的选项信息');
|
|
|
}
|
|
|
|
|
|
+
|
|
|
$update['user_info_ids'] = $data['user_info_ids'];
|
|
|
if ($update['user_info_ids']) {
|
|
|
$update['user_info_ids'] = explode(',', $update['user_info_ids']);
|
|
@@ -444,6 +597,7 @@ class Api
|
|
|
} else {
|
|
|
$update['user_info_ids'] = $data['id'];
|
|
|
}
|
|
|
+ */
|
|
|
|
|
|
$update['score'] = $data['score'] + $data['content'][$option]['score'];
|
|
|
$update['where_id'] = $data['user_data_id'];
|
|
@@ -490,6 +644,7 @@ class Api
|
|
|
$result['status'] = $update['status'];
|
|
|
$result['question_id'] = $question_id;
|
|
|
$result['option'] = $option;
|
|
|
+ $result['continue_num'] = $this->getOverContinueNum();
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
@@ -505,9 +660,10 @@ class Api
|
|
|
'uid' => $this->user['uid'],
|
|
|
'exam_id' => $this->user['exam_id'],
|
|
|
));
|
|
|
-
|
|
|
+ $state = false;
|
|
|
if ($ranking) {
|
|
|
if ($ranking['score'] < $score) {
|
|
|
+ $state = true;
|
|
|
$update['uid'] = $this->user['uid'];
|
|
|
$update['exam_id'] = $this->user['exam_id'];
|
|
|
$update['score'] = $score;
|
|
@@ -517,12 +673,20 @@ class Api
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
+ $state = true;
|
|
|
$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);
|
|
|
}
|
|
|
+
|
|
|
+ if ($state == true) {
|
|
|
+ $this->redis();
|
|
|
+ if ($this->redis) {
|
|
|
+ $this->redis->zAdd($this->rank, $score, $this->user['uid']);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
@@ -530,7 +694,7 @@ class Api
|
|
|
*
|
|
|
* @return mixed
|
|
|
*/
|
|
|
- public function continue()
|
|
|
+ public function go_continue()
|
|
|
{
|
|
|
$this->getUser();
|
|
|
|
|
@@ -555,7 +719,7 @@ class Api
|
|
|
*
|
|
|
* @return mixed
|
|
|
*/
|
|
|
- public function go_continue()
|
|
|
+ public function go_all_continue()
|
|
|
{
|
|
|
$this->getUser();
|
|
|
|
|
@@ -583,41 +747,8 @@ class Api
|
|
|
{
|
|
|
$continue_num = $this->exam['continue_num'];
|
|
|
if ($continue_num > 0) {
|
|
|
- $continue_num_user = $this->exam['continue_num_user'];
|
|
|
- $continue_num_type = $this->exam['continue_num_type'];
|
|
|
- $continue_num_type_times = $this->exam['continue_num_type_times'];
|
|
|
-
|
|
|
- $where['exam_id'] = $this->user['exam_id'];
|
|
|
- if ($continue_num_user == 1) {
|
|
|
- $where['uid'] = $this->user['uid'];
|
|
|
- } elseif ($continue_num_user == 2) {
|
|
|
- $where['user_id'] = $this->user['id'];
|
|
|
- }
|
|
|
-
|
|
|
- $continue = Dever::db('question/continue')->one($where);
|
|
|
-
|
|
|
- if ($continue) {
|
|
|
- $time = time();
|
|
|
-
|
|
|
- if ($continue_num_type == 1) {
|
|
|
-
|
|
|
- $continue_num_type_times = 0;
|
|
|
- $date = Dever::maketime(date('Y-m-d 00:00:00', strtotime("+1 day", $continue['cdate'])));
|
|
|
- if ($time >= $date) {
|
|
|
- $continue['num'] = 0;
|
|
|
- }
|
|
|
- } elseif ($continue_num_type == 2) {
|
|
|
-
|
|
|
- $continue_num_type_times = $continue_num_type_times * 3600;
|
|
|
- } elseif ($continue_num_type == 3) {
|
|
|
-
|
|
|
- $continue_num_type_times = $continue_num_type_times * 86400;
|
|
|
- }
|
|
|
-
|
|
|
- if ($continue_num_type_times > 0 && $time - $continue['cdate'] >= $continue_num_type_times) {
|
|
|
- $continue['num'] = 0;
|
|
|
- }
|
|
|
-
|
|
|
+ $continue = $this->getContinueNum();
|
|
|
+ if ($continue['id'] > 0) {
|
|
|
if ($continue['num'] < $continue_num) {
|
|
|
|
|
|
$update['where_id'] = $continue['id'];
|
|
@@ -636,6 +767,51 @@ class Api
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private function getContinueNum()
|
|
|
+ {
|
|
|
+ $continue_num_user = $this->exam['continue_num_user'];
|
|
|
+
|
|
|
+ $where['exam_id'] = $this->user['exam_id'];
|
|
|
+ if ($continue_num_user == 1) {
|
|
|
+ $where['uid'] = $this->user['uid'];
|
|
|
+ } elseif ($continue_num_user == 2) {
|
|
|
+ $where['user_id'] = $this->user['id'];
|
|
|
+ }
|
|
|
+
|
|
|
+ $continue = Dever::db('question/continue')->one($where);
|
|
|
+
|
|
|
+ if ($continue) {
|
|
|
+ $time = time();
|
|
|
+ $continue_num_type = $this->exam['continue_num_type'];
|
|
|
+ $continue_num_type_times = $this->exam['continue_num_type_times'];
|
|
|
+
|
|
|
+
|
|
|
+ if ($continue_num_type == 1) {
|
|
|
+
|
|
|
+ $continue_num_type_times = 0;
|
|
|
+ $date = Dever::maketime(date('Y-m-d 00:00:00', strtotime("+1 day", $continue['cdate'])));
|
|
|
+ if ($time >= $date) {
|
|
|
+ $continue['num'] = 0;
|
|
|
+ }
|
|
|
+ } elseif ($continue_num_type == 2) {
|
|
|
+
|
|
|
+ $continue_num_type_times = $continue_num_type_times * 3600;
|
|
|
+ } elseif ($continue_num_type == 3) {
|
|
|
+
|
|
|
+ $continue_num_type_times = $continue_num_type_times * 86400;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($continue_num_type_times > 0 && $time - $continue['cdate'] >= $continue_num_type_times) {
|
|
|
+ $continue['num'] = 0;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $continue['id'] = 0;
|
|
|
+ $continue['num'] = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $continue;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
* 重新挑战接口,重新开始,当前结束
|
|
|
*
|
|
@@ -650,4 +826,26 @@ class Api
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ * 初始化redis
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ private function redis()
|
|
|
+ {
|
|
|
+ $this->redis = false;
|
|
|
+ if ($this->redis) {
|
|
|
+ return $this->redis;
|
|
|
+ }
|
|
|
+ $config = Dever::config('database')->redis;
|
|
|
+ if ($config) {
|
|
|
+ if (class_exists('\\Redis')) {
|
|
|
+ $this->redis = new \Redis;
|
|
|
+ $this->redis->connect($config['host'], $config['port']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->redis;
|
|
|
+ }
|
|
|
}
|