| Conditions | 29 |
| Paths | > 20000 |
| Total Lines | 182 |
| Code Lines | 113 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| 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 | true |
||
| 186 | ); |
||
| 187 | |||
| 188 | $flatList = $lpList->get_flat_list(); |
||
| 189 | |||
| 190 | if (count($lpCategories) > 1) { |
||
| 191 | $html .= Display::page_subheader3($item->getName()); |
||
| 192 | } |
||
| 193 | |||
| 194 | $lpTable = [$columnHeaders]; |
||
| 195 | |||
| 196 | foreach ($flatList as $learnpath) { |
||
| 197 | $lpId = $learnpath['lp_old_id']; |
||
| 198 | |||
| 199 | if ($hideInvisibleViews |
||
| 200 | && !StudentFollowPage::isViewVisible($lpId, $student->getId(), $courseInfo['real_id'], $sessionId) |
||
| 201 | ) { |
||
| 202 | continue; |
||
| 203 | } |
||
| 204 | |||
| 205 | $contentToExport = []; |
||
| 206 | |||
| 207 | if (in_array('lp', $columnHeadersKeys)) { |
||
| 208 | $contentToExport[] = api_html_entity_decode(stripslashes($learnpath['lp_name']), ENT_QUOTES); |
||
| 209 | } |
||
| 210 | |||
| 211 | if (in_array('time', $columnHeadersKeys)) { |
||
| 212 | // Get time in lp |
||
| 213 | if (!empty($timeCourse)) { |
||
| 214 | $lpTime = $timeCourse[TOOL_LEARNPATH] ?? 0; |
||
| 215 | $totalTime = isset($lpTime[$lpId]) ? (int) $lpTime[$lpId] : 0; |
||
| 216 | } else { |
||
| 217 | $totalTime = Tracking::get_time_spent_in_lp( |
||
| 218 | $student->getId(), |
||
| 219 | $courseInfo['code'], |
||
| 220 | [$lpId], |
||
| 221 | $sessionId |
||
| 222 | ); |
||
| 223 | } |
||
| 224 | |||
| 225 | $contentToExport[] = api_time_to_hms($totalTime); |
||
| 226 | } |
||
| 227 | |||
| 228 | if (in_array('best_score', $columnHeadersKeys)) { |
||
| 229 | $bestScore = Tracking::get_avg_student_score( |
||
| 230 | $student->getId(), |
||
| 231 | $courseInfo['code'], |
||
| 232 | [$lpId], |
||
| 233 | $sessionId, |
||
| 234 | false, |
||
| 235 | false, |
||
| 236 | true |
||
| 237 | ); |
||
| 238 | |||
| 239 | $contentToExport[] = empty($bestScore) ? '' : "$bestScore %"; |
||
| 240 | } |
||
| 241 | |||
| 242 | if (in_array('latest_attempt_avg_score', $columnHeadersKeys)) { |
||
| 243 | $scoreLatest = Tracking::get_avg_student_score( |
||
| 244 | $student->getId(), |
||
| 245 | $courseInfo['code'], |
||
| 246 | [$lpId], |
||
| 247 | $sessionId, |
||
| 248 | false, |
||
| 249 | true |
||
| 250 | ); |
||
| 251 | |||
| 252 | if (isset($scoreLatest) && is_numeric($scoreLatest)) { |
||
| 253 | $scoreLatest = "$scoreLatest %"; |
||
| 254 | } |
||
| 255 | |||
| 256 | $contentToExport[] = $scoreLatest; |
||
| 257 | } |
||
| 258 | |||
| 259 | if (in_array('progress', $columnHeadersKeys)) { |
||
| 260 | $progress = Tracking::get_avg_student_progress( |
||
| 261 | $student->getId(), |
||
| 262 | $courseInfo['code'], |
||
| 263 | [$lpId], |
||
| 264 | $sessionId |
||
| 265 | ); |
||
| 266 | |||
| 267 | $contentToExport[] = is_numeric($progress) ? "$progress %" : '0 %'; |
||
| 268 | } |
||
| 269 | |||
| 270 | if (in_array('last_connection', $columnHeadersKeys)) { |
||
| 271 | // Get last connection time in lp |
||
| 272 | $startTime = Tracking::get_last_connection_time_in_lp( |
||
| 273 | $student->getId(), |
||
| 274 | $courseInfo['code'], |
||
| 275 | $lpId, |
||
| 276 | $sessionId |
||
| 277 | ); |
||
| 278 | |||
| 279 | $contentToExport[] = empty($startTime) |
||
| 280 | ? '-' |
||
| 281 | : api_convert_and_format_date($startTime, DATE_TIME_FORMAT_LONG); |
||
| 282 | } |
||
| 283 | |||
| 284 | if (in_array('student_follow_page_add_LP_subscription_info', $columnHeadersKeys)) { |
||
| 285 | $lpSubscription = StudentFollowPage::getLpSubscription( |
||
| 286 | $learnpath, |
||
| 287 | $student->getId(), |
||
| 288 | $courseInfo['real_id'], |
||
| 289 | $sessionId |
||
| 290 | ); |
||
| 291 | $contentToExport[] = strip_tags(str_replace('<br>', "\n", $lpSubscription)); |
||
| 292 | } |
||
| 293 | |||
| 294 | if (in_array('student_follow_page_add_LP_acquisition_info', $columnHeadersKeys)) { |
||
| 295 | $lpAcquisition = StudentFollowPage::getLpAcquisition( |
||
| 296 | $learnpath, |
||
| 297 | $student->getId(), |
||
| 298 | $courseInfo['real_id'], |
||
| 299 | $sessionId |
||
| 300 | ); |
||
| 301 | $contentToExport[] = strip_tags(str_replace('<br>', "\n", $lpAcquisition)); |
||
| 302 | } |
||
| 303 | |||
| 304 | $lpTable[] = $contentToExport; |
||
| 305 | } |
||
| 306 | |||
| 307 | $html .= Export::convert_array_to_html($lpTable); |
||
| 308 | } |
||
| 309 | |||
| 310 | return $html; |
||
| 311 | } |
||
| 503 |