Passed
Pull Request — master (#18)
by Michael
02:59
created

multipoll.php ➔ xoopspollBlockMultiShow()   D

Complexity

Conditions 24
Paths 30

Size

Total Lines 135
Code Lines 102

Duplication

Lines 38
Ratio 28.15 %

Importance

Changes 0
Metric Value
cc 24
eloc 102
nc 30
nop 1
dl 38
loc 135
rs 4.5989
c 0
b 0
f 0

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
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 http://www.fsf.org/copyleft/gpl.html GNU public license}
31
 * @package   ::  xoopspoll
32
 * @subpackage::  blocks
33
 * @since     ::  1.0
34
 *
35
 **/
36
37
use XoopsModules\Newbb;
38
use XoopsModules\Xoopspoll\{
39
    Constants,
40
    Helper,
41
    Utility
42
};
43
44
xoops_loadLanguage('main', 'xoopspoll');
45
/*
46
require_once $GLOBALS['xoops']->path( "modules"
47
                                    . "/xoopspoll"
48
                                    . "/class"
49
                                    . "/pollutility.php"
50
);
51
*/
52
53
/**
54
 * Display XOOPS polls in a block
55
 *
56
 * @access public
57
 * @param  array $options block options array
58
 * @return array block keys and values to be used by block template
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'));
0 ignored issues
show
Bug introduced by
The method getConfigsByCat() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
    /** @scrutinizer ignore-call */ 
72
    $this_module_config = $configHandler->getConfigsByCat(0, $thisModule->getVar('mid'));
Loading history...
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;
195
}
196
197
/**
198
 * Display a form to edit poll block display option
199
 *
200
 * @access public
201
 * @param mixed  $options
202
 * @return string HTML form for display by block admin
203
 * @global mixed $GLOBALS ['xoopsUser']
204
 * @uses   xoops_getModuleHandler() function to get class handler for this modules class(es)
205
 */
206
function xoopspollBlockMultiEdit($options)
207
{
208
    /**
209
     * Options[]
210
     *        [0]    0|1 = show as option|select
211
     *        [1]    0|1 show expired polls in block
212
     */
213
214
    // find out if want to show expired polls in block
215
    // (otherwise it will hide block once it expires)
216
    if (0 === $options[1]) {
217
        $chk0no  = ' checked';
218
        $chk0yes = '';
219
    } else {
220
        $chk0no  = '';
221
        $chk0yes = ' checked';
222
    }
223
    $form = "<table>\n"
224
            . "  <tr>\n"
225
            . "    <td class='width25 middle'>"
226
            . _MB_XOOPSPOLL_SHOW_EXP
227
            . ":</td>\n"
228
            . "    <td>\n"
229
            . "      <label class='middle' for='yes'>"
230
            . _YES
231
            . "</label>\n"
232
            . "      <input type='radio' name='options[1]' value='1'{$chk0yes} id='yes'>\n"
233
            . "      <label class='middle' style='margin-left: 2em;' for='no'>&nbsp;&nbsp;&nbsp;"
234
            . _NO
235
            . "</label>\n"
236
            . "      <input type='radio' name='options[1]' value='0'{$chk0no} id='no'>\n"
237
            . "    </td>\n"
238
            . "  </tr>\n";
239
240
    // find out if want to show options as a lists or as a select boxes
241
    if (Constants::POLL_OPTIONS_SELECT === $options[0]) {
242
        $chk0select = ' checked';
243
        $chk0list   = '';
244
    } else {
245
        $chk0select = '';
246
        $chk0list   = ' checked';
247
    }
248
    $form .= "  <tr>\n"
249
             . "    <td class='width25 middle'>"
250
             . _MB_XOOPSPOLL_SHOW_OPTIONS
251
             . ":</td>\n"
252
             . "    <td>\n"
253
             . "      <label class='middle' for='list'>"
254
             . _MB_XOOPSPOLL_LIST
255
             . "</label>\n"
256
             . "      <input type='radio' name='options[0]' value='"
257
             . Constants::POLL_OPTIONS_LIST
258
             . "'{$chk0list} id='list'>\n"
259
             . "      <label class='middle' style='margin-left: 2em;' for='select'>&nbsp;&nbsp;&nbsp;"
260
             . _MB_XOOPSPOLL_SELECT
261
             . "</label>\n"
262
             . "      <input type='radio' name='options[0]' value='"
263
             . Constants::POLL_OPTIONS_SELECT
264
             . "'{$chk0select} id='select'>\n"
265
             . "    </td>\n"
266
             . "  </tr>\n"
267
             . "</table>\n";
268
269
    return $form;
270
}
271