1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* ExtGallery Class Manager |
4
|
|
|
* |
5
|
|
|
* You may not change or alter any portion of this comment or credits |
6
|
|
|
* of supporting developers from this source code or any supporting source code |
7
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
8
|
|
|
* This program is distributed in the hope that it will be useful, |
9
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
10
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
11
|
|
|
* |
12
|
|
|
* @copyright {@link http://xoops.org/ XOOPS Project} |
13
|
|
|
* @license GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html) |
14
|
|
|
* @author Zoullou (http://www.zoullou.net) |
15
|
|
|
* @package ExtGallery |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined'); |
|
|
|
|
19
|
|
|
|
20
|
|
|
include_once __DIR__ . '/ExtgalleryPersistableObjectHandler.php'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class ExtgalleryPublicRating |
24
|
|
|
*/ |
25
|
|
|
class ExtgalleryPublicRating extends XoopsObject |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
public $externalKey = array(); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* ExtgalleryPublicRating constructor. |
31
|
|
|
*/ |
32
|
|
|
public function __construct() |
33
|
|
|
{ |
34
|
|
|
parent::__construct(); |
35
|
|
|
$this->initVar('rating_id', XOBJ_DTYPE_INT, 0, false); |
36
|
|
|
$this->initVar('photo_id', XOBJ_DTYPE_INT, 0, false); |
37
|
|
|
$this->initVar('uid', XOBJ_DTYPE_INT, 0, false); |
38
|
|
|
$this->initVar('rating_rate', XOBJ_DTYPE_INT, 0, false); |
39
|
|
|
|
40
|
|
|
$this->externalKey['photo_id'] = array( |
41
|
|
|
'className' => 'publicphoto', |
42
|
|
|
'getMethodeName' => 'getPhoto', |
43
|
|
|
'keyName' => 'photo', |
44
|
|
|
'core' => false |
45
|
|
|
); |
46
|
|
|
$this->externalKey['uid'] = array( |
47
|
|
|
'className' => 'user', |
48
|
|
|
'getMethodeName' => 'get', |
49
|
|
|
'keyName' => 'user', |
50
|
|
|
'core' => true |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param $key |
56
|
|
|
* |
57
|
|
|
* @return mixed |
58
|
|
|
*/ |
59
|
|
|
public function getExternalKey($key) |
60
|
|
|
{ |
61
|
|
|
return $this->externalKey[$key]; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Class ExtgalleryPublicRatingHandler |
67
|
|
|
*/ |
68
|
|
|
class ExtgalleryPublicRatingHandler extends ExtgalleryPersistableObjectHandler |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
/** |
71
|
|
|
* @param XoopsDatabase $db |
72
|
|
|
*/ |
73
|
|
|
public function __construct(XoopsDatabase $db) |
74
|
|
|
{ |
75
|
|
|
parent::__construct($db, 'extgallery_publicrating', 'ExtgalleryPublicRating', 'rating_id'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param $photoId |
80
|
|
|
* @param $rating |
81
|
|
|
* |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function rate($photoId, $rating) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
/** @var ExtgalleryPublicPhotoHandler $photoHandler */ |
87
|
|
|
$photoHandler = xoops_getModuleHandler('publicphoto', 'extgallery'); |
88
|
|
|
|
89
|
|
|
$userId = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0; |
90
|
|
|
$rate = $this->create(); |
91
|
|
|
$rate->assignVar('photo_id', $photoId); |
92
|
|
|
$rate->assignVar('uid', $userId); |
93
|
|
|
$rate->assignVar('rating_rate', $rating); |
94
|
|
|
|
95
|
|
|
if ($this->_haveRated($rate)) { |
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (!$this->insert($rate, true)) { |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $photoHandler->updateNbRating($photoId); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param $photoId |
108
|
|
|
* |
109
|
|
|
* @return float |
110
|
|
|
*/ |
111
|
|
|
public function getRate($photoId) |
112
|
|
|
{ |
113
|
|
|
$criteria = new Criteria('photo_id', $photoId); |
114
|
|
|
$avg = $this->getAvg($criteria, 'rating_rate'); |
115
|
|
|
|
116
|
|
|
return round($avg); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param $rate |
121
|
|
|
* |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
public function _haveRated(&$rate) |
125
|
|
|
{ |
126
|
|
|
// If the user is annonymous |
127
|
|
|
if ($rate->getVar('uid') == 0) { |
128
|
|
|
return false; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$criteria = new CriteriaCompo(); |
132
|
|
|
$criteria->add(new Criteria('photo_id', $rate->getVar('photo_id'))); |
133
|
|
|
$criteria->add(new Criteria('uid', $rate->getVar('uid'))); |
134
|
|
|
|
135
|
|
|
if ($this->getCount($criteria) > 0) { |
136
|
|
|
return true; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.