Passed
Pull Request — master (#18)
by Michael
04:31
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

1 Method

Rating   Name   Duplication   Size   Complexity  
F xoopspollBlockMultiShow() 0 132 23

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
use XoopsModules\Xoopspoll\Constants;
40
41
xoops_loadLanguage('main', 'xoopspoll');
42
/*
43
require_once $GLOBALS['xoops']->path( "modules"
44
                                    . "/xoopspoll"
45
                                    . "/class"
46
                                    . "/pollutility.php"
47
);
48
*/
49
//xoops_load('pollUtility', 'xoopspoll');
50
//xoops_load('constants', 'xoopspoll');
51
52
/**
53
 * Display XOOPS polls in a block
54
 *
55
 * @access public
56
 * @param  array $options block options array
57
 * @return array block keys and values to be used by block template
58
 * @uses   Criteria
59
 * @global mixed $GLOBALS ['xoopsUser']
60
 * @uses   CriteriaCompo
61
 */
62
function xoopspollBlockMultiShow($options)
63
{
64
    $block           = [];
65
    $pollOptionArray = [];
66
    /** @var \XoopsModuleHandler $moduleHandler */
67
    $moduleHandler = xoops_getHandler('module');
68
    $thisModule         = $moduleHandler->getByDirname('xoopspoll');
69
    $configHandler      = xoops_getHandler('config');
70
    $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

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