|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
*@author nicolaas [at] sunnysideup up .co .nz |
|
5
|
|
|
* <% loop $PageRatings %> |
|
6
|
|
|
* |
|
7
|
|
|
* <% end_loop %> |
|
8
|
|
|
* |
|
9
|
|
|
**/ |
|
10
|
|
|
|
|
11
|
|
|
class PageRaterExtension extends DataExtension |
|
|
|
|
|
|
12
|
|
|
{ |
|
13
|
|
|
private static $has_many = array( |
|
|
|
|
|
|
14
|
|
|
'PageRatings' => 'PageRating' |
|
15
|
|
|
); |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* add the default rating to each page ... |
|
19
|
|
|
* @var boolean |
|
20
|
|
|
*/ |
|
21
|
|
|
private static $add_default_rating = false; |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var boolean |
|
26
|
|
|
*/ |
|
27
|
|
|
private static $number_of_default_records_to_be_added = 5; |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
public function updateCMSFields(FieldList $fields) |
|
30
|
|
|
{ |
|
31
|
|
|
if ($this->owner->PageRatings() && $this->owner->PageRatings()->count()) { |
|
32
|
|
|
$fields->addFieldToTab( |
|
33
|
|
|
"Root.Ratings", |
|
34
|
|
|
GridField::create( |
|
35
|
|
|
"PageRatings", |
|
36
|
|
|
Injector::inst()->get("PageRating")->plural_name(), |
|
37
|
|
|
$this->owner->PageRatings(), |
|
38
|
|
|
GridFieldConfig_RecordViewer::create() |
|
39
|
|
|
) |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
public function requireDefaultRecords() |
|
46
|
|
|
{ |
|
47
|
|
|
parent::requireDefaultRecords(); |
|
48
|
|
|
$step = 50; |
|
49
|
|
|
if (Config::inst()->get("PageRaterExtension", "add_default_rating")) { |
|
50
|
|
|
for ($i = 0; $i < 1000000; $i = $i + $step) { |
|
|
|
|
|
|
51
|
|
|
$pages = SiteTree::get() |
|
52
|
|
|
->leftJoin("PageRating", "\"PageRating\".\"ParentID\" = \"SiteTree\".\"ID\"") |
|
53
|
|
|
->where("\"PageRating\".\"ID\" IS NULL") |
|
54
|
|
|
->limit($step, $i); |
|
55
|
|
|
|
|
56
|
|
|
if ($pages->count()) { |
|
57
|
|
|
foreach ($pages as $page) { |
|
58
|
|
|
$count = 0; |
|
59
|
|
|
$max = PageRating::get_number_of_stars(); |
|
60
|
|
|
$goingBackTo = ($max / rand(1, $max)) - 1; |
|
61
|
|
|
$stepsBack = $max - $goingBackTo; |
|
62
|
|
|
$ratings = Config::inst()->get("PageRaterExtension", "number_of_default_records_to_be_added") / $stepsBack; |
|
63
|
|
|
for ($i = 1; $i <= $ratings; $i++) { |
|
64
|
|
|
for ($j = $max; $j > $goingBackTo; $j--) { |
|
65
|
|
|
$PageRating = new PageRating(); |
|
66
|
|
|
$PageRating->Rating = round(rand(1, $j)); |
|
|
|
|
|
|
67
|
|
|
$PageRating->IsDefault = 1; |
|
|
|
|
|
|
68
|
|
|
$PageRating->ParentID = $page->ID; |
|
|
|
|
|
|
69
|
|
|
$PageRating->write(); |
|
70
|
|
|
$count++; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
DB::alteration_message("Created Initial Ratings for Page with title ".$page->Title.". Ratings created: $count", "created"); |
|
74
|
|
|
} |
|
75
|
|
|
} else { |
|
76
|
|
|
$i = 1000000; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* return the average rating... |
|
85
|
|
|
* @return Double |
|
86
|
|
|
*/ |
|
87
|
|
|
public function StarRating() |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->getStarRating(); |
|
90
|
|
|
} |
|
91
|
|
|
public function getStarRating() |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
|
|
$ratings = $this->owner->PageRatingResults(); |
|
94
|
|
|
$rating = 0; |
|
95
|
|
|
if ($ratings->Count() == 1) { |
|
96
|
|
|
foreach ($ratings as $ratingItem) { |
|
97
|
|
|
$rating = $ratingItem->Stars; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
return $rating; |
|
101
|
|
|
} |
|
102
|
|
|
/** |
|
103
|
|
|
* |
|
104
|
|
|
* @return int |
|
105
|
|
|
*/ |
|
106
|
|
|
public function NumberOfPageRatings() |
|
107
|
|
|
{ |
|
108
|
|
|
$doSet = new ArrayList(); |
|
|
|
|
|
|
109
|
|
|
$sqlQuery = new SQLQuery(); |
|
|
|
|
|
|
110
|
|
|
$sqlQuery->setSelect("COUNT(\"PageRating\".\"Rating\") RatingCount"); |
|
111
|
|
|
$sqlQuery->setFrom("\"PageRating\" "); |
|
112
|
|
View Code Duplication |
if ($this->onlyShowApprovedPageRatings()) { |
|
|
|
|
|
|
113
|
|
|
$sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID." AND \"PageRating\".\"IsApproved\" = 1"); |
|
114
|
|
|
} else { |
|
115
|
|
|
$sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID.""); |
|
116
|
|
|
} |
|
117
|
|
|
$sqlQuery->setOrderBy("RatingCount ASC"); |
|
118
|
|
|
$sqlQuery->setGroupBy("\"ParentID\""); |
|
119
|
|
|
$sqlQuery->setLimit(1); |
|
120
|
|
|
$data = $sqlQuery->execute(); |
|
121
|
|
|
if ($data) { |
|
122
|
|
|
foreach ($data as $record) { |
|
123
|
|
|
return $record["RatingCount"]; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
return 0; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* rating for this page ... |
|
132
|
|
|
* @return ArrayList |
|
133
|
|
|
*/ |
|
134
|
|
|
public function PageRatingResults() |
|
135
|
|
|
{ |
|
136
|
|
|
$sqlQuery = new SQLQuery(); |
|
|
|
|
|
|
137
|
|
|
$sqlQuery->setSelect("AVG(\"PageRating\".\"Rating\") RatingAverage, ParentID"); |
|
138
|
|
|
$sqlQuery->setFrom("\"PageRating\" "); |
|
139
|
|
View Code Duplication |
if ($this->onlyShowApprovedPageRatings()) { |
|
|
|
|
|
|
140
|
|
|
$sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID." AND \"PageRating\".\"IsApproved\" = 1"); |
|
141
|
|
|
} else { |
|
142
|
|
|
$sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID.""); |
|
143
|
|
|
} |
|
144
|
|
|
$sqlQuery->setOrderBy("RatingAverage DESC"); |
|
145
|
|
|
$sqlQuery->setGroupby("\"ParentID\""); |
|
146
|
|
|
$sqlQuery->setLimit(1); |
|
147
|
|
|
return $this->turnPageRaterSQLIntoArrayList($sqlQuery, "PageRatingResults"); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* rating of this page by this user ... |
|
152
|
|
|
* @return ArrayList |
|
153
|
|
|
*/ |
|
154
|
|
|
public function CurrentUserRating() |
|
155
|
|
|
{ |
|
156
|
|
|
$sqlQuery = new SQLQuery(); |
|
|
|
|
|
|
157
|
|
|
$sqlQuery->setSelect("AVG(\"PageRating\".\"Rating\") RatingAverage, ParentID"); |
|
158
|
|
|
$sqlQuery->setFrom("\"PageRating\" "); |
|
159
|
|
|
if ($this->onlyShowApprovedPageRatings()) { |
|
160
|
|
|
$sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID." AND \"PageRating\".\"ID\" = '".Session::get('PageRated'.$this->owner->ID)."' AND \"PageRating\".\"IsApproved\" = 1"); |
|
161
|
|
|
} else { |
|
162
|
|
|
$sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID." AND \"PageRating\".\"ID\" = '".Session::get('PageRated'.$this->owner->ID)."'"); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$sqlQuery->setOrderBy("RatingAverage DESC"); |
|
166
|
|
|
$sqlQuery->setGroupby("\"ParentID\""); |
|
167
|
|
|
$sqlQuery->setLimit(1); |
|
168
|
|
|
return $this->turnPageRaterSQLIntoArrayList($sqlQuery, "CurrentUserRating"); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param $data $sqlQuery | DataList |
|
|
|
|
|
|
174
|
|
|
* @param string $method |
|
175
|
|
|
* |
|
176
|
|
|
* @return ArrayList |
|
177
|
|
|
*/ |
|
178
|
|
|
protected function turnPageRaterSQLIntoArrayList($data, $method = "unknown") |
|
179
|
|
|
{ |
|
180
|
|
|
if ($data instanceof SQLQuery) { |
|
181
|
|
|
$data = $data->execute(); |
|
182
|
|
|
} |
|
183
|
|
|
$al = new ArrayList(); |
|
184
|
|
|
if ($data) { |
|
185
|
|
|
foreach ($data as $record) { |
|
186
|
|
|
if ($record instanceof PageRating) { |
|
187
|
|
|
$record->Method = $method; |
|
|
|
|
|
|
188
|
|
|
//do nothing |
|
189
|
|
|
} else { |
|
190
|
|
|
$score = $record["RatingAverage"]; |
|
191
|
|
|
$parentID = $record["ParentID"]; |
|
192
|
|
|
$record = PageRating::get_star_details_as_array_data($score, $parentID, $method); |
|
193
|
|
|
} |
|
194
|
|
|
$al->push($record); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
return $al; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
protected function onlyShowApprovedPageRatings() |
|
201
|
|
|
{ |
|
202
|
|
|
return Config::inst()->get("PageRaterExtension_Controller", "only_show_approved"); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
} |
|
206
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.