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 | /** |
||
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 | View Code Duplication | public function requireDefaultRecords() |
|
0 ignored issues
–
show
|
|||
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 | /** |
||
92 | * |
||
93 | * @param string $character optional character (e.g. ★, |
||
94 | * if supplied and the number of stars is 3 |
||
95 | * then it will return ★★★) |
||
96 | * @return int|string |
||
97 | */ |
||
98 | 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...
|
|||
99 | { |
||
100 | $ratings = $this->owner->PageRatingResults(); |
||
101 | $rating = 0; |
||
102 | if ($ratings->Count() == 1) { |
||
103 | foreach ($ratings as $ratingItem) { |
||
104 | $rating = $ratingItem->Stars; |
||
105 | } |
||
106 | } |
||
107 | if ($character && $rating) { |
||
108 | return str_repeat($character, $rating); |
||
109 | } else { |
||
110 | return $rating; |
||
111 | } |
||
112 | } |
||
113 | /** |
||
114 | * |
||
115 | * @return int |
||
116 | */ |
||
117 | 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...
|
|||
118 | { |
||
119 | $doSet = new ArrayList(); |
||
120 | $sqlQuery = new SQLQuery(); |
||
121 | $sqlQuery->setSelect("COUNT(\"PageRating\".\"Rating\") RatingCount"); |
||
122 | $sqlQuery->setFrom("\"PageRating\" "); |
||
123 | if ($this->onlyShowApprovedPageRatings()) { |
||
124 | $sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID." AND \"PageRating\".\"IsApproved\" = 1"); |
||
125 | } else { |
||
126 | $sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID.""); |
||
127 | } |
||
128 | $sqlQuery->setOrderBy("RatingCount ASC"); |
||
129 | $sqlQuery->setGroupBy("\"ParentID\""); |
||
130 | $sqlQuery->setLimit(1); |
||
131 | $data = $sqlQuery->execute(); |
||
132 | if ($data) { |
||
133 | foreach ($data as $record) { |
||
134 | return $record["RatingCount"]; |
||
135 | } |
||
136 | } |
||
137 | return 0; |
||
138 | } |
||
139 | |||
140 | |||
141 | /** |
||
142 | * rating for this page ... |
||
143 | * @return ArrayList |
||
144 | */ |
||
145 | 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...
|
|||
146 | { |
||
147 | $sqlQuery = new SQLQuery(); |
||
148 | $sqlQuery->setSelect("AVG(\"PageRating\".\"Rating\") RatingAverage, ParentID"); |
||
149 | $sqlQuery->setFrom("\"PageRating\" "); |
||
150 | if ($this->onlyShowApprovedPageRatings()) { |
||
151 | $sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID." AND \"PageRating\".\"IsApproved\" = 1"); |
||
152 | } else { |
||
153 | $sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID.""); |
||
154 | } |
||
155 | $sqlQuery->setOrderBy("RatingAverage DESC"); |
||
156 | $sqlQuery->setGroupby("\"ParentID\""); |
||
157 | $sqlQuery->setLimit(1); |
||
158 | return $this->turnPageRaterSQLIntoArrayList($sqlQuery, "PageRatingResults"); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * rating of this page by this user ... |
||
163 | * @return ArrayList |
||
164 | */ |
||
165 | 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...
|
|||
166 | { |
||
167 | $sqlQuery = new SQLQuery(); |
||
168 | $sqlQuery->setSelect("AVG(\"PageRating\".\"Rating\") RatingAverage, ParentID"); |
||
169 | $sqlQuery->setFrom("\"PageRating\" "); |
||
170 | if ($this->onlyShowApprovedPageRatings()) { |
||
171 | $sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID." AND \"PageRating\".\"ID\" = '".Session::get('PageRated'.$this->owner->ID)."' AND \"PageRating\".\"IsApproved\" = 1"); |
||
172 | } else { |
||
173 | $sqlQuery->setWhere("\"ParentID\" = ".$this->owner->ID." AND \"PageRating\".\"ID\" = '".Session::get('PageRated'.$this->owner->ID)."'"); |
||
174 | } |
||
175 | |||
176 | $sqlQuery->setOrderBy("RatingAverage DESC"); |
||
177 | $sqlQuery->setGroupby("\"ParentID\""); |
||
178 | $sqlQuery->setLimit(1); |
||
179 | return $this->turnPageRaterSQLIntoArrayList($sqlQuery, "CurrentUserRating"); |
||
180 | } |
||
181 | |||
182 | |||
183 | /** |
||
184 | * @param $data $sqlQuery | DataList |
||
185 | * @param string $method |
||
186 | * |
||
187 | * @return ArrayList |
||
188 | */ |
||
189 | 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...
|
|||
190 | { |
||
191 | if ($data instanceof SQLQuery) { |
||
192 | $data = $data->execute(); |
||
193 | } |
||
194 | $al = new ArrayList(); |
||
195 | if ($data) { |
||
196 | foreach ($data as $record) { |
||
197 | if ($record instanceof PageRating) { |
||
198 | $record->Method = $method; |
||
199 | //do nothing |
||
200 | } else { |
||
201 | $score = $record["RatingAverage"]; |
||
202 | $parentID = $record["ParentID"]; |
||
203 | $record = PageRating::get_star_details_as_array_data($score, $parentID, $method); |
||
204 | } |
||
205 | $al->push($record); |
||
206 | } |
||
207 | } |
||
208 | return $al; |
||
209 | } |
||
210 | |||
211 | public function onlyShowApprovedPageRatings() |
||
212 | { |
||
213 | return Config::inst()->get("PageRaterExtension_Controller", "only_show_approved"); |
||
214 | } |
||
215 | } |
||
216 |
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.