-
Notifications
You must be signed in to change notification settings - Fork 15
/
ezsrratingdataobject.php
423 lines (390 loc) · 15.6 KB
/
ezsrratingdataobject.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
<?php
//
// Definition of ezsrRatingObject class
//
// SOFTWARE NAME: eZ Star Rating
// SOFTWARE RELEASE: 2.x
// COPYRIGHT NOTICE: Copyright (C) 2008 Bruce Morrison, 2009-2014 eZ Systems AS
// SOFTWARE LICENSE: GNU General Public License v2.0
// NOTICE: >
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 2.0 of the GNU General
// Public License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of version 2.0 of the GNU General
// Public License along with this program; if not, write to the Free
// Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
// MA 02110-1301, USA.
//
//
class ezsrRatingDataObject extends eZPersistentObject
{
/**
* Used by {@link ezsrRatingDataObject::userHasRated()} to cache value.
*
* @var bool $currentUserHasRated
*/
protected $currentUserHasRated = null;
/**
* Construct, shouldn't be called directly
* Use {@link ezsrRatingDataObject::create()} to create new objects.
*
* @access protected
* @param array $row
*/
public function __construct( $row )
{
$this->eZPersistentObject( $row );
}
static function definition()
{
static $def = array( 'fields' => array(
'id' => array(
'name' => 'id',
'datatype' => 'integer',
'default' => 0,
'required' => true ),
'created_at' => array(
'name' => 'created_at',
'datatype' => 'integer',
'default' => 0,
'required' => true ),
'user_id' => array(
'name' => 'user_id',
'datatype' => 'integer',
'default' => 0,
'required' => true,
'foreign_class' => 'eZUser',
'foreign_attribute' => 'contentobject_id',
'multiplicity' => '0..1' ),
'session_key' => array(
'name' => 'session_key',
'datatype' => 'string',
'default' => '',
'required' => true ),
'rating' => array(
'name' => 'rating',
'datatype' => 'integer',
'default' => 0,
'required' => true ),
'contentobject_id' => array(
'name' => 'contentobject_id',
'datatype' => 'integer',
'default' => 0,
'required' => true,
'foreign_class' => 'eZContentObject',
'foreign_attribute' => 'id',
'multiplicity' => '1..*' ),
'contentobject_attribute_id' => array(
'name' => 'contentobject_attribute_id',
'datatype' => 'integer',
'default' => 0,
'required' => true,
'foreign_class' => 'eZContentObjectAttribute',
'foreign_attribute' => 'id',
'multiplicity' => '1..*' ),
),
'keys' => array( 'id' ),
'function_attributes' => array(
'average_rating' => 'getAverageRating'
),
'increment_key' => 'id',
'class_name' => 'ezsrRatingDataObject',
'name' => 'ezstarrating_data' );
return $def;
}
/**
* Fetch Average Rating
* Will create a unstored one if none could be fetched ( it will have rating_avrage and rating_count of 0 )
*
* @return ezsrRatingObject
*/
function getAverageRating()
{
$avgRating = ezsrRatingObject::fetchByObjectId( $this->attribute('contentobject_id'), $this->attribute('contentobject_attribute_id') );
if ( !$avgRating instanceof ezsrRatingObject )
{
$avgRating = ezsrRatingObject::create( array('contentobject_id' => $this->attribute('contentobject_id'),
'contentobject_attribute_id' => $this->attribute('contentobject_attribute_id') ) );
}
return $avgRating;
}
/**
* Figgure out if current user has rated, since eZ Publish changes session id as of 4.1
* on login / logout, a couple of things needs to be checked.
* 1. Session variable 'ezsrRatedAttributeIdList' for list of attribute_id's
* 2a. (annonymus user) check against session key
* 2b. (logged in user) check against user id
*
* @param bool $returnRatedObject Return object if user has rated and [eZStarRating]AllowChangeRating=enabled
* @return bool|ezsrRatingDataObject
*/
function userHasRated( $returnRatedObject = false )
{
if ( $this->currentUserHasRated === null )
{
$ini = eZINI::instance();
$useUserSession = $ini->variable( 'eZStarRating', 'UseUserSession' ) === 'enabled';
$http = eZHTTPTool::instance();
if ( $http->hasSessionVariable( 'ezsrRatedAttributeIdList', $useUserSession ) )
$attributeIdList = explode( ',', $http->sessionVariable('ezsrRatedAttributeIdList') );
else
$attributeIdList = array();
$contentobjectAttributeId = $this->attribute('contentobject_attribute_id');
if ( in_array( $contentobjectAttributeId, $attributeIdList ) && $useUserSession )
{
$this->currentUserHasRated = true;
}
$returnRatedObject = $returnRatedObject && $ini->variable( 'eZStarRating', 'AllowChangeRating' ) === 'enabled';
if ( $this->currentUserHasRated === null || $returnRatedObject )
{
$sessionKey = $this->attribute('session_key');
$userId = $this->attribute('user_id');
if ( $userId == eZUser::anonymousId() )
{
$cond = array( 'user_id' => $userId, // for table index
'session_key' => $sessionKey,
'contentobject_id' => $this->attribute('contentobject_id'), // for table index
'contentobject_attribute_id' => $contentobjectAttributeId );
}
else
{
$cond = array( 'user_id' => $userId,
'contentobject_id' => $this->attribute('contentobject_id'), // for table index
'contentobject_attribute_id' => $contentobjectAttributeId );
}
if ( $returnRatedObject )
{
$this->currentUserHasRated = eZPersistentObject::fetchObject( self::definition(), null, $cond );
if ( $this->currentUserHasRated === null)
$this->currentUserHasRated = false;
}
else
{
$this->currentUserHasRated = eZPersistentObject::count( self::definition(), $cond, 'id' ) != 0;
}
}
}
return $this->currentUserHasRated;
}
/**
* Override store function to add some custom logic for setting create time and
* store contentobject_attribute_id in session to avoid several ratings from same user.
*
* @param array $fieldFilters
*/
function store( $fieldFilters = null )
{
if (
$this->attribute( 'user_id' ) == eZUser::currentUserID()
&& eZINI::instance()->variable( 'eZStarRating', 'UseUserSession' ) === 'enabled'
)
{
// Store attribute id in session to avoid multiple ratings by same user even if he logs out (gets new session key)
$http = eZHTTPTool::instance();
$attributeIdList = $this->attribute( 'contentobject_attribute_id' );
if ( $http->hasSessionVariable('ezsrRatedAttributeIdList') )
{
$attributeIdList = $http->sessionVariable('ezsrRatedAttributeIdList') . ',' . $attributeIdList;
}
$http->setSessionVariable('ezsrRatedAttributeIdList', $attributeIdList );
}
eZPersistentObject::store( $fieldFilters );
}
/**
* Remove rating data by content object id and optionally attribute id.
*
* @param int $contentobjectID
* @param int $contentobjectAttributeId
*/
static function removeByObjectId( $contentobjectID, $contentobjectAttributeId = null )
{
$cond = array( 'contentobject_id' => $contentobjectID );
if ( $contentobjectAttributeId !== null )
{
$cond['contentobject_attribute_id'] = $contentobjectAttributeId;
}
eZPersistentObject::removeObject( self::definition(), $cond );
}
/**
* Fetch rating by rating id!
*
* @param int $id
* @return null|ezsrRatingDataObject
*/
static function fetch( $id )
{
$cond = array( 'id' => $id );
$return = eZPersistentObject::fetchObject( self::definition(), null, $cond );
return $return;
}
/**
* Fetch rating data by content object id and optionally attribute id!
*
* @param int $contentobjectID
* @param int $contentobjectAttributeId
* @return null|ezsrRatingDataObject
*/
static function fetchByObjectId( $contentobjectID, $contentobjectAttributeId = null )
{
$cond = array( 'contentobject_id' => $contentobjectID );
if ( $contentobjectAttributeId !== null )
{
$cond['contentobject_attribute_id'] = $contentobjectAttributeId;
}
$return = eZPersistentObject::fetchObjectList( self::definition(), null, $cond );
return $return;
}
/**
* Create a ezsrRatingDataObject and store it.
* Note: No access checks or input validation is done other then on rating
*
* @param int $contentobjectId
* @param int $contentobjectAttributeId
* @param int $rate (above 0 and bellow or equal 5)
* @param bool $onlyStoreIfUserNotAlreadyRated
* @return null|ezsrRatingDataObject
*/
static function rate( $contentobjectId, $contentobjectAttributeId, $rate )
{
$rating = null;
if ( is_numeric( $contentobjectId ) &&
is_numeric( $contentobjectAttributeId ) &&
is_numeric( $rate) &&
$rate <= 5 && $rate > 0 )
{
$row = array ('contentobject_attribute_id' => $contentobjectAttributeId,
'contentobject_id' => $contentobjectId,
'rating' => $rate);
$rating = self::create( $row );
if ( !$rating->userHasRated() )
{
$rating->store();
$avgRating = $rating->getAverageRating();
$avgRating->updateFromRatingData();
$avgRating->store();
// clear the cache for all nodes associated with this object
eZContentCacheManager::clearContentCacheIfNeeded( $contentobjectId, true, false );
}
}
return $rating;
}
/**
* Create a ezsrRatingDataObject by definition data (but do not store it, thats up to you!)
* NOTE: you have to provide the following attributes:
* contentobject_id
* contentobject_attribute_id
* rating (this is only requried if you plan to store the object)
*
* @param array $row
* @return ezsrRatingDataObject
*/
static function create( $row = array() )
{
if ( !isset( $row['session_key'] ) )
{
$http = eZHTTPTool::instance();
if (
eZINI::instance()->variable( 'eZStarRating', 'UseUserSession' ) === 'enabled'
&& !eZSession::hasStarted()
)
{
// Creates a session for anonymous
eZSession::start();
}
$row['session_key'] = $http->sessionID();
}
if ( !isset( $row['user_id'] ) )
{
$row['user_id'] = eZUser::currentUserID();
}
if ( !isset( $row['created_at'] ) )
{
$row['created_at'] = time();
}
if ( !isset( $row['contentobject_id'] ) )
{
eZDebug::writeError( 'Missing \'contentobject_id\' parameter!', __METHOD__ );
}
if ( !isset( $row['contentobject_attribute_id'] ) )
{
eZDebug::writeError( 'Missing \'contentobject_attribute_id\' parameter!', __METHOD__ );
}
$object = new self( $row );
return $object;
}
/**
* Fetch rating ( avrage + total + raw rating data ) by conditions
*
* @param array $params (see inline doc for possible conditions)
* @return array Array of rating data
*/
static function fetchByConds( $params )
{
/**
* Conditions possible in $param (array hash):
* int 'contentobject_id'
* int 'contentobject_attribute_id'
* int 'user_id'
* string 'session_key'
* bool 'as_object' By default: true
*
* Conditions can be combined as you wish,
*/
$conds = array();
$limit = null;
$sorts = array();
$asObject = isset( $params['as_object'] ) ? $params['as_object'] : true;
if ( isset( $params['contentobject_id'] ) )
{
$conds['contentobject_id'] = $params['contentobject_id'];
}
else if ( isset( $params['object_id'] ) )// Alias
{
$conds['contentobject_id'] = $params['object_id'];
}
if ( isset( $params['contentobject_attribute_id'] ) )
{
if ( !isset( $conds['contentobject_id'] ) ) $conds['contentobject_id'] = array( '!=', 0 );// to make sure index is used
$conds['contentobject_attribute_id'] = $params['contentobject_attribute_id'];
}
if ( isset( $params['user_id'] ) )
{
$conds['user_id'] = $params['user_id'];
}
if ( isset( $params['session_key'] ) )
{
if ( !isset( $conds['user_id'] ) ) $conds['user_id'] = array( '!=', 0 );// to make sure index is used
$conds['session_key'] = $params['session_key'];
}
if ( isset( $params['limit'] ) )
{
$limit = array( 'length' => $params['limit'], 'offset' => (isset( $params['offset'] ) ? $params['offset'] : 0 ) );
}
else if ( isset( $params['offset'] ) )
{
$limit = array( 'offset' => $params['offset'] );
}
if ( isset( $params['sort_by'] ) )
{
if ( isset( $params['sort_by'][1] ) && is_array( $params['sort_by'] ) )
$sorts = array( $params['sort_by'][0] => ( $params['sort_by'][1] ? 'asc' : 'desc' ) );
else
$sorts[ $params['sort_by'] ] = 'asc';
}
$rows = eZPersistentObject::fetchObjectList( self::definition(), null, $conds, $sorts, $limit, $asObject );
if ( $rows === null )
{
eZDebug::writeError( 'The ezstarrating table seems to be missing,
contact your administrator', __METHOD__ );
return false;
}
return $rows;
}
}
?>