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