answer.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/7/24
  6. * Time: 下午3:18
  7. */
  8. namespace ugc;
  9. use user_session\storage;
  10. use trans_wapper;
  11. use Exception;
  12. use session_helper;
  13. class special_support extends storage
  14. {
  15. private $special_id;
  16. private $comment_id;
  17. public function __construct($special_id,$comment_id)
  18. {
  19. $this->special_id = intval($special_id);
  20. $this->comment_id = intval($comment_id);
  21. parent::__construct();
  22. }
  23. public function is_special() {
  24. return ($this->special_id > 0 && $this->comment_id <= 0);
  25. }
  26. public function limit_type()
  27. {
  28. return storage::NORMAL_SUPPORT;
  29. }
  30. public function storage_tag()
  31. {
  32. if($this->is_special()) {
  33. return "special_support";
  34. } else {
  35. return "special_comment_{$this->special_id}";
  36. }
  37. }
  38. public function supported()
  39. {
  40. if($this->is_special()) {
  41. return parent::base_supported($this->special_id);
  42. } else {
  43. return parent::base_supported($this->comment_id);
  44. }
  45. }
  46. public function support()
  47. {
  48. if($this->is_special())
  49. {
  50. $ret = parent::base_support($this->special_id);
  51. if($ret) {
  52. $this->add_special_likes();
  53. }
  54. }
  55. else
  56. {
  57. $ret = parent::base_support($this->comment_id);
  58. if($ret) {
  59. $this->add_comment_likes();
  60. }
  61. }
  62. return $ret;
  63. }
  64. private function add_special_likes()
  65. {
  66. $mb_special = Model('mb_special');
  67. $mb_special->table('mb_special')->where(['special_id' => $this->special_id])->update([ 'likes' => ['exp', 'likes + 1'] ]);
  68. }
  69. private function sub_special_likes()
  70. {
  71. $mb_special = Model('mb_special');
  72. $mb_special->table('mb_special')->where(['special_id' => $this->special_id])->update([ 'likes' => ['exp', 'likes - 1'] ]);
  73. }
  74. private function add_comment_likes()
  75. {
  76. $mod_comment = Model('ugc_comment');
  77. $mod_comment->where(['comment_id' => $this->comment_id])->update([ 'likes' => ['exp', 'likes + 1'] ]);
  78. }
  79. private function sub_comment_likes()
  80. {
  81. $mod_comment = Model('ugc_comment');
  82. $mod_comment->where(['comment_id' => $this->comment_id])->update([ 'likes' => ['exp', 'likes - 1'] ]);
  83. }
  84. public function unsupport()
  85. {
  86. if($this->is_special())
  87. {
  88. $ret = parent::base_unsupport($this->special_id);
  89. if($ret) {
  90. $this->sub_special_likes();
  91. }
  92. }
  93. else
  94. {
  95. $ret = parent::base_unsupport($this->comment_id);
  96. if($ret) {
  97. $this->sub_comment_likes();
  98. }
  99. }
  100. return $ret;
  101. }
  102. }
  103. class special_submitor extends storage
  104. {
  105. private $special_id;
  106. private $vote_single;
  107. public function __construct($special_id,$vote_single)
  108. {
  109. $this->special_id = $special_id;
  110. if($vote_single) {
  111. $this->vote_single = storage::NORMAL_SUPPORT;
  112. } else {
  113. $this->vote_single = storage::DAILY_SUPPORT;
  114. }
  115. parent::__construct();
  116. }
  117. public function limit_type()
  118. {
  119. return $this->vote_single;
  120. }
  121. public function storage_tag()
  122. {
  123. if($this->vote_single == storage::NORMAL_SUPPORT) {
  124. return 'special_submit';
  125. } else {
  126. return 'special_submit_daily';
  127. }
  128. }
  129. public function submited() {
  130. return $this->base_supported($this->special_id);
  131. }
  132. public function submit() {
  133. parent::base_support($this->special_id);
  134. }
  135. }
  136. class special_vote
  137. {
  138. private $mod_special;
  139. private $special_id;
  140. public function __construct($special_id)
  141. {
  142. $this->mod_special = Model('mb_special');
  143. $this->special_id = $special_id;
  144. }
  145. public function vote($options,&$num)
  146. {
  147. try
  148. {
  149. $trans = new trans_wapper($this->mod_special,"special_vote");
  150. $special_info = $this->mod_special->getMbSpecialByID($this->special_id,'*',true);
  151. $special = new special($special_info);
  152. $vote_result = $special->vote_result();
  153. $num = 0;
  154. if($vote_result != false)
  155. {
  156. $count = count($vote_result);
  157. foreach ($options as $val)
  158. {
  159. $index = intval($val['index']);
  160. $answer = $val['answer'];
  161. if($index >= $count) continue;
  162. $result = &$vote_result[$index];
  163. foreach ($answer as $item)
  164. {
  165. $opval = intval($item);
  166. if(array_key_exists($opval,$result)) {
  167. $result[$opval] += 1;
  168. $num++;
  169. }
  170. }
  171. }
  172. }
  173. $this->mod_special->table('mb_special')->where(['special_id' => $this->special_id])->update(['vote_result' => serialize($vote_result)]);
  174. $trans->commit();
  175. return $vote_result;
  176. }
  177. catch (Exception $ex) {
  178. $trans->rollback();
  179. return false;
  180. }
  181. }
  182. }
  183. class special_answer
  184. {
  185. private $correct_answers;
  186. private $submit_answers;
  187. private $mSpecial;
  188. public function __construct(special $special)
  189. {
  190. $this->mSpecial = $special;
  191. $params = $this->mSpecial->question_param();
  192. $this->correct_answers = [];
  193. foreach ($params as $pos => $val) {
  194. $this->correct_answers[$pos] = $val['answer'];
  195. }
  196. }
  197. public function diff()
  198. {
  199. $result = [];
  200. $sub_answers = $this->submit_answers;
  201. foreach ($this->correct_answers as $pos => $val)
  202. {
  203. $item = [];
  204. $item['index'] = $pos;
  205. $item['origin'] = $val;
  206. if(array_key_exists($pos,$sub_answers)) {
  207. $item['submit'] = $sub_answers[$pos];
  208. $item['correct'] = ($sub_answers[$pos] === $val);
  209. } else {
  210. $item['submit'] = [];
  211. $item['correct'] = false;
  212. }
  213. $result[] = $item;
  214. }
  215. return $result;
  216. }
  217. private function format_answer($sub_answers)
  218. {
  219. $this->submit_answers = [];
  220. foreach ($sub_answers as $item)
  221. {
  222. $pos = $item['index'];
  223. $answer = $item['answer'];
  224. $tmp = [];
  225. foreach ($answer as $val) {
  226. $tmp[] = intval($val);
  227. }
  228. sort($tmp);
  229. $tmp = array_unique($tmp);
  230. $this->submit_answers[$pos] = $tmp;
  231. }
  232. return $this->submit_answers;
  233. }
  234. public function answer($sub_answers,&$answer_id)
  235. {
  236. $sub_answers = $this->format_answer($sub_answers);
  237. $correct_num = $this->match_answer($sub_answers,$this->correct_answers);
  238. $spid = $this->mSpecial->special_id();
  239. $member_id = session_helper::memberid();
  240. if($member_id > 0)
  241. {
  242. $answer_id = Model()->table('special_answer')->insert(['special_id' => $spid,'member_id' => $member_id,'answer' => serialize($sub_answers),
  243. 'total_num' => count($this->correct_answers),'correct_num' => $correct_num,'answer_time' => time()]);
  244. } else {
  245. $answer_id = false;
  246. }
  247. return $correct_num;
  248. }
  249. private function match_answer($sub_answers,$correct_answers)
  250. {
  251. $correct_num = 0;
  252. foreach ($sub_answers as $pos => $answer)
  253. {
  254. if(!array_key_exists($pos,$correct_answers)) continue;
  255. if($answer === $correct_answers[$pos]) {
  256. $correct_num += 1;
  257. }
  258. }
  259. return $correct_num;
  260. }
  261. }