Passed
Pull Request — master (#18)
by Michael
04:40
created

PollHandler   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 58
c 1
b 0
f 0
dl 0
loc 106
rs 10
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A updateCount() 0 15 2
A __construct() 0 8 2
B mailResults() 0 55 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Xoopspoll;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
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
 * XOOPS Poll Class Definitions
18
 *
19
 * @copyright ::  {@link https://xoops.org/ XOOPS Project}
20
 * @license   ::    {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
21
 * @package   ::    xoopspoll
22
 * @subpackage:: class
23
 * @since     ::         1.40
24
 * @author    ::     zyspec <[email protected]>
25
 */
26
27
28
 /**
29
 * Class PollHandler
30
 */
31
class PollHandler extends \XoopsPersistableObjectHandler
32
{
33
    /**
34
     * @var \XoopsModules\Xoopspoll\Helper
35
     */
36
    protected $helper;
37
38
    /**
39
     * PollHandler::__construct()
40
     *
41
     * @param null|\XoopsDatabase                 $db
42
     * @param \XoopsModules\Xoopspoll\Helper|null $helper
43
     */
44
    public function __construct(\XoopsDatabase $db = null, Helper $helper = null)
45
    {
46
        if (null === $helper) {
47
            $this->helper = Helper::getInstance();
48
        } else {
49
            $this->helper = $helper;
50
        }
51
        parent::__construct($db, 'xoopspoll_desc', Poll::class, 'poll_id', 'question');
52
    }
53
54
    /**
55
     * Update the Vote count from the log and polls
56
     * @access public
57
     * @param \XoopsObject $pollObj
58
     * @return bool $success
59
     */
60
    public function updateCount($pollObj)
61
    {
62
        $success = false;
63
        if ($pollObj instanceof Poll) {
64
            $pollId = $pollObj->getVar('poll_id');
65
            /** @var LogHandler $logHandler */
66
            $logHandler = $this->helper->getHandler('Log');
67
            $votes      = $logHandler->getTotalVotesByPollId($pollId);
68
            $voters     = $logHandler->getTotalVotersByPollId($pollId);
69
            $pollObj->setVar('votes', $votes);
70
            $pollObj->setVar('voters', $voters);
71
            $success = $this->insert($pollObj);
72
        }
73
74
        return $success;
75
    }
76
77
    /**
78
     * Mail the results of poll when expired
79
     * @param mixed $pollObj
80
     * @return array true|false indicating sendmail status
81
     */
82
    public function mailResults($pollObj = null)
83
    {
84
        $criteria = new \CriteriaCompo();
85
        $criteria->add(new \Criteria('end_time', \time(), '<'));  // expired polls
86
        $criteria->add(new \Criteria('mail_status', Constants::POLL_NOT_MAILED, '=')); // email not previously sent
87
        if (!empty($pollObj) && ($pollObj instanceof Poll)) {
88
            $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

88
            $criteria->add(new \Criteria('poll_id', /** @scrutinizer ignore-type */ $pollObj->getVar('poll_id'), '='));
Loading history...
89
            $criteria->setLimit(1);
90
        }
91
        $pollObjs = &$this->getAll($criteria);
92
        $tplFile  = 'mail_results.tpl';
93
        $lang     = 'english';
94
        if (\file_exists($GLOBALS['xoops']->path('modules/xoopspoll/language/' . $GLOBALS['xoopsConfig']['language'] . '/mail_template/' . $tplFile))) {
95
            $lang = $GLOBALS['xoopsConfig']['language'];
96
        }
97
        \xoops_loadLanguage('main', 'xoopspoll', $lang);
98
99
        $ret = [];
100
101
        // setup mailer
102
        $xoopsMailer = \xoops_getMailer();
103
        $xoopsMailer->useMail();
104
        $xoopsMailer->setTemplateDir($GLOBALS['xoops']->path('modules/xoopspoll/language/' . $lang . '/mail_template/'));
105
106
        $xoopsMailer->setTemplate($tplFile);
107
        $xoopsMailer->assign('SITENAME', $GLOBALS['xoopsConfig']['sitename']);
108
        $xoopsMailer->assign('ADMINMAIL', $GLOBALS['xoopsConfig']['adminmail']);
109
        $xoopsMailer->assign('SITEURL', $GLOBALS['xoops']->url(''));
110
        $xoopsMailer->assign('MODULEURL', $GLOBALS['xoops']->url('modules/xoopspoll/'));
111
        $xoopsMailer->setFromEmail($GLOBALS['xoopsConfig']['adminmail']);
112
        $xoopsMailer->setFromName($GLOBALS['xoopsConfig']['sitename']);
113
        foreach ($pollObjs as $pollObject) {
114
            $pollValues = $pollObject->getValues();
115
            // get author info
116
            $author = new \XoopsUser($pollValues['user_id']);
117
            if (($author instanceof \XoopsUser) && ($author->uid() > 0)) {
118
                $xoopsMailer->setToUsers($author);
119
                // initialize variables
120
                $xoopsMailer->assign('POLL_QUESTION', $pollValues['question']);
121
                $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

121
                $xoopsMailer->assign('POLL_START', \formatTimestamp($pollValues['start_time'], 'l', /** @scrutinizer ignore-type */ $author->timezone()));
Loading history...
122
                $xoopsMailer->assign('POLL_END', \formatTimestamp($pollValues['end_time'], 'l', $author->timezone()));
123
                $xoopsMailer->assign('POLL_VOTES', $pollValues['votes']);
124
                $xoopsMailer->assign('POLL_VOTERS', $pollValues['voters']);
125
                $xoopsMailer->assign('POLL_ID', $pollValues['poll_id']);
126
                $xoopsMailer->setSubject(\sprintf(\_MD_XOOPSPOLL_YOURPOLLAT, $author->uname(), $GLOBALS['xoopsConfig']['sitename']));
127
                if ($xoopsMailer->send(false)) {
128
                    $pollObject->setVar('mail_status', Constants::POLL_MAILED);
129
                    $ret[] = $this->insert($pollObject);
130
                } else {
131
                    $ret[] = $xoopsMailer->getErrors(false); // return error array from mailer
132
                }
133
            }
134
        }
135
136
        return $ret;
137
    }
138
}
139