Dotclear

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

Revision 1486, 9.6 KB checked in by JcDenis, 13 years ago (diff)

rateIt 0.9.6:

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

Sites map