1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use CMSFactory\assetManager; |
4
|
|
|
|
5
|
|
|
if (!defined('BASEPATH')) { |
6
|
|
|
exit('No direct script access allowed'); |
7
|
|
|
} |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Image CMS |
11
|
|
|
* |
12
|
|
|
* In oder to show "Star rating" type in template: |
13
|
|
|
* {$CI->load->module('star_rating')->show_star_rating()} |
14
|
|
|
* |
15
|
|
|
* If you want to show "Star rating" for product |
16
|
|
|
* {$CI->load->module('star_rating')->show_star_rating(SProducts $product)} |
17
|
|
|
* |
18
|
|
|
* More turn on autoload and url access. |
19
|
|
|
* |
20
|
|
|
* Star rating module |
21
|
|
|
* |
22
|
|
|
* @property Rating_model rating_model |
23
|
|
|
*/ |
24
|
|
|
class Star_rating extends MY_Controller |
25
|
|
|
{ |
|
|
|
|
26
|
|
|
|
27
|
|
|
private $new_votes = 0; |
28
|
|
|
|
29
|
|
|
private $new_rating = 0; |
30
|
|
|
|
31
|
|
|
private $list_for_show = ['main', 'category', 'brand', 'product', 'shop_category', 'page']; |
32
|
|
|
|
33
|
|
|
public function __construct() { |
34
|
|
|
|
35
|
|
|
parent::__construct(); |
36
|
|
|
$this->load->helper('path'); |
37
|
|
|
$this->load->model('rating_model'); |
38
|
|
|
$obj = new MY_Lang(); |
39
|
|
|
$obj->load('star_rating'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public static function adminAutoload() { |
43
|
|
|
|
44
|
|
|
parent::adminAutoload(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function autoload() { |
48
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Show star_rating |
53
|
|
|
* @param SProducts $item |
|
|
|
|
54
|
|
|
* @param bool $registerScript |
55
|
|
|
* @return $this|bool |
|
|
|
|
56
|
|
|
*/ |
57
|
|
|
public function show_star_rating($item = null, $registerScript = true) { |
58
|
|
|
$this->list_for_show = $this->rating_model->get_settings(); |
|
|
|
|
59
|
|
|
if ($this->list_for_show == null) { |
60
|
|
|
$this->list_for_show = []; |
61
|
|
|
} |
62
|
|
|
$id = $this->core->core_data['id']; |
63
|
|
|
$type = $this->core->core_data['data_type']; |
64
|
|
|
|
65
|
|
|
// product rating |
66
|
|
|
if ($item != null && $item instanceof SProducts) { |
|
|
|
|
67
|
|
|
if ($item->getRating() != null) { |
68
|
|
|
$rating_s = (int) $item->getRating() * 20; // rating in percent |
69
|
|
|
} else { |
70
|
|
|
$rating_s = 0; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$data = [ |
74
|
|
|
'id_type' => $item->getId(), |
75
|
|
|
'type' => 'product', |
76
|
|
|
'votes' => $item->getVotes(), |
77
|
|
|
'rating' => $rating_s |
78
|
|
|
]; |
79
|
|
|
$template = 'product_star_rating'; |
80
|
|
|
} else { |
81
|
|
|
if (in_array($type, array_keys($this->list_for_show))) { |
82
|
|
|
$rating = $this->rating_model->get_rating($id, $type); |
83
|
|
|
if ($rating->votes != 0) { |
84
|
|
|
$rating_s = $rating->rating / $rating->votes * 20; //rating in percent |
85
|
|
|
} else { |
86
|
|
|
$rating_s = 0; |
87
|
|
|
} |
88
|
|
|
$data = [ |
89
|
|
|
'id' => $rating->id, |
90
|
|
|
'type' => $rating->type, |
91
|
|
|
'votes' => $rating->votes, |
92
|
|
|
'rating' => $rating_s |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
$template = 'star_rating'; |
96
|
|
|
} else { |
97
|
|
|
$template = null; |
|
|
|
|
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
//Show template with prepared parameters |
103
|
|
|
if ($template !== null) { |
104
|
|
|
$renderTemplate = assetManager::create(); |
105
|
|
|
} |
106
|
|
|
$renderTemplate->setData($data) |
107
|
|
|
->registerStyle('style'); |
108
|
|
|
if ($registerScript) { |
109
|
|
|
$renderTemplate->registerScript('scripts'); |
110
|
|
|
} |
111
|
|
|
$renderTemplate->render($template, true); |
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Change rating for pages / product |
117
|
|
|
* @return string|null |
118
|
|
|
*/ |
119
|
|
|
public function ajax_rate() { |
120
|
|
|
|
121
|
|
|
$id = $this->input->post('cid'); |
122
|
|
|
$type = $this->input->post('type'); |
123
|
|
|
$rating = (int) $this->input->post('val'); |
124
|
|
|
|
125
|
|
|
if ($id != null && $type != null && !$this->session->userdata('voted_g' . $id . $type) == true) { |
126
|
|
|
//Check if rating exists |
127
|
|
|
$check = $this->rating_model->get_rating($id, $type); |
128
|
|
|
if ($check != null) { |
129
|
|
|
$this->new_votes = $check->votes + 1; |
130
|
|
|
$this->new_rating = $check->rating + $rating; |
131
|
|
|
$data = [ |
132
|
|
|
'votes' => $this->new_votes, |
133
|
|
|
'rating' => $this->new_rating |
134
|
|
|
]; |
135
|
|
|
$rating_res = $this->new_rating / $this->new_votes * 20; |
136
|
|
|
$votes_res = $this->new_votes; |
137
|
|
|
$this->rating_model->update_rating($id, $type, $data); |
138
|
|
|
} else { |
139
|
|
|
$data = [ |
140
|
|
|
'id_type' => $id, |
141
|
|
|
'type' => $type, |
142
|
|
|
'votes' => 1, |
143
|
|
|
'rating' => $rating |
144
|
|
|
]; |
145
|
|
|
$votes_res = 1; |
146
|
|
|
$rating_res = $rating * 20; |
147
|
|
|
$this->rating_model->insert_rating($data); |
148
|
|
|
} |
149
|
|
|
//Change rating for product |
150
|
|
|
if ($type == 'product') { |
151
|
|
|
if (SProductsQuery::create()->findPk($id) !== null) { |
152
|
|
|
$model = SProductsRatingQuery::create()->findPk($id); |
153
|
|
|
if ($model === null) { |
154
|
|
|
$model = new SProductsRating; |
|
|
|
|
155
|
|
|
$model->setProductId($id); |
156
|
|
|
} |
157
|
|
|
$rating_res = (($model->getRating() + $rating) / ($model->getVotes() + 1)) * 20; |
158
|
|
|
$votes_res = $model->getVotes() + 1; |
159
|
|
|
|
160
|
|
|
$model->setVotes($model->getVotes() + 1); |
161
|
|
|
$model->setRating($model->getRating() + $rating); |
162
|
|
|
$model->save(); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
//Save in session user's info |
166
|
|
|
$this->session->set_userdata('voted_g' . $id . $type, true); |
167
|
|
|
|
168
|
|
|
//If ajax request than return data for with new rating and votes |
169
|
|
|
if ($this->input->is_ajax_request()) { |
170
|
|
|
return json_encode( |
171
|
|
|
[ |
172
|
|
|
'rate' => "$rating_res", |
173
|
|
|
'votes' => "$votes_res" |
174
|
|
|
] |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
} else { |
178
|
|
|
return json_encode(['rate' => null]); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Install module |
184
|
|
|
*/ |
185
|
|
View Code Duplication |
public function _install() { |
186
|
|
|
|
187
|
|
|
$this->load->dbforge(); |
188
|
|
|
($this->dx_auth->is_admin()) OR exit; |
189
|
|
|
$fields = [ |
190
|
|
|
'id' => [ |
191
|
|
|
'type' => 'INT', |
192
|
|
|
'auto_increment' => TRUE |
193
|
|
|
], |
194
|
|
|
'id_type' => [ |
195
|
|
|
'type' => 'VARCHAR', |
196
|
|
|
'constraint' => '25', |
197
|
|
|
'null' => TRUE, |
198
|
|
|
], |
199
|
|
|
'type' => [ |
200
|
|
|
'type' => 'VARCHAR', |
201
|
|
|
'constraint' => '25', |
202
|
|
|
'null' => TRUE, |
203
|
|
|
], |
204
|
|
|
'votes' => [ |
205
|
|
|
'type' => 'INT', |
206
|
|
|
], |
207
|
|
|
'rating' => [ |
208
|
|
|
'type' => 'INT', |
209
|
|
|
], |
210
|
|
|
]; |
211
|
|
|
|
212
|
|
|
$this->dbforge->add_field($fields); |
213
|
|
|
$this->dbforge->add_key('id', TRUE); |
214
|
|
|
$this->dbforge->create_table('rating'); |
215
|
|
|
|
216
|
|
|
$this->db->where('name', 'star_rating'); |
217
|
|
|
$this->db->update('components', ['enabled' => 1]); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Deinstall module |
222
|
|
|
*/ |
223
|
|
|
public function _deinstall() { |
224
|
|
|
|
225
|
|
|
$this->load->dbforge(); |
226
|
|
|
($this->dx_auth->is_admin()) OR exit; |
227
|
|
|
$this->dbforge->drop_table('rating'); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
} |