Conditions | 26 |
Paths | 2306 |
Total Lines | 231 |
Code Lines | 148 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
83 | public function display_graph_by_resource() |
||
84 | { |
||
85 | $headerName = $this->datagen->get_header_names(); |
||
86 | $total_users = $this->datagen->get_total_users_count(); |
||
87 | $customdisplays = ScoreDisplay::instance()->get_custom_score_display_settings(); |
||
88 | |||
89 | if (empty($customdisplays)) { |
||
90 | echo get_lang('To view graph score rule must be enabled'); |
||
91 | |||
92 | return ''; |
||
93 | } |
||
94 | |||
95 | $user_results = $this->datagen->get_data_to_graph2(false); |
||
96 | |||
97 | if (empty($user_results) || empty($total_users)) { |
||
98 | echo get_lang('No results found'); |
||
99 | |||
100 | return ''; |
||
101 | } |
||
102 | |||
103 | // Removing first name |
||
104 | array_shift($headerName); |
||
105 | // Removing last name |
||
106 | array_shift($headerName); |
||
107 | // Removing username |
||
108 | array_shift($headerName); |
||
109 | |||
110 | $pre_result = []; |
||
111 | foreach ($user_results as $result) { |
||
112 | for ($i = 0; $i < count($headerName); $i++) { |
||
|
|||
113 | if (isset($result[$i + 1])) { |
||
114 | $pre_result[$i + 3][] = $result[$i + 1]; |
||
115 | } |
||
116 | } |
||
117 | } |
||
118 | |||
119 | $i = 0; |
||
120 | $resource_list = []; |
||
121 | $pre_result2 = []; |
||
122 | foreach ($pre_result as $key => $res_array) { |
||
123 | rsort($res_array); |
||
124 | $pre_result2[] = $res_array; |
||
125 | } |
||
126 | |||
127 | //@todo when a display custom does not exist the order of the color does not match |
||
128 | //filling all the answer that are not responded with 0 |
||
129 | rsort($customdisplays); |
||
130 | |||
131 | if ($total_users > 0) { |
||
132 | foreach ($pre_result2 as $key => $res_array) { |
||
133 | $key_list = []; |
||
134 | foreach ($res_array as $user_result) { |
||
135 | $userResult = isset($user_result[1]) ? $user_result[1] : null; |
||
136 | if (!isset($resource_list[$key][$userResult])) { |
||
137 | $resource_list[$key][$userResult] = 0; |
||
138 | } |
||
139 | $resource_list[$key][$userResult]++; |
||
140 | $key_list[] = $userResult; |
||
141 | } |
||
142 | |||
143 | foreach ($customdisplays as $display) { |
||
144 | if (!in_array($display['display'], $key_list)) { |
||
145 | $resource_list[$key][$display['display']] = 0; |
||
146 | } |
||
147 | } |
||
148 | $i++; |
||
149 | } |
||
150 | } |
||
151 | |||
152 | //fixing $resource_list |
||
153 | $max = 0; |
||
154 | $new_list = []; |
||
155 | foreach ($resource_list as $key => $value) { |
||
156 | $new_value = []; |
||
157 | foreach ($customdisplays as $item) { |
||
158 | if ($value[$item['display']] > $max) { |
||
159 | $max = $value[$item['display']]; |
||
160 | } |
||
161 | $new_value[$item['display']] = strip_tags($value[$item['display']]); |
||
162 | } |
||
163 | $new_list[] = $new_value; |
||
164 | } |
||
165 | $resource_list = $new_list; |
||
166 | $i = 1; |
||
167 | // Cache definition |
||
168 | $cachePath = api_get_path(SYS_ARCHIVE_PATH) . 'chart/'; |
||
169 | if (!file_exists($cachePath)) { |
||
170 | mkdir($cachePath, 0755, true); |
||
171 | } |
||
172 | |||
173 | if (!is_writable($cachePath)) { |
||
174 | chmod($cachePath, 0755); |
||
175 | } |
||
176 | |||
177 | foreach ($resource_list as $key => $resource) { |
||
178 | // Reverse array, otherwise we get highest values first |
||
179 | $resource = array_reverse($resource, true); |
||
180 | |||
181 | $dataSet = new pData(); |
||
182 | $dataSet->addPoints($resource, 'Serie'); |
||
183 | $dataSet->addPoints(array_keys($resource), 'Labels'); |
||
184 | $header = $headerName[$i - 1]; |
||
185 | if (is_array($header) && isset($header['header'])) { |
||
186 | $header = $header['header']; |
||
187 | } |
||
188 | $header = strip_tags(api_html_entity_decode($header)); |
||
189 | $dataSet->setSerieDescription('Labels', $header); |
||
190 | $dataSet->setAbscissa('Labels'); |
||
191 | $dataSet->setAbscissaName(get_lang('Skills ranking')); |
||
192 | $dataSet->setAxisName(0, get_lang('Learners')); |
||
193 | $palette = [ |
||
194 | '0' => ['R' => 186, 'G' => 206, 'B' => 151, 'Alpha' => 100], |
||
195 | '1' => ['R' => 210, 'G' => 148, 'B' => 147, 'Alpha' => 100], |
||
196 | '2' => ['R' => 148, 'G' => 170, 'B' => 208, 'Alpha' => 100], |
||
197 | '3' => ['R' => 221, 'G' => 133, 'B' => 61, 'Alpha' => 100], |
||
198 | '4' => ['R' => 65, 'G' => 153, 'B' => 176, 'Alpha' => 100], |
||
199 | '5' => ['R' => 114, 'G' => 88, 'B' => 144, 'Alpha' => 100], |
||
200 | '6' => ['R' => 138, 'G' => 166, 'B' => 78, 'Alpha' => 100], |
||
201 | '7' => ['R' => 171, 'G' => 70, 'B' => 67, 'Alpha' => 100], |
||
202 | '8' => ['R' => 69, 'G' => 115, 'B' => 168, 'Alpha' => 100], |
||
203 | ]; |
||
204 | $myCache = new pCache(['CacheFolder' => substr($cachePath, 0, strlen($cachePath) - 1)]); |
||
205 | $chartHash = $myCache->getHash($dataSet); |
||
206 | if ($myCache->isInCache($chartHash)) { |
||
207 | $imgPath = $cachePath.$chartHash; |
||
208 | $myCache->saveFromCache($chartHash, $imgPath); |
||
209 | } else { |
||
210 | /* Create the pChart object */ |
||
211 | $widthSize = 480; |
||
212 | $heightSize = 250; |
||
213 | $myPicture = new pImage($widthSize, $heightSize, $dataSet); |
||
214 | |||
215 | /* Turn of Antialiasing */ |
||
216 | $myPicture->Antialias = false; |
||
217 | |||
218 | /* Add a border to the picture */ |
||
219 | $myPicture->drawRectangle( |
||
220 | 0, |
||
221 | 0, |
||
222 | $widthSize - 1, |
||
223 | $heightSize - 1, |
||
224 | [ |
||
225 | 'R' => 0, |
||
226 | 'G' => 0, |
||
227 | 'B' => 0, |
||
228 | ] |
||
229 | ); |
||
230 | |||
231 | /* Set the default font */ |
||
232 | $myPicture->setFontProperties( |
||
233 | [ |
||
234 | 'FontName' => api_get_path(SYS_FONTS_PATH).'opensans/OpenSans-Regular.ttf', |
||
235 | 'FontSize' => 10, |
||
236 | ] |
||
237 | ); |
||
238 | /* Write the chart title */ |
||
239 | $myPicture->drawText( |
||
240 | 250, |
||
241 | 30, |
||
242 | $header, |
||
243 | [ |
||
244 | 'FontSize' => 12, |
||
245 | 'Align' => TEXT_ALIGN_BOTTOMMIDDLE, |
||
246 | ] |
||
247 | ); |
||
248 | |||
249 | /* Define the chart area */ |
||
250 | $myPicture->setGraphArea(50, 40, $widthSize - 20, $heightSize - 50); |
||
251 | |||
252 | /* Draw the scale */ |
||
253 | $scaleSettings = [ |
||
254 | 'GridR' => 200, |
||
255 | 'GridG' => 200, |
||
256 | 'GridB' => 200, |
||
257 | 'DrawSubTicks' => true, |
||
258 | 'CycleBackground' => true, |
||
259 | 'Mode' => SCALE_MODE_START0, |
||
260 | ]; |
||
261 | $myPicture->drawScale($scaleSettings); |
||
262 | |||
263 | /* Turn on shadow computing */ |
||
264 | $myPicture->setShadow( |
||
265 | true, |
||
266 | [ |
||
267 | 'X' => 1, |
||
268 | 'Y' => 1, |
||
269 | 'R' => 0, |
||
270 | 'G' => 0, |
||
271 | 'B' => 0, |
||
272 | 'Alpha' => 10, |
||
273 | ] |
||
274 | ); |
||
275 | |||
276 | /* Draw the chart */ |
||
277 | $myPicture->setShadow( |
||
278 | true, |
||
279 | [ |
||
280 | 'X' => 1, |
||
281 | 'Y' => 1, |
||
282 | 'R' => 0, |
||
283 | 'G' => 0, |
||
284 | 'B' => 0, |
||
285 | 'Alpha' => 10, |
||
286 | ] |
||
287 | ); |
||
288 | $settings = [ |
||
289 | 'OverrideColors' => $palette, |
||
290 | 'Gradient' => false, |
||
291 | 'GradientMode' => GRADIENT_SIMPLE, |
||
292 | 'DisplayPos' => LABEL_POS_TOP, |
||
293 | 'DisplayValues' => true, |
||
294 | 'DisplayR' => 0, |
||
295 | 'DisplayG' => 0, |
||
296 | 'DisplayB' => 0, |
||
297 | 'DisplayShadow' => true, |
||
298 | 'Surrounding' => 10, |
||
299 | ]; |
||
300 | $myPicture->drawBarChart($settings); |
||
301 | /* Render the picture (choose the best way) */ |
||
302 | |||
303 | $myCache->writeToCache($chartHash, $myPicture); |
||
304 | $imgPath = $cachePath.$chartHash; |
||
305 | $myCache->saveFromCache($chartHash, $imgPath); |
||
306 | } |
||
307 | echo '<img src="data:image/png;base64,' . base64_encode(file_get_contents($imgPath)) . '" >'; |
||
308 | if (0 == $i % 2 && 0 != $i) { |
||
309 | echo '<br /><br />'; |
||
310 | } else { |
||
311 | echo ' '; |
||
312 | } |
||
313 | $i++; |
||
314 | } |
||
515 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: