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