예제 #1
0
 /**
  * 创建一个新的回答   [已重构]
  * @param content
  * @param type
  * @param problem_id
  * @param language
  * @return bool
  */
 public function create_detail()
 {
     parent::require_login();
     $params = $this->get_params('POST', array('type' => true, 'problem_id' => true, 'language'));
     extract($params);
     $content = $_POST['content'];
     $code = $_POST["code"];
     switch ($language) {
         case '0':
             $language = "html";
             break;
         case '1':
             $language = "php";
             break;
         case '2':
             $language = "c";
             break;
         case '3':
             $language = "javascript";
             break;
         case '4':
             $language = "java";
             break;
         default:
             $language = "php";
             break;
     }
     parent::is_length(array(array("name" => "解答描述", "value" => $content, "min" => 6)));
     $problem = $this->problem_model->get(array('id' => $problem_id));
     if ($this->me['id'] !== $problem['answer_id'] && $this->me['type'] !== 1) {
         $this->finish(false, '您并不是大神用户,无法回答问题');
     }
     if (!isset($problem['id'])) {
         $this->finish(false, '该问题不存在,请您回答其他的问题!');
     }
     if ($problem['type'] != 1) {
         $this->finish(false, '问题还没有被认领,无法回答问题!');
     }
     // Problem answer time out
     if ($problem["answer_time"] + 1200 < time()) {
         $this->problem_model->def($problem_id);
         $this->finish(false, '问题已经过期,无法回答!');
     }
     // Create problem detail
     $this->problem_model->edit($problem_id, array("answer_time" => time()));
     $new_detail_id = $this->problem_detail_model->create(array('owner_id' => $this->me['id'], 'content' => parent::HTML($content), 'problem_id' => $problem_id, 'code' => htmlspecialchars($code), 'type' => $type, 'language' => $language));
     // add tag active user
     $tags = json_decode($problem['tags']);
     foreach ($tags as $tag) {
         $tag_id = $this->tag_model->get(array('name' => $tag->t))['id'];
         $this->tag_model->add_active_user($tag_id, $this->me['id'], true);
     }
     //Empty problem temp data
     $this->problem_detail_model->remove_where(array("problem_id" => $problem_id, "type" => 3));
     //Close problem
     $this->problem_model->done($problem_id);
     $is_keep = ModelFactory::Usertask()->is_keep_answer($this->me['id']);
     ModelFactory::Usertask()->answer_sign($this->me['id'], date('Y-m-d'), $is_keep + 1);
     $cg_value = 0;
     if ($is_keep + 1 >= CONSTFILE::USER_TASK_GOD_ANSWER_QUESTION_VALUE) {
         $god_level = ModelFactory::User()->get_god_level($this->me['id']);
         $cg_value = 0;
         if ($god_level >= 2 && $god_level <= 3) {
             $cg_value = 5;
         }
         if ($god_level <= 7 && $god_level >= 4) {
             $cg_value = 8;
         }
         if ($god_level <= 10 && $god_level >= 8) {
             $cg_value = 15;
         }
         if ($cg_value) {
             try {
                 ModelFactory::Usertask()->begin();
                 $result = ModelFactory::Usertask()->answer_sign($this->me['id'], date('Y-m-d'), 1);
                 if ($result) {
                     ModelFactory::User()->coin($this->me['id'], $cg_value, true, 'prestige', CONSTFILE::CHANGE_LOG_COUNT_TYPE_ANSWER_30);
                 }
             } catch (Exception $e) {
                 ModelFactory::Usertask()->rollback();
             }
         }
     }
     $this->news_model->create(array('target' => $problem['owner_id'], 'type' => '201', 'problem_id' => $problem_id, 'from_id' => $this->me['id']));
     foreach (json_decode($problem['who']) as $key => $value) {
         $this->news_model->create(array('target' => $value, 'type' => '301', 'problem_id' => $problem_id, 'from_id' => $this->me['id']));
     }
     // 给大神结算问题报酬
     //        $max_coin = (100 + count(json_decode($problem['who'])) * 50);
     //        ModelFactory::User()->coin($problem['answer_id'] , $max_coin);
     $this->news_model->create(array('target' => $this->me['id'], 'type' => '402', 'problem_id' => $problem_id, 'from_id' => $problem['silver_coin']));
     // 给大神威望
     $prestige = $problem['gold_coin'] + $problem['silver_coin'] / 100;
     ModelFactory::User()->coin($problem['answer_id'], $prestige, true, 'prestige', CONSTFILE::CHANGE_LOG_COUNT_TYPE_ANSWER_PROBLEM);
     if ($problem['gold_coin']) {
         ModelFactory::User()->coin($problem['answer_id'], $problem['gold_coin'], true, 'gold_coin', CONSTFILE::CHANGE_LOG_COUNT_TYPE_ANSWER_PROBLEM);
     }
     if ($problem['silver_coin']) {
         ModelFactory::User()->coin($problem['answer_id'], $problem['silver_coin'], true, 'silver_coin', CONSTFILE::CHANGE_LOG_COUNT_TYPE_ANSWER_PROBLEM);
     }
     if ($this->problem_model->close(array('pid' => $problem_id)) === false) {
         $this->finish(false, '服务器异常,请尝试重新提交请求!');
     }
     $this->finish(true);
 }