| Conditions | 24 |
| Paths | 1623 |
| Total Lines | 131 |
| Code Lines | 87 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 12 | function nws_render(&$user, &$template, $query_where = '', $query_limit = 20) { |
||
| 13 | if($user['authlevel'] < AUTH_LEVEL_ADMINISTRATOR) { |
||
| 14 | $query_where .= (!empty($query_where) ? ' AND ' : '') |
||
| 15 | . '`tsTimeStamp` <= (NOW())'; |
||
| 16 | } |
||
| 17 | |||
| 18 | $mmModuleIsActive = !empty(SN::$gc->modules->getModulesInGroup('payment')); |
||
| 19 | |||
| 20 | $sqlText = "SELECT a.*, UNIX_TIMESTAMP(`tsTimeStamp`) AS unix_time, u.authlevel, s.* |
||
| 21 | FROM |
||
| 22 | `{{announce}}` AS a |
||
| 23 | LEFT JOIN `{{survey}}` AS s ON s.survey_announce_id = a.idAnnounce |
||
| 24 | LEFT JOIN `{{users}}` AS u ON u.id = a.user_id |
||
| 25 | WHERE 1 {$query_where} |
||
| 26 | ORDER BY `tsTimeStamp` DESC, idAnnounce"; |
||
| 27 | |||
| 28 | $announce_list = new DbSqlPaging($sqlText, $query_limit, sys_get_param_int(PagingRenderer::KEYWORD)); |
||
| 29 | $pager = new PagingRenderer($announce_list, 'announce.php'); |
||
| 30 | |||
| 31 | $users = array(); |
||
| 32 | foreach ($announce_list as $announce) { |
||
| 33 | if ($announce['user_id'] && !isset($users[$announce['user_id']])) { |
||
| 34 | $users[$announce['user_id']] = db_user_by_id($announce['user_id']); |
||
|
|
|||
| 35 | } |
||
| 36 | |||
| 37 | $survey_vote = array('survey_vote_id' => 1); |
||
| 38 | $survey_complete = strtotime($announce['survey_until']) < SN_TIME_NOW; |
||
| 39 | |||
| 40 | if ($announce['survey_id'] && !empty($user['id'])) { |
||
| 41 | $survey_vote = !$survey_complete ? $survey_vote = doquery("SELECT `survey_vote_id` FROM `{{survey_votes}}` WHERE survey_parent_id = {$announce['survey_id']} AND survey_vote_user_id = {$user['id']} LIMIT 1;", true) : array(); |
||
| 42 | } |
||
| 43 | |||
| 44 | $announce_exploded = explode("<br /><br />", SN::$gc->bbCodeParser->expandBbCode($announce['strAnnounce'], intval($announce['authlevel']))); |
||
| 45 | |||
| 46 | $template->assign_block_vars('announces', array( |
||
| 47 | 'ID' => $announce['idAnnounce'], |
||
| 48 | 'TIME' => date(FMT_DATE_TIME, $announce['unix_time'] + SN_CLIENT_TIME_DIFF), |
||
| 49 | 'ANNOUNCE' => SN::$gc->bbCodeParser->expandBbCode($announce['strAnnounce'], intval($announce['authlevel'])), |
||
| 50 | 'DETAIL_URL' => $announce['detail_url'], |
||
| 51 | 'USER_NAME' => !empty($users[$announce['user_id']]) |
||
| 52 | ? player_nick_render_to_html($users[$announce['user_id']], array('color' => true)) |
||
| 53 | : js_safe_string($announce['user_name']), |
||
| 54 | 'NEW' => $announce['unix_time'] + SN::$config->game_news_actual >= SN_TIME_NOW, |
||
| 55 | 'FUTURE' => $announce['unix_time'] > SN_TIME_NOW, |
||
| 56 | 'SURVEY_ID' => $announce['survey_id'], |
||
| 57 | 'SURVEY_TEXT' => $announce['survey_question'], |
||
| 58 | 'SURVEY_CAN_VOTE' => empty($survey_vote) && !$survey_complete, |
||
| 59 | 'SURVEY_COMPLETE' => $survey_complete, |
||
| 60 | 'SURVEY_UNTIL' => $announce['survey_until'], |
||
| 61 | )); |
||
| 62 | |||
| 63 | foreach ($announce_exploded as $announce_paragraph) { |
||
| 64 | $template->assign_block_vars('announces.paragraph', array( |
||
| 65 | 'TEXT' => $announce_paragraph, |
||
| 66 | )); |
||
| 67 | } |
||
| 68 | |||
| 69 | if ($announce['survey_id']) { |
||
| 70 | $survey_query = doquery( |
||
| 71 | "SELECT survey_answer_id AS `ID`, survey_answer_text AS `TEXT`, count(DISTINCT survey_vote_id) AS `VOTES` |
||
| 72 | FROM `{{survey_answers}}` AS sa |
||
| 73 | LEFT JOIN `{{survey_votes}}` AS sv ON sv.survey_parent_answer_id = sa.survey_answer_id |
||
| 74 | WHERE sa.survey_parent_id = {$announce['survey_id']} |
||
| 75 | GROUP BY survey_answer_id |
||
| 76 | ORDER BY survey_answer_id;" |
||
| 77 | ); |
||
| 78 | $survey_vote_result = array(); |
||
| 79 | $total_votes = 0; |
||
| 80 | $total_mm = 0; |
||
| 81 | $total_money = 0; |
||
| 82 | while ($row = db_fetch($survey_query)) { |
||
| 83 | $survey_vote_result[$row['ID']] = $row; |
||
| 84 | $total_votes += $row['VOTES']; |
||
| 85 | } |
||
| 86 | |||
| 87 | if ($mmModuleIsActive && $user['authlevel'] >= AUTH_LEVEL_ADMINISTRATOR) { |
||
| 88 | $mQuery = doquery( |
||
| 89 | "SELECT |
||
| 90 | sa.survey_answer_id, |
||
| 91 | sum(acc.account_metamatter_total) AS `mm`, |
||
| 92 | ( |
||
| 93 | SELECT sum(payment_amount) |
||
| 94 | FROM `{{payment}}` AS pay |
||
| 95 | WHERE payment_currency = 'USD' AND pay.payment_user_id = sv.survey_vote_user_id |
||
| 96 | GROUP BY payment_user_id, payment_currency |
||
| 97 | ) AS `money` |
||
| 98 | FROM `{{survey_votes}}` AS sv |
||
| 99 | LEFT JOIN `{{survey_answers}}` AS sa ON sa.survey_answer_id = sv.survey_parent_answer_id |
||
| 100 | LEFT JOIN `{{account_translate}}` AS act ON act.user_id = sv.survey_vote_user_id |
||
| 101 | LEFT JOIN `{{account}}` AS acc ON acc.account_id = act.provider_account_id |
||
| 102 | WHERE sv.survey_parent_id = {$announce['survey_id']} |
||
| 103 | GROUP BY sv.survey_parent_id, sv.survey_parent_answer_id;" |
||
| 104 | ); |
||
| 105 | while ($row = db_fetch($mQuery)) { |
||
| 106 | $survey_vote_result[$row['survey_answer_id']] += [ |
||
| 107 | 'MM' => $row['mm'], |
||
| 108 | 'MONEY' => $row['money'], |
||
| 109 | ]; |
||
| 110 | $total_mm += $row['mm']; |
||
| 111 | $total_money += $row['money']; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | // Show result |
||
| 116 | foreach ($survey_vote_result as &$vote_result) { |
||
| 117 | $vote_percent = $total_votes ? $vote_result['VOTES'] / $total_votes * 100 : 0; |
||
| 118 | $vote_result['PERCENT'] = $vote_percent; |
||
| 119 | $vote_result['PERCENT_TEXT'] = round($vote_percent, 1); |
||
| 120 | $vote_result['VOTES'] = HelperString::numberFloorAndFormat($vote_result['VOTES']); |
||
| 121 | |||
| 122 | if ($mmModuleIsActive && $user['authlevel'] >= AUTH_LEVEL_ADMINISTRATOR) { |
||
| 123 | $vote_result['PERCENT_MM'] = $total_mm ? $vote_result['MM'] / $total_mm * 100 : 0; |
||
| 124 | $vote_result['PERCENT_MONEY'] = $total_money ? $vote_result['MONEY'] / $total_money * 100 : 0; |
||
| 125 | } |
||
| 126 | |||
| 127 | $template->assign_block_vars('announces.survey_answers', $vote_result); |
||
| 128 | } |
||
| 129 | // Dirty hack |
||
| 130 | $template->assign_block_vars('announces.total_votes', array( |
||
| 131 | 'TOTAL_VOTES' => $total_votes, |
||
| 132 | 'TOTAL_MM' => $total_mm, |
||
| 133 | 'TOTAL_MONEY' => $total_money, |
||
| 134 | )); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | $template->assign_vars([ |
||
| 139 | 'PAGER_MESSAGES' => $pager ? $pager->render() : '', |
||
| 140 | 'NEWS_COUNT' => HelperString::numberFloorAndFormat($announce_list->getTotalRecords()), |
||
| 141 | |||
| 142 | 'MM_MODULE_ACTIVE' => $mmModuleIsActive, |
||
| 143 | ]); |
||
| 175 |