PollHandler   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 55
c 1
b 0
f 0
dl 0
loc 100
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A updateCount() 0 15 2
A __construct() 0 4 1
B mailResults() 0 55 8
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Xoopspoll;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
 This program is distributed in the hope that it will be useful,
10
 but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
*/
13
14
/**
15
 * XOOPS Poll Class Definitions
16
 *
17
 * @copyright ::  {@link https://xoops.org/ XOOPS Project}
18
 * @license   ::    {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2.0 or later}
19
 * @subpackage:: class
20
 * @since     ::         1.40
21
 * @author    ::     zyspec <[email protected]>
22
 */
23
24
/**
25
 * Class PollHandler
26
 */
27
class PollHandler extends \XoopsPersistableObjectHandler
28
{
29
    /**
30
     * @var \XoopsModules\Xoopspoll\Helper
31
     */
32
    protected Helper $helper;
33
34
    /**
35
     * PollHandler::__construct()
36
     *
37
     * @param \XoopsModules\Xoopspoll\Helper|null $helper
38
     */
39
    public function __construct(\XoopsDatabase $db = null, Helper $helper = null)
40
    {
41
        $this->helper = $helper ?? Helper::getInstance();
42
        parent::__construct($db, 'xoopspoll_desc', Poll::class, 'poll_id', 'question');
43
    }
44
45
    /**
46
     * Update the Vote count from the log and polls
47
     * @param \XoopsObject $pollObj
48
     * @return int|false object ID or false
49
     */
50
    public function updateCount(\XoopsObject $pollObj)
51
    {
52
        $success = false;
53
        if ($pollObj instanceof Poll) {
54
            $pollId = $pollObj->getVar('poll_id');
55
            /** @var LogHandler $logHandler */
56
            $logHandler = $this->helper->getHandler('Log');
57
            $votes      = $logHandler->getTotalVotesByPollId($pollId);
58
            $voters     = $logHandler->getTotalVotersByPollId($pollId);
59
            $pollObj->setVar('votes', $votes);
60
            $pollObj->setVar('voters', $voters);
61
            $success = $this->insert($pollObj);
62
        }
63
64
        return $success;
65
    }
66
67
    /**
68
     * Mail the results of poll when expired
69
     * @param mixed|null $pollObj
70
     * @return array true|false indicating sendmail status
71
     */
72
    public function mailResults(mixed $pollObj = null): array
73
    {
74
        $criteria = new \CriteriaCompo();
75
        $criteria->add(new \Criteria('end_time', \time(), '<'));  // expired polls
76
        $criteria->add(new \Criteria('mail_status', Constants::POLL_NOT_MAILED, '=')); // email not previously sent
77
        if (!empty($pollObj) && ($pollObj instanceof Poll)) {
78
            $criteria->add(new \Criteria('poll_id', $pollObj->getVar('poll_id'), '='));
0 ignored issues
show
Bug introduced by
It seems like $pollObj->getVar('poll_id') can also be of type array and array; however, parameter $value of Criteria::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

78
            $criteria->add(new \Criteria('poll_id', /** @scrutinizer ignore-type */ $pollObj->getVar('poll_id'), '='));
Loading history...
79
            $criteria->setLimit(1);
80
        }
81
        $pollObjs = &$this->getAll($criteria);
82
        $tplFile  = 'mail_results.tpl';
83
        $lang     = 'english';
84
        if (\file_exists($GLOBALS['xoops']->path('modules/xoopspoll/language/' . $GLOBALS['xoopsConfig']['language'] . '/mail_template/' . $tplFile))) {
85
            $lang = $GLOBALS['xoopsConfig']['language'];
86
        }
87
        \xoops_loadLanguage('main', 'xoopspoll', $lang);
88
89
        $ret = [];
90
91
        // setup mailer
92
        $xoopsMailer = \xoops_getMailer();
93
        $xoopsMailer->useMail();
94
        $xoopsMailer->setTemplateDir($GLOBALS['xoops']->path('modules/xoopspoll/language/' . $lang . '/mail_template/'));
95
96
        $xoopsMailer->setTemplate($tplFile);
97
        $xoopsMailer->assign('SITENAME', $GLOBALS['xoopsConfig']['sitename']);
98
        $xoopsMailer->assign('ADMINMAIL', $GLOBALS['xoopsConfig']['adminmail']);
99
        $xoopsMailer->assign('SITEURL', $GLOBALS['xoops']->url(''));
100
        $xoopsMailer->assign('MODULEURL', $GLOBALS['xoops']->url('modules/xoopspoll/'));
101
        $xoopsMailer->setFromEmail($GLOBALS['xoopsConfig']['adminmail']);
102
        $xoopsMailer->setFromName($GLOBALS['xoopsConfig']['sitename']);
103
        foreach ($pollObjs as $pollObject) {
104
            $pollValues = $pollObject->getValues();
105
            // get author info
106
            $author = new \XoopsUser($pollValues['user_id']);
107
            if (($author instanceof \XoopsUser) && ($author->uid() > 0)) {
108
                $xoopsMailer->setToUsers($author);
109
                // initialize variables
110
                $xoopsMailer->assign('POLL_QUESTION', $pollValues['question']);
111
                $xoopsMailer->assign('POLL_START', \formatTimestamp($pollValues['start_time'], 'l', $author->timezone()));
0 ignored issues
show
Bug introduced by
It seems like $author->timezone() can also be of type array and array; however, parameter $timeoffset of formatTimestamp() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

111
                $xoopsMailer->assign('POLL_START', \formatTimestamp($pollValues['start_time'], 'l', /** @scrutinizer ignore-type */ $author->timezone()));
Loading history...
112
                $xoopsMailer->assign('POLL_END', \formatTimestamp($pollValues['end_time'], 'l', $author->timezone()));
113
                $xoopsMailer->assign('POLL_VOTES', $pollValues['votes']);
114
                $xoopsMailer->assign('POLL_VOTERS', $pollValues['voters']);
115
                $xoopsMailer->assign('POLL_ID', $pollValues['poll_id']);
116
                $xoopsMailer->setSubject(\sprintf(\_MD_XOOPSPOLL_YOURPOLLAT, $author->uname(), $GLOBALS['xoopsConfig']['sitename']));
117
                if ($xoopsMailer->send(false)) {
118
                    $pollObject->setVar('mail_status', Constants::POLL_MAILED);
119
                    $ret[] = $this->insert($pollObject);
120
                } else {
121
                    $ret[] = $xoopsMailer->getErrors(false); // return error array from mailer
122
                }
123
            }
124
        }
125
126
        return $ret;
127
    }
128
}
129