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