Browse Source

add question

stanley-king 7 years ago
parent
commit
962bf7e569

+ 0 - 20
helper/model/special_info.php

@@ -1,20 +0,0 @@
-<?php
-/**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2017/7/24
- * Time: 下午7:41
- */
-
-class special_info
-{
-    private $mInfo;
-    public function __construct($info)
-    {
-        $this->mInfo = $info;
-    }
-
-    public function special_id() {
-        return intval($this->mInfo['special_id']);
-    }
-}

+ 1 - 2
helper/model_helper.php

@@ -10,5 +10,4 @@ require_once (BASE_ROOT_PATH . "/helper/model/member_info.php");
 require_once (BASE_ROOT_PATH . "/helper/model/order.php");
 require_once (BASE_ROOT_PATH . "/helper/model/ranklist.php");
 require_once (BASE_ROOT_PATH . "/helper/model/goods_summary.php");
-require_once (BASE_ROOT_PATH . "/helper/model/goods_common.php");
-require_once (BASE_ROOT_PATH . "/helper/model/special_info.php");
+require_once (BASE_ROOT_PATH . "/helper/model/goods_common.php");

+ 117 - 20
helper/ugc/answer.php

@@ -9,9 +9,9 @@
 namespace ugc;
 
 use user_session\storage;
-use Log;
 use trans_wapper;
 use Exception;
+use session_helper;
 
 class special_support
 {
@@ -38,33 +38,48 @@ class special_support
     }
 }
 
-class special_vote extends storage
+class special_submitor extends storage
 {
-    private $mod_special;
     private $special_id;
 
-    public function __construct($special_id)
+    public function limit_type()
     {
-        $this->mod_special = Model('mb_special');
-        $this->special_id = $special_id;
-
-        parent::__construct();
+        return storage::NORMAL_SUPPORT;
     }
-    public function __destruct()
+    public function storage_tag()
     {
-        Log::record(__METHOD__,Log::DEBUG);
+        return 'special_submit';
     }
 
-    public function limit_type()
+    public function submited() {
+        return $this->supported($this->special_id);
+    }
+    public function submit() {
+        parent::support($this->special_id);
+    }
+    public function support_count()
     {
-        return storage::NORMAL_SUPPORT;
     }
-    public function storage_tag()
+    public function onSuppored()
     {
-        return 'special_vote';
     }
-    public function voted() {
-        return $this->supported($this->special_id);
+    public function onUnSupported()
+    {
+    }
+}
+
+class special_vote
+{
+    private $mod_special;
+    private $special_id;
+
+    public function __construct($special_id)
+    {
+        $this->mod_special = Model('mb_special');
+        $this->special_id = $special_id;
+    }
+    public function __destruct()
+    {
     }
 
     public function vote($options)
@@ -88,7 +103,6 @@ class special_vote extends storage
 
             $this->mod_special->table('mb_special')->where(['special_id' => $this->special_id])->update(['vote_result' => serialize($result)]);
             $trans->commit();
-            parent::support($this->special_id);
 
             return $result;
         }
@@ -97,14 +111,97 @@ class special_vote extends storage
             return false;
         }
     }
+}
+
+class special_answer
+{
+    private $correct_answers;
+    private $submit_answers;
+    private $mSpecial;
+
+    public function __construct(special $special)
+    {
+        $this->mSpecial = $special;
+        $params = $this->mSpecial->question_param();
+
+        $this->correct_answers = [];
+        foreach ($params as $pos => $val) {
+            $this->correct_answers[$pos] = $val['answer'];
+        }
+    }
 
-    public function support_count()
+    public function diff()
     {
+        $result = [];
+
+        $sub_answers = $this->submit_answers;
+        foreach ($this->correct_answers as $pos => $val)
+        {
+            $item = [];
+            $item['index'] = $pos;
+            $item['origin'] = $val;
+            if(array_key_exists($pos,$sub_answers)) {
+                $item['submit'] = $sub_answers[$pos];
+                $item['correct'] = ($sub_answers[$pos] === $val);
+            } else {
+                $item['submit'] = [];
+                $item['correct'] = false;
+            }
+
+            $result[] = $item;
+        }
+
+        return $result;
     }
-    public function onSuppored()
+
+    private function format_answer($sub_answers)
     {
+        $this->submit_answers = [];
+        foreach ($sub_answers as $item)
+        {
+            $pos    = $item['index'];
+            $answer = $item['answer'];
+
+            $tmp = [];
+            foreach ($answer as $val) {
+                $tmp[] = intval($val);
+            }
+            sort($tmp);
+            $tmp = array_unique($tmp);
+
+            $this->submit_answers[$pos] = $tmp;
+        }
+
+        return $this->submit_answers;
     }
-    public function onUnSupported()
+
+    public function answer($sub_answers)
     {
+        $sub_answers = $this->format_answer($sub_answers);
+        $correct_num = $this->match_answer($sub_answers,$this->correct_answers);
+
+        $spid = $this->mSpecial->special_id();
+        $member_id = session_helper::memberid();
+        if($member_id > 0)
+        {
+            Model()->table('special_answer')->insert(['special_id' => $spid,'member_id' => $member_id,'answer' => serialize($sub_answers),
+                'total_num' => count($this->correct_answers),'correct_num' => $correct_num,'answer_time' => time()]);
+        }
+
+        return $correct_num;
+    }
+
+    private function match_answer($sub_answers,$correct_answers)
+    {
+        $correct_num = 0;
+        foreach ($sub_answers as $pos => $answer)
+        {
+            if(!array_key_exists($pos,$correct_answers)) continue;
+            if($answer === $correct_answers[$pos]) {
+                $correct_num += 1;
+            }
+        }
+
+        return $correct_num;
     }
 }

