where('category_id', $category_id)->first(); if($res){ $res = collect($res)->toArray(); $res = Api::dateFormat($res); } return $res; } /** * 添加题库 * @param $params * @return array */ public static function saveCategory($params){ $act_info = ActivityService::getActivityInfo($params['activity_id'], $params['corp_id']); if(!$act_info){ return Api::arr(config('code.activity_not_exist'), trans('msg.activity_not_exist')); } $query = []; $tmp = explode("-",$params["category_name"]); Log::info("category_name:".$params["category_name"]); Log::info(json_encode($tmp,256)); $query['category_name'] = $tmp[0]; if (!empty($tmp[1]) && $tmp[1] == "直接雇员") { $query["direct_employees"] = 1; } else { $query["direct_employees"] = 0; } $query['update_time'] = time(); if(!empty($params['category_id']) && isset($params['category_id'])){ $res = Categorys::where('activity_id', $params['activity_id'])->where('corp_id', $params['corp_id'])->where('category_id', $params['category_id'])->update($query); $category_id = $params['category_id']; } else { $query['corp_id'] = $params['corp_id']; $query['activity_id'] = $params['activity_id']; $query['create_time'] = time(); $res = Categorys::insertGetId($query); $category_id = $res; } if($res){ $data['category_id'] = $category_id; return Api::arr(config('code.success'), trans('msg.success'), $data); } else { return Api::arr(config('code.sql_error'), trans('msg.sql_error')); } } /** * 获取题库列表 * @param $params * @return array */ public static function getCategoryList($params){ $act_info = ActivityService::getActivityInfo($params['activity_id'], $params['corp_id']); if(!$act_info){ return Api::arr(config('code.activity_not_exist'), trans('msg.activity_not_exist')); } $categorys = Categorys::where('corp_id', $params['corp_id'])->where('activity_id', $params['activity_id'])->get()->toArray(); foreach ($categorys as $k=>$v) { $question_num = Questions::where('category_id', $v['category_id']) ->where('corp_id', $params['corp_id']) ->where('activity_id', $params['activity_id']) ->where('status', 1) //题目状态 0 无效 1有效 ->count(); $v['questionNum'] = $question_num?$question_num:0; //时间格式化 $categorys[$k] = Api::dateFormat($v); } return Api::arr(config('code.success'), trans('msg.success'), $categorys); } /** * 计算多个题库内的题目总数 */ public static function getQuestionsCount($categorys, $corp_id, $activity_id){ $category = explode(',', $categorys); $count = 0; foreach($category as $k=>$v){ $category_count = Questions::where('corp_id', $corp_id)->where('activity_id', $activity_id)->where('category_id', $v)->count(); $count += $category_count; } return $count; } /** * 答题设置 * @param $params * @return array */ public static function questionConfigDetail($params){ //闯关、必答、PK 题库不能为空 必选项 if(empty($params['must_category_ids']) || empty($params['blockade_category_ids']) || empty($params['pk_category_ids'])){ return Api::arr(config('code.params_error'), trans('msg.category_ids_null')); } $must_category_ids = $params['must_category_ids']; $blockade_category_ids = $params['blockade_category_ids']; $pk_category_ids = $params['pk_category_ids']; //计算选中的题库内题目总数 $must_count = self::getQuestionsCount($must_category_ids, $params['corp_id'], $params['activity_id']); $blockade_count = self::getQuestionsCount($blockade_category_ids, $params['corp_id'], $params['activity_id']); $pk_count = self::getQuestionsCount($pk_category_ids, $params['corp_id'], $params['activity_id']); //题目数不能大于选中的题库内题目的总数 if ($must_count < $params['must_question_num'] || $blockade_count < $params['blockade_question_num'] || $pk_count < $params['pk_question_num']) { return Api::arr(config('code.params_error'), trans('msg.num_greater_category')); } //允许答错的题目数不能大于总题目数 if ($params['must_answer_wrong_num'] > $params['must_question_num'] || $params['blockade_answer_wrong_num'] > $params['blockade_question_num']) { return Api::arr(config('code.params_error'), trans('msg.wrong_greater_num')); } //获取配置项信息 $config_info = Configs::where('corp_id', $params['corp_id'])->where('activity_id', $params['activity_id'])->first(); if(!$config_info){ return Api::arr(config('code.activity_not_exist'), trans('msg.activity_not_exist')); } //配置项入库 $config_info = collect($config_info)->toArray(); $extend_configs = json_decode($config_info['extend_configs'], true); $extend_configs['must_question_num'] = $params['must_question_num']; $extend_configs['must_answer_wrong_num'] = $params['must_answer_wrong_num']; $extend_configs['must_answer_success_km'] = $params['must_answer_success_km']; $extend_configs['blockade_question_num'] = $params['blockade_question_num']; $extend_configs['blockade_answer_wrong_num'] = $params['blockade_answer_wrong_num']; $extend_configs['blockade_answer_success_km'] = $params['blockade_answer_success_km']; $extend_configs['pk_question_num'] = $params['pk_question_num']; $extend_configs['pk_last_double'] = $params['pk_last_double']; $extend_configs['pk_kms'] = $params['pk_kms']; $extend_configs['must_category_ids'] = $must_category_ids; $extend_configs['blockade_category_ids'] = $blockade_category_ids; $extend_configs['pk_category_ids'] = $pk_category_ids; $extend_configs['top_prize_open'] = $params['top_prize_open']; $extend_configs['top_prize_text'] = $params['top_prize_text']; $extend_configs['top_prize_num'] = $params['top_prize_num']; $extend_configs['must_answer_count_down'] = isset($params['must_answer_count_down']) && $params['must_answer_count_down']>=5?$params['must_answer_count_down']:10; $extend_configs['blockade_answer_count_down'] = isset($params['blockade_answer_count_down']) && $params['blockade_answer_count_down']>=5?$params['blockade_answer_count_down']:10; $extend_configs = json_encode($extend_configs); $res_config = Configs::where('corp_id', $params['corp_id'])->where('activity_id', $params['activity_id'])->update(['extend_configs' => $extend_configs, 'update_time' => time()]); return Api::arr(config('code.success'), trans('msg.success')); } /** * 修改/新增题目 * @param $params * @return array */ public static function questionModify($params){ $query = []; $question_options= []; $answers = []; if(!empty($params['option_a'])){ $question_options['A'] = Api::trimAll($params['option_a']); $answers[] = 'A'; } if(!empty($params['option_b'])){ $question_options['B'] = Api::trimAll($params['option_b']); $answers[] = 'B'; } if(!empty($params['option_c'])){ $question_options['C'] = Api::trimAll($params['option_c']); $answers[] = 'C'; } if(!empty($params['option_d'])){ $question_options['D'] = Api::trimAll($params['option_d']); $answers[] = 'D'; } $correct_answer = self::answerChange($params['correct_answer']); //题目和答案不可为空 if(empty(Api::trimAll($params['question_title'])) || empty($correct_answer)){ return Api::arr(config('code.params_error'), trans('msg.title_answer_null')); } //选项最少需要两个 if(count($question_options)<2){ return Api::arr(config('code.params_error'), trans('msg.option_error')); } //答案必须在已有可用选项当中 foreach($correct_answer as $k=>$v){ if(!in_array($v, $answers)){ return Api::arr(config('code.params_error'), trans('msg.answer_error')); } } //参数组装 $query['question_title'] = Api::trimAll($params['question_title']); $query['question_options'] = serialize($question_options); $query['correct_answer'] = implode('', $correct_answer); $query['question_type'] = count($correct_answer) > 1 ? 2 : 1; //题目类型 单选/多选 $query['category_id'] = intval($params['category_id']); $query['update_time'] = time(); //题目ID存在则修改题目,不存在则新增题目 if(isset($params['question_id']) && $params['question_id'] != 0){ $question = Questions::where('activity_id', $params['activity_id'])->where('corp_id', $params['corp_id'])->where('question_id', $params['question_id'])->first(); if(!$question){ return Api::arr(config('code.question_not_exist'), trans('msg.question_not_exist')); } $res = Questions::where('activity_id', $params['activity_id'])->where('corp_id', $params['corp_id'])->where('question_id', $params['question_id'])->update($query); if(!$res){ return Api::arr(config('code.sql_error'), trans('msg.sql_error')); } } else { $query['corp_id'] = $params['corp_id']; $query['activity_id'] = $params['activity_id']; $query['create_time'] = time(); $res = Questions::insert($query); if(!$res){ return Api::arr(config('code.sql_error'), trans('msg.sql_error')); } } return Api::arr(config('code.success'), trans('msg.success')); } /** * 获取题库内题目数量 * @param $corp_id * @param $activity_id * @return array */ public static function getCategoryCount($corp_id, $activity_id){ $categorys = Categorys::where('activity_id', $activity_id)->where('corp_id', $corp_id)->get()->toArray(); $data = []; if($categorys) { foreach ($categorys as $category) { $data[$category['category_name']] = Questions::where('activity_id', $activity_id)->where('corp_id', $corp_id)->where('category_id', $category['category_id'])->count(); } } return $data; } /** * 新增题库及题目上传 * @param $data * @param $params * @return array|string */ public static function questionUpload($data, $params){ if(empty($data)){ return Api::arr(config('code.fail'), trans('msg.question_not_exist')); } //添加题库 $categoryRes = self::saveCategory($params); if($categoryRes['code'] != config('code.success')){ return Api::arr(config('code.fail'), trans('msg.category_add_fail')); } $category_id = $categoryRes['data']['category_id']; DB::beginTransaction(); $errors = []; $error_num = 0; foreach ($data as $k => $v) { $question_options = array(); $question_title = $v[0]; $answers = array(); if(!empty($v[1]) || $v[1] == '0'){ $question_options['A'] = $v[1]; $answers[] = 'A'; } if(!empty($v[2]) || $v[2] == '0'){ $question_options['B'] = $v[2]; $answers[] = 'B'; } if(!empty($v[3]) || $v[3] == '0'){ $question_options['C'] = $v[3]; $answers[] = 'C'; } if(!empty($v[4]) || $v[4] == '0'){ $question_options['D'] = $v[4]; $answers[] = 'D'; } $correct_answer = $v[5]; $correct_answer = self::answerChange($correct_answer); $query = array(); $query['corp_id'] = $params['corp_id']; $query['activity_id'] = $params['activity_id']; $query['category_id'] = $category_id; $query['question_title'] = $question_title; //答案单选 答案不能为空 题目不能为空 选项最少两个 答案不在选项中 都视为错误题目 if(empty($correct_answer)){ $v[6] = '答案为空'; $errors[] = $v; $error_num += 1; continue; } if(empty($query['question_title'])){ $v[6] = '题目为空'; $errors[] = $v; $error_num += 1; continue; } if(count($answers) < 2){ $v[6] = '选项少于2个'; $errors[] = $v; $error_num += 1; continue; } //支持多选 $correct_type = 0; foreach($correct_answer as $key=>$val){ if(!in_array($val, $answers)){ $v[6] = '答案中选项为空或不存在'; $errors[] = $v; $error_num += 1; $correct_type = 1; continue; } } if($correct_type == 1){ continue; } $query['correct_answer'] = implode('', $correct_answer); $query['question_type'] = count($correct_answer) > 1 ? 2 : 1; $query['question_options'] = serialize($question_options); //序列化 $query['create_time'] = time(); $query['update_time'] = time(); $res = Questions::insert($query); if (!$res) { DB::rollBack(); return Api::arr(config('code.sql_error'), trans('msg.sql_error')); } } DB::commit(); //错误记录存入redis Redis::set(self::QUESTION_UPLOAD_FAIL.$params['activity_id'], json_encode($errors),'EX',3600); $res = []; $res['total'] = count($data); $res['error_num'] = $error_num; $res['success_num'] = $res['total'] - $error_num; $res['category_id'] = $category_id; return Api::arr(config('code.success'), trans('msg.success'), $res); } /** * 删除题目 * 2018-06-22 10:32:00 * @param $params * @return array */ public static function delQuestion($params){ //题目信息 $questionInfo = Questions::where('activity_id', $params['activity_id']) ->where('corp_id', $params['corp_id']) ->where('category_id', $params['category_id']) ->where('question_id', $params['question_id']) ->where('status', 1) ->first(); if(!$questionInfo){ return Api::arr(config('code.question_not_exist'), trans('msg.question_not_exist')); } //删除题目 $res = Questions::where('activity_id', $params['activity_id']) ->where('corp_id', $params['corp_id']) ->where('category_id', $params['category_id']) ->where('question_id', $params['question_id']) ->where('status', 1) ->delete(); if($res){ return Api::arr(config('code.success'), trans('msg.success')); } else { return Api::arr(config('code.sql_error'), trans('msg.sql_error')); } } /** * 下载上传失败的题目数据 * @param $data */ public static function downloadQuestionExcel($data){ $cellData = [ ['题目标题', 'A选项', 'B选项', 'C选项', 'D选项', '答案', '错误原因'], ['填写规则'], ]; foreach($cellData as $k=>$v){ array_unshift($data, $v); } Excel::create('题目导入错误数据'.date('mdHi'),function($excel) use ($data){ $excel->sheet('题目信息', function($sheet) use ($data){ $tot = count($data) ; $sheet->setWidth(array( 'A' => 50, 'B' => 15, 'C' => 15, 'D' => 15, 'E' => 15, 'F' => 15, 'G' => 15, ))->rows($data)->setFontSize(12); //设置第一行的表格格式 $sheet->setStyle([ 'font' => [ 'size' => 12, 'bold' => false, ] ]) ->mergeCells('A1:G1') //合并单元格 ->setHeight(1, 100) //设置行高 ->setHeight(2, 30) ->cells('A1:G1', function($cells){ //设置单元格格式 $cells->setValignment('center'); //文本对齐方式 $cells->setFontWeight('bold'); //粗体 }) ->cells('A2:G2', function($cells){ $cells->setValignment('center'); $cells->setBackground('#92D050'); $cells->setFontWeight('bold'); $cells->setFontSize(10); $cells->setAlignment('center'); }) ->cells('B2:G'.$tot, function($cells) { $cells->setAlignment('center'); }) ->cells('G3:G'.$tot, function($cells) { $cells->setFontColor('#ff0000'); $cells->setFontWeight('bold'); }); }); })->export('xls'); } /** * 添加默认题库 * @param $activity_id * @return array */ public static function addDefault($activity_id){ $activity_info = ActivityService::getActivityInfo($activity_id); if(!$activity_info){ return Api::arr(config('code.fail'), trans('msg.activity_not_exist')); } $default_questions = Questions::where('corp_id', 0)->where('activity_id', 0)->get()->toArray(); $query_category = []; $query_category['corp_id'] = $activity_info['corp_id']; $query_category['activity_id'] = $activity_id; $query_category['category_name'] = '样例题库'; $query_category['create_time'] = time(); $query_category['update_time'] = time(); $res = Categorys::insertGetId($query_category); if(!$res){ return Api::arr(config('code.sql_error'), trans('msg.sql_error')); } foreach($default_questions as $k=>$v){ $query_question = []; $query_question['corp_id'] = $activity_info['corp_id']; $query_question['activity_id'] = $activity_id; $query_question['question_title'] = $v['question_title']; $query_question['category_id'] = $res; $query_question['question_options'] = $v['question_options']; $query_question['correct_answer'] = $v['correct_answer']; $query_question['create_time'] = time(); $query_question['update_time'] = time(); Questions::insert($query_question); } $data['category_id'] = $res; return Api::arr(config('code.success'), trans('msg.success'), $data); } /** * 删除题库(活动委发布状态才可删除) * @param $activity_id * @param $category_id * @return array */ public static function delCategory($activity_id, $category_id){ $activityInfo = ActivityService::getActivityInfo($activity_id); if(!$activityInfo){ return Api::arr(config('code.fail'), trans('msg.activity_not_exist')); } if($activityInfo['is_complete_set'] == 1){ return Api::arr(config('code.fail'), '活动已发布,无法删除题库'); } $categoryInfo = Categorys::where('category_id', $category_id)->first(); if(!$categoryInfo){ return Api::arr(config('code.fail'), trans('msg.category_not_exist')); } Categorys::where('category_id', $category_id)->delete(); $questions = Questions::where('category_id', $category_id)->get(); foreach($questions as $k=>$v){ Questions::where('question_id', $v['question_id'])->delete(); } return Api::arr(config('code.success'), trans('msg.success')); } /** * 题目答案转换 * @param $correct_answer * @return array|string */ public static function answerChange($correct_answer){ //去除中文字并转换大写 $arr = strtoupper(preg_replace('/([\x80-\xff]*)/i','',$correct_answer)); //拆分单个选项并去除重复选项 $arr = array_unique(str_split($arr)); //排序 sort($arr); return $arr; } } ?>