These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | * @var boolean |
||
25 | */ |
||
26 | private static $number_of_default_records_to_be_added = 5; |
||
27 | |||
28 | public function updateCMSFields(FieldList $fields) |
||
29 | { |
||
30 | if ($this->owner->PageRatings() && $this->owner->PageRatings()->count()) { |
||
31 | $fields->addFieldToTab( |
||
32 | "Root.Ratings", |
||
33 | GridField::create( |
||
34 | "PageRatings", |
||
35 | Injector::inst()->get("PageRating")->plural_name(), |
||
36 | $this->owner->PageRatings(), |
||
37 | GridFieldConfig_RecordViewer::create() |
||
38 | ) |
||
39 | ); |
||
40 | } |
||
41 | } |
||
42 | |||
43 | |||
44 | public function requireDefaultRecords() |
||
45 | { |
||
46 | parent::requireDefaultRecords(); |
||
47 | $step = 50; |
||
48 | if (Config::inst()->get("PageRaterExtension", "add_default_rating")) { |
||
49 | for ($i = 0; $i < 1000000; $i = $i + $step) { |
||
50 | $pages = SiteTree::get() |
||
51 | ->leftJoin("PageRating", "\"PageRating\".\"ParentID\" = \"SiteTree\".\"ID\"") |
||
52 | ->where("\"PageRating\".\"ID\" IS NULL") |
||
53 | ->limit($step, $i); |
||
54 | |||
55 | if ($pages->count()) { |
||
56 | foreach ($pages as $page) { |
||
57 | $count = 0; |
||
58 | $max = PageRating::get_number_of_stars(); |
||
59 | $goingBackTo = ($max / rand(1, $max)) - 1; |
||
60 | $stepsBack = $max - $goingBackTo; |
||
61 | $ratings = Config::inst()->get("PageRaterExtension", "number_of_default_records_to_be_added") / $stepsBack; |
||
62 | for ($i = 1; $i <= $ratings; $i++) { |
||
63 | for ($j = $max; $j > $goingBackTo; $j--) { |
||
64 | $PageRating = new PageRating(); |
||
65 | $PageRating->Rating = round(rand(1, $j)); |
||
66 | $PageRating->IsDefault = 1; |
||
67 | $PageRating->ParentID = $page->ID; |
||
68 | $PageRating->write(); |
||
69 | $count++; |
||
70 | } |
||
71 | } |
||
72 | DB::alteration_message("Created Initial Ratings for Page with title ".$page->Title.". Ratings created: $count", "created"); |
||
73 | } |
||
74 | } else { |
||
75 | $i = 1000000; |
||
76 | } |
||
77 | } |
||
78 | } |
||
79 | } |
||
80 | } |
||
81 | |||
82 | class PageRaterExtension_Controller extends Extension |
||
83 | { |
||
84 | |||
85 | |||
86 | /** |
||
87 | * @var string |
||
88 | */ |
||
89 | private static $field_title = "Click on any star to rate:"; |
||
90 | |||
91 | /** |
||
92 | * @var string |
||
93 | */ |
||
94 | private static $field_right_title = "On a scale from 1 to 5, with 5 being the best"; |
||
95 | |||
96 | /** |
||
97 | * @var boolean |
||
98 | */ |
||
99 | private static $show_average_rating_in_rating_field = false; |
||
100 | |||
101 | /** |
||
102 | * @var boolean |
||
103 | */ |
||
104 | private static $only_show_approved = false; |
||
105 | |||
106 | private static $allowed_actions = array( |
||
107 | "PageRatingForm", |
||
108 | "rateagain", |
||
109 | "dopagerating", |
||
110 | "removedefaultpageratings", |
||
111 | "removeallpageratings" |
||
112 | ); |
||
113 | |||
114 | /** |
||
115 | * action to allow use to rate again... |
||
116 | */ |
||
117 | public function rateagain($request) |
||
0 ignored issues
–
show
|
|||
118 | { |
||
119 | $id = intval(Session::get('PageRated'.$this->owner->dataRecord->ID))-0; |
||
120 | $pageRating = PageRating::get()->byID($id); |
||
121 | if ($pageRating) { |
||
122 | $pageRating->delete(); |
||
123 | } |
||
124 | Session::set('PageRated'.$this->owner->dataRecord->ID, false); |
||
125 | Session::clear('PageRated'.$this->owner->dataRecord->ID); |
||
126 | return $this->owner->redirect($this->owner->Link()); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @return Form |
||
131 | */ |
||
132 | public function PageRatingForm() |
||
133 | { |
||
134 | Requirements::themedCSS('PageRater', "pagerater"); |
||
135 | if ($this->owner->PageHasBeenRatedByUser()) { |
||
136 | $ratingField = LiteralField::create("RatingFor".$this->owner->dataRecord->ID, $this->owner->renderWith("PageRaterAjaxReturn")); |
||
137 | $actions = FieldList::create(); |
||
138 | $requiredFields = null; |
||
139 | } else { |
||
140 | if (Config::inst()->get("PageRaterExtension_Controller", "show_average_rating_in_rating_field")) { |
||
141 | $defaultStart = $this->owner->getStarRating(); |
||
142 | } else { |
||
143 | $defaultStart = 0; |
||
144 | } |
||
145 | $ratingField = PageRaterStarField::create( |
||
146 | 'RatingFor'.$this->owner->dataRecord->ID, |
||
147 | Config::inst()->get("PageRaterExtension_Controller", "field_title"), |
||
148 | $defaultStart, |
||
149 | PageRating::get_number_of_stars() |
||
150 | ); |
||
151 | $ratingField->setRightTitle(Config::inst()->get("PageRaterExtension_Controller", "field_right_title")); |
||
152 | $requiredFields = RequiredFields::create($ratingField->getRequiredFields()); |
||
153 | $actions = FieldList::create(FormAction::create('dopagerating', 'Submit')); |
||
154 | } |
||
155 | $fields = FieldList::create( |
||
156 | $ratingField, |
||
157 | HiddenField::create('ParentID', "ParentID", $this->owner->dataRecord->ID) |
||
158 | ); |
||
159 | |||
160 | return Form::create($this->owner, 'PageRatingForm', $fields, $actions, $requiredFields); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * action Page Rating Form |
||
165 | */ |
||
166 | public function dopagerating($data, $form) |
||
0 ignored issues
–
show
The return type could not be reliably inferred; please add a
@return annotation.
Our type inference engine in quite powerful, but sometimes the code does not
provide enough clues to go by. In these cases we request you to add a
Loading history...
|
|||
167 | { |
||
168 | $id = $this->owner->dataRecord->ID; |
||
169 | $fieldName = "RatingFor".$id; |
||
170 | $data = Convert::raw2sql($data); |
||
171 | $pageRating = PageRating::create(); |
||
172 | $form->saveInto($pageRating); |
||
173 | $pageRating->ParentID = $this->owner->dataRecord->ID; |
||
174 | if (isset($data[$fieldName])) { |
||
175 | $pageRating->Rating = floatval($data[$fieldName]); |
||
176 | } |
||
177 | if (isset($data[$fieldName."_Comment"])) { |
||
178 | $pageRating->Comment = Convert::raw2sql($data[$fieldName."_Comment"]); |
||
179 | } |
||
180 | if (isset($data[$fieldName."_Name"])) { |
||
181 | $pageRating->Name = Convert::raw2sql($data[$fieldName."_Name"]); |
||
182 | } |
||
183 | if (isset($data[$fieldName."_Title"])) { |
||
184 | $pageRating->Title = Convert::raw2sql($data[$fieldName."_Title"]); |
||
185 | } |
||
186 | $pageRating->write(); |
||
187 | Session::set('PageRated'.$this->owner->dataRecord->ID, $pageRating->ID); |
||
188 | if (Director::is_ajax()) { |
||
189 | return $this->owner->renderWith("PageRaterAjaxReturn"); |
||
190 | } else { |
||
191 | $this->owner->redirectBack(); |
||
192 | } |
||
193 | } |
||
194 | |||
195 | |||
196 | View Code Duplication | public function removedefaultpageratings() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
197 | { |
||
198 | if (Permission::check("ADMIN")) { |
||
199 | DB::query("DELETE FROM PageRating WHERE IsDefault = 1;"); |
||
200 | debug::show("removed all default ratings for all pages"); |
||
201 | } else { |
||
202 | Security::permissionFailure($this->owner, _t('Security.PERMFAILURE', ' This page is secured and you need administrator rights to access it. Enter your credentials below and we will send you right along.')); |
||
203 | } |
||
204 | } |
||
205 | |||
206 | View Code Duplication | public function removeallpageratings() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
207 | { |
||
208 | if (Permission::check("ADMIN")) { |
||
209 | DB::query("DELETE FROM PageRating;"); |
||
210 | debug::show("removed all ratings for all pages"); |
||
211 | } else { |
||
212 | Security::permissionFailure($this->owner, _t('Security.PERMFAILURE', ' This page is secured and you need administrator rights to access it. Enter your credentials below and we will send you right along.')); |
||
213 | } |
||
214 | } |
||
215 | |||
216 | |||
217 | |||
218 | |||
219 | |||
220 | /** |
||
221 | * rating for this page ... |
||
222 | * @return ArrayList |
||
223 | */ |
||
224 | public function PageRatingResults() |
||
225 | { |
||
226 | $sqlQuery = new SQLQuery(); |
||
227 | $sqlQuery->setSelect("AVG(\"PageRating\".\"Rating\") RatingAverage, ParentID"); |
||
228 | $sqlQuery->setFrom("\"PageRating\" "); |
||
229 | View Code Duplication | if ($this->onlyShowApprovedPageRatings()) { |
|
230 | $sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID." AND \"PageRating\".\"IsApproved\" = 1"); |
||
231 | } else { |
||
232 | $sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID.""); |
||
233 | } |
||
234 | $sqlQuery->setOrderBy("RatingAverage DESC"); |
||
235 | $sqlQuery->setGroupby("\"ParentID\""); |
||
236 | $sqlQuery->setLimit(1); |
||
237 | return $this->turnPageRaterSQLIntoArrayList($sqlQuery, "PageRatingResults"); |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * rating of this page by this user ... |
||
242 | * @return ArrayList |
||
243 | */ |
||
244 | public function CurrentUserRating() |
||
245 | { |
||
246 | $sqlQuery = new SQLQuery(); |
||
247 | $sqlQuery->setSelect("AVG(\"PageRating\".\"Rating\") RatingAverage, ParentID"); |
||
248 | $sqlQuery->setFrom("\"PageRating\" "); |
||
249 | if ($this->onlyShowApprovedPageRatings()) { |
||
250 | $sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID." AND \"PageRating\".\"ID\" = '".Session::get('PageRated'.$this->owner->ID)."' AND \"PageRating\".\"IsApproved\" = 1"); |
||
251 | } else { |
||
252 | $sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID." AND \"PageRating\".\"ID\" = '".Session::get('PageRated'.$this->owner->ID)."'"); |
||
253 | } |
||
254 | |||
255 | $sqlQuery->setOrderBy("RatingAverage DESC"); |
||
256 | $sqlQuery->setGroupby("\"ParentID\""); |
||
257 | $sqlQuery->setLimit(1); |
||
258 | return $this->turnPageRaterSQLIntoArrayList($sqlQuery, "CurrentUserRating"); |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * list of all rated pages ... |
||
263 | * @return ArrayList |
||
264 | */ |
||
265 | public function PageRaterListOfAllForPage($paginated = false) |
||
266 | { |
||
267 | if ($this->onlyShowApprovedPageRatings()) { |
||
268 | $list = $this->turnPageRaterSQLIntoArrayList( |
||
269 | $this->owner->PageRatings()->filter(array("IsApproved" => 1)), |
||
270 | "PageRaterListOfAllForPage" |
||
271 | ); |
||
272 | } else { |
||
273 | $list = $this->turnPageRaterSQLIntoArrayList( |
||
274 | $this->owner->PageRatings(), |
||
275 | "PageRaterListOfAllForPage" |
||
276 | ); |
||
277 | } |
||
278 | if ($paginated) { |
||
279 | $list = PaginatedList::create($list, $this->owner->getRequest()); |
||
280 | $list->setPageLength(8); |
||
281 | } |
||
282 | return $list; |
||
283 | } |
||
284 | |||
285 | |||
286 | public function PageRaterListAll() |
||
287 | { |
||
288 | $sqlQuery = new SQLQuery(); |
||
289 | $sqlQuery->setSelect("\"PageRating\".\"Rating\" AS RatingAverage, \"PageRating\".\"ParentID\""); |
||
290 | if ($this->onlyShowApprovedPageRatings()) { |
||
291 | $sqlQuery->setWhere("\"PageRating\".\"IsApproved\" = 1"); |
||
292 | } |
||
293 | $sqlQuery->setFrom(" \"PageRating\""); |
||
294 | $sqlQuery->addInnerJoin("SiteTree", " \"PageRating\".\"ParentID\" = \"SiteTree\".\"ID\""); |
||
295 | $sqlQuery->setOrderBy("RatingAverage DESC"); |
||
296 | $sqlQuery->setGroupby("\"SiteTree\".\"ParentID\""); |
||
297 | return $this->turnPageRaterSQLIntoArrayList($sqlQuery, "PageRaterList"); |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * @param $data $sqlQuery | DataList |
||
302 | * @param string $method |
||
303 | * |
||
304 | * @return ArrayList |
||
305 | */ |
||
306 | protected function turnPageRaterSQLIntoArrayList($data, $method = "unknown") |
||
307 | { |
||
308 | if ($data instanceof SQLQuery) { |
||
309 | $data = $data->execute(); |
||
310 | } |
||
311 | $al = new ArrayList(); |
||
312 | if ($data) { |
||
313 | foreach ($data as $record) { |
||
314 | if ($record instanceof PageRating) { |
||
315 | $record->Method = $method; |
||
316 | //do nothing |
||
317 | } else { |
||
318 | $score = $record["RatingAverage"]; |
||
319 | $parentID = $record["ParentID"]; |
||
320 | $record = PageRating::get_star_details_as_array_data($score, $parentID, $method); |
||
321 | } |
||
322 | $al->push($record); |
||
323 | } |
||
324 | } |
||
325 | return $al; |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * @return boolean |
||
330 | */ |
||
331 | public function PageHasBeenRatedByUser() |
||
332 | { |
||
333 | return Session::get('PageRated'.$this->owner->ID) ? true : false; |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * |
||
338 | * @return int |
||
339 | */ |
||
340 | public function NumberOfPageRatings() |
||
341 | { |
||
342 | $doSet = new ArrayList(); |
||
343 | $sqlQuery = new SQLQuery(); |
||
344 | $sqlQuery->setSelect("COUNT(\"PageRating\".\"Rating\") RatingCount"); |
||
345 | $sqlQuery->setFrom("\"PageRating\" "); |
||
346 | View Code Duplication | if ($this->onlyShowApprovedPageRatings()) { |
|
347 | $sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID." AND \"PageRating\".\"IsApproved\" = 1"); |
||
348 | } else { |
||
349 | $sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID.""); |
||
350 | } |
||
351 | $sqlQuery->setOrderBy("RatingCount ASC"); |
||
352 | $sqlQuery->setGroupBy("\"ParentID\""); |
||
353 | $sqlQuery->setLimit(1); |
||
354 | $data = $sqlQuery->execute(); |
||
355 | if ($data) { |
||
356 | foreach ($data as $record) { |
||
357 | return $record["RatingCount"]; |
||
358 | } |
||
359 | } |
||
360 | return 0; |
||
361 | } |
||
362 | |||
363 | protected function onlyShowApprovedPageRatings() |
||
364 | { |
||
365 | return Config::inst()->get("PageRaterExtension_Controller", "only_show_approved"); |
||
366 | } |
||
367 | |||
368 | |||
369 | /** |
||
370 | * return the average rating... |
||
371 | * @return Double |
||
372 | */ |
||
373 | public function getStarRating() |
||
374 | { |
||
375 | $ratings = $this->owner->PageRatingResults(); |
||
376 | $rating = 0; |
||
377 | if ($ratings->Count() == 1) { |
||
378 | foreach ($ratings as $ratingItem) { |
||
379 | $rating = $ratingItem->Stars; |
||
380 | } |
||
381 | } |
||
382 | return $rating; |
||
383 | } |
||
384 | } |
||
385 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.