Passed
Pull Request — master (#18)
by Michael
02:50
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
//xoops_load('pollUtility', 'xoopspoll');
53
//xoops_load('constants', 'xoopspoll');
54
55
/**
56
 * Display XOOPS polls in a block
57
 *
58
 * @access public
59
 * @param  array $options block options array
60
 * @return array block keys and values to be used by block template
61
 * @uses   Criteria
62
 * @global mixed $GLOBALS ['xoopsUser']
63
 * @uses   CriteriaCompo
64
 */
65
function xoopspollBlockMultiShow($options)
66
{
67
    $block           = [];
68
    $pollOptionArray = [];
69
    /** @var \XoopsModuleHandler $moduleHandler */
70
    $moduleHandler = xoops_getHandler('module');
71
    $thisModule         = $moduleHandler->getByDirname('xoopspoll');
72
    $configHandler      = xoops_getHandler('config');
73
    $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

73
    /** @scrutinizer ignore-call */ 
74
    $this_module_config = $configHandler->getConfigsByCat(0, $thisModule->getVar('mid'));
Loading history...
74
75
    $pollHandler = Helper::getInstance()->getHandler('Poll');
76
    $criteria    = new \CriteriaCompo();
77
    $criteria->add(new \Criteria('display', Constants::DISPLAY_POLL_IN_BLOCK, '='));
78
    $criteria->add(new \Criteria('start_time', time(), '<='));
79
    if (0 === $options[1]) {
80
        $criteria->add(new \Criteria('end_time', time(), '>='));
81
    }
82
83
    /**
84
     * now check to see if we want to hide polls that were created using newbb
85
     */
86
    if ($this_module_config['hide_forum_polls'] && ($thisModule instanceof \XoopsModule) && $thisModule->isactive()) {
87
        $newbbModule = $moduleHandler->getByDirname('newbb');
88
        if ($newbbModule instanceof \XoopsModule && $newbbModule->isactive()) {
89
            /** @var Newbb\TopicHandler $topicHandler */
90
            $topicHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Topic');
0 ignored issues
show
Bug introduced by
The type XoopsModules\Newbb\Helper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
91
            $tFields       = ['topic_id', 'poll_id'];
92
            $tArray        = $topicHandler->getAll(new Criteria('topic_haspoll', 0, '>'), $tFields, false);
93
            if (!empty($tArray)) {
94
                $tcriteria = [];
95
                foreach ($tArray as $t) {
96
                    $tcriteria[] = $t['poll_id'];
97
                }
98
                if (!empty($tcriteria)) {
99
                    $tstring = '(' . implode(',', $tcriteria) . ')';
100
                    $criteria->add(new \Criteria('poll_id', $tstring, 'NOT IN'));
101
                }
102
            }
103
            unset($topicHandler, $tFields, $tArray);
104
        }
105
        unset($newbbModule);
106
    }
107
108
    $criteria->setSort('weight ASC, end_time');  // trick criteria to allow 2 sort criteria
109
    $criteria->setOrder('DESC');
110
    $pollObjs = $pollHandler->getAll($criteria);
111
    $count    = count($pollObjs);
112
    if ($count) {
113
        $block['langVote']      = _MD_XOOPSPOLL_VOTE;
114
        $block['langResults']   = _MD_XOOPSPOLL_RESULTS;
115
        $block['langExpires']   = _MB_XOOPSPOLL_WILLEXPIRE;
116
        $block['langExpired']   = _MB_XOOPSPOLL_HASEXPIRED;
117
        $block['langComments']  = _MB_XOOPSPOLL_COMMENTS;
118
        $block['langComment']   = _MB_XOOPSPOLL_COMMENT;
119
        $block['url']           = 'http' . (!empty($_SERVER['HTTPS']) ? 's' : '') . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
120
        $block['dispVotes']     = $this_module_config['disp_vote_nums'];
121
        $block['thisModuleDir'] = 'xoopspoll';
122
        $block['asList']        = $options[0];
123
124
        $optionHandler = Helper::getInstance()->getHandler('Option');
125
        $logHandler    = Helper::getInstance()->getHandler('Log');
126
127
        foreach ($pollObjs as $pollObj) {
128
            $criteria = new \CriteriaCompo();
129
            $pollVars = $pollObj->getValues();
130
            $criteria->add(new \Criteria('poll_id', $pollVars['poll_id'], '='));
131
            $criteria->setSort('option_id');
132
            $pollOptionObjs = $optionHandler->getAll($criteria);
133
            if (Constants::MULTIPLE_SELECT_POLL === $pollVars['multiple']) {
134
                $pollOptionType = 'checkbox';
135
                $pollOptionName = 'option_id[]';
136
            } else {
137
                $pollOptionType = 'radio';
138
                $pollOptionName = 'option_id';
139
            }
140
141
                $uid = 0;
142
            if (isset($GLOBALS['xoopsUser']) && ($GLOBALS['xoopsUser'] instanceof \XoopsUser)) {
143
                $uid = $GLOBALS['xoopsUser']->getVar('uid');
144
            }
145
146
            $totalVotes = $pollVars['votes'];
147
            $hasVoted   = $logHandler->hasVoted($pollVars['poll_id'], xoops_getenv('REMOTE_ADDR'), $uid) ? true : false;
148
            $canVote    = (!$hasVoted) && $pollObj->isAllowedToVote();
149
            foreach ($pollOptionObjs as $pollOptionObj) {
150
                $optionObjVars = $pollOptionObj->getValues();
151
                $percent       = ($totalVotes > 0) ? (100 * $optionObjVars['option_count'] / $totalVotes) : 0;
152
                //                $percent = ($totalVotes > 0) ? (int)(100 * $optionObjVars['option_count'] / $totalVotes) . '%' : '0%';
153
                $pollOptionArray[] = [
154
                    'id'      => $optionObjVars['option_id'],
155
                    'text'    => $optionObjVars['option_text'],
156
                    'count'   => $optionObjVars['option_count'],
157
                    'percent' => sprintf(' %01.1f%%', $percent),
158
                    'color'   => $optionObjVars['option_color'],
159
                ];
160
            }
161
            unset($pollOptionObjs, $optionObjVars);
162
            $xuEndTimestamp     = xoops_getUserTimestamp($pollObj->getVar('end_time'));
163
            $xuEndFormattedTime = ucfirst(date(_MEDIUMDATESTRING, $xuEndTimestamp));
164
165
            $isVisible  = true === $pollObj->isResultVisible();
166
            $multiple   = $pollVars['multiple'] ? true : false;
167
            $multiLimit = (int)$pollVars['multilimit'];
168
            $lang_multi = '';
169
            if ($multiple && ($multiLimit > 0)) {
170
                $lang_multi = sprintf(_MB_XOOPSPOLL_MULTITEXT, $multiLimit);
171
            }
172
173
            $poll             = [
174
                'id'          => $pollVars['poll_id'],
175
                'visible'     => $isVisible,
176
                'question'    => $pollVars['question'],
177
                'multiple'    => $multiple,
178
                'lang_multi'  => $lang_multi,
179
                'optionType'  => $pollOptionType,
180
                'optionName'  => $pollOptionName,
181
                'options'     => $pollOptionArray,
182
                'hasExpired'  => $pollObj->hasExpired(),
183
                'canVote'     => $canVote,
184
                'votes'       => $pollVars['votes'],
185
                'hasVoted'    => $hasVoted,
186
                'totalVotes'  => sprintf(_MD_XOOPSPOLL_TOTALVOTES, $totalVotes),
187
                'comments'    => $pollObj->getComments($pollVars['poll_id']),
188
                'endTime'     => $xuEndFormattedTime,
189
                'commentMode' => Utility::commentMode(),
190
            ];
191
            $block['polls'][] = $poll;
192
            unset($pollOptionArray, $poll, $pollVars);
193
        }
194
    }
195
196
    return $block;
197
}
198
199
/**
200
 * Display a form to edit poll block display option
201
 *
202
 * @access public
203
 * @param mixed  $options
204
 * @return string HTML form for display by block admin
205
 * @global mixed $GLOBALS ['xoopsUser']
206
 * @uses   xoops_getModuleHandler() function to get class handler for this modules class(es)
207
 */
208
function xoopspollBlockMultiEdit($options)
209
{
210
    /**
211
     * Options[]
212
     *        [0]    0|1 = show as option|select
213
     *        [1]    0|1 show expired polls in block
214
     */
215
216
    // find out if want to show expired polls in block
217
    // (otherwise it will hide block once it expires)
218
    if (0 === $options[1]) {
219
        $chk0no  = ' checked';
220
        $chk0yes = '';
221
    } else {
222
        $chk0no  = '';
223
        $chk0yes = ' checked';
224
    }
225
    $form = "<table>\n"
226
            . "  <tr>\n"
227
            . "    <td class='width25 middle'>"
228
            . _MB_XOOPSPOLL_SHOW_EXP
229
            . ":</td>\n"
230
            . "    <td>\n"
231
            . "      <label class='middle' for='yes'>"
232
            . _YES
233
            . "</label>\n"
234
            . "      <input type='radio' name='options[1]' value='1'{$chk0yes} id='yes'>\n"
235
            . "      <label class='middle' style='margin-left: 2em;' for='no'>&nbsp;&nbsp;&nbsp;"
236
            . _NO
237
            . "</label>\n"
238
            . "      <input type='radio' name='options[1]' value='0'{$chk0no} id='no'>\n"
239
            . "    </td>\n"
240
            . "  </tr>\n";
241
242
    // find out if want to show options as a lists or as a select boxes
243
    if (Constants::POLL_OPTIONS_SELECT === $options[0]) {
244
        $chk0select = ' checked';
245
        $chk0list   = '';
246
    } else {
247
        $chk0select = '';
248
        $chk0list   = ' checked';
249
    }
250
    $form .= "  <tr>\n"
251
             . "    <td class='width25 middle'>"
252
             . _MB_XOOPSPOLL_SHOW_OPTIONS
253
             . ":</td>\n"
254
             . "    <td>\n"
255
             . "      <label class='middle' for='list'>"
256
             . _MB_XOOPSPOLL_LIST
257
             . "</label>\n"
258
             . "      <input type='radio' name='options[0]' value='"
259
             . Constants::POLL_OPTIONS_LIST
260
             . "'{$chk0list} id='list'>\n"
261
             . "      <label class='middle' style='margin-left: 2em;' for='select'>&nbsp;&nbsp;&nbsp;"
262
             . _MB_XOOPSPOLL_SELECT
263
             . "</label>\n"
264
             . "      <input type='radio' name='options[0]' value='"
265
             . Constants::POLL_OPTIONS_SELECT
266
             . "'{$chk0select} id='select'>\n"
267
             . "    </td>\n"
268
             . "  </tr>\n"
269
             . "</table>\n";
270
271
    return $form;
272
}
273