Dotclear

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

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

rateIt 0.9.7:

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

Sites map