123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2017/7/24
- * Time: 下午3:18
- */
- namespace ugc;
- use user_session\storage;
- use trans_wapper;
- use Exception;
- use session_helper;
- class special_support extends storage
- {
- private $special_id;
- private $comment_id;
- public function __construct($act_id, $image_id)
- {
- $this->special_id = intval($act_id);
- $this->comment_id = intval($image_id);
- parent::__construct();
- }
- public function is_special() {
- return ($this->special_id > 0 && $this->comment_id <= 0);
- }
- public function limit_type()
- {
- return storage::NORMAL_SUPPORT;
- }
- public function storage_tag()
- {
- if($this->is_special()) {
- return "special_support";
- } else {
- return "special_comment_{$this->special_id}";
- }
- }
- public function supported()
- {
- if($this->is_special()) {
- return parent::base_supported($this->special_id);
- } else {
- return parent::base_supported($this->comment_id);
- }
- }
- public function support()
- {
- if($this->is_special())
- {
- $ret = parent::base_support($this->special_id);
- if($ret) {
- $this->add_special_likes();
- }
- }
- else
- {
- $ret = parent::base_support($this->comment_id);
- if($ret) {
- $this->add_comment_likes();
- }
- }
- return $ret;
- }
- private function add_special_likes()
- {
- $mb_special = Model('mb_special');
- $mb_special->table('mb_special')->where(['special_id' => $this->special_id])->update([ 'likes' => ['exp', 'likes + 1'] ]);
- }
- private function sub_special_likes()
- {
- $mb_special = Model('mb_special');
- $mb_special->table('mb_special')->where(['special_id' => $this->special_id])->update([ 'likes' => ['exp', 'likes - 1'] ]);
- }
- private function add_comment_likes()
- {
- $mod_comment = Model('ugc_comment');
- $mod_comment->where(['comment_id' => $this->comment_id])->update([ 'likes' => ['exp', 'likes + 1'] ]);
- }
- private function sub_comment_likes()
- {
- $mod_comment = Model('ugc_comment');
- $mod_comment->where(['comment_id' => $this->comment_id])->update([ 'likes' => ['exp', 'likes - 1'] ]);
- }
- public function unsupport()
- {
- if($this->is_special())
- {
- $ret = parent::base_unsupport($this->special_id);
- if($ret) {
- $this->sub_special_likes();
- }
- }
- else
- {
- $ret = parent::base_unsupport($this->comment_id);
- if($ret) {
- $this->sub_comment_likes();
- }
- }
- return $ret;
- }
- }
- class special_submitor extends storage
- {
- private $special_id;
- private $vote_single;
- public function __construct($act_id, $vote_single)
- {
- $this->special_id = $act_id;
- if($vote_single) {
- $this->vote_single = storage::NORMAL_SUPPORT;
- } else {
- $this->vote_single = storage::DAILY_SUPPORT;
- }
- parent::__construct();
- }
- public function limit_type()
- {
- return $this->vote_single;
- }
- public function storage_tag()
- {
- if($this->vote_single == storage::NORMAL_SUPPORT) {
- return 'special_submit';
- } else {
- return 'special_submit_daily';
- }
- }
- public function submited() {
- return $this->base_supported($this->special_id);
- }
- public function submit() {
- parent::base_support($this->special_id);
- }
- }
- 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 vote($options,&$num)
- {
- try
- {
- $trans = new trans_wapper($this->mod_special,"special_vote");
- $special_info = $this->mod_special->getMbSpecialByID($this->special_id,'*',true);
- $special = new special($special_info);
- $vote_result = $special->vote_result();
- $num = 0;
- if($vote_result != false)
- {
- $count = count($vote_result);
- foreach ($options as $val)
- {
- $index = intval($val['index']);
- $answer = $val['answer'];
- if($index >= $count) continue;
- $result = &$vote_result[$index];
- foreach ($answer as $item)
- {
- $opval = intval($item);
- if(array_key_exists($opval,$result)) {
- $result[$opval] += 1;
- $num++;
- }
- }
- }
- }
- $this->mod_special->table('mb_special')->where(['special_id' => $this->special_id])->update(['vote_result' => serialize($vote_result)]);
- $trans->commit();
- return $vote_result;
- }
- catch (Exception $ex) {
- $trans->rollback();
- 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 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;
- }
- 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 answer($sub_answers,&$answer_id)
- {
- $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)
- {
- $answer_id = 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()]);
- } else {
- $answer_id = false;
- }
- 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;
- }
- }
|