| Conditions | 26 |
| Paths | 1553 |
| Total Lines | 151 |
| Code Lines | 102 |
| Lines | 40 |
| Ratio | 26.49 % |
| 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 |
||
| 43 | * @return array block keys and values to be used by block template |
||
| 44 | * @global mixed $GLOBALS ['xoopsUser'] |
||
| 45 | * @uses CriteriaCompo |
||
| 46 | * @uses Criteria |
||
| 47 | * @uses xoops_getUserTimestamp() function to convert time to user time |
||
| 48 | * @uses formatTimestamp() takes timestamp and converts to human readable format |
||
| 49 | */ |
||
| 50 | function xoopspollBlockSinglepollShow($options) |
||
| 51 | { |
||
| 52 | $block = []; |
||
| 53 | |||
| 54 | $configHandler = xoops_getHandler('config'); |
||
| 55 | $pollHandler = Helper::getInstance()->getHandler('Poll'); |
||
| 56 | /** @var \XoopsModuleHandler $moduleHandler */ |
||
| 57 | $moduleHandler = xoops_getHandler('module'); |
||
| 58 | $thisModule = $moduleHandler->getByDirname('xoopspoll'); |
||
| 59 | $this_module_config = $configHandler->getConfigsByCat(0, $thisModule->getVar('mid')); |
||
|
|
|||
| 60 | |||
| 61 | /* if admin hasn't initialized block then we'll pick a poll for them |
||
| 62 | * provided that one exists in the database |
||
| 63 | */ |
||
| 64 | if (0 === $options[1]) { |
||
| 65 | $criteria = null; |
||
| 66 | /** |
||
| 67 | * check to see if we want to include polls created with forum (newbb) |
||
| 68 | */ |
||
| 69 | if ($this_module_config['hide_forum_polls'] |
||
| 70 | && ($thisModule instanceof \XoopsModule) |
||
| 71 | && $thisModule->isactive()) { |
||
| 72 | $newbbModule = $moduleHandler->getByDirname('newbb'); |
||
| 73 | if ($newbbModule instanceof \XoopsModule && $newbbModule->isactive()) { |
||
| 74 | /** @var Newbb\TopicHandler $topicHandler */ |
||
| 75 | $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic'); |
||
| 76 | $tFields = ['topic_id', 'poll_id']; |
||
| 77 | $tArray = $topicHandler->getAll(new \Criteria('topic_haspoll', 0, '>'), $tFields, false); |
||
| 78 | if (!empty($tArray)) { |
||
| 79 | $tcriteria = []; |
||
| 80 | foreach ($tArray as $t) { |
||
| 81 | $tcriteria[] = $t['poll_id']; |
||
| 82 | } |
||
| 83 | if (!empty($tcriteria)) { |
||
| 84 | $tstring = '(' . implode(',', $tcriteria) . ')'; |
||
| 85 | $criteria = new \Criteria('poll_id', $tstring, 'NOT IN'); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | unset($topicHandler, $tFields, $tArray); |
||
| 89 | } |
||
| 90 | unset($newbbModule); |
||
| 91 | } |
||
| 92 | |||
| 93 | if ($pollHandler->getCount($criteria) > 0) { |
||
| 94 | $pollIdArray = $pollHandler->getIds(); |
||
| 95 | $thisId = array_shift($pollIdArray); |
||
| 96 | $pollObj = $pollHandler->get($thisId); |
||
| 97 | } else { |
||
| 98 | return $block; |
||
| 99 | } |
||
| 100 | } else { |
||
| 101 | $pollObj = $pollHandler->get((int)$options[1]); |
||
| 102 | } |
||
| 103 | |||
| 104 | if ($pollObj instanceof Poll) { |
||
| 105 | if ((1 === $options[0]) || !$pollObj->hasExpired()) { |
||
| 106 | $block['langVote'] = _MD_XOOPSPOLL_VOTE; |
||
| 107 | $block['langResults'] = _MD_XOOPSPOLL_RESULTS; |
||
| 108 | $block['langExpires'] = _MB_XOOPSPOLL_WILLEXPIRE; |
||
| 109 | $block['langExpired'] = _MB_XOOPSPOLL_HASEXPIRED; |
||
| 110 | $block['langComments'] = _MB_XOOPSPOLL_COMMENTS; |
||
| 111 | $block['langComment'] = _MB_XOOPSPOLL_COMMENT; |
||
| 112 | $block['showResultsLink'] = $options[2]; |
||
| 113 | $block['asList'] = $options[3]; |
||
| 114 | $block['thisModuleDir'] = 'xoopspoll'; |
||
| 115 | $block['url'] = 'http' . (!empty($_SERVER['HTTPS']) ? 's' : '') . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; |
||
| 116 | $block['dispVotes'] = $this_module_config['disp_vote_nums']; |
||
| 117 | |||
| 118 | $optionHandler = Helper::getInstance()->getHandler('Option'); |
||
| 119 | |||
| 120 | $pollVars = $pollObj->getValues(); |
||
| 121 | $criteria = new \CriteriaCompo(); |
||
| 122 | $criteria->add(new \Criteria('poll_id', $pollVars['poll_id'], '=')); |
||
| 123 | $criteria->setSort('option_id'); |
||
| 124 | $optionsObjArray = $optionHandler->getAll($criteria); |
||
| 125 | // $optionsObjArray = $optionHandler->getAll($criteria, null, false); |
||
| 126 | |||
| 127 | if (Constants::MULTIPLE_SELECT_POLL === $pollVars['multiple']) { |
||
| 128 | $pollOptionType = 'checkbox'; |
||
| 129 | $pollOptionName = 'option_id[]'; |
||
| 130 | } else { |
||
| 131 | $pollOptionType = 'radio'; |
||
| 132 | $pollOptionName = 'option_id'; |
||
| 133 | } |
||
| 134 | |||
| 135 | $uid = 0; |
||
| 136 | if (isset($GLOBALS['xoopsUser']) && ($GLOBALS['xoopsUser'] instanceof \XoopsUser)) { |
||
| 137 | $uid = $GLOBALS['xoopsUser']->getVar('uid'); |
||
| 138 | } |
||
| 139 | |||
| 140 | $totalVotes = $pollVars['votes']; |
||
| 141 | $logHandler = Helper::getInstance()->getHandler('Log'); |
||
| 142 | $hasVoted = $logHandler->hasVoted($pollVars['poll_id'], xoops_getenv('REMOTE_ADDR'), $uid) ? true : false; |
||
| 143 | $canVote = (!$hasVoted) && $pollObj->isAllowedToVote(); |
||
| 144 | $pollOptionsArray = []; |
||
| 145 | foreach ($optionsObjArray as $optionObj) { |
||
| 146 | $percent = ($totalVotes > 0) ? (100 * $optionObj->getVar('option_count') / $totalVotes) : 0; |
||
| 147 | // $percent = ($totalVotes > 0) ? ceil(100 * $optionObj->getVar('option_count') / $totalVotes) . '%' : '0%'; |
||
| 148 | /*@TODO:: Change block templates to use Smarty html_options to support this... then comment |
||
| 149 | out old $pollOptionsArray assignment |
||
| 150 | $pollOptionsArray[] = array('options' => array($optionObj['option_id'] => $optionObj['option_text']), |
||
| 151 | 'count' => $optionObj['option_count'], |
||
| 152 | 'percent' => $percent, |
||
| 153 | 'color' => $optionObj['option_color'] |
||
| 154 | ); */ |
||
| 155 | $pollOptionsArray[] = [ |
||
| 156 | 'id' => $optionObj->getVar('option_id'), |
||
| 157 | 'text' => $optionObj->getVar('option_text'), |
||
| 158 | 'count' => $optionObj->getVar('option_count'), |
||
| 159 | 'percent' => sprintf(' %01.1f%%', $percent), |
||
| 160 | 'color' => $optionObj->getVar('option_color'), |
||
| 161 | ]; |
||
| 162 | } |
||
| 163 | |||
| 164 | $xuEndTimestamp = xoops_getUserTimestamp($pollObj->getVar('end_time')); |
||
| 165 | $xuEndFormattedTime = ucfirst(date(_MEDIUMDATESTRING, $xuEndTimestamp)); |
||
| 166 | |||
| 167 | $isVisible = true === $pollObj->isResultVisible(); |
||
| 168 | |||
| 169 | $multiple = $pollVars['multiple'] ? true : false; |
||
| 170 | $multiLimit = (int)$pollVars['multilimit']; |
||
| 171 | $lang_multi = ''; |
||
| 172 | if ($multiple && ($multiLimit > 0)) { |
||
| 173 | $lang_multi = sprintf(_MB_XOOPSPOLL_MULTITEXT, $multiLimit); |
||
| 174 | } |
||
| 175 | |||
| 176 | $block['id'] = $pollVars['poll_id']; |
||
| 177 | $block['visible'] = $isVisible; |
||
| 178 | $block['question'] = $pollVars['question']; |
||
| 179 | $block['multiple'] = $multiple; |
||
| 180 | $block['lang_multi'] = $lang_multi; |
||
| 181 | $block['optionType'] = $pollOptionType; |
||
| 182 | $block['optionName'] = $pollOptionName; |
||
| 183 | $block['options'] = $pollOptionsArray; |
||
| 184 | $block['hasExpired'] = $pollObj->hasExpired(); |
||
| 185 | $block['votes'] = $pollVars['votes']; |
||
| 186 | $block['hasVoted'] = $hasVoted; |
||
| 187 | $block['canVote'] = $canVote; |
||
| 188 | $block['totalVotes'] = sprintf(_MD_XOOPSPOLL_TOTALVOTES, $totalVotes); |
||
| 189 | $block['endTime'] = $xuEndFormattedTime; // formatted output for current user |
||
| 190 | $block['comments'] = $pollObj->getComments($pollVars['poll_id']); |
||
| 191 | $block['commentMode'] = Utility::commentMode(); |
||
| 192 | |||
| 193 | unset($optionsObjArray, $pollOptionsArray, $pollObj, $pollVars, $timeArray); |
||
| 194 | } |
||
| 349 |