public function visit() { $survey_code = $_GET['code']; if (!$survey_code) { // 如果URL没有传入调查编码,随机生成一个推荐调查页面并跳转 $user_code = cookie('user_code'); $survey_code = userRecommendSurvey($user_code, 1, 1); redirect(U() . '?code=' . $survey_code); } else { if ($survey_code == 'preview') { // 预览调查模式 $page = '预览调查'; $survey = json_decode(cookie('survey_preview'), true); // 取cookie的调查信息 } else { // 取获得的调查编码生成调查相关信息 $page = '访问调查'; $data = surveyInfoSelect($survey_code); $survey = array_merge($data['info'], $data['stats']); } cookie('survey', json_encode($survey)); $this->assign('page', $page); $this->assign('survey', $survey); $this->display(); } }
function userRecommendSurvey($user_code, $recomm_grade, $limit) { $tbBasSurveyInfo = M(TB_BAS_SURVEY_INFO)->getTableName(); $tbBasSurveyRecommendUser = M(TB_BAS_SURVEY_RECOMMEND_USER)->getTableName(); if (!$user_code || $user_code < 10000000) { // 非登录用户随机推荐一个有效调查 $sql = "select survey_code from {$tbBasSurveyInfo} a " . "where survey_state = 3 " . "order by rand() limit 1"; $survey_code = M()->query($sql)[0]['survey_code']; } else { // 登录用户按推荐规则进行推荐 if ($recomm_grade <= 5) { // 按推荐规则顺序查找调查 switch ($recomm_grade) { // 推荐清单中该用户还未参与过的调查 case '1': $condition = '1 = 1'; break; // 确认用户设置的兴趣调查类型,再查找同类型的有效调查推荐 // 确认用户设置的兴趣调查类型,再查找同类型的有效调查推荐 case '2': $interest = M(TB_BAS_USER_INTEREST)->where("user_code = '{$user_code}'")->find(); $condition = "survey_type in (" . $interest['survey_type'] . ")"; break; // 查找用户已参与的调查,取参与较多的调查类型对应的调查 // 查找用户已参与的调查,取参与较多的调查类型对应的调查 case '3': $tbBasSurveyAction = M(TB_BAS_SURVEY_ACTION)->getTableName(); $sql = "select a.survey_type , count(1) " . "from {$tbBasSurveyInfo} a , {$tbBasSurveyAction} b " . "where a.survey_code = b.survey_code " . "and a.user_code <> {$user_code} " . "and b.user_code = {$user_code} " . "group by a.survey_type order by count(1) desc limit 1"; $condition = "survey_type in (" . M()->query($sql)[0]['survey_type'] . ")"; break; // 通过调查类型与用户属性关系表分析出的该用户属性对应的调查类型(待完善关系表后再开发) // 通过调查类型与用户属性关系表分析出的该用户属性对应的调查类型(待完善关系表后再开发) case '4': $condition = "1 <> 1"; break; // 从用户未参与的调查中随机抽取调查,按调查推荐优先级抽取调查 // 从用户未参与的调查中随机抽取调查,按调查推荐优先级抽取调查 case '5': $condition = "1 = 1"; break; // 从用户未参与的调查中随机抽取调查,按调查推荐优先级抽取调查 // 从用户未参与的调查中随机抽取调查,按调查推荐优先级抽取调查 default: $condition = "1 = 1"; break; } // 推荐条件生成 $recommend['user_code'] = $user_code; $recommend['condition'] = $condition; // $survey_code = recommendTo($recommend, $limit) ; // 已知用户推荐调查 if ($user_code) { $user = M(TB_BAS_SURVEY_RECOMMEND_USER)->where("user_code = '{$user_code}' and state_code in(1,2)")->find(); if ($user) { // 先查找调查推荐表中的可推荐调查 return $user['survey_code']; } else { // 如果上面推荐表中找不到再根据入参条件查找调查信息表活动状态的调查 $tbBasSurveyInfo = M(TB_BAS_SURVEY_INFO)->getTableName(); $sql = "select survey_code from {$tbBasSurveyInfo} a " . "where survey_state = 3 " . "and {$condition} and not exists ( " . " select survey_code from {$tbBasSurveyRecommendUser} b " . " where a.survey_code = b.survey_code and b.user_code = {$user_code}) " . "order by rand() limit 1"; // $user = M() -> query($sql) ; if ($survey_code = M()->query($sql)[0]['survey_code']) { return $survey_code; } } } if (!$survey_code) { $recomm_grade++; $survey_code = userRecommendSurvey($user_code, $recomm_grade, $limit); } } else { return 0; } } return $survey_code; }