Dotclear

source: plugins/rateIt/inc/class.rateit.php @ 1307

Revision 1307, 8.9 KB checked in by JcDenis, 14 years ago (diff)

rateIt 0.5:

  • fixed PostgreSQL compatibility
Line 
1<?php
2# -- BEGIN LICENSE BLOCK ----------------------------------
3# This file is part of rateIt, a plugin for Dotclear 2.
4#
5# Copyright (c) 2009 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#
12# -- END LICENSE BLOCK ------------------------------------
13
14class rateIt
15{
16     public $core;
17     private $table;
18     private $quotient;
19     private $digit;
20     private $types;
21     private $ident;
22     public $ip;
23
24     public function __construct(&$core)
25     {
26          $this->core =& $core;
27          $this->table = $core->prefix.'rateit';
28          $this->quotient = $core->blog->settings->rateit_quotient;
29          $this->digit = $core->blog->settings->rateit_digit;
30
31          $types = new ArrayObject();
32          $types[] = 'post';
33
34          # --BEHAVIOR-- addRateItType
35          $core->callBehavior('addRateItType',$types);
36
37          $this->types = (array) $types;
38          $this->ident = (integer) $core->blog->settings->rateit_userident;
39
40          if ($this->ident == 2)
41               $this->ip = $core->getNonce();
42          else
43               $this->ip = $_SERVER['REMOTE_ADDR'];
44     }
45
46     public function set($type,$id,$note)
47     {
48          if (!in_array($type,$this->types))
49               return false;
50
51          $cur = $this->core->con->openCursor($this->table);
52          $this->core->con->writeLock($this->table);
53
54          $cur->blog_id = $this->core->blog->id;
55          $cur->rateit_type = (string) $type;
56          $cur->rateit_id = (integer) $id;
57          $cur->rateit_ip = (string) $this->ip;
58          $cur->rateit_note = $note;
59          $cur->rateit_quotient = $this->quotient;
60          $cur->rateit_time = date('Y-m-d H:i:00');
61
62          $cur->insert();
63          $this->core->con->unlock();
64          $this->core->blog->triggerBlog();
65
66          if ($this->ident > 0)
67               setcookie('rateit-'.$type.'-'.$id,1,(time()+3600*365));
68         
69          return true;
70     }
71     
72     public function get($type=null,$id=null,$ip=null)
73     {
74          $req=
75               'SELECT rateit_note, rateit_quotient '.
76               'FROM '.$this->table.' WHERE blog_id=\''.$this->core->con->escape($this->core->blog->id).'\' ';
77          if ($type!=null)
78               $req .= 'AND rateit_type=\''.$this->core->con->escape($type).'\' ';
79          if ($id!=null)
80               $req .= 'AND rateit_id=\''.$this->core->con->escape($id).'\' ';
81          if ($ip!=null)
82               $req .= 'AND rateit_ip=\''.$this->core->con->escape($ip).'\' ';
83
84          $rs = $this->core->con->select($req);
85          $rs->toStatic();
86
87          $note = $sum = $max = $total = 0;
88          $min = 10000;
89          while($rs->fetch()){
90               $note = $rs->rateit_note / $rs->rateit_quotient;
91               $sum += $note;
92               $max = $max < $note ? $note : $max;
93               $min = $min > $note ? $note : $min;
94               $total += 1;
95          }
96          if ($rs->count())
97               $note = $sum / $total;
98          else
99               $min = 0;
100
101          $res = new ArrayObject();
102          $res->max = self::trans($max);
103          $res->min = self::trans($min);
104          $res->note = self::trans($note);
105          $res->total = $total;
106          $res->sum = $sum;
107          $res->quotient = $this->quotient;
108          $res->digit = $this->digit;
109
110          return $res;
111     }
112
113     public function voted($type=null,$id=null)
114     {
115          $rs = $this->core->con->select(
116               'SELECT COUNT(*) '.
117               'FROM '.$this->table.' '.
118               'WHERE blog_id=\''.$this->core->con->escape($this->core->blog->id).'\' '.
119               'AND rateit_ip=\''.$this->core->con->escape($this->ip).'\' '.
120               ($type!=null ? 
121               'AND rateit_type=\''.$this->core->con->escape($type).'\' ' : '').
122               ($id!=null ? 
123               'AND rateit_id=\''.$this->core->con->escape($id).'\' ' : '')
124          );
125          $sql = (boolean) $rs->f(0);
126          $cookie = false;
127          if ($this->ident > 0 && $id !== null && $type !== null)
128               $cookie = isset($_COOKIE['rateit-'.$type.'-'.$id]);
129
130          return $sql || $cookie;
131     }
132
133     public function del($type=null,$id=null,$ip=null)
134     {
135          $req = 
136               'DELETE FROM '.$this->table.' '.
137               'WHERE blog_id=\''.$this->core->con->escape($this->core->blog->id).'\' ';
138          if ($type!=null)
139               $req .= 'AND rateit_type=\''.$this->core->con->escape($type).'\' ';
140          if ($id!=null)
141               $req .= 'AND rateit_id=\''.$this->core->con->escape($id).'\' ';
142          if ($ip!=null)
143               $req .= 'AND rateit_ip=\''.$this->core->con->escape($ip).'\' ';
144
145          $rs = $this->core->con->select($req);
146          $this->core->blog->triggerBlog();
147     }
148
149     public function trans($note)
150     {
151          return round($note * $this->quotient,$this->digit);
152     }
153
154     public function getPostsByRate($params=array(),$count_only=false)
155     {
156          $params['columns'][] = 'COUNT(rateit_id) as rateit_count';
157     
158          $params['from'] = 'INNER JOIN '.$this->table.' ON CAST(P.post_id as char)=rateit_id ';
159         
160          if (!isset($params['sql'])) $params['sql'] = '';
161
162          if (!empty($params['rateit_type'])) {
163               $params['sql'] .= "AND rateit_type = '".$this->core->con->escape($params['rateit_type'])."' ";
164               unset($params['rateit_type']);
165          }
166
167          $params['sql'] .= 'GROUP BY rateit_id, rateit_type ';
168          if (!$count_only) {
169
170               if (!empty($params['no_content'])) {
171                    $c_req = '';
172               } else {
173                    $c_req =
174                    'post_excerpt, post_excerpt_xhtml, '.
175                    'post_content, post_content_xhtml, post_notes, ';
176               }
177
178               if (!empty($params['columns']) && is_array($params['columns'])) {
179                    $c = $params['columns'];
180                    $cols = array();
181                    foreach($c AS $k => $v) {
182                         if (!preg_match('/(\sas\s)/',$v)) $cols[] = $v;
183                    }
184                    if (!empty($cols)) 
185                         $c_req .= implode(', ',$cols).', ';
186               }
187
188               $params['sql'] .= ', '.
189               'P.post_id, P.blog_id, P.user_id, P.cat_id, post_dt, '.
190               'post_tz, post_creadt, post_upddt, post_format, post_password, '.
191               'post_url, post_lang, post_title, '.$c_req.
192               'post_type, post_meta, post_status, post_selected, post_position, '.
193               'post_open_comment, post_open_tb, nb_comment, nb_trackback, '.
194               'U.user_name, U.user_firstname, U.user_displayname, U.user_email, '.
195               'U.user_url, '.
196               'C.cat_title, C.cat_url, C.cat_desc ';
197          }
198
199          return $this->core->blog->getPosts($params,$count_only);
200     }
201
202     public function getRates($params,$count_only=false)
203     {
204          if ($count_only)
205               $strReq = 'SELECT count(RI.rateit_id) ';
206          else {
207               $strReq =
208               'SELECT DISTINCT '.
209               'SUM(RI.rateit_note / RI.rateit_quotient) as rateit_sum, '.
210               'MAX(RI.rateit_note / RI.rateit_quotient) as rateit_max, '.
211               'MIN(RI.rateit_note / RI.rateit_quotient) as rateit_min, '.
212               '(SUM(RI.rateit_note / RI.rateit_quotient) / COUNT(RI.rateit_note)) as rateit_avg, ';
213
214               if (!empty($params['columns']) && is_array($params['columns'])) 
215                    $strReq .= implode(', ',$params['columns']).', ';
216
217               $strReq .= 
218               'COUNT(RI.rateit_id) as rateit_total ';
219          }
220
221          $strReq .=
222          'FROM '.$this->table.' RI ';
223         
224          if (!empty($params['from']))
225               $strReq .= $params['from'].' ';
226
227          $strReq .=
228          " WHERE RI.blog_id = '".$this->core->con->escape($this->core->blog->id)."' ";
229
230          # rate type
231          if (isset($params['rateit_type'])) {
232
233               if (is_array($params['rateit_type']) && !empty($params['rateit_type']))
234                    $strReq .= 'AND RI.rateit_type '.$this->core->con->in($params['rateit_type']);
235               elseif ($params['rateit_type'] != '')
236                    $strReq .= "AND RI.rateit_type = '".$this->core->con->escape($params['rateit_type'])."' ";
237          } else
238               $strReq .= "AND RI.rateit_type = 'post' ";
239
240          # rate id
241          if (!empty($params['rateit_id'])) {
242
243               if (is_array($params['rateit_id']))
244                    array_walk($params['rateit_id'],create_function('&$v,$k','if($v!==null){$v=(integer)$v;}'));
245               else
246                    $params['rateit_id'] = array((integer) $params['rateit_id']);
247
248               $strReq .= 'AND RI.rateit_id '.$this->core->con->in($params['rateit_id']);
249          }
250
251          # rate ip
252          if (!empty($params['rateit_ip'])) {
253
254               if (is_array($params['rateit_ip']))
255                    array_walk($params['rateit_ip'],create_function('&$v,$k','if($v!==null){$v=(integer)$v;}'));
256               else
257                    $params['rateit_ip'] = array((integer) $params['rateit_ip']);
258
259               $strReq .= 'AND RI.rateit_ip '.$this->core->con->in($params['rateit_ip']);
260          }
261
262          if (!empty($params['sql']))
263               $strReq .= $params['sql'].' ';
264
265          if (!$count_only) {
266               $strReq .= 'GROUP BY RI.rateit_id ';
267               if (!empty($params['groups']) && is_array($params['groups'])) 
268                    $strReq .= ', '.implode(', ',$params['groups']).' ';
269
270               if (!empty($params['order']))
271                    $strReq .= 'ORDER BY '.$this->core->con->escape($params['order']).' ';
272               else
273                    $strReq .= 'ORDER BY rateit_time DESC ';
274          }
275
276          if (!$count_only && !empty($params['limit']))
277               $strReq .= $this->core->con->limit($params['limit']);
278
279          $rs = $this->core->con->select($strReq);
280
281          # --BEHAVIOR-- rateitGetRates
282          $this->core->callBehavior('rateitGetRates',$rs);
283
284          return $rs;
285     }
286
287     public function getDetails($type=null,$id=null,$ip=null)
288     {
289          $req=
290               'SELECT rateit_id,rateit_type,rateit_note,rateit_quotient,rateit_ip,rateit_time '.
291               'FROM '.$this->table.' WHERE blog_id=\''.$this->core->blog->id.'\' ';
292          if ($type!=null)
293               $req .= 'AND rateit_type=\''.$this->core->con->escape($type).'\' ';
294          if ($id!=null)
295               $req .= 'AND rateit_id=\''.$this->core->con->escape($id).'\' ';
296          if ($ip!=null)
297               $req .= 'AND rateit_ip=\''.$this->core->con->escape($ip).'\' ';
298
299          $rs = $this->core->con->select($req);
300          $rs->toStatic();
301
302          return $rs;
303     }
304}
305
306?>
Note: See TracBrowser for help on using the repository browser.

Sites map