PageRenderTime 328ms CodeModel.GetById 45ms RepoModel.GetById 2ms app.codeStats 0ms

/models/reputation.php

https://gitlab.com/Ltaimao/wecenter
PHP | 403 lines | 324 code | 57 blank | 22 comment | 51 complexity | 6131c492d85fa44ee38a1acb04be773d MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------------
  4. | WeCenter [#RELEASE_VERSION#]
  5. | ========================================
  6. | by WeCenter Software
  7. | © 2011 - 2014 WeCenter. All Rights Reserved
  8. | http://www.wecenter.com
  9. | ========================================
  10. | Support: WeCenter@qq.com
  11. |
  12. +---------------------------------------------------------------------------
  13. */
  14. if (!defined('IN_ANWSION'))
  15. {
  16. die;
  17. }
  18. class reputation_class extends AWS_MODEL
  19. {
  20. public function get_reputation_topic($uids)
  21. {
  22. if (!is_array($uids))
  23. {
  24. return false;
  25. }
  26. array_walk_recursive($uids, 'intval_string');
  27. return $this->fetch_all('reputation_topic', 'uid IN(' . implode(',', $uids) . ')', 'topic_count DESC');
  28. }
  29. public function calculate_by_uid($uid)
  30. {
  31. if (!$user_info = $this->model('account')->get_user_info_by_uid($uid))
  32. {
  33. return false;
  34. }
  35. if ($user_articles = $this->query_all('SELECT id FROM ' . get_table('article') . ' WHERE uid = ' . $user_info['uid']))
  36. {
  37. foreach ($user_articles as $articles_key => $articles_val)
  38. {
  39. $articles_ids[] = $articles_val['id'];
  40. }
  41. if ($articles_ids)
  42. {
  43. $articles_vote_agree_users = $this->model('article')->get_article_vote_by_ids('article', $articles_ids, 1);
  44. $articles_vote_against_users = $this->model('article')->get_article_vote_by_ids('article', $articles_ids, -1);
  45. if ($article_topics_query = $this->query_all('SELECT item_id, topic_id FROM ' . get_table('topic_relation') . ' WHERE item_id IN(' . implode(',', $articles_ids) . ") AND `type` = 'article'"))
  46. {
  47. foreach ($article_topics_query AS $article_topics_key => $article_topics_val)
  48. {
  49. $article_topics[$article_topics_val['item_id']][] = $article_topics_val;
  50. }
  51. unset($article_topics_query);
  52. }
  53. }
  54. foreach ($user_articles as $articles_key => $articles_val)
  55. {
  56. // 赞同的用户
  57. if ($articles_vote_agree_users[$articles_val['id']])
  58. {
  59. foreach($articles_vote_agree_users[$articles_val['id']] AS $articles_vote_agree_user)
  60. {
  61. $s_agree_value = $s_agree_value + $articles_vote_agree_user['reputation_factor'];
  62. }
  63. }
  64. // 反对的用户
  65. if ($articles_vote_against_users[$articles_val['id']])
  66. {
  67. foreach($articles_vote_against_users[$articles_val['id']] AS $articles_vote_against_user)
  68. {
  69. $s_against_value = $s_against_value + $articles_vote_against_user['reputation_factor'];
  70. }
  71. }
  72. }
  73. $article_reputation = $s_agree_value - $s_against_value;
  74. $reputation_log_factor = get_setting('reputation_log_factor');
  75. if ($article_reputation < 0)
  76. {
  77. $article_reputation = (0 - $article_reputation) - 0.5;
  78. if ($reputation_log_factor > 1)
  79. {
  80. $article_reputation = (0 - log($article_reputation, $reputation_log_factor));
  81. }
  82. }
  83. else if ($article_reputation > 0)
  84. {
  85. $article_reputation = $article_reputation + 0.5;
  86. if ($reputation_log_factor > 1)
  87. {
  88. $article_reputation = log($article_reputation, $reputation_log_factor);
  89. }
  90. }
  91. // 计算在话题中的威望
  92. if ($article_reputation)
  93. {
  94. if ($article_topics[$articles_val['id']])
  95. {
  96. foreach ($article_topics[$articles_val['id']] as $key => $topic_info)
  97. {
  98. $user_topics[$topic_info['topic_id']] = array(
  99. 'topic_id' => $topic_info['topic_id'],
  100. 'count' => (intval($user_topics[$topic_info['topic_id']]['count']) + 1),
  101. 'agree_count' => (intval($user_topics[$topic_info['topic_id']]['agree_count']) + sizeof($articles_vote_agree_users[$articles_val['id']])),
  102. 'thanks_count' => 0,
  103. 'reputation' => ($user_topics[$topic_info['topic_id']]['reputation'] + $article_reputation)
  104. );
  105. }
  106. }
  107. }
  108. $user_reputation = $user_reputation + $article_reputation;
  109. }
  110. if ($users_anwsers = $this->query_all('SELECT answer_id, question_id, agree_count, thanks_count FROM ' . get_table('answer') . ' WHERE uid = ' . $user_info['uid']))
  111. {
  112. foreach ($users_anwsers as $anwsers_key => $answers_val)
  113. {
  114. $answer_ids[] = $answers_val['answer_id'];
  115. $question_ids[] = $answers_val['question_id'];
  116. }
  117. if ($question_ids)
  118. {
  119. if ($questions_info_query = $this->query_all('SELECT question_id, best_answer, published_uid, category_id FROM ' . get_table('question') . ' WHERE question_id IN(' . implode(',', $question_ids) . ')'))
  120. {
  121. foreach ($questions_info_query AS $questions_info_key => $questions_info_val)
  122. {
  123. $questions_info[$questions_info_val['question_id']] = $questions_info_val;
  124. }
  125. unset($questions_info_query);
  126. }
  127. if ($question_topics_query = $this->query_all('SELECT item_id, topic_id FROM ' . get_table('topic_relation') . ' WHERE item_id IN(' . implode(',', $question_ids) . ") AND `type` = 'question'"))
  128. {
  129. foreach ($question_topics_query AS $question_topics_key => $question_topics_val)
  130. {
  131. $question_topics[$question_topics_val['item_id']][] = $question_topics_val;
  132. }
  133. unset($question_topics_query);
  134. }
  135. }
  136. if ($answer_ids)
  137. {
  138. $vote_agree_users = $this->model('answer')->get_vote_agree_by_answer_ids($answer_ids);
  139. $vote_against_users = $this->model('answer')->get_vote_against_by_answer_ids($answer_ids);
  140. }
  141. foreach ($users_anwsers as $answers_key => $answers_val)
  142. {
  143. if (!$questions_info[$answers_val['question_id']])
  144. {
  145. continue;
  146. }
  147. $answer_reputation = 0; // 回复威望系数
  148. $s_publisher_agree = 0; // 得到发起者赞同
  149. $s_publisher_against = 0; // 得道发起者反对
  150. $s_agree_value = 0; // 赞同威望系数
  151. $s_against_value = 0; // 反对威望系数
  152. // 是否最佳回复
  153. if ($questions_info[$answers_val['question_id']]['best_answer'] == $answers_val['answer_id'])
  154. {
  155. $s_best_answer = 1;
  156. }
  157. else
  158. {
  159. $s_best_answer = 0;
  160. }
  161. // 赞同的用户
  162. if ($vote_agree_users[$answers_val['answer_id']])
  163. {
  164. foreach ($vote_agree_users[$answers_val['answer_id']] AS $key => $val)
  165. {
  166. // 排除发起者
  167. if ($questions_info[$answers_val['question_id']]['published_uid'] != $val['answer_uid'])
  168. {
  169. $s_agree_value = $s_agree_value + $val['reputation_factor'];
  170. if ($questions_info[$answers_val['question_id']]['published_uid'] == $val['vote_uid'] AND !$s_publisher_agree)
  171. {
  172. $s_publisher_agree = 1;
  173. }
  174. }
  175. }
  176. }
  177. // 反对的用户
  178. if ($vote_against_users[$answers_val['answer_id']])
  179. {
  180. foreach ($vote_against_users[$answers_val['answer_id']] AS $key => $val)
  181. {
  182. // 排除发起者
  183. if ($questions_info[$answers_val['question_id']]['published_uid'] != $val['answer_uid'])
  184. {
  185. $s_against_value = $s_against_value + $val['reputation_factor'];
  186. if ($questions_info[$answers_val['question_id']]['published_uid'] == $val['vote_uid'] AND !$s_publisher_against)
  187. {
  188. $s_publisher_against = 1;
  189. }
  190. }
  191. }
  192. }
  193. if ($s_publisher_agree)
  194. {
  195. $s_agree_value = $s_agree_value - 1;
  196. }
  197. if ($s_publisher_against)
  198. {
  199. $s_against_value = $s_against_value - 1;
  200. }
  201. $best_answer_reput = get_setting('best_answer_reput'); // 最佳回复威望系数
  202. $publisher_reputation_factor = get_setting('publisher_reputation_factor'); // 发起者赞同/反对威望系数
  203. $reputation_log_factor = get_setting('reputation_log_factor');
  204. //(用户组威望系数 x 赞同数量 - 用户组威望系数 x 反对数量)+ 发起者赞同反对系数 + 最佳答案系数
  205. $answer_reputation = $s_agree_value - $s_against_value + ($s_publisher_agree * $publisher_reputation_factor) - ($s_publisher_against * $publisher_reputation_factor) + ($s_best_answer * $best_answer_reput);
  206. if ($answer_reputation < 0)
  207. {
  208. $answer_reputation = (0 - $answer_reputation) - 0.5;
  209. if ($reputation_log_factor > 1)
  210. {
  211. $answer_reputation = (0 - log($answer_reputation, $reputation_log_factor));
  212. }
  213. }
  214. else if ($answer_reputation > 0)
  215. {
  216. $answer_reputation = $answer_reputation + 0.5;
  217. if ($reputation_log_factor > 1)
  218. {
  219. $answer_reputation = log($answer_reputation, $reputation_log_factor);
  220. }
  221. }
  222. // 计算在话题中的威望
  223. if ($answer_reputation)
  224. {
  225. if ($question_topics[$answers_val['question_id']])
  226. {
  227. foreach ($question_topics[$answers_val['question_id']] as $key => $topic_info)
  228. {
  229. $user_topics[$topic_info['topic_id']] = array(
  230. 'topic_id' => $topic_info['topic_id'],
  231. 'count' => (intval($user_topics[$topic_info['topic_id']]['count']) + 1),
  232. 'agree_count' => (intval($user_topics[$topic_info['topic_id']]['agree_count']) + $answers_val['agree_count']),
  233. 'thanks_count' => (intval($user_topics[$topic_info['topic_id']]['thanks_count']) + $answers_val['thanks_count']),
  234. 'reputation' => ($user_topics[$topic_info['topic_id']]['reputation'] + $answer_reputation)
  235. );
  236. }
  237. }
  238. }
  239. if ($questions_info[$answers_val['question_id']]['category_id'])
  240. {
  241. $user_reputation_category[$questions_info[$answers_val['question_id']]['category_id']]['reputation'] += $answer_reputation;
  242. $user_reputation_category[$questions_info[$answers_val['question_id']]['category_id']]['agree_count'] += $answers_val['agree_count'];
  243. $user_reputation_category[$questions_info[$answers_val['question_id']]['category_id']]['questions'][$answers_val['question_id']] = $answers_val['question_id'];
  244. }
  245. $user_reputation = $user_reputation + $answer_reputation;
  246. }
  247. }
  248. if (is_array($user_topics))
  249. {
  250. if ($user_topics = aasort($user_topics, 'count', 'DESC'))
  251. {
  252. $user_topics = array_slice($user_topics, 0, 20);
  253. }
  254. foreach ($user_topics as $t_key => $t_val)
  255. {
  256. if ($reputation_topic_id = $this->fetch_one('reputation_topic', 'auto_id', 'uid = ' . $uid . ' AND topic_id = ' . $t_val['topic_id']))
  257. {
  258. $this->update('reputation_topic', array(
  259. 'uid' => $uid,
  260. 'topic_id' => $t_val['topic_id'],
  261. 'topic_count' => $t_val['count'],
  262. 'update_time' => time(),
  263. 'agree_count' => $t_val['agree_count'],
  264. 'thanks_count' => $t_val['thanks_count'],
  265. 'reputation' => round($t_val['reputation'])
  266. ), 'auto_id = ' . $reputation_topic_id);
  267. }
  268. else
  269. {
  270. $this->insert('reputation_topic', array(
  271. 'uid' => $uid,
  272. 'topic_id' => $t_val['topic_id'],
  273. 'topic_count' => $t_val['count'],
  274. 'update_time' => time(),
  275. 'agree_count' => $t_val['agree_count'],
  276. 'thanks_count' => $t_val['thanks_count'],
  277. 'reputation' => round($t_val['reputation'])
  278. ));
  279. }
  280. }
  281. }
  282. if (is_array($user_reputation_category))
  283. {
  284. foreach ($user_reputation_category as $t_key => $t_val)
  285. {
  286. if ($user_reputation_category_id = $this->fetch_one('reputation_category', 'auto_id', 'uid = ' . intval($uid) . ' AND category_id = ' . $t_key))
  287. {
  288. $this->update('reputation_category', array(
  289. 'uid' => intval($uid),
  290. 'category_id' => $t_key,
  291. 'update_time' => time(),
  292. 'reputation' => round($t_val['reputation']),
  293. 'agree_count' => $t_val['agree_count'],
  294. 'question_count' => count($t_val['questions'])
  295. ), 'auto_id = ' . $user_reputation_category_id);
  296. }
  297. else
  298. {
  299. $this->insert('reputation_category', array(
  300. 'uid' => intval($uid),
  301. 'category_id' => $t_key,
  302. 'update_time' => time(),
  303. 'reputation' => round($t_val['reputation']),
  304. 'agree_count' => $t_val['agree_count'],
  305. 'question_count' => count($t_val['questions'])
  306. ));
  307. }
  308. }
  309. }
  310. $this->model('account')->update_users_fields(array(
  311. 'reputation' => round($user_reputation),
  312. 'reputation_update_time' => time()
  313. ), $uid);
  314. $this->model('account')->update_user_reputation_group($uid);
  315. }
  316. public function calculate($start = 0, $limit = 100)
  317. {
  318. if ($users_list = $this->query_all('SELECT uid FROM ' . get_table('users') . ' ORDER BY uid ASC', intval($start) . ',' . intval($limit)))
  319. {
  320. foreach ($users_list as $key => $val)
  321. {
  322. $this->calculate_by_uid($val['uid']);
  323. }
  324. return true;
  325. }
  326. return false;
  327. }
  328. public function calculate_agree_count($uid, $topic_ids)
  329. {
  330. if (!is_array($topic_ids))
  331. {
  332. return false;
  333. }
  334. array_walk_recursive($topic_ids, 'intval_string');
  335. return $this->sum('reputation_topic', 'agree_count', 'uid = ' . intval($uid) . ' AND topic_id IN(' . implode(',', $topic_ids) . ')');
  336. }
  337. public function calculate_thanks_count($uid, $topic_ids)
  338. {
  339. if (!is_array($topic_ids))
  340. {
  341. return false;
  342. }
  343. array_walk_recursive($topic_ids, 'intval_string');
  344. return $this->sum('reputation_topic', 'thanks_count', 'uid = ' . intval($uid) . ' AND topic_id IN(' . implode(',', $topic_ids) . ')');
  345. }
  346. }