Answerhist::getValuesAnswerhists()   B
last analyzed

Complexity

Conditions 10
Paths 96

Size

Total Lines 52
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 41
c 0
b 0
f 0
nc 96
nop 4
dl 0
loc 52
rs 7.6666

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
4
namespace XoopsModules\Wgevents;
5
6
/*
7
 You may not change or alter any portion of this comment or credits
8
 of supporting developers from this source code or any supporting source code
9
 which is considered copyrighted (c) material of the original comment or credit authors.
10
11
 This program is distributed in the hope that it will be useful,
12
 but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
*/
15
16
/**
17
 * wgEvents module for xoops
18
 *
19
 * @copyright    2021 XOOPS Project (https://xoops.org)
20
 * @license      GPL 2.0 or later
21
 * @package      wgevents
22
 * @since        1.0.0
23
 * @min_xoops    2.5.11 Beta1
24
 * @author       Goffy - Wedega - Email:[email protected] - Website:https://xoops.wedega.com
25
 */
26
27
use XoopsModules\Wgevents;
28
29
\defined('XOOPS_ROOT_PATH') || die('Restricted access');
30
31
/**
32
 * Class Object Answerhist
33
 */
34
class Answerhist extends \XoopsObject
35
{
36
    /**
37
     * Constructor
38
     *
39
     */
40
    public function __construct()
41
    {
42
        $this->initVar('hist_id', \XOBJ_DTYPE_INT);
43
        $this->initVar('hist_info', \XOBJ_DTYPE_TXTBOX);
44
        $this->initVar('hist_datecreated', \XOBJ_DTYPE_INT);
45
        $this->initVar('hist_submitter', \XOBJ_DTYPE_INT);
46
        $this->initVar('id', \XOBJ_DTYPE_INT);
47
        $this->initVar('regid', \XOBJ_DTYPE_INT);
48
        $this->initVar('queid', \XOBJ_DTYPE_INT);
49
        $this->initVar('evid', \XOBJ_DTYPE_INT);
50
        $this->initVar('text', \XOBJ_DTYPE_TXTBOX);
51
        $this->initVar('datecreated', \XOBJ_DTYPE_INT);
52
        $this->initVar('submitter', \XOBJ_DTYPE_INT);
53
    }
54
55
    /**
56
     * @static function &getInstance
57
     *
58
     */
59
    public static function getInstance()
60
    {
61
        static $instance = false;
62
        if (!$instance) {
63
            $instance = new self();
64
        }
65
    }
66
67
    /**
68
     * Get Values
69
     * @param array $questionsArr
70
     * @param null  $keys
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $keys is correct as it would always require null to be passed?
Loading history...
71
     * @param null  $format
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $format is correct as it would always require null to be passed?
Loading history...
72
     * @param null  $maxDepth
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $maxDepth is correct as it would always require null to be passed?
Loading history...
73
     * @return array
74
     */
75
    public function getValuesAnswerhists($questionsArr, $keys = null, $format = null, $maxDepth = null)
76
    {
77
        $helper  = \XoopsModules\Wgevents\Helper::getInstance();
78
        $ret = $this->getValues($keys, $format, $maxDepth);
79
        $ret['hist_datecreated_text'] = \formatTimestamp($this->getVar('hist_datecreated'), 's');
80
        $ret['hist_submitter_text']   = \XoopsUser::getUnameFromId($this->getVar('hist_submitter'));
81
        $questionHandler = $helper->getHandler('Question');
82
        $questionObj = $questionHandler->get($this->getVar('queid'));
83
        $queCaption = '';
84
        if (\is_object($questionObj)) {
85
            $queCaption = $questionObj->getVar('caption');
86
        }
87
        $ret['quecaption'] = $queCaption;
88
        $queItem = $questionsArr[$this->getVar('queid')];
89
        $ansText = $this->getVar('text', 'n');
90
        if (Constants::FIELD_RADIOYN == $queItem['type']) {
91
            if ((bool)$ansText) {
92
                $ansText = \_YES;
93
            } else {
94
                $ansText = \_NO;
95
            }
96
        }
97
        if (Constants::FIELD_CHECKBOX == $queItem['type'] ||
98
            Constants::FIELD_COMBOBOX == $queItem['type']) {
99
            $queValues = \unserialize($queItem['values'], ['allowed_classes' => false]);
100
            $ansItems = \unserialize($ansText, ['allowed_classes' => false]);
101
            $ansText = '';
102
            foreach ($ansItems as $ansItem) {
103
                $ansText .= $queValues[(int)$ansItem] . ' <br>';
104
            }
105
        }
106
        if (Constants::FIELD_SELECTBOX == $queItem['type']) {
107
            $queValues = \unserialize($queItem['values'], ['allowed_classes' => false]);
108
            $ansItem = (string)\unserialize($ansText, ['allowed_classes' => false]);
109
            $ansText = $queValues[(int)$ansItem];
110
        }
111
        if (Constants::FIELD_RADIO == $queItem['type']) {
112
            $queValues = \unserialize($queItem['values']);
113
            $ansText = $queValues[$ansText];
114
        }
115
        $ret['text_text'] = $ansText;
116
        $eventHandler = $helper->getHandler('Event');
117
        $eventObj = $eventHandler->get($this->getVar('evid'));
118
        $evName = 'invalid event';
119
        if (\is_object($eventObj)) {
120
            $evName = $eventObj->getVar('name');
121
        }
122
        $ret['eventname']        = $evName;
123
        $ret['datecreated_text'] = \formatTimestamp($this->getVar('datecreated'), 's');
124
        $ret['submitter_text']   = \XoopsUser::getUnameFromId($this->getVar('submitter'));
125
126
        return $ret;
127
    }
128
}
129