1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK ---------------------------------- |
---|
3 | # This file is part of pollsFactory, a plugin for Dotclear 2. |
---|
4 | # |
---|
5 | # Copyright (c) 2009-2010 JC Denis and contributors |
---|
6 | # jcdenis@gdwd.com |
---|
7 | # |
---|
8 | # Licensed under the GPL version 2.0 license. |
---|
9 | # A copy of this license is available in LICENSE file or at |
---|
10 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
11 | # -- END LICENSE BLOCK ------------------------------------ |
---|
12 | |
---|
13 | if (!defined('DC_RC_PATH')){return;} |
---|
14 | |
---|
15 | class pollsFactory extends postOption |
---|
16 | { |
---|
17 | public $core; |
---|
18 | public $con; |
---|
19 | protected $table; |
---|
20 | protected $blog; |
---|
21 | |
---|
22 | public function __construct($core) |
---|
23 | { |
---|
24 | $this->core = $core; |
---|
25 | $this->con = $core->con; |
---|
26 | $this->blog = $core->con->escape($core->blog->id); |
---|
27 | $this->table = $core->con->escape($core->prefix.'post_option'); |
---|
28 | } |
---|
29 | |
---|
30 | |
---|
31 | # Check if a people has voted on a poll |
---|
32 | public function checkVote($poll_id) |
---|
33 | { |
---|
34 | $chk = false; |
---|
35 | $poll_id = (integer) $poll_id; |
---|
36 | $ident = (integer) $this->core->blog->settings->pollsFactory_people_ident; |
---|
37 | |
---|
38 | # Cookie |
---|
39 | if($ident < 2) |
---|
40 | { |
---|
41 | $list = isset($_COOKIE['pollsFactoryVotes']) ? |
---|
42 | explode(',',$_COOKIE['pollsFactoryVotes']) : array(); |
---|
43 | |
---|
44 | if(in_array($poll_id,$list)) $chk = true; |
---|
45 | } |
---|
46 | # IP |
---|
47 | if($ident > 0) |
---|
48 | { |
---|
49 | $rs = $this->con->select( |
---|
50 | 'SELECT option_id '. |
---|
51 | 'FROM '.$this->table.' '. |
---|
52 | 'WHERE post_id = '.$poll_id.' '. |
---|
53 | "AND option_title = '".$this->con->escape(http::realIP())."' ". |
---|
54 | "AND option_type = 'pollsresponse' ". |
---|
55 | $this->con->limit(1) |
---|
56 | ); |
---|
57 | |
---|
58 | if (!$rs->isEmpty()) $chk = true; |
---|
59 | } |
---|
60 | return $chk; |
---|
61 | } |
---|
62 | |
---|
63 | # Get records of peoples that have voted on a poll |
---|
64 | public function getVotes($poll_id,$start=null,$limit=null) |
---|
65 | { |
---|
66 | $poll_id = (integer) $poll_id; |
---|
67 | |
---|
68 | $q = ''; |
---|
69 | if ($start !== null) { |
---|
70 | $start = (integer) $start; |
---|
71 | $limit = (integer) $limit; |
---|
72 | $q = $this->con->limit(array($start,$limit)); |
---|
73 | } |
---|
74 | |
---|
75 | return $this->con->select( |
---|
76 | 'SELECT option_title, post_id '. |
---|
77 | 'FROM '.$this->table.' '. |
---|
78 | 'WHERE post_id = '.$poll_id.' '. |
---|
79 | "AND option_type = 'pollsresponse' ". |
---|
80 | 'GROUP BY option_title, post_id '. |
---|
81 | 'ORDER BY option_title DESC '. |
---|
82 | $q |
---|
83 | ); |
---|
84 | } |
---|
85 | |
---|
86 | # Count peoples have voted on a poll |
---|
87 | public function countVotes($poll_id) |
---|
88 | { |
---|
89 | $poll_id = (integer) $poll_id; |
---|
90 | |
---|
91 | return $this->con->select( |
---|
92 | 'SELECT option_title '. |
---|
93 | 'FROM '.$this->table.' '. |
---|
94 | 'WHERE post_id = '.$poll_id.' '. |
---|
95 | "AND option_type = 'pollsresponse' ". |
---|
96 | 'GROUP BY option_title ' |
---|
97 | )->count(); |
---|
98 | } |
---|
99 | |
---|
100 | # Set ident of a people that has voted on a poll |
---|
101 | public function setVote($poll_id) |
---|
102 | { |
---|
103 | $poll_id = (integer) $poll_id; |
---|
104 | |
---|
105 | # Cookie |
---|
106 | if($this->core->blog->settings->pollsFactory_people_ident < 2) |
---|
107 | { |
---|
108 | $list = isset($_COOKIE['pollsFactoryVotes']) ? |
---|
109 | explode(',',$_COOKIE['pollsFactoryVotes']) : array(); |
---|
110 | |
---|
111 | $list[] = $poll_id; |
---|
112 | setcookie('pollsFactoryVotes',implode(',',$list),time()+60*60*24*30,'/'); |
---|
113 | } |
---|
114 | # Ident |
---|
115 | $ip = $this->core->blog->settings->pollsFactory_people_ident > 0 ? |
---|
116 | $this->con->escape(http::realIP()) : |
---|
117 | substr(http::browserUID(DC_MASTER_KEY),0,24); |
---|
118 | |
---|
119 | return $ip; |
---|
120 | } |
---|
121 | |
---|
122 | # Update post_open_tb (polls opened) |
---|
123 | public function updPostOpened($id,$opened) |
---|
124 | { |
---|
125 | if (!$this->core->auth->check('usage,contentadmin',$this->core->blog->id)) { |
---|
126 | throw new Exception(__('You are not allowed to update polls')); |
---|
127 | } |
---|
128 | |
---|
129 | $id = (integer) $id; |
---|
130 | $opened = (integer) $opened; |
---|
131 | |
---|
132 | if (empty($id)) { |
---|
133 | throw new Exception(__('No such poll ID')); |
---|
134 | } |
---|
135 | |
---|
136 | if (!$this->core->auth->check('contentadmin',$this->core->blog->id)) |
---|
137 | { |
---|
138 | $strReq = 'SELECT post_id '. |
---|
139 | 'FROM '.$this->core->prefix.'post '. |
---|
140 | 'WHERE post_id = '.$id.' '. |
---|
141 | "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; |
---|
142 | |
---|
143 | $rs = $this->con->select($strReq); |
---|
144 | |
---|
145 | if ($rs->isEmpty()) { |
---|
146 | throw new Exception(__('You are not allowed to edit this poll')); |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | $cur = $this->con->openCursor($this->core->prefix.'post'); |
---|
151 | $cur->post_open_tb = $opened; |
---|
152 | $cur->post_upddt = date('Y-m-d H:i:s'); |
---|
153 | $cur->update("WHERE post_id = ".$id." AND blog_id = '".$this->blog."' "); |
---|
154 | |
---|
155 | $this->trigger(); |
---|
156 | } |
---|
157 | |
---|
158 | # Get list of urls where to show full poll |
---|
159 | public static function getPublicUrlTypes($core) |
---|
160 | { |
---|
161 | $types = array(); |
---|
162 | $core->callBehavior('pollsFactoryPublicUrlTypes',$types); |
---|
163 | |
---|
164 | $types[__('home page')] = 'default'; |
---|
165 | $types[__('post pages')] = 'post'; |
---|
166 | $types[__('static pages')] = 'pages'; |
---|
167 | $types[__('tags pages')] = 'tag'; |
---|
168 | $types[__('archives pages')] = 'archive'; |
---|
169 | $types[__('category pages')] = 'category'; |
---|
170 | $types[__('entries feed')] = 'feed'; |
---|
171 | |
---|
172 | return $types; |
---|
173 | } |
---|
174 | |
---|
175 | # Get list of queries types |
---|
176 | public static function getQueryTypes() |
---|
177 | { |
---|
178 | return array( |
---|
179 | __('multiple choice list') => 'checkbox', |
---|
180 | __('single choice list') => 'radio', |
---|
181 | __('options box') => 'combo', |
---|
182 | __('text field') => 'field', |
---|
183 | __('text area') => 'textarea' |
---|
184 | ); |
---|
185 | } |
---|
186 | } |
---|
187 | ?> |
---|