Dotclear

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

Revision 1301, 8.2 KB checked in by JcDenis, 14 years ago (diff)

rateIt 0.4:

  • changed database rateit_id to allow text id
  • fixed some js bugs
  • fixed translation
  • Added option to edit thank msg
  • Added cookie and/or ip option
  • Added images choice
  • Added some options to widget
  • Added extension ability
  • Added import/export support
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($p=array(),$count_only=false)
155     {
156          if (!isset($p['columns'])) $p['columns'] = array();
157          $p['columns'][] = 'SUM(RI.rateit_note / RI.rateit_quotient) as rateit_sum';
158          $p['columns'][] = 'MAX(RI.rateit_note / RI.rateit_quotient) as rateit_max';
159          $p['columns'][] = 'MIN(RI.rateit_note / RI.rateit_quotient) as rateit_min';
160          $p['columns'][] = '(SUM(RI.rateit_note / RI.rateit_quotient) / COUNT(RI.rateit_note)) as rateit_avg';
161          $p['columns'][] = 'COUNT(RI.rateit_note) as rateit_total';
162
163          if (!isset($p['from'])) $p['from'] = '';
164               $p['from'] .= ' LEFT OUTER JOIN '.$this->table.' RI ON P.post_id = RI.rateit_id ';
165
166          if (!isset($p['sql'])) $p['sql'] = '';
167
168          if (!empty($p['rateit_type'])) {
169               $p['sql'] .= "AND RI.rateit_type = '".$this->core->con->escape($p['rateit_type'])."' ";
170               unset($p['rateit_type']);
171          }
172
173          if (!$count_only)
174               $p['sql'] .= 'GROUP BY RI.rateit_id ';
175
176          return $this->core->blog->getPosts($p,$count_only);
177     }
178
179     public function getRates($params,$count_only=false)
180     {
181          if ($count_only)
182               $strReq = 'SELECT count(RI.rateit_id) ';
183          else {
184               $strReq =
185               'SELECT '.
186               'SUM(RI.rateit_note / RI.rateit_quotient) as rateit_sum, '.
187               'MAX(RI.rateit_note / RI.rateit_quotient) as rateit_max, '.
188               'MIN(RI.rateit_note / RI.rateit_quotient) as rateit_min, '.
189               '(SUM(RI.rateit_note / RI.rateit_quotient) / COUNT(RI.rateit_note)) as rateit_avg, ';
190
191               if (!empty($params['columns']) && is_array($params['columns'])) 
192                    $strReq .= implode(', ',$params['columns']).', ';
193
194               $strReq .= 
195               'COUNT(RI.rateit_note) as rateit_total ';
196          }
197
198          $strReq .=
199          'FROM '.$this->table.' RI ';
200         
201          if (!empty($params['from']))
202               $strReq .= $params['from'].' ';
203
204          $strReq .=
205          " WHERE RI.blog_id = '".$this->core->con->escape($this->core->blog->id)."' ";
206
207          # rate type
208          if (isset($params['rateit_type'])) {
209
210               if (is_array($params['rateit_type']) && !empty($params['rateit_type']))
211                    $strReq .= 'AND RI.rateit_type '.$this->core->con->in($params['rateit_type']);
212               elseif ($params['rateit_type'] != '')
213                    $strReq .= "AND RI.rateit_type = '".$this->core->con->escape($params['rateit_type'])."' ";
214          } else
215               $strReq .= "AND RI.rateit_type = 'post' ";
216
217          # rate id
218          if (!empty($params['rateit_id'])) {
219
220               if (is_array($params['rateit_id']))
221                    array_walk($params['rateit_id'],create_function('&$v,$k','if($v!==null){$v=(integer)$v;}'));
222               else
223                    $params['rateit_id'] = array((integer) $params['rateit_id']);
224
225               $strReq .= 'AND RI.rateit_id '.$this->core->con->in($params['rateit_id']);
226          }
227
228          # rate ip
229          if (!empty($params['rateit_ip'])) {
230
231               if (is_array($params['rateit_ip']))
232                    array_walk($params['rateit_ip'],create_function('&$v,$k','if($v!==null){$v=(integer)$v;}'));
233               else
234                    $params['rateit_ip'] = array((integer) $params['rateit_ip']);
235
236               $strReq .= 'AND RI.rateit_ip '.$this->core->con->in($params['rateit_ip']);
237          }
238
239          if (!empty($params['sql']))
240               $strReq .= $params['sql'].' ';
241
242          if (!$count_only) {
243               $strReq .= 'GROUP BY RI.rateit_id ';
244
245               if (!empty($params['order']))
246                    $strReq .= 'ORDER BY '.$this->core->con->escape($params['order']).' ';
247               else
248                    $strReq .= 'ORDER BY rateit_time DESC ';
249          }
250
251          if (!$count_only && !empty($params['limit']))
252               $strReq .= $this->core->con->limit($params['limit']);
253
254          $rs = $this->core->con->select($strReq);
255
256          # --BEHAVIOR-- rateitGetRates
257          $this->core->callBehavior('rateitGetRates',$rs);
258
259          return $rs;
260     }
261
262     public function getDetails($type=null,$id=null,$ip=null)
263     {
264          $req=
265               'SELECT rateit_id,rateit_type,rateit_note,rateit_quotient,rateit_ip,rateit_time '.
266               'FROM '.$this->table.' WHERE blog_id=\''.$this->core->blog->id.'\' ';
267          if ($type!=null)
268               $req .= 'AND rateit_type=\''.$this->core->con->escape($type).'\' ';
269          if ($id!=null)
270               $req .= 'AND rateit_id=\''.$this->core->con->escape($id).'\' ';
271          if ($ip!=null)
272               $req .= 'AND rateit_ip=\''.$this->core->con->escape($ip).'\' ';
273
274          $rs = $this->core->con->select($req);
275          $rs->toStatic();
276
277          return $rs;
278     }
279}
280
281?>
Note: See TracBrowser for help on using the repository browser.

Sites map