answer.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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
  14. {
  15. public function limit_type()
  16. {
  17. }
  18. public function storage_tag()
  19. {
  20. }
  21. public function support_count()
  22. {
  23. }
  24. public function onSuppored()
  25. {
  26. }
  27. public function onUnSupported()
  28. {
  29. }
  30. }
  31. class special_submitor extends storage
  32. {
  33. private $special_id;
  34. public function limit_type()
  35. {
  36. return storage::NORMAL_SUPPORT;
  37. }
  38. public function storage_tag()
  39. {
  40. return 'special_submit';
  41. }
  42. public function submited() {
  43. return $this->supported($this->special_id);
  44. }
  45. public function submit() {
  46. parent::support($this->special_id);
  47. }
  48. public function support_count()
  49. {
  50. }
  51. public function onSuppored()
  52. {
  53. }
  54. public function onUnSupported()
  55. {
  56. }
  57. }
  58. class special_vote
  59. {
  60. private $mod_special;
  61. private $special_id;
  62. public function __construct($special_id)
  63. {
  64. $this->mod_special = Model('mb_special');
  65. $this->special_id = $special_id;
  66. }
  67. public function __destruct()
  68. {
  69. }
  70. public function vote($options)
  71. {
  72. try
  73. {
  74. $trans = new trans_wapper($this->mod_special,"special_vote");
  75. $special_info = $this->mod_special->getMbSpecialByID($this->special_id,'*',true);
  76. $special = new special($special_info);
  77. $result = $special->vote_result();
  78. if($result != false)
  79. {
  80. foreach ($options as $val)
  81. {
  82. $val = intval($val);
  83. if(array_key_exists($val,$result)) {
  84. $result[$val] += 1;
  85. }
  86. }
  87. }
  88. $this->mod_special->table('mb_special')->where(['special_id' => $this->special_id])->update(['vote_result' => serialize($result)]);
  89. $trans->commit();
  90. return $result;
  91. }
  92. catch (Exception $ex) {
  93. $trans->rollback();
  94. return false;
  95. }
  96. }
  97. }
  98. class special_answer
  99. {
  100. private $correct_answers;
  101. private $submit_answers;
  102. private $mSpecial;
  103. public function __construct(special $special)
  104. {
  105. $this->mSpecial = $special;
  106. $params = $this->mSpecial->question_param();
  107. $this->correct_answers = [];
  108. foreach ($params as $pos => $val) {
  109. $this->correct_answers[$pos] = $val['answer'];
  110. }
  111. }
  112. public function diff()
  113. {
  114. $result = [];
  115. $sub_answers = $this->submit_answers;
  116. foreach ($this->correct_answers as $pos => $val)
  117. {
  118. $item = [];
  119. $item['index'] = $pos;
  120. $item['origin'] = $val;
  121. if(array_key_exists($pos,$sub_answers)) {
  122. $item['submit'] = $sub_answers[$pos];
  123. $item['correct'] = ($sub_answers[$pos] === $val);
  124. } else {
  125. $item['submit'] = [];
  126. $item['correct'] = false;
  127. }
  128. $result[] = $item;
  129. }
  130. return $result;
  131. }
  132. private function format_answer($sub_answers)
  133. {
  134. $this->submit_answers = [];
  135. foreach ($sub_answers as $item)
  136. {
  137. $pos = $item['index'];
  138. $answer = $item['answer'];
  139. $tmp = [];
  140. foreach ($answer as $val) {
  141. $tmp[] = intval($val);
  142. }
  143. sort($tmp);
  144. $tmp = array_unique($tmp);
  145. $this->submit_answers[$pos] = $tmp;
  146. }
  147. return $this->submit_answers;
  148. }
  149. public function answer($sub_answers)
  150. {
  151. $sub_answers = $this->format_answer($sub_answers);
  152. $correct_num = $this->match_answer($sub_answers,$this->correct_answers);
  153. $spid = $this->mSpecial->special_id();
  154. $member_id = session_helper::memberid();
  155. if($member_id > 0)
  156. {
  157. Model()->table('special_answer')->insert(['special_id' => $spid,'member_id' => $member_id,'answer' => serialize($sub_answers),
  158. 'total_num' => count($this->correct_answers),'correct_num' => $correct_num,'answer_time' => time()]);
  159. }
  160. return $correct_num;
  161. }
  162. private function match_answer($sub_answers,$correct_answers)
  163. {
  164. $correct_num = 0;
  165. foreach ($sub_answers as $pos => $answer)
  166. {
  167. if(!array_key_exists($pos,$correct_answers)) continue;
  168. if($answer === $correct_answers[$pos]) {
  169. $correct_num += 1;
  170. }
  171. }
  172. return $correct_num;
  173. }
  174. }