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 |
||
0 ignored issues
–
show
|
|||
12 | { |
||
13 | private static $db = array( |
||
14 | 'PageRating' => 'Double(4,2)' |
||
15 | ); |
||
16 | |||
17 | private static $has_many = array( |
||
18 | 'PageRatings' => 'PageRating' |
||
19 | ); |
||
20 | |||
21 | private static $indexes = array( |
||
22 | 'PageRating' => true |
||
23 | ); |
||
24 | /** |
||
25 | * add the default rating to each page ... |
||
26 | * @var boolean |
||
27 | */ |
||
28 | private static $add_default_rating = false; |
||
29 | |||
30 | |||
31 | /** |
||
32 | * @var boolean |
||
33 | */ |
||
34 | private static $number_of_default_records_to_be_added = 5; |
||
35 | |||
36 | public function updateCMSFields(FieldList $fields) |
||
37 | { |
||
38 | if ($this->owner->PageRatings() && $this->owner->PageRatings()->count()) { |
||
39 | $fields->addFieldToTab( |
||
40 | "Root.Ratings", |
||
41 | GridField::create( |
||
42 | "PageRatings", |
||
43 | Injector::inst()->get("PageRating")->plural_name(), |
||
44 | $this->owner->PageRatings(), |
||
45 | GridFieldConfig_RecordViewer::create() |
||
46 | ) |
||
47 | ); |
||
48 | $fields->addFieldToTab( |
||
49 | "Root.Ratings", |
||
50 | ReadonlyField::create( |
||
51 | "PageRating", |
||
52 | 'Average Rating' |
||
53 | ) |
||
54 | ); |
||
55 | } |
||
56 | } |
||
57 | |||
58 | |||
59 | View Code Duplication | public function requireDefaultRecords() |
|
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...
|
|||
60 | { |
||
61 | parent::requireDefaultRecords(); |
||
62 | $step = 50; |
||
63 | if (Config::inst()->get("PageRaterExtension", "add_default_rating")) { |
||
64 | for ($i = 0; $i < 1000000; $i = $i + $step) { |
||
65 | $pages = SiteTree::get() |
||
66 | ->leftJoin("PageRating", "\"PageRating\".\"ParentID\" = \"SiteTree\".\"ID\"") |
||
67 | ->where("\"PageRating\".\"ID\" IS NULL") |
||
68 | ->limit($step, $i); |
||
69 | |||
70 | if ($pages->count()) { |
||
71 | foreach ($pages as $page) { |
||
72 | $count = 0; |
||
73 | $max = PageRating::get_number_of_stars(); |
||
74 | $goingBackTo = ($max / rand(1, $max)) - 1; |
||
75 | $stepsBack = $max - $goingBackTo; |
||
76 | $ratings = Config::inst()->get("PageRaterExtension", "number_of_default_records_to_be_added") / $stepsBack; |
||
77 | for ($i = 1; $i <= $ratings; $i++) { |
||
78 | for ($j = $max; $j > $goingBackTo; $j--) { |
||
79 | $PageRating = new PageRating(); |
||
80 | $PageRating->Rating = round(rand(1, $j)); |
||
81 | $PageRating->IsDefault = 1; |
||
82 | $PageRating->ParentID = $page->ID; |
||
83 | $PageRating->write(); |
||
84 | $count++; |
||
85 | } |
||
86 | } |
||
87 | DB::alteration_message("Created Initial Ratings for Page with title ".$page->Title.". Ratings created: $count", "created"); |
||
88 | } |
||
89 | } else { |
||
90 | $i = 1000000; |
||
91 | } |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | |||
96 | |||
97 | /** |
||
98 | * return the average rating... |
||
99 | * @return Double |
||
100 | */ |
||
101 | public function StarRating() |
||
102 | { |
||
103 | return $this->getStarRating(); |
||
104 | } |
||
105 | /** |
||
106 | * |
||
107 | * @param string $character optional character (e.g. ★, |
||
108 | * if supplied and the number of stars is 3 |
||
109 | * then it will return ★★★) |
||
110 | * @return int|string |
||
111 | */ |
||
112 | View Code Duplication | public function getStarRating($character = '') |
|
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...
|
|||
113 | { |
||
114 | $ratings = $this->owner->PageRatingResults(); |
||
115 | $rating = 0; |
||
116 | if ($ratings->Count() == 1) { |
||
117 | foreach ($ratings as $ratingItem) { |
||
118 | $rating = $ratingItem->Stars; |
||
119 | } |
||
120 | } |
||
121 | if ($character && $rating) { |
||
122 | return str_repeat($character, $rating); |
||
123 | } else { |
||
124 | return $rating; |
||
125 | } |
||
126 | } |
||
127 | /** |
||
128 | * |
||
129 | * @return int |
||
130 | */ |
||
131 | View Code Duplication | public function NumberOfPageRatings() |
|
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...
|
|||
132 | { |
||
133 | $doSet = new ArrayList(); |
||
134 | $sqlQuery = new SQLQuery(); |
||
135 | $sqlQuery->setSelect("COUNT(\"PageRating\".\"Rating\") RatingCount"); |
||
136 | $sqlQuery->setFrom("\"PageRating\" "); |
||
137 | if ($this->onlyShowApprovedPageRatings()) { |
||
138 | $sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID." AND \"PageRating\".\"IsApproved\" = 1"); |
||
139 | } else { |
||
140 | $sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID.""); |
||
141 | } |
||
142 | $sqlQuery->setOrderBy("RatingCount ASC"); |
||
143 | $sqlQuery->setGroupBy("\"ParentID\""); |
||
144 | $sqlQuery->setLimit(1); |
||
145 | $data = $sqlQuery->execute(); |
||
146 | if ($data) { |
||
147 | foreach ($data as $record) { |
||
148 | return $record["RatingCount"]; |
||
149 | } |
||
150 | } |
||
151 | return 0; |
||
152 | } |
||
153 | |||
154 | |||
155 | /** |
||
156 | * rating for this page ... |
||
157 | * @return ArrayList |
||
158 | */ |
||
159 | View Code Duplication | public function PageRatingResults() |
|
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...
|
|||
160 | { |
||
161 | $sqlQuery = new SQLQuery(); |
||
162 | $sqlQuery->setSelect("AVG(\"PageRating\".\"Rating\") RatingAverage, ParentID"); |
||
163 | $sqlQuery->setFrom("\"PageRating\" "); |
||
164 | if ($this->onlyShowApprovedPageRatings()) { |
||
165 | $sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID." AND \"PageRating\".\"IsApproved\" = 1"); |
||
166 | } else { |
||
167 | $sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID.""); |
||
168 | } |
||
169 | $sqlQuery->setOrderBy("RatingAverage DESC"); |
||
170 | $sqlQuery->setGroupby("\"ParentID\""); |
||
171 | $sqlQuery->setLimit(1); |
||
172 | return $this->turnPageRaterSQLIntoArrayList($sqlQuery, "PageRatingResults"); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * rating of this page by this user ... |
||
177 | * @return ArrayList |
||
178 | */ |
||
179 | View Code Duplication | public function CurrentUserRating() |
|
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...
|
|||
180 | { |
||
181 | $sqlQuery = new SQLQuery(); |
||
182 | $sqlQuery->setSelect("AVG(\"PageRating\".\"Rating\") RatingAverage, ParentID"); |
||
183 | $sqlQuery->setFrom("\"PageRating\" "); |
||
184 | if ($this->onlyShowApprovedPageRatings()) { |
||
185 | $sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID." AND \"PageRating\".\"ID\" = '".Session::get('PageRated'.$this->owner->ID)."' AND \"PageRating\".\"IsApproved\" = 1"); |
||
186 | } else { |
||
187 | $sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID." AND \"PageRating\".\"ID\" = '".Session::get('PageRated'.$this->owner->ID)."'"); |
||
188 | } |
||
189 | |||
190 | $sqlQuery->setOrderBy("RatingAverage DESC"); |
||
191 | $sqlQuery->setGroupby("\"ParentID\""); |
||
192 | $sqlQuery->setLimit(1); |
||
193 | return $this->turnPageRaterSQLIntoArrayList($sqlQuery, "CurrentUserRating"); |
||
194 | } |
||
195 | |||
196 | |||
197 | /** |
||
198 | * @param $data $sqlQuery | DataList |
||
199 | * @param string $method |
||
200 | * |
||
201 | * @return ArrayList |
||
202 | */ |
||
203 | View Code Duplication | public function turnPageRaterSQLIntoArrayList($data, $method = "unknown") |
|
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...
|
|||
204 | { |
||
205 | if ($data instanceof SQLQuery) { |
||
206 | $data = $data->execute(); |
||
207 | } |
||
208 | $al = new ArrayList(); |
||
209 | if ($data) { |
||
210 | foreach ($data as $record) { |
||
211 | if ($record instanceof PageRating) { |
||
212 | $record->Method = $method; |
||
213 | //do nothing |
||
214 | } else { |
||
215 | $score = $record["RatingAverage"]; |
||
216 | $parentID = $record["ParentID"]; |
||
217 | $record = PageRating::get_star_details_as_array_data($score, $parentID, $method); |
||
218 | } |
||
219 | $al->push($record); |
||
220 | } |
||
221 | } |
||
222 | return $al; |
||
223 | } |
||
224 | |||
225 | public function onlyShowApprovedPageRatings() |
||
226 | { |
||
227 | return Config::inst()->get("PageRaterExtension_Controller", "only_show_approved"); |
||
228 | } |
||
229 | } |
||
230 |
This check looks for classes that have been defined more than once.
If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.
This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.