xoopspollBlockMultiShow()   F
last analyzed

Complexity

Conditions 21
Paths 30

Size

Total Lines 132
Code Lines 100

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 21
eloc 100
c 2
b 0
f 0
nc 30
nop 1
dl 0
loc 132
rs 3.3333

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php declare(strict_types=1);
2
/*
3
                XOOPS - PHP Content Management System
4
                    Copyright (c) 2000-2020 XOOPS.org
5
                       <https://xoops.org>
6
  This program is free software; you can redistribute it and/or modify
7
  it under the terms of the GNU General Public License as published by
8
  the Free Software Foundation; either version 2 of the License, or
9
  (at your option) any later version.
10
11
  You may not change or alter any portion of this comment or credits
12
  of supporting developers from this source code or any supporting
13
  source code which is considered copyrighted (c) material of the
14
  original comment or credit authors.
15
16
  This program is distributed in the hope that it will be useful,
17
  but WITHOUT ANY WARRANTY; without even the implied warranty of
18
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
  GNU General Public License for more details.
20
21
  You should have received a copy of the GNU General Public License
22
  along with this program; if not, write to the Free Software
23
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
24
*/
25
26
/**
27
 * XoopsPoll Display Multi-poll Block
28
 *
29
 * @copyright ::   {@link https://xoops.org/ XOOPS Project}
30
 * @license   ::  {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2.0 or later}
31
 * @subpackage::  blocks
32
 * @since     ::  1.0
33
 *
34
 **/
35
36
use XoopsModules\Newbb;
37
use XoopsModules\Xoopspoll\{
38
    Constants,
39
    Helper,
40
    Utility
41
};
42
43
xoops_loadLanguage('main', 'xoopspoll');
44
/*
45
require_once $GLOBALS['xoops']->path( "modules"
46
                                    . "/xoopspoll"
47
                                    . "/class"
48
                                    . "/pollutility.php"
49
);
50
*/
51
52
/**
53
 * Display XOOPS polls in a block
54
 *
55
 * @param array $options block options array
56
 * @return array block keys and values to be used by block template
57
 * @uses   Criteria
58
 * @global mixed $GLOBALS ['xoopsUser']
59
 * @uses   CriteriaCompo
60
 */
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
}
194
195
/**
196
 * Display a form to edit poll block display option
197
 *
198
 * @param mixed  $options
199
 * @return string HTML form for display by block admin
200
 * @global mixed $GLOBALS ['xoopsUser']
201
 * @uses   xoops_getModuleHandler() function to get class handler for this modules class(es)
202
 */
203
function xoopspollBlockMultiEdit(mixed $options): string
204
{
205
    /**
206
     * Options[]
207
     *        [0]    0|1 = show as option|select
208
     *        [1]    0|1 show expired polls in block
209
     */
210
211
    // find out if want to show expired polls in block
212
    // (otherwise it will hide block once it expires)
213
    if (0 === $options[1]) {
214
        $chk0no  = ' checked';
215
        $chk0yes = '';
216
    } else {
217
        $chk0no  = '';
218
        $chk0yes = ' checked';
219
    }
220
    $form = "<table>\n"
221
            . "  <tr>\n"
222
            . "    <td class='width25 middle'>"
223
            . _MB_XOOPSPOLL_SHOW_EXP
224
            . ":</td>\n"
225
            . "    <td>\n"
226
            . "      <label class='middle' for='yes'>"
227
            . _YES
228
            . "</label>\n"
229
            . "      <input type='radio' name='options[1]' value='1'{$chk0yes} id='yes'>\n"
230
            . "      <label class='middle' style='margin-left: 2em;' for='no'>&nbsp;&nbsp;&nbsp;"
231
            . _NO
232
            . "</label>\n"
233
            . "      <input type='radio' name='options[1]' value='0'{$chk0no} id='no'>\n"
234
            . "    </td>\n"
235
            . "  </tr>\n";
236
237
    // find out if want to show options as a lists or as a select boxes
238
    if (Constants::POLL_OPTIONS_SELECT === $options[0]) {
239
        $chk0select = ' checked';
240
        $chk0list   = '';
241
    } else {
242
        $chk0select = '';
243
        $chk0list   = ' checked';
244
    }
245
    $form .= "  <tr>\n"
246
             . "    <td class='width25 middle'>"
247
             . _MB_XOOPSPOLL_SHOW_OPTIONS
248
             . ":</td>\n"
249
             . "    <td>\n"
250
             . "      <label class='middle' for='list'>"
251
             . _MB_XOOPSPOLL_LIST
252
             . "</label>\n"
253
             . "      <input type='radio' name='options[0]' value='"
254
             . Constants::POLL_OPTIONS_LIST
255
             . "'{$chk0list} id='list'>\n"
256
             . "      <label class='middle' style='margin-left: 2em;' for='select'>&nbsp;&nbsp;&nbsp;"
257
             . _MB_XOOPSPOLL_SELECT
258
             . "</label>\n"
259
             . "      <input type='radio' name='options[0]' value='"
260
             . Constants::POLL_OPTIONS_SELECT
261
             . "'{$chk0select} id='select'>\n"
262
             . "    </td>\n"
263
             . "  </tr>\n"
264
             . "</table>\n";
265
266
    return $form;
267
}
268