+ 3 - 0
helper/ugc/special.php

@@ -132,6 +132,9 @@ class special
     {
         return ($this->vote_param() != false);
     }
+    public function has_question() {
+        return ($this->question_param() != false);
+    }
     public function vote_param()
     {
         $params = $this->params();

+ 53 - 17
helper/ugc_helper.php

@@ -20,6 +20,8 @@ require_once(BASE_ROOT_PATH . '/helper/predeposit_helper.php');
 
 use ugc\special_vote;
 use ugc\special;
+use ugc\special_answer;
+use ugc\special_submitor;
 
 class ugc_helper
 {
@@ -81,30 +83,64 @@ class ugc_helper
         }
     }
 
-    public static function vote($special_id,$options,&$err)
+    public static function submit($special_id,$options,&$err)
     {
-        $voter = new special_vote($special_id);
-        if($voter->voted()) {
-            $err = ['code' => errcode::ErrSpecial,"您已经投过票"];
-            return false;
-        }
-        else
+//        $submitor = new special_submitor($special_id);
+//        if($submitor->submited()) {
+//            $err = ['code' => errcode::ErrSpecial,"您已经提交过"];
+//            return false;
+//        } else {
+//            $submitor->submit();
+//        }
+
+        $mod_special = Model('mb_special');
+        $special_info = $mod_special->getMbSpecialByID($special_id,'*',true);
+        $special = new special($special_info);
+
+        if(!empty($options))
         {
-            $mod_special = Model('mb_special');
-            $special_info = $mod_special->getMbSpecialByID($special_id,'*',true);
-            $special = new special($special_info);
+            $vote = $options['vote'];
+            $answers = $options['question'];
+        } else {
+            $vote = false;
+            $answers = false;
+        }
 
-            $vote_result = $voter->vote($options);
-            if($vote_result != false)
+
+        $answer_diff = [];
+        $vote_result = [];
+        $num = 0;
+        if($special->has_vote() || $special->has_question())
+        {
+            if($special->has_vote() && $vote != false)
             {
-                $rule = $special->submit_rule();
-                if($rule != false) {
-                    $type_sn = $rule->make_bonus(1);
-                    return ['vote_result' => $vote_result,'type_sn' => $type_sn];
+                $voter = new special_vote($special->special_id());
+                $vote_result = $voter->vote($options);
+                if($vote_result == false) {
+                    return false;
+                } else {
+                    $num++;
                 }
             }
-            return ['vote_result' => $vote_result,'type_sn' => ''];
+            if($special->has_question() && $answers != false)
+            {
+                $answor = new special_answer($special);
+                $correct_num = $answor->answer($answers);
+                $num += $correct_num;
+                $answer_diff = $answor->diff();
+            }
+        }
+        else {
+            $num = 1;
+        }
+
+        $rule = $special->submit_rule();
+        if($rule != false) {
+            $type_sn = $rule->make_bonus($num);
+        } else {
+            $type_sn = "";
         }
+        return ['vote_result' => $vote_result,'answer_diff' => $answer_diff,'type_sn' => $type_sn];
     }
 
     public static function init_server()

+ 2 - 2
mobile/control/control.php

@@ -284,9 +284,9 @@ class mbMemberControl extends mobileControl
 
 function bonus_version()
 {
-    return "v=2017072401";
+    return "v=20170728";
 }
 function shop_version()
 {
-    return "v=2017072401";
+    return "v=20170728";
 }

+ 108 - 48
mobile/control/special.php

@@ -15,7 +15,6 @@ require_once(BASE_ROOT_PATH . '/helper/model_helper.php');
 require_once(BASE_ROOT_PATH . '/helper/user_session/storage.php');
 require_once(BASE_ROOT_PATH . '/helper/ugc_helper.php');
 
-
 class specialControl extends mobileHomeControl
 {
     public function __construct() {
@@ -53,40 +52,39 @@ class specialControl extends mobileHomeControl
         }
     }
 
-    public function voteOp()
+    public function submitOp()
     {
         $special_id = intval($_GET['special_id']);
+        $spitem = spid_helper::instance()->special($special_id);
 
-        if(!empty($_GET['options'])) {
-            $options = explode(',',$_GET['options']);
+        if($special_id < 0 || $spitem == false) {
+            return self::outerr(errcode::ErrParamter,"该文章不存在");
         }
-
-        if($special_id < 0 || empty($options)) {
-            return self::outerr(errcode::ErrParamter,"该文章不存在或者没有选中选项");
+        $content = urldecode($_GET['content']);
+        if(empty($content)) {
+            $options = false;
+        }
+        else {
+            $options = json_decode($content,true);
+            if($options == null) $options = false;
         }
 
-        $spitem = spid_helper::instance()->special($special_id);
-        if($spitem->has_vote())
+        $result = ugc_helper::submit($special_id,$options,$err);
+        if($result == false) {
+            return self::outerr($err['code'],$err['msg']);
+        }
+        else
         {
-            $result = ugc_helper::vote($special_id,$options,$err);
-            if($result == false) {
-                return self::outerr($err['code'],$err['msg']);
-            }
-            else
-            {
-                $vote_result = $result['vote_result'];
-                $type_sn = $result['type_sn'];
-                if(!empty($type_sn)) {
-                    $url = BASE_SITE_URL . "/mobile/index.php?act=bonusex&op=open&type_sn={$type_sn}";
-                } else {
-                    $url = "";
-                }
-
-                return self::outsuccess(['special_id' => $special_id,'vote_result' => $vote_result,'bonus_url' => $url]);
+            $vote_result = $result['vote_result'];
+            $type_sn = $result['type_sn'];
+            if(!empty($type_sn)) {
+                $url = BASE_SITE_URL . "/mobile/index.php?act=bonusex&op=open&type_sn={$type_sn}";
+            } else {
+                $url = "";
             }
-        }
-        else {
-            return self::outerr(errcode::ErrSpecial,"本文不支持投票");
+            $answer_diff = $result['answer_diff'];
+
+            return self::outsuccess(['special_id' => $special_id,'vote_result' => $vote_result,'answer_diff' => $answer_diff,'bonus_url' => $url]);
         }
     }
 
@@ -152,10 +150,17 @@ class tpl_ugc
     private $special_list;
     private $summary;
     private $mem_info;
-    private $special_info;
+    private $special;
 
+    private $mQuestionIndex;
+
+    public function special_id()
+    {
+        return $this->special->special_id();
+    }
     public function __construct($output)
     {
+        $this->mQuestionIndex = 0;
         $this->spitem = $output['spitem'];
         $this->special_list = $output['special_list'];
         $this->summary = $output['summary'];
@@ -165,9 +170,9 @@ class tpl_ugc
             $this->mem_info = null;
         }
         if(!empty($output['special_info'])) {
-            $this->special_info = new special_info($output['special_info']);
+            $this->special = new ugc\special($output['special_info']);
         } else {
-            $this->special_info = null;
+            $this->special = null;
         }
     }
     public function title() {
@@ -202,6 +207,7 @@ class tpl_ugc
 
     public function show_blocks()
     {
+        $this->mQuestionIndex = 0;
         foreach ($this->special_list as $block)
         {
             $item_type = $block['item_type'];
@@ -213,7 +219,6 @@ class tpl_ugc
     }
     private function show_items($items)
     {
-        $qindex = 0;
         foreach ($items as $item)
         {
             $show_type = $item['show_type'];
@@ -227,8 +232,11 @@ class tpl_ugc
                 $this->show_vote($item);
             }
             elseif($show_type == 'question') {
-                $this->show_question($item,$qindex);
-                $qindex++;
+                $this->show_question($item);
+                $this->mQuestionIndex++;
+            }
+            elseif($show_type == 'goods') {
+
             }
             else {
 
@@ -236,9 +244,67 @@ class tpl_ugc
         }
     }
 
-    private function show_question($item,$qindex)
+    private function show_question($item)
     {
+        if(empty($item['data'])) return false;
+        $options = json_decode($item['data'],true);
 
+        $title = $item['title'];
+        $qindex = $this->mQuestionIndex;
+        $i = $qindex + 1;
+        $show_title = "第{$i}题、{$title}";
+
+        $reserved = $item['reserved'];
+        $kv = preg_split('/=/',$reserved);
+        if(!empty($kv))
+        {
+            $k = trim($kv[0]);
+            $v = trim($kv[1]);
+            if(!empty($k) && $k == 'answer_type') {
+                $answer_type = $v;
+            }
+        }
+        $answer_type = intval($answer_type);
+        if($answer_type == 0) {
+            $sanswer_type = '单选';
+            $box = "radio";
+        }
+        elseif ($answer_type == 1) {
+            $sanswer_type = '最多选两项';
+            $box = "checkbox";
+        }
+        else {
+            $sanswer_type = '多选';
+            $box = "checkbox";
+        }
+
+        $header = "<div class=\"question_list\">
+                        <div class=\"question\">
+                            <div class=\"question_pro\">
+                                <div class=\"question_title\">{$show_title}</div>
+                                <div class=\"question_type\" data-type=\"{$answer_type}\">/{$sanswer_type}</div>
+                            </div>
+                            <div class=\"question_options\">";
+        $opindex = 65;
+        $soptions = '';
+        foreach ($options as $val)
+        {
+            $key = $val['id'];
+            $option = $val['text'];
+
+            $si = sprintf("%c",$opindex++);
+            $soptions .= "<div class=\"question_option\">
+                            <label>
+                                <input type=\"{$box}\" value=\"{$key}\" class=\"check\" name=\"question{$qindex}\">
+                                <span class=\"label\">{$si}、{$option}</span>
+                            </label>
+                        </div>";
+        }
+        $end = "</div>
+                    </div>
+                </div>";
+        $str = "{$header}{$soptions}{$end}";
+        echo $str;
     }
 
     private function show_vote($item)
@@ -262,21 +328,18 @@ class tpl_ugc
         $vote_type = intval($vote_type);
         if($vote_type == 0) {
             $svote_type = '单选';
+            $box = "radio";
         }
         elseif ($vote_type == 1) {
             $svote_type = '最多选两项';
+            $box = "checkbox";
         }
         else {
             $svote_type = '多选';
+            $box = "checkbox";
         }
 
-        if($this->special_info != null) {
-            $special_id = $this->special_info->special_id();
-        } else {
-            $special_id = -1;
-        }
-
-        $str = "<div class=\"vote\" data-special_id=\"{$special_id}\">
+        $str = "<div class=\"vote\">
                     <div class=\"vote_pro\">
                         <div class=\"vote_question\">{$title}</div>
                         <div class=\"vote_type\">/{$svote_type}</div>
@@ -284,11 +347,12 @@ class tpl_ugc
                     <div class=\"vote_options\">";
         foreach ($options as $val)
         {
+
             $key = $val['id'];
             $option = $val['text'];
             $soption = "<div class=\"vote_option\">
                             <label>
-                                <input type=\"checkbox\" value=\"{$key}\" class=\"check\" name=\"submit_vote\">
+                                <input type=\"{$box}\" value=\"{$key}\" class=\"check\" name=\"submit_vote\">
                                 <span class=\"label\">{$option}</span>
                             </label>
                         </div>";
@@ -326,12 +390,8 @@ class tpl_ugc
         {
             $video  = $item['data'];
             $str = "<div class=\"thumbnail\">
-                        <video poster=\"$image\" src=\"{$video}\">
-                            您的设备不支持视频播放
-                        </video>
-                        <div class=\"thumbnail_pro\">
-                            《化妆小教程》
-                        </div>
+                        <div class=\"video_box\" data-poster=\"{$image}\" data-src=\"{$video}\"></div>
+                        <div class=\"thumbnail_pro\">$title</div>
                     </div>";
         }
         else

+ 2 - 4
mobile/templates/default/ugc/content.php

@@ -11,16 +11,14 @@
     <meta name="x5-fullscreen" content="true"/>
     <link rel="stylesheet" href="<?php echo RESOURCE_SITE_URL; ?>/mobile/ugc/css/main.css?<?php echo bonus_version(); ?>">
 </head>
-<body>
-
+<body data-special_id="<?php $tpl_obj = $output['tpl_obj']; echo $tpl_obj->special_id();?>">
 <?php $tpl_obj = $output['tpl_obj']; $tpl_obj->show_title();?>
-<div class="author">
+<div class="author" >
     <div class="author_msg" style="font-size: 34px;">
         <?php $tpl_obj = $output['tpl_obj']; $tpl_obj->show_sender();?>
     </div>
 </div>
 <?php $tpl_obj = $output['tpl_obj']; $tpl_obj->show_blocks();?>
-
 <div class="comment">
     <div class="pro">
         <div class="pro_title">评论/</div>

+ 6 - 0
test/TestPush.php

@@ -35,4 +35,10 @@ class TestPush extends PHPUnit_Framework_TestCase
         $x = strlen($src);
         $y = substr($src,7,3);
     }
+
+    public function testOption()
+    {
+        $ch = 65;
+        $x = sprintf("%c",$ch);
+    }
 }