国内流行的内容管理系统(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.

248 lines
9.6KB

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