| Conditions | 7 |
| Paths | 3 |
| Total Lines | 111 |
| Code Lines | 73 |
| 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 |
||
| 91 | public function getContent() |
||
| 92 | { |
||
| 93 | $teachers = $this->teachers; |
||
| 94 | $graph = ''; |
||
| 95 | $user_ids = array_keys($teachers); |
||
| 96 | $a_last_week = get_last_week(); |
||
| 97 | |||
| 98 | if (is_array($user_ids) && count($user_ids) > 0) { |
||
| 99 | $dataSet = new pData(); |
||
| 100 | foreach ($user_ids as $user_id) { |
||
| 101 | $teacher_info = api_get_user_info($user_id); |
||
| 102 | $username = $teacher_info['username']; |
||
| 103 | $time_by_days = []; |
||
| 104 | foreach ($a_last_week as $day) { |
||
| 105 | // day is received as y-m-d 12:00:00 |
||
| 106 | $start_date = api_get_utc_datetime($day); |
||
| 107 | $end_date = api_get_utc_datetime($day + (3600 * 24 - 1)); |
||
| 108 | |||
| 109 | $time_on_platform_by_day = Tracking::get_time_spent_on_the_platform( |
||
| 110 | $user_id, |
||
| 111 | 'custom', |
||
| 112 | $start_date, |
||
| 113 | $end_date |
||
| 114 | ); |
||
| 115 | $hours = floor($time_on_platform_by_day / 3600); |
||
| 116 | $min = floor(($time_on_platform_by_day - ($hours * 3600)) / 60); |
||
| 117 | $time_by_days[] = $min; |
||
| 118 | } |
||
| 119 | $dataSet->addPoints($time_by_days, $username); |
||
| 120 | } |
||
| 121 | |||
| 122 | $last_week = date('Y-m-d', $a_last_week[0]).' '.get_lang('To').' '.date('Y-m-d', $a_last_week[6]); |
||
| 123 | $days_on_week = []; |
||
| 124 | foreach ($a_last_week as $weekday) { |
||
| 125 | $days_on_week[] = date('d/m', $weekday); |
||
| 126 | } |
||
| 127 | |||
| 128 | $dataSet->addPoints($days_on_week, 'Days'); |
||
| 129 | $dataSet->setAbscissaName($last_week); |
||
| 130 | $dataSet->setAxisName(0, get_lang('Minutes')); |
||
| 131 | $dataSet->setAbscissa('Days'); |
||
| 132 | $dataSet->loadPalette(api_get_path(SYS_CODE_PATH).'palettes/pchart/default.color', true); |
||
| 133 | |||
| 134 | // Cache definition |
||
| 135 | $cachePath = api_get_path(SYS_ARCHIVE_PATH); |
||
| 136 | $myCache = new pCache(['CacheFolder' => substr($cachePath, 0, strlen($cachePath) - 1)]); |
||
| 137 | $chartHash = $myCache->getHash($dataSet); |
||
| 138 | if ($myCache->isInCache($chartHash)) { |
||
| 139 | $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash; |
||
| 140 | $myCache->saveFromCache($chartHash, $imgPath); |
||
| 141 | $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash; |
||
| 142 | } else { |
||
| 143 | /* Create the pChart object */ |
||
| 144 | $widthSize = 440; |
||
| 145 | $heightSize = 350; |
||
| 146 | $angle = 50; |
||
| 147 | $myPicture = new pImage($widthSize, $heightSize, $dataSet); |
||
| 148 | |||
| 149 | /* Turn of Antialiasing */ |
||
| 150 | $myPicture->Antialias = false; |
||
| 151 | |||
| 152 | /* Add a border to the picture */ |
||
| 153 | $myPicture->drawRectangle(0, 0, $widthSize - 1, $heightSize - 1, ['R' => 0, 'G' => 0, 'B' => 0]); |
||
| 154 | |||
| 155 | /* Set the default font */ |
||
| 156 | $myPicture->setFontProperties(['FontName' => api_get_path(SYS_FONTS_PATH).'opensans/OpenSans-Regular.ttf', 'FontSize' => 10]); |
||
| 157 | |||
| 158 | /* Do NOT Write the chart title */ |
||
| 159 | |||
| 160 | /* Define the chart area */ |
||
| 161 | $myPicture->setGraphArea(40, 40, $widthSize - 20, $heightSize - 80); |
||
| 162 | |||
| 163 | /* Draw the scale */ |
||
| 164 | $scaleSettings = [ |
||
| 165 | 'GridR' => 200, |
||
| 166 | 'GridG' => 200, |
||
| 167 | 'GridB' => 200, |
||
| 168 | 'DrawSubTicks' => true, |
||
| 169 | 'CycleBackground' => true, |
||
| 170 | 'Mode' => SCALE_MODE_ADDALL_START0, |
||
|
|
|||
| 171 | 'LabelRotation' => $angle, |
||
| 172 | ]; |
||
| 173 | |||
| 174 | $myPicture->drawScale($scaleSettings); |
||
| 175 | |||
| 176 | /* Turn on shadow computing */ |
||
| 177 | $myPicture->setShadow(true, ['X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10]); |
||
| 178 | |||
| 179 | /* Draw the chart */ |
||
| 180 | $myPicture->setShadow(true, ['X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10]); |
||
| 181 | $settings = [ |
||
| 182 | 'DisplayValues' => true, |
||
| 183 | 'DisplayR' => 0, |
||
| 184 | 'DisplayG' => 0, |
||
| 185 | 'DisplayB' => 0, |
||
| 186 | ]; |
||
| 187 | $myPicture->drawFilledSplineChart($settings); |
||
| 188 | $myPicture->drawLegend(40, 20, ['Mode' => LEGEND_HORIZONTAL]); |
||
| 189 | |||
| 190 | /* Write and save into cache */ |
||
| 191 | $myCache->writeToCache($chartHash, $myPicture); |
||
| 192 | $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash; |
||
| 193 | $myCache->saveFromCache($chartHash, $imgPath); |
||
| 194 | $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash; |
||
| 195 | } |
||
| 196 | $graph = '<img src="'.$imgPath.'" >'; |
||
| 197 | } else { |
||
| 198 | $graph = '<p>'.api_convert_encoding(get_lang('GraphicNotAvailable'), 'UTF-8').'</p>'; |
||
| 199 | } |
||
| 200 | |||
| 201 | return $graph; |
||
| 202 | } |
||
| 214 |