dever 7 years ago
parent
commit
bf8594c38b
5 changed files with 419 additions and 65 deletions
  1. 97 0
      database/continue.php
  2. 112 10
      database/exam.php
  3. 1 1
      database/ranking.php
  4. 17 3
      database/user.php
  5. 192 51
      src/Api.php

+ 97 - 0
database/continue.php

@@ -0,0 +1,97 @@
+<?php
+
+return array
+(
+	# 表名
+	'name' => 'continue',
+	# 显示给用户看的名称
+	'lang' => '用户复活次数',
+	# 是否显示在后台菜单
+	'menu' => false,
+
+	# 数据结构
+	'struct' => array
+	(
+		'id' 		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> 'ID',
+			'default' 	=> '',
+			'desc' 		=> '',
+			'match' 	=> 'is_numeric',
+			'order'		=> 'desc',
+			'list'		=> true,
+		),
+
+		'uid'		=> array
+		(
+			'type' 		=> 'varchar-150',
+			'name' 		=> '用户',
+			'default' 	=> '',
+			'desc' 		=> '用户id',
+			'match' 	=> 'is_string',
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'list'		=> true,
+		),
+
+		'user_id'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '用户参与id',
+			'default' 	=> '',
+			'desc' 		=> '用户参与id',
+			'match' 	=> 'is_numeric',
+			//'list'		=> true,
+		),
+
+		'exam_id'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '活动',
+			'default' 	=> '1',
+			'desc' 		=> '活动id',
+			'match' 	=> 'is_numeric',
+			'list'		=> '{exam_id} > 0 ? Dever::load("question/exam-one#name", {exam_id}) : "未知"',
+		),
+
+		'num'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '复活次数',
+			'default' 	=> '0',
+			'desc' 		=> '复活次数',
+			'match' 	=> 'is_string',
+			'update'	=> 'text',
+			'list'		=> true,
+		),
+
+		'state'		=> array
+		(
+			'type' 		=> 'tinyint-1',
+			'name' 		=> '状态',
+			'default' 	=> '1',
+			'desc' 		=> '请选择状态',
+			'match' 	=> 'is_numeric',
+		),
+		
+		'cdate'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '更新时间',
+			'match' 	=> array('is_numeric', time()),
+			'desc' 		=> '',
+			# 只有insert时才生效
+			//'insert'	=> true,
+			'search'	=> 'date',
+			'list'		=> 'date("Y-m-d H:i:s", {cdate})',
+		),
+	),
+
+	'manage' => array
+	(
+		'insert' => false,
+		'edit' => false,
+		'delete' => false,
+	),
+);

+ 112 - 10
database/exam.php

@@ -12,6 +12,7 @@ $continue = array
 (
 	1 => '答错之后继续答题',
 	2 => '答错之后停止答题',
+	3 => '答错之后停止答题,并且无法重新开始',
 );
 
 $cate = function()
@@ -25,6 +26,20 @@ $cate = function()
 	return $array;
 };
 
+$continue_num_type = array
+(
+	1 => '自然日,每天凌晨自动恢复',
+	2 => '按照小时间隔',
+	3 => '按照天间隔',
+);
+
+
+$continue_num_user = array
+(
+	1 => '对用户所有答题有效',
+	2 => '对用户本次答题有效',
+);
+
 return array
 (
 	# 表名
@@ -48,6 +63,13 @@ return array
 			'list'		=> true,
 		),
 
