| Conditions | 29 |
| Paths | > 20000 |
| Total Lines | 183 |
| Code Lines | 114 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 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 |
||
| 129 | function generateHtmlForLearningPaths(User $student, array $courseInfo, int $sessionId): string |
||
| 130 | { |
||
| 131 | $html = Display::page_subheader2(get_lang('ToolLearnpath')); |
||
| 132 | |||
| 133 | $columnHeaders = []; |
||
| 134 | $columnHeaders['lp'] = get_lang('LearningPath'); |
||
| 135 | $columnHeaders['time'] = get_lang('Time'); |
||
| 136 | $columnHeaders['best_score'] = get_lang('BestScore'); |
||
| 137 | $columnHeaders['latest_attempt_avg_score'] = get_lang('LatestAttemptAverageScore'); |
||
| 138 | $columnHeaders['progress'] = get_lang('Progress'); |
||
| 139 | $columnHeaders['last_connection'] = get_lang('LastConnexion'); |
||
| 140 | |||
| 141 | $trackingColumns = api_get_configuration_value('tracking_columns'); |
||
| 142 | |||
| 143 | if (isset($trackingColumns['my_students_lp'])) { |
||
| 144 | foreach ($columnHeaders as $key => $value) { |
||
| 145 | if (!isset($trackingColumns['my_progress_lp'][$key]) || $trackingColumns['my_students_lp'][$key] == false) { |
||
| 146 | unset($columnHeaders[$key]); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | if (true === api_get_configuration_value('student_follow_page_add_LP_subscription_info')) { |
||
| 152 | $columnHeaders['student_follow_page_add_LP_subscription_info'] = get_lang('Unlock'); |
||
| 153 | } |
||
| 154 | |||
| 155 | if (true === api_get_configuration_value('student_follow_page_add_LP_acquisition_info')) { |
||
| 156 | $columnHeaders['student_follow_page_add_LP_acquisition_info'] = get_lang('Acquisition'); |
||
| 157 | } |
||
| 158 | |||
| 159 | $columnHeadersKeys = array_keys($columnHeaders); |
||
| 160 | |||
| 161 | $hideInvisibleViews = api_get_configuration_value('student_follow_page_add_LP_invisible_checkbox'); |
||
| 162 | |||
| 163 | $timeCourse = Tracking::minimumTimeAvailable($sessionId, $courseInfo['real_id']) |
||
| 164 | ? Tracking::getCalculateTime($student->getId(), $courseInfo['real_id'], $sessionId) |
||
| 165 | : null; |
||
| 166 | |||
| 167 | $lpCategories = learnpath::getCategories($courseInfo['real_id'], true); |
||
| 168 | |||
| 169 | /** @var CLpCategory $item */ |
||
| 170 | foreach ($lpCategories as $item) { |
||
| 171 | $categoryId = $item->getId(); |
||
| 172 | |||
| 173 | if (!learnpath::categoryIsVisibleForStudent($item, $student, $courseInfo['real_id'], $sessionId)) { |
||
| 174 | continue; |
||
| 175 | } |
||
| 176 | |||
| 177 | $lpList = new LearnpathList( |
||
| 178 | api_get_user_id(), |
||
| 179 | $courseInfo, |
||
| 180 | $sessionId, |
||
| 181 | null, |
||
| 182 | false, |
||
| 183 | $categoryId, |
||
| 184 | false, |
||
| 185 | false, |
||
| 186 | false |
||
| 187 | ); |
||
| 188 | |||
| 189 | $flatList = $lpList->get_flat_list(); |
||
| 190 | |||
| 191 | if (count($lpCategories) > 1) { |
||
| 192 | $html .= Display::page_subheader3($item->getName()); |
||
| 193 | } |
||
| 194 | |||
| 195 | $lpTable = [$columnHeaders]; |
||
| 196 | |||
| 197 | foreach ($flatList as $learnpath) { |
||
| 198 | $lpId = $learnpath['lp_old_id']; |
||
| 199 | |||
| 200 | if ($hideInvisibleViews |
||
| 201 | && !StudentFollowPage::isViewVisible($lpId, $student->getId(), $courseInfo['real_id'], $sessionId) |
||
| 202 | ) { |
||
| 203 | continue; |
||
| 204 | } |
||
| 205 | |||
| 206 | $contentToExport = []; |
||
| 207 | |||
| 208 | if (in_array('lp', $columnHeadersKeys)) { |
||
| 209 | $contentToExport[] = api_html_entity_decode(stripslashes($learnpath['lp_name']), ENT_QUOTES); |
||
| 210 | } |
||
| 211 | |||
| 212 | if (in_array('time', $columnHeadersKeys)) { |
||
| 213 | // Get time in lp |
||
| 214 | if (!empty($timeCourse)) { |
||
| 215 | $lpTime = $timeCourse[TOOL_LEARNPATH] ?? 0; |
||
| 216 | $totalTime = isset($lpTime[$lpId]) ? (int) $lpTime[$lpId] : 0; |
||
| 217 | } else { |
||
| 218 | $totalTime = Tracking::get_time_spent_in_lp( |
||
| 219 | $student->getId(), |
||
| 220 | $courseInfo['code'], |
||
| 221 | [$lpId], |
||
| 222 | $sessionId |
||
| 223 | ); |
||
| 224 | } |
||
| 225 | |||
| 226 | $contentToExport[] = api_time_to_hms($totalTime); |
||
| 227 | } |
||
| 228 | |||
| 229 | if (in_array('best_score', $columnHeadersKeys)) { |
||
| 230 | $bestScore = Tracking::get_avg_student_score( |
||
| 231 | $student->getId(), |
||
| 232 | $courseInfo['code'], |
||
| 233 | [$lpId], |
||
| 234 | $sessionId, |
||
| 235 | false, |
||
| 236 | false, |
||
| 237 | true |
||
| 238 | ); |
||
| 239 | |||
| 240 | $contentToExport[] = empty($bestScore) ? '' : "$bestScore %"; |
||
| 241 | } |
||
| 242 | |||
| 243 | if (in_array('latest_attempt_avg_score', $columnHeadersKeys)) { |
||
| 244 | $scoreLatest = Tracking::get_avg_student_score( |
||
| 245 | $student->getId(), |
||
| 246 | $courseInfo['code'], |
||
| 247 | [$lpId], |
||
| 248 | $sessionId, |
||
| 249 | false, |
||
| 250 | true |
||
| 251 | ); |
||
| 252 | |||
| 253 | if (isset($scoreLatest) && is_numeric($scoreLatest)) { |
||
| 254 | $scoreLatest = "$scoreLatest %"; |
||
| 255 | } |
||
| 256 | |||
| 257 | $contentToExport[] = $scoreLatest; |
||
| 258 | } |
||
| 259 | |||
| 260 | if (in_array('progress', $columnHeadersKeys)) { |
||
| 261 | $progress = Tracking::get_avg_student_progress( |
||
| 262 | $student->getId(), |
||
| 263 | $courseInfo['code'], |
||
| 264 | [$lpId], |
||
| 265 | $sessionId |
||
| 266 | ); |
||
| 267 | |||
| 268 | $contentToExport[] = is_numeric($progress) ? "$progress %" : '0 %'; |
||
| 269 | } |
||
| 270 | |||
| 271 | if (in_array('last_connection', $columnHeadersKeys)) { |
||
| 272 | // Get last connection time in lp |
||
| 273 | $startTime = Tracking::get_last_connection_time_in_lp( |
||
| 274 | $student->getId(), |
||
| 275 | $courseInfo['code'], |
||
| 276 | $lpId, |
||
| 277 | $sessionId |
||
| 278 | ); |
||
| 279 | |||
| 280 | $contentToExport[] = empty($startTime) |
||
| 281 | ? '-' |
||
| 282 | : api_convert_and_format_date($startTime, DATE_TIME_FORMAT_LONG); |
||
| 283 | } |
||
| 284 | |||
| 285 | if (in_array('student_follow_page_add_LP_subscription_info', $columnHeadersKeys)) { |
||
| 286 | $lpSubscription = StudentFollowPage::getLpSubscription( |
||
| 287 | $learnpath, |
||
| 288 | $student->getId(), |
||
| 289 | $courseInfo['real_id'], |
||
| 290 | $sessionId |
||
| 291 | ); |
||
| 292 | $contentToExport[] = strip_tags(str_replace('<br>', "\n", $lpSubscription)); |
||
| 293 | } |
||
| 294 | |||
| 295 | if (in_array('student_follow_page_add_LP_acquisition_info', $columnHeadersKeys)) { |
||
| 296 | $lpAcquisition = StudentFollowPage::getLpAcquisition( |
||
| 297 | $learnpath, |
||
| 298 | $student->getId(), |
||
| 299 | $courseInfo['real_id'], |
||
| 300 | $sessionId |
||
| 301 | ); |
||
| 302 | $contentToExport[] = strip_tags(str_replace('<br>', "\n", $lpAcquisition)); |
||
| 303 | } |
||
| 304 | |||
| 305 | $lpTable[] = $contentToExport; |
||
| 306 | } |
||
| 307 | |||
| 308 | $html .= Export::convert_array_to_html($lpTable); |
||
| 309 | } |
||
| 310 | |||
| 311 | return $html; |
||
| 312 | } |
||
| 504 |