Passed
Pull Request — master (#18)
by Michael
02:50
created

PollHandler   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A updateCount() 0 15 2
A __construct() 0 8 2
B mailResults() 0 57 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
        //        xoops_load('constants', 'xoopspoll');
85
86
        $criteria = new \CriteriaCompo();
87
        $criteria->add(new \Criteria('end_time', \time(), '<'));  // expired polls
88
        $criteria->add(new \Criteria('mail_status', Constants::POLL_NOT_MAILED, '=')); // email not previously sent
89
        if (!empty($pollObj) && ($pollObj instanceof Poll)) {
90
            $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

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

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