| Conditions | 24 |
| Paths | 30 |
| Total Lines | 135 |
| Code Lines | 102 |
| Lines | 38 |
| Ratio | 28.15 % |
| Changes | 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 |
||
| 59 | * @uses Criteria |
||
| 60 | * @global mixed $GLOBALS ['xoopsUser'] |
||
| 61 | * @uses CriteriaCompo |
||
| 62 | */ |
||
| 63 | function xoopspollBlockMultiShow($options) |
||
| 64 | { |
||
| 65 | $block = []; |
||
| 66 | $pollOptionArray = []; |
||
| 67 | /** @var \XoopsModuleHandler $moduleHandler */ |
||
| 68 | $moduleHandler = xoops_getHandler('module'); |
||
| 69 | $thisModule = $moduleHandler->getByDirname('xoopspoll'); |
||
| 70 | $configHandler = xoops_getHandler('config'); |
||
| 71 | $this_module_config = $configHandler->getConfigsByCat(0, $thisModule->getVar('mid')); |
||
|
|
|||
| 72 | |||
| 73 | $pollHandler = Helper::getInstance()->getHandler('Poll'); |
||
| 74 | $criteria = new \CriteriaCompo(); |
||
| 75 | $criteria->add(new \Criteria('display', Constants::DISPLAY_POLL_IN_BLOCK, '=')); |
||
| 76 | $criteria->add(new \Criteria('start_time', time(), '<=')); |
||
| 77 | if (0 === $options[1]) { |
||
| 78 | $criteria->add(new \Criteria('end_time', time(), '>=')); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * now check to see if we want to hide polls that were created using newbb |
||
| 83 | */ |
||
| 84 | if ($this_module_config['hide_forum_polls'] && ($thisModule instanceof \XoopsModule) && $thisModule->isactive()) { |
||
| 85 | $newbbModule = $moduleHandler->getByDirname('newbb'); |
||
| 86 | if ($newbbModule instanceof \XoopsModule && $newbbModule->isactive()) { |
||
| 87 | /** @var Newbb\TopicHandler $topicHandler */ |
||
| 88 | $topicHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Topic'); |
||
| 89 | $tFields = ['topic_id', 'poll_id']; |
||
| 90 | $tArray = $topicHandler->getAll(new Criteria('topic_haspoll', 0, '>'), $tFields, false); |
||
| 91 | if (!empty($tArray)) { |
||
| 92 | $tcriteria = []; |
||
| 93 | foreach ($tArray as $t) { |
||
| 94 | $tcriteria[] = $t['poll_id']; |
||
| 95 | } |
||
| 96 | if (!empty($tcriteria)) { |
||
| 97 | $tstring = '(' . implode(',', $tcriteria) . ')'; |
||
| 98 | $criteria->add(new \Criteria('poll_id', $tstring, 'NOT IN')); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | unset($topicHandler, $tFields, $tArray); |
||
| 102 | } |
||
| 103 | unset($newbbModule); |
||
| 104 | } |
||
| 105 | |||
| 106 | $criteria->setSort('weight ASC, end_time'); // trick criteria to allow 2 sort criteria |
||
| 107 | $criteria->setOrder('DESC'); |
||
| 108 | $pollObjs = $pollHandler->getAll($criteria); |
||
| 109 | $count = count($pollObjs); |
||
| 110 | if ($count) { |
||
| 111 | $block['langVote'] = _MD_XOOPSPOLL_VOTE; |
||
| 112 | $block['langResults'] = _MD_XOOPSPOLL_RESULTS; |
||
| 113 | $block['langExpires'] = _MB_XOOPSPOLL_WILLEXPIRE; |
||
| 114 | $block['langExpired'] = _MB_XOOPSPOLL_HASEXPIRED; |
||
| 115 | $block['langComments'] = _MB_XOOPSPOLL_COMMENTS; |
||
| 116 | $block['langComment'] = _MB_XOOPSPOLL_COMMENT; |
||
| 117 | $block['url'] = 'http' . (!empty($_SERVER['HTTPS']) ? 's' : '') . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; |
||
| 118 | $block['dispVotes'] = $this_module_config['disp_vote_nums']; |
||
| 119 | $block['thisModuleDir'] = 'xoopspoll'; |
||
| 120 | $block['asList'] = $options[0]; |
||
| 121 | |||
| 122 | $optionHandler = Helper::getInstance()->getHandler('Option'); |
||
| 123 | $logHandler = Helper::getInstance()->getHandler('Log'); |
||
| 124 | |||
| 125 | foreach ($pollObjs as $pollObj) { |
||
| 126 | $criteria = new \CriteriaCompo(); |
||
| 127 | $pollVars = $pollObj->getValues(); |
||
| 128 | $criteria->add(new \Criteria('poll_id', $pollVars['poll_id'], '=')); |
||
| 129 | $criteria->setSort('option_id'); |
||
| 130 | $pollOptionObjs = $optionHandler->getAll($criteria); |
||
| 131 | if (Constants::MULTIPLE_SELECT_POLL === $pollVars['multiple']) { |
||
| 132 | $pollOptionType = 'checkbox'; |
||
| 133 | $pollOptionName = 'option_id[]'; |
||
| 134 | } else { |
||
| 135 | $pollOptionType = 'radio'; |
||
| 136 | $pollOptionName = 'option_id'; |
||
| 137 | } |
||
| 138 | |||
| 139 | $uid = 0; |
||
| 140 | if (isset($GLOBALS['xoopsUser']) && ($GLOBALS['xoopsUser'] instanceof \XoopsUser)) { |
||
| 141 | $uid = $GLOBALS['xoopsUser']->getVar('uid'); |
||
| 142 | } |
||
| 143 | |||
| 144 | $totalVotes = $pollVars['votes']; |
||
| 145 | $hasVoted = $logHandler->hasVoted($pollVars['poll_id'], xoops_getenv('REMOTE_ADDR'), $uid) ? true : false; |
||
| 146 | $canVote = (!$hasVoted) && $pollObj->isAllowedToVote(); |
||
| 147 | foreach ($pollOptionObjs as $pollOptionObj) { |
||
| 148 | $optionObjVars = $pollOptionObj->getValues(); |
||
| 149 | $percent = ($totalVotes > 0) ? (100 * $optionObjVars['option_count'] / $totalVotes) : 0; |
||
| 150 | // $percent = ($totalVotes > 0) ? (int)(100 * $optionObjVars['option_count'] / $totalVotes) . '%' : '0%'; |
||
| 151 | $pollOptionArray[] = [ |
||
| 152 | 'id' => $optionObjVars['option_id'], |
||
| 153 | 'text' => $optionObjVars['option_text'], |
||
| 154 | 'count' => $optionObjVars['option_count'], |
||
| 155 | 'percent' => sprintf(' %01.1f%%', $percent), |
||
| 156 | 'color' => $optionObjVars['option_color'], |
||
| 157 | ]; |
||
| 158 | } |
||
| 159 | unset($pollOptionObjs, $optionObjVars); |
||
| 160 | $xuEndTimestamp = xoops_getUserTimestamp($pollObj->getVar('end_time')); |
||
| 161 | $xuEndFormattedTime = ucfirst(date(_MEDIUMDATESTRING, $xuEndTimestamp)); |
||
| 162 | |||
| 163 | $isVisible = true === $pollObj->isResultVisible(); |
||
| 164 | $multiple = $pollVars['multiple'] ? true : false; |
||
| 165 | $multiLimit = (int)$pollVars['multilimit']; |
||
| 166 | $lang_multi = ''; |
||
| 167 | if ($multiple && ($multiLimit > 0)) { |
||
| 168 | $lang_multi = sprintf(_MB_XOOPSPOLL_MULTITEXT, $multiLimit); |
||
| 169 | } |
||
| 170 | |||
| 171 | $poll = [ |
||
| 172 | 'id' => $pollVars['poll_id'], |
||
| 173 | 'visible' => $isVisible, |
||
| 174 | 'question' => $pollVars['question'], |
||
| 175 | 'multiple' => $multiple, |
||
| 176 | 'lang_multi' => $lang_multi, |
||
| 177 | 'optionType' => $pollOptionType, |
||
| 178 | 'optionName' => $pollOptionName, |
||
| 179 | 'options' => $pollOptionArray, |
||
| 180 | 'hasExpired' => $pollObj->hasExpired(), |
||
| 181 | 'canVote' => $canVote, |
||
| 182 | 'votes' => $pollVars['votes'], |
||
| 183 | 'hasVoted' => $hasVoted, |
||
| 184 | 'totalVotes' => sprintf(_MD_XOOPSPOLL_TOTALVOTES, $totalVotes), |
||
| 185 | 'comments' => $pollObj->getComments($pollVars['poll_id']), |
||
| 186 | 'endTime' => $xuEndFormattedTime, |
||
| 187 | 'commentMode' => Utility::commentMode(), |
||
| 188 | ]; |
||
| 189 | $block['polls'][] = $poll; |
||
| 190 | unset($pollOptionArray, $poll, $pollVars); |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | return $block; |
||
| 271 |