+		'hr1'		=> array
+		(
+			'name' 		=> '基本信息',
+			'class'		=> '',//本项必须填写
+			'attr'		=> '',
+		),
+
 		'name'		=> array
 		(
 			'type' 		=> 'varchar-150',
@@ -66,7 +88,7 @@ return array
 			'name' 		=> '描述',
 			'default' 	=> '',
 			'desc' 		=> '描述',
-			'match' 	=> 'is_string',
+			'match' 	=> 'option',
 			'update'	=> 'textarea',
 			'search'	=> 'fulltext',
 			//'list'		=> true,
@@ -75,19 +97,36 @@ return array
 		'pic'		=> array
 		(
 			'type' 		=> 'varchar-150',
-			'name' 		=> '测试图',
+			'name' 		=> '规则介绍图',
 			'default' 	=> '',
-			'desc' 		=> '测试图',
+			'desc' 		=> '规则介绍图',
 			'match' 	=> 'option',
 			'update'	=> 'image',
 			'key' 		=> '1',
 			'place'		=> '150',
 		),
 
+		'hr2'		=> array
+		(
+			'name' 		=> '题目抽取规则',
+			'class'		=> '',//本项必须填写
+			'attr'		=> '',
+		),
+
+		'num'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '题目数量',
+			'default' 	=> '10',
+			'desc' 		=> '题目数量',
+			'match' 	=> 'is_string',
+			'update'	=> 'text',
+		),
+
 		'cate_id'		=> array
 		(
 			'type' 		=> 'varchar-150',
-			'name' 		=> '分类',
+			'name' 		=> '抽取题目所属分类',
 			'default' 	=> '',
 			'desc' 		=> '请选择分类',
 			'match' 	=> 'is_string',
@@ -112,7 +151,7 @@ return array
 		'level_ids'		=> array
 		(
 			'type' 		=> 'varchar-300',
-			'name' 		=> '难度数量设置-多个用半角逗号隔开,如3,3,4,则简单选出3个,普通选出3个,困难选出4个,以此类推',
+			'name' 		=> '难度数量设置-多个用换行隔开,如1,3 换行 3,4,则难度id为1的选出3个,难度id为3的选出4个,以此类推,总数量以题目数量为准,请准确输入',
 			'default' 	=> '',
 			'desc' 		=> '描述',
 			'match' 	=> 'is_string',
@@ -134,6 +173,13 @@ return array
 			'show'		=> 'type=3',
 		),
 
+		'hr3'		=> array
+		(
+			'name' 		=> '答题复活规则',
+			'class'		=> '',//本项必须填写
+			'attr'		=> '',
+		),
+
 		'continue'		=> array
 		(
 			'type' 		=> 'tinyint-1',
@@ -143,16 +189,72 @@ return array
 			'match' 	=> 'is_numeric',
 			'update'	=> 'radio',
 			'option'	=> $continue,
+			'control'	=> 'continue',
 		),
 
-		'num'		=> array
+		'continue_times'		=> array
 		(
 			'type' 		=> 'int-11',
-			'name' 		=> '题目数量',
-			'default' 	=> '10',
-			'desc' 		=> '题目数量',
-			'match' 	=> 'is_string',
+			'name' 		=> '超时时间-请直接输入秒数,由于网络消耗,可以设置为前端时间的2倍',
+			'default' 	=> '20',
+			'desc' 		=> '超时时间',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'text',
+			'show'		=> array('continue=2', 'continue=3'),
+		),
+
+		'continue_num'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '答错复活次数',
+			'default' 	=> '3',
+			'desc' 		=> '答错复活次数',
+			'match' 	=> 'is_numeric',
 			'update'	=> 'text',
+			'show'		=> array('continue=2', 'continue=3'),
+		),
+
+		'continue_num_type'		=> array
+		(
+			'type' 		=> 'tinyint-1',
+			'name' 		=> '答题复活时间间隔',
+			'default' 	=> '1',
+			'desc' 		=> '答题复活时间间隔',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'radio',
+			'option'	=> $continue_num_type,
+			'show'		=> array('continue=2', 'continue=3'),
+			'control'	=> 'continue_num_type',
+		),
+
+		'continue_num_type_times'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '答题复活时间间隔时间-如选择按照小时间隔则为24小时',
+			'default' 	=> '24',
+			'desc' 		=> '答题复活时间间隔时间',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'text',
+			'show'		=> array('continue_num_type=2', 'continue_num_type=3'),
+		),
+
+		'continue_num_user'		=> array
+		(
+			'type' 		=> 'tinyint-1',
+			'name' 		=> '答题复活有效规则',
+			'default' 	=> '1',
+			'desc' 		=> '答题复活有效规则',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'radio',
+			'option'	=> $continue_num_user,
+			'show'		=> array('continue=2', 'continue=3'),
+		),
+
+		'hr4'		=> array
+		(
+			'name' 		=> '结果规则',
+			'class'		=> '',//本项必须填写
+			'attr'		=> '',
 		),
 
 		'content'		=> array

+ 1 - 1
database/ranking.php

@@ -4,7 +4,7 @@ return array
 	# 表名
 	'name' => 'ranking',
 	# 显示给用户看的名称
-	'lang' => '排行榜',
+	'lang' => '积分榜单',
 	# 是否显示在后台菜单
 	'order' => 1,
 

+ 17 - 3
database/user.php

@@ -144,11 +144,11 @@ return array
 		'cdate'		=> array
 		(
 			'type' 		=> 'int-11',
-			'name' 		=> '录入时间',
+			'name' 		=> '更新时间',
 			'match' 	=> array('is_numeric', time()),
 			'desc' 		=> '',
 			# 只有insert时才生效
-			'insert'	=> true,
+			//'insert'	=> true,
 			'search'	=> 'date',
 			'list'		=> 'date("Y-m-d H:i:s", {cdate})',
 		),
