| Conditions | 10 |
| Paths | 16 |
| Total Lines | 148 |
| Code Lines | 95 |
| 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 |
||
| 93 | public function getContent() |
||
| 94 | { |
||
| 95 | $students = $this->students; |
||
| 96 | $attendance = new Attendance(); |
||
| 97 | |||
| 98 | // get data |
||
| 99 | $attendances_faults_avg = []; |
||
| 100 | if (is_array($students) && count($students) > 0) { |
||
| 101 | foreach ($students as $student) { |
||
| 102 | $student_id = $student['user_id']; |
||
| 103 | //$student_info = api_get_user_info($student_id); |
||
| 104 | // get average of faults in attendances by student |
||
| 105 | $results_faults_avg = $attendance->get_faults_average_inside_courses($student_id); |
||
| 106 | |||
| 107 | if (!empty($results_faults_avg)) { |
||
| 108 | $attendances_faults_avg[$student['lastname']] = $results_faults_avg['porcent']; |
||
| 109 | } else { |
||
| 110 | $attendances_faults_avg[$student['lastname']] = 0; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | arsort($attendances_faults_avg); |
||
| 116 | $usernames = array_keys($attendances_faults_avg); |
||
| 117 | |||
| 118 | $faults = []; |
||
| 119 | foreach ($usernames as $username) { |
||
| 120 | $faults[] = $attendances_faults_avg[$username]; |
||
| 121 | } |
||
| 122 | |||
| 123 | $graph = ''; |
||
| 124 | $img_file = ''; |
||
| 125 | if (is_array($usernames) && count($usernames) > 0) { |
||
| 126 | // Defining data |
||
| 127 | $dataSet = new pData(); |
||
| 128 | $dataSet->addPoints($faults, 'Serie1'); |
||
| 129 | $dataSet->addPoints($usernames, 'Labels'); |
||
| 130 | $dataSet->setSerieDescription('Series1', get_lang('Average')); |
||
| 131 | $dataSet->setSerieDescription('Labels', get_lang('User')); |
||
| 132 | $dataSet->setAbscissa('Labels'); |
||
| 133 | $dataSet->setAbscissaName(get_lang('User')); |
||
| 134 | $dataSet->setAxisName(0, get_lang('Attendance')); |
||
| 135 | $palette = [ |
||
| 136 | '0' => ['R' => 186, 'G' => 206, 'B' => 151, 'Alpha' => 100], |
||
| 137 | '1' => ['R' => 210, 'G' => 148, 'B' => 147, 'Alpha' => 100], |
||
| 138 | '2' => ['R' => 148, 'G' => 170, 'B' => 208, 'Alpha' => 100], |
||
| 139 | '3' => ['R' => 221, 'G' => 133, 'B' => 61, 'Alpha' => 100], |
||
| 140 | '4' => ['R' => 65, 'G' => 153, 'B' => 176, 'Alpha' => 100], |
||
| 141 | '5' => ['R' => 114, 'G' => 88, 'B' => 144, 'Alpha' => 100], |
||
| 142 | '6' => ['R' => 138, 'G' => 166, 'B' => 78, 'Alpha' => 100], |
||
| 143 | '7' => ['R' => 171, 'G' => 70, 'B' => 67, 'Alpha' => 100], |
||
| 144 | '8' => ['R' => 69, 'G' => 115, 'B' => 168, 'Alpha' => 100], |
||
| 145 | ]; |
||
| 146 | // Cache definition |
||
| 147 | $cachePath = api_get_path(SYS_ARCHIVE_PATH); |
||
| 148 | $myCache = new pCache( |
||
| 149 | [ |
||
| 150 | 'CacheFolder' => substr( |
||
| 151 | $cachePath, |
||
| 152 | 0, |
||
| 153 | strlen($cachePath) - 1 |
||
| 154 | ), |
||
| 155 | ] |
||
| 156 | ); |
||
| 157 | $chartHash = $myCache->getHash($dataSet); |
||
| 158 | if ($myCache->isInCache($chartHash)) { |
||
| 159 | $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash; |
||
| 160 | $myCache->saveFromCache($chartHash, $imgPath); |
||
| 161 | $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash; |
||
| 162 | } else { |
||
| 163 | $maxCounts = max(count($usernames), count($faults)); |
||
| 164 | if ($maxCounts < 5) { |
||
| 165 | $heightSize = 200; |
||
| 166 | } else { |
||
| 167 | $heightSize = $maxCounts * 40; |
||
| 168 | } |
||
| 169 | |||
| 170 | /* Create the pChart object */ |
||
| 171 | $widthSize = 480; |
||
| 172 | $angle = 40; |
||
| 173 | |||
| 174 | $myPicture = new pImage($widthSize, $heightSize, $dataSet); |
||
| 175 | |||
| 176 | /* Turn of Antialiasing */ |
||
| 177 | $myPicture->Antialias = false; |
||
| 178 | |||
| 179 | /* Add a border to the picture */ |
||
| 180 | $myPicture->drawRectangle(0, 0, $widthSize - 1, $heightSize - 1, ['R' => 0, 'G' => 0, 'B' => 0]); |
||
| 181 | |||
| 182 | /* Set the default font */ |
||
| 183 | $myPicture->setFontProperties( |
||
| 184 | [ |
||
| 185 | 'FontName' => api_get_path(SYS_FONTS_PATH).'opensans/OpenSans-Regular.ttf', |
||
| 186 | 'FontSize' => 10, |
||
| 187 | ] |
||
| 188 | ); |
||
| 189 | |||
| 190 | /* Do NOT Write the chart title */ |
||
| 191 | |||
| 192 | /* Define the chart area */ |
||
| 193 | $myPicture->setGraphArea(80, 40, $widthSize - 20, $heightSize - 40); |
||
| 194 | |||
| 195 | /* Draw the scale */ |
||
| 196 | $scaleSettings = [ |
||
| 197 | 'GridR' => 200, |
||
| 198 | 'GridG' => 200, |
||
| 199 | 'GridB' => 200, |
||
| 200 | 'DrawSubTicks' => true, |
||
| 201 | 'CycleBackground' => true, |
||
| 202 | 'Mode' => SCALE_MODE_ADDALL_START0, |
||
|
|
|||
| 203 | 'Pos' => SCALE_POS_TOPBOTTOM, |
||
| 204 | 'DrawXLines' => false, |
||
| 205 | 'LabelRotation' => $angle, |
||
| 206 | ]; |
||
| 207 | |||
| 208 | $myPicture->drawScale($scaleSettings); |
||
| 209 | |||
| 210 | /* Turn on shadow computing */ |
||
| 211 | $myPicture->setShadow(true, ['X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10]); |
||
| 212 | |||
| 213 | /* Draw the chart */ |
||
| 214 | $myPicture->setShadow(true, ['X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10]); |
||
| 215 | $settings = [ |
||
| 216 | 'OverrideColors' => $palette, |
||
| 217 | 'Gradient' => false, |
||
| 218 | 'GradientMode' => GRADIENT_SIMPLE, |
||
| 219 | 'DisplayPos' => LABEL_POS_TOP, |
||
| 220 | 'DisplayValues' => true, |
||
| 221 | 'DisplayR' => 0, |
||
| 222 | 'DisplayG' => 0, |
||
| 223 | 'DisplayB' => 0, |
||
| 224 | 'DisplayShadow' => true, |
||
| 225 | 'Surrounding' => 10, |
||
| 226 | ]; |
||
| 227 | $myPicture->drawBarChart($settings); |
||
| 228 | |||
| 229 | /* Write and save into cache */ |
||
| 230 | $myCache->writeToCache($chartHash, $myPicture); |
||
| 231 | $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash; |
||
| 232 | $myCache->saveFromCache($chartHash, $imgPath); |
||
| 233 | $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash; |
||
| 234 | } |
||
| 235 | $graph = '<img src="'.$imgPath.'" >'; |
||
| 236 | } else { |
||
| 237 | $graph = '<p>'.api_convert_encoding(get_lang('GraphicNotAvailable'), 'UTF-8').'</p>'; |
||
| 238 | } |
||
| 239 | |||
| 240 | return $graph; |
||
| 241 | } |
||
| 253 |