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'), '=')); |
|
|
|
|
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())); |
|
|
|
|
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
|
|
|
|