@@ -163,6 +163,20 @@ return array
 
 	'request' => array
 	(
+		# 获取最新一条的用户信息
+		'get' => array
+		(
+			# 匹配的正则或函数 选填项
+			'where' => array
+			(
+				'uid' => 'yes',
+				'state' => 1,
+			),
+			'type' => 'one',
+			'order' => array('times' => 'desc', 'id' => 'desc'),
+			'col' => '*',
+		),
+
 		# 获取已完成记录
 		'getFinish' => array
 		(
@@ -176,7 +190,7 @@ return array
 			),
 			'type' => 'one',
 			'order' => array('times' => 'desc', 'id' => 'desc'),
-			'col' => 'times',
+			'col' => 'times,status',
 		),
 
 		# 获取未完成记录

+ 192 - 51
src/Api.php

@@ -72,31 +72,7 @@ class Api
 		}
 
 		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('难度获取失败');
-			}
+			$where['level_id'] = $this->getLevel($level_ids, $index);
 		}
 
 		$data = Dever::db('question/info')->getAll($where);
@@ -131,6 +107,61 @@ class Api
 		return $result;
 	}
 
+	/**
+	 * 获取等级id
+	 *
+	 * @return mixed
+	 */
+	private function getLevel($level_ids, $index)
+	{
+		# 如果有难度 比较自由的模式 1,3 换行 2,3
+		$level = explode("\r\n", $level_ids);
+		$level_data = Dever::db('question/level')->all();
+		$index = $index + 1;
+		$level_info = array();
+		$i = 1;
+		foreach ($level as $v) {
+			$v = explode(',', $v);
+			$start = $i;
+			$end = $v[1];
+			$i = $end + $i;
+			$end = $start + $end - 1;
+			if ($index >= $start && $index <= $end) {
+				if (isset($level_data[$v[0]])) {
+					$level_info = $level_data[$v[0]];
+				}
+				
+				break;
+			}
+		}
+		/*
+		3,3,4形式,已废弃
+		$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)) {
+			return $level_info['id'];
+		} else {
+			Dever::alert('难度获取失败');
+		}
+	}
+
 
 	/**
 	 * 初始化用户信息
@@ -148,7 +179,7 @@ class Api
 		if ($this->exam['continue'] == 1) {
 			# 答错可以继续答题 则答完所有题才算完成
 			$where['where_status'] = 4;
-		} elseif ($this->exam['continue'] == 2) {
+		} elseif ($this->exam['continue'] >= 2) {
 			# 答错不可以继续答题 则错误也算完成
 			$where['where_status'] = 3;
 		}
@@ -157,7 +188,7 @@ class Api
 		# 获取已完成记录
 		$finish = Dever::db('question/user')->getFinish($where);
 
-		if ($this->exam['continue'] == 2 && $finish && $finish['status'] == 3) {
+		if ($this->exam['continue'] == 3 && $finish && $finish['status'] == 3) {
 			# 最新一次如果是错误的,则停止继续
 			$this->user = $finish;
 			return;
@@ -249,26 +280,25 @@ class Api
 	{
 		$this->getUser();
 
-		$this->exam = Dever::db('question/exam')->one($this->user['exam_id']);
-
 		return $this->getQuestionInfo();
 	}
 
+	/**
+	 * 获取最新一条用户参与信息
+	 *
+	 * @return mixed
+	 */
 	private function getUser()
 	{
 		$uid = $this->getUid();
-
-		$id = Dever::input('user_data_id');
-
-		$this->user = Dever::db('question/user')->one($id);
+		//$id = Dever::input('user_data_id');
+		$this->user = Dever::db('question/user')->get(array('where_uid' => $uid));
 
 		if (!$this->user) {
 			Dever::alert('错误的用户信息');
 		}
 
-		if ($this->user && $uid != $this->user['uid']) {
-			Dever::alert('错误的用户信息');
-		}
+		$this->exam = Dever::db('question/exam')->one($this->user['exam_id']);
 	}
 
 
@@ -285,7 +315,7 @@ class Api
 				Dever::alert('已经答完所有题目');
 			}
 			# 验证是否答错
