国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

241 lines
9.4KB

  1. <?php
  2. if (!defined('DEDEINC')) exit ('dedebiz');
  3. /**
  4. * 投票
  5. *
  6. * @version $id:dedevote.class.php 10:31 2010年7月6日 tianya $
  7. * @package DedeBIZ.Libraries
  8. * @copyright Copyright (c) 2022 DedeBIZ.COM
  9. * @license https://www.dedebiz.com/license
  10. * @link https://www.dedebiz.com
  11. */
  12. require_once(DEDEINC."/dedetag.class.php");
  13. class DedeVote
  14. {
  15. var $VoteInfos;
  16. var $VoteNotes;
  17. var $VoteCount;
  18. var $VoteID;
  19. var $dsql;
  20. //php5构造函数
  21. function __construct($aid)
  22. {
  23. $this->dsql = $GLOBALS['dsql'];
  24. $this->VoteInfos = $this->dsql->GetOne("SELECT * FROM `#@__vote` WHERE aid='$aid'");
  25. $this->VoteNotes = array();
  26. $this->VoteCount = 0;
  27. $this->VoteID = $aid;
  28. if (!is_array($this->VoteInfos)) {
  29. return;
  30. }
  31. $dtp = new DedeTagParse();
  32. $dtp->SetNameSpace("v", "<", ">");
  33. $dtp->LoadSource($this->VoteInfos['votenote']);
  34. if (is_array($dtp->CTags)) {
  35. foreach ($dtp->CTags as $ctag) {
  36. $this->VoteNotes[$ctag->GetAtt('id')]['count'] = $ctag->GetAtt('count');
  37. $this->VoteNotes[$ctag->GetAtt('id')]['name'] = trim($ctag->GetInnerText());
  38. $this->VoteCount++;
  39. }
  40. }
  41. $dtp->Clear();
  42. }
  43. //兼容php4的构造函数
  44. function DedeVote($aid)
  45. {
  46. $this->__construct($aid);
  47. }
  48. function Close()
  49. {
  50. }
  51. /**
  52. * 获得投票项目总投票次数
  53. *
  54. * @access public
  55. * @return int
  56. */
  57. function GetTotalCount()
  58. {
  59. if (!empty($this->VoteInfos["totalcount"])) {
  60. return $this->VoteInfos["totalcount"];
  61. } else {
  62. return 0;
  63. }
  64. }
  65. /**
  66. * 添加指定的投票节点的票数
  67. *
  68. * @access public
  69. * @param int $aid 投票ID
  70. * @return string
  71. */
  72. function AddVoteCount($aid)
  73. {
  74. if (isset($this->VoteNotes[$aid])) {
  75. $this->VoteNotes[$aid]['count']++;
  76. }
  77. }
  78. /**
  79. * 获得项目的投票表单
  80. *
  81. * @access public
  82. * @param string $tablewidth 表格宽度
  83. * @param string $titlebgcolor 标题颜色
  84. * @param string $titlebackgroup 标题背景
  85. * @param string $tablebg 表格背景
  86. * @param string $itembgcolor 项目背景
  87. * @return string
  88. */
  89. function GetVoteForm($tablewidth = "100%", $titlebgcolor = "#edede2", $titlebackgroup = "", $tablebg = "#ffffff", $itembgcolor = "#ffffff")
  90. {
  91. //省略参数
  92. if ($tablewidth == "") {
  93. $tablewidth = "100%";
  94. }
  95. if ($titlebgcolor == "") {
  96. $titlebgcolor = "#98C6EF";
  97. }
  98. if ($titlebackgroup != "") {
  99. $titlebackgroup = "background='$titlebackgroup'";
  100. }
  101. if ($tablebg == "") {
  102. $tablebg = "#ffffff";
  103. }
  104. if ($itembgcolor == "") {
  105. $itembgcolor = "#ffffff";
  106. }
  107. $items = "<table width='$tablewidth' cellspacing='1' cellpadding='1' id='voteitem' class='table'>\r\n";
  108. $items .= "<form name='voteform' method='post' action='".$GLOBALS['cfg_phpurl']."/vote.php' target='_blank'>\r\n";
  109. $items .= "<input type='hidden' name='dopost' value='send' />\r\n";
  110. $items .= "<input type='hidden' name='aid' value='".$this->VoteID."' />\r\n";
  111. $items .= "<input type='hidden' name='ismore' value='".$this->VoteInfos['ismore']."' />\r\n";
  112. $items .= "<tr align='center'><td id='votetitle' $titlebackgroup>".$this->VoteInfos['votename']."</td></tr>\r\n";
  113. if ($this->VoteCount > 0) {
  114. foreach ($this->VoteNotes as $k => $arr) {
  115. if ($this->VoteInfos['ismore'] == 0) {
  116. $items .= "<tr><td bgcolor='$itembgcolor'><label class='mb-0'><input type='radio' name='voteitem' value='$k'> ".$arr['name']."</label></td></tr>\r\n";
  117. } else {
  118. $items .= "<tr><td bgcolor='$itembgcolor'><label class='mb-0'><input type=checkbox name='voteitem[]' value='$k'> ".$arr['name']."</label></td></tr>\r\n";
  119. }
  120. }
  121. $items .= "<tr><td>\r\n";
  122. $items .= "<input type='submit' name='vbt1' class='btn btn-success' value='投票'>\r\n";
  123. $items .= "<input type='button' name='vbt2' class='btn btn-success' value='查看结果' onClick=window.open('".$GLOBALS['cfg_phpurl']."/vote.php?dopost=view&aid=".$this->VoteID."');>";
  124. $items .= "</td></tr>\r\n";
  125. }
  126. $items .= "</form>\r\n</table>\r\n";
  127. return $items;
  128. }
  129. /**
  130. * 保存投票数据
  131. * 请不要在输出任何文档之前使用SaveVote()方法!
  132. *
  133. * @access public
  134. * @param string $voteitem 投票项目
  135. * @return string
  136. */
  137. function SaveVote($voteitem)
  138. {
  139. global $ENV_GOBACK_URL, $memberID, $row;
  140. if (empty($voteitem)) {
  141. return '您没选中任何项目';
  142. }
  143. $items = '';
  144. //检查投票是否已过期
  145. $nowtime = time();
  146. if ($nowtime > $this->VoteInfos['endtime']) {
  147. ShowMsg('投票已经过期', $ENV_GOBACK_URL);
  148. exit();
  149. }
  150. if ($nowtime < $this->VoteInfos['starttime']) {
  151. ShowMsg('投票还没有开始', $ENV_GOBACK_URL);
  152. exit();
  153. }
  154. //检测游客是否已投过票
  155. if (isset($_COOKIE['VOTE_MEMBER_IP'])) {
  156. if ($_COOKIE['VOTE_MEMBER_IP'] == $_SERVER['REMOTE_ADDR']) {
  157. ShowMsg('您已投过票', $ENV_GOBACK_URL);
  158. exit();
  159. } else {
  160. setcookie('VOTE_MEMBER_IP', $_SERVER['REMOTE_ADDR'], time() * $row['spec'] * 3600, '/');
  161. }
  162. } else {
  163. setcookie('VOTE_MEMBER_IP', $_SERVER['REMOTE_ADDR'], time() * $row['spec'] * 3600, '/');
  164. }
  165. //检查会员是否已投过票
  166. $nowtime = time();
  167. $VoteMem = $this->dsql->GetOne("SELECT * FROM `#@__vote_member` WHERE voteid = '$this->VoteID' and userid='$memberID'");
  168. if (!empty($memberID)) {
  169. if (isset($VoteMem['id'])) {
  170. $voteday = date("Y-m-d", $VoteMem['uptime']);
  171. $day = strtotime("-".$row['spec']." day");
  172. $day = date("Y-m-d", $day);
  173. if ($day < $voteday) {
  174. ShowMsg('在'.$row['spec'].'天内不能重复投票', $ENV_GOBACK_URL);
  175. exit();
  176. } else {
  177. $query = "UPDATE `#@__vote_member` SET uptime='$nowtime' WHERE voteid='$this->VoteID' AND userid='$memberID'";
  178. if ($this->dsql->ExecuteNoneQuery($query) == false) {
  179. ShowMsg('插入数据过程中出现错误', $ENV_GOBACK_URL);
  180. exit();
  181. }
  182. }
  183. } else {
  184. $query = "INSERT INTO `#@__vote_member` (id,voteid,userid,uptime) VALUES ('','$this->VoteID','$memberID','$nowtime')";
  185. if ($this->dsql->ExecuteNoneQuery($query) == false) {
  186. ShowMsg('插入数据过程中出现错误', $ENV_GOBACK_URL);
  187. exit();
  188. }
  189. }
  190. }
  191. //必须存在投票项目
  192. if ($this->VoteCount > 0) {
  193. foreach ($this->VoteNotes as $k => $v) {
  194. if ($this->VoteInfos['ismore'] == 0) {
  195. //单选项
  196. if ($voteitem == $k) {
  197. $this->VoteNotes[$k]['count']++;
  198. break;
  199. }
  200. } else {
  201. //多选项
  202. if (is_array($voteitem) && in_array($k, $voteitem)) {
  203. $this->VoteNotes[$k]['count']++;
  204. }
  205. }
  206. }
  207. foreach ($this->VoteNotes as $k => $arr) {
  208. $items .= "<v:note id='$k' count='".$arr['count']."'>".$arr['name']."</v:note>\r\n";
  209. }
  210. }
  211. $this->dsql->ExecuteNoneQuery("UPDATE `#@__vote` SET totalcount='".($this->VoteInfos['totalcount'] + 1)."',votenote='".addslashes($items)."' WHERE aid='".$this->VoteID."'");
  212. return "投票成功";
  213. }
  214. /**
  215. * 获得项目的投票结果
  216. *
  217. * @access public
  218. * @param string $tablewidth 表格宽度
  219. * @param string $tablesplit 表格分隔
  220. * @return string
  221. */
  222. function GetVoteResult($tablewidth = "600", $tablesplit = "40%")
  223. {
  224. $totalcount = $this->VoteInfos['totalcount'];
  225. if ($totalcount == 0) {
  226. $totalcount = 1;
  227. }
  228. $res = "<table width='$tablewidth' cellspacing='1' cellpadding='1' class='table'>\r\n";
  229. $i = 1;
  230. foreach ($this->VoteNotes as $k => $arr) {
  231. $res .= "<tr><td width='260'>".$i."、".$arr['name']."</td>";
  232. $c = $arr['count'];
  233. $res .= "<td><div class='progress'><div class='progress-bar' role='progressbar' style='width: ".(($c / $totalcount) * 100)."%' aria-valuenow='".(($c / $totalcount) * 100)."' aria-valuemin='0' aria-valuemax='100'></div></div></td></tr>\r\n";
  234. $i++;
  235. }
  236. $res .= "<tr><td></td><td></td></tr>\r\n";
  237. $res .= "</table>\r\n";
  238. return $res;
  239. }
  240. }//End Class
  241. ?>