-			if ($this->exam['continue'] == 2 && $this->user['status'] == 3) {
+			if ($this->exam['continue'] >= 2 && $this->user['status'] == 3) {
 				Dever::alert('不能继续答题');
 			}
 
@@ -337,17 +367,14 @@ class Api
 	}
 
 	/**
-	 * 开始答题接口: api.submit?question_id=1&option=0&user_data_id=1&uid=1&next=1 获取下一题的信息
+	 * 开始答题接口: api.submit?question_id=1&option=0&uid=1
 	 * 返回参数:status 2为进行中 3答题错误 4答题正确
 	 *
 	 * @return mixed
 	 */
 	public function submit()
 	{
-		$id = Dever::input('user_data_id');
-		if (!$id) {
-			Dever::alert('错误的用户信息');
-		}
+		$this->getUser();
 
 		$question_id = Dever::input('question_id');
 		if (!$question_id) {
@@ -355,18 +382,33 @@ class Api
 		}
 		$option = Dever::input('option', 0);
 
-		$one['user_id'] = $id;
+		$one['user_id'] = $this->user['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 {
+			if ($this->exam['continue'] >= 2 && $this->exam['continue_times'] > 0) {
+				$cur = time();
+				# 后端超时设置
+				$times = $this->exam['continue_times'];
+
+				if ($cur - $this->user['cdate'] > $times) {
+					# 超时
+					$option = -1;
+				}
+			}
 
 			$this->hidden = false;
-			$data = $this->getInfo();
+			$data = $this->getQuestionInfo();
 			
+			if ($option == -1) {
+				# 超时
+				$data['content'][$option] = array();
+				$data['content'][$option]['score'] = 0;
+				$data['content'][$option]['title'] = '超时';
+			}
 			if (!isset($data['content'][$option])) {
 				Dever::alert('错误的选项信息');
 			}
@@ -392,7 +434,7 @@ class Api
 				# 将当前分数同步到排行榜中去
 				$this->setRanking($update['score']);
 			}
-			if ($this->exam['continue'] == 2 && $data['content'][$option]['score'] == 0) {
+			if ($this->exam['continue'] >= 2 && $data['content'][$option]['score'] == 0) {
 				#答题错误
 				$update['status'] = 3;
 				# 将当前分数同步到排行榜中去
@@ -414,11 +456,14 @@ class Api
 		$result['status'] = $update['status'];
 		$result['question_id'] = $question_id;
 		$result['option'] = $option;
-		$result['user_data_id'] = $id;
-
 		return $result;
 	}
 
+	/**
+	 * 积分榜单
+	 *
+	 * @return mixed
+	 */
 	private function setRanking($score)
 	{
 		$ranking = Dever::db('question/ranking')->one(array
@@ -454,13 +499,109 @@ class Api
 	public function continue()
 	{
 		$this->getUser();
-		$update['where_id'] = $this->user['id'];
-		$update['status'] = 2;
-		Dever::db('question/user')->update($update);
+		# 先验证当前的状态是否是3
+		if ($this->user['status'] == 3) {
+			
+			$this->checkContinue();
+
+			$update['where_id'] = $this->user['id'];
+			if ($this->user['index'] >= $this->user['num']) {
+				$update['status'] = 2;
+			} else {
+				$update['status'] = 2;
+			}
+			Dever::db('question/user')->update($update);
+		}
+
+		return true;
+	}
+
+	/**
+	 * 无限复活接口 慎用
+	 *
+	 * @return mixed
+	 */
+	public function go_continue()
+	{
+		$this->getUser();
+		# 先验证当前的状态是否是3
+		if ($this->user['status'] == 3) {
+			$update['where_id'] = $this->user['id'];
+
+			if ($this->user['index'] >= $this->user['num']) {
+				$update['status'] = 2;
+			} else {
+				$update['status'] = 2;
+			}
+			
+			Dever::db('question/user')->update($update);
+		}
 
 		return true;
 	}
 
+	/**
+	 * 验证当前的复活次数够不够
+	 *
+	 * @return mixed
+	 */
+	private function checkContinue()
+	{
+		$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;
+				}
+
+				if ($continue['num'] < $continue_num) {
+					#可以继续复活
+					$update['where_id'] = $continue['id'];
+					$update['num'] = $continue['num'] + 1;
+					Dever::db('question/continue')->update($update);
+				} else {
+					Dever::alert('无法复活');
+				}
+			} else {
+				$insert['exam_id'] = $this->user['exam_id'];
+				$insert['uid'] = $this->user['uid'];
+				$insert['user_id'] = $this->user['id'];
+				$insert['num'] = 1;
+				Dever::db('question/continue')->insert($insert);
+			}
+		}
+	}
+
 	/**
 	 * 重新挑战接口,重新开始,当前结束
